alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.nbbuild;

import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import java.io.File;
import java.util.Vector;


/** Makes an XML file representing update information from NBMs.
 *
 * @author Jesse Glick
 */
public class MakeUpdateDesc extends MatchingTask {

    protected boolean usedMatchingTask = false;
    /** Set of NBMs presented as a folder in the Update Center. */
    public /*static*/ class Group {
        public Vector nbms = new Vector ();
        public Vector filesets = new Vector ();
        public Vector scanners = new Vector ();
	public String name;

        /** Displayed name of the group. */
	public void setName (String s) {
	    name = s;
	}

        /** Add fileset to the group of NetBeans modules **/
        public void addFileSet (FileSet set) {
            filesets.add(set);
        }
    }
    
    /** pointer to another xml entity to include **/
    public class Entityinclude {
        public String file;
        /** Path to the entity file.
         * It included as an xml-entity pointer in master .xml file.
         */
	public void setFile (String f) {
	    file = f;
	}
    }

    private Vector entityincludes = new Vector();
    private Vector groups = new Vector ();
    private Vector filesets = new Vector ();

    private String desc_name;
    private File desc;
    private File desc_ent;

    /** Description file to create. */
    public void setDesc (String d) {
	desc_name = d;
        desc = new File (desc_name);
    }

    /** Module group to create **/
    public Group createGroup () {
	Group g = new Group ();
	groups.add (g);
	return g;
    }

    /** External XML entity include **/
    public Entityinclude createEntityinclude () {
        Entityinclude i = new Entityinclude ();
        entityincludes.add (i);
        return i;
    }

   /**
     * Adds a set of files (nested fileset attribute).
     */
    public void addFileset(FileSet set) {
        filesets.addElement(set);
    }

    
    private String dist_base;
   /**
    * Set distribution base, which will be enforced
    */
    public void setDistBase(String dbase) {
        dist_base = dbase;
    }
    
    // Similar to org.openide.xml.XMLUtil methods.
    private static String xmlEscape(String s) {
        int max = s.length();
        StringBuffer s2 = new StringBuffer((int)(max * 1.1 + 1));
        for (int i = 0; i < max; i++) {
            char c = s.charAt(i);
            switch (c) {
                case '<':
                    s2.append("<"); //NOI18N
                    break;
                case '>':
                    s2.append(">"); //NOI18N
                    break;
                case '&':
                    s2.append("&"); //NOI18N
                    break;
                case '"':
                    s2.append("""); //NOI18N
                    break;
                default:
                    s2.append(c);
                    break;
            }
        }
        return s2.toString();
    }

    public void execute () throws BuildException {
        Group root = new Group();
        root.setName("root"); //NOI18N
        for (int i=0; i < filesets.size(); i++) {
            root.addFileSet((FileSet) filesets.elementAt(i));
        }
        groups.addElement(root);
	if (desc.exists ()) {
	    // Simple up-to-date check.
	    long time = desc.lastModified ();
	    boolean uptodate = true;

	CHECK:
            for (int i=0; i time) {
                                uptodate = false;
                                break CHECK;
                            }
                        }
		    }
		}
	    }
	    if (uptodate) return;
	}
	log ("Creating update description " + desc.getAbsolutePath ());
	// [PENDING] this would be easier with a proper XML read/write library;
	// unfortunately I don't have docs for any handy at the moment.
	// So e.g. it is assumed that  is on its own line(s), etc.
	try {
            desc.delete();
	    java.io.OutputStream os = new java.io.FileOutputStream (desc);
	    try {
                
                java.io.PrintWriter pw = new java.io.PrintWriter (new java.io.OutputStreamWriter (os, "UTF-8")); //NOI18N
		pw.println (""); //NOI18N
		pw.println ();
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat ("ss/mm/HH/dd/MM/yyyy"); //NOI18N
		format.setTimeZone (java.util.TimeZone.getTimeZone ("GMT")); //NOI18N
		String date = format.format (new java.util.Date ());
                
            if ( entityincludes.size() > 0 ) {
                    // prepare .ent file
                    String ent_name=desc_name.toString();                
                    int xml_idx = ent_name.indexOf(".xml"); //NOI18N
                    if (xml_idx != -1) {
                        ent_name = ent_name.substring (0, xml_idx) + ".ent"; //NOI18N
                    } else {
                        ent_name = ent_name + ".ent"; //NOI18N
                    }
                    desc_ent = new File(ent_name);               
                    desc_ent.delete();
                    pw.println (""); //NOI18N
                    int inc_num=0;
                    for (int i=0; i"); //NOI18N
                    }
                    pw.println ("]>"); //NOI18N
                    pw.println ();
                    pw.println (""); //NOI18N
                    pw.println ("    &entity;"); //NOI18N
                    for (int i=0; i"); //NOI18N
                    pw.println ();
                    pw.flush ();
                    pw.close ();
                
                    os = new java.io.FileOutputStream (desc_ent);
                    pw = new java.io.PrintWriter (new java.io.OutputStreamWriter (os, "UTF-8")); //NOI18N
                    pw.println (""); //NOI18N
                    pw.println ("");
                    pw.println ();
                    
                } else {
                    pw.println (""); //NOI18N
                    pw.println (""); //NOI18N
                    pw.println ();
                }

                pw.println ();
		StringBuffer licenses = new StringBuffer ();
		java.util.Set licenseNames = new java.util.HashSet (); // Set
                
                for (int gi=0; gi < groups.size(); gi++) {
		    Group g = (Group) groups.elementAt(gi);
		    // Don't indent; embedded descriptions would get indented otherwise.
                    log ("Creating group \"" + g.name + "\"");
                    if ( ! g.name.equals("root")) { //NOI18N
                        pw.println (""); //NOI18N
                        pw.println ();
                    }
                    for (int fsi=0; fsi < g.filesets.size(); fsi++) {
                        FileSet fs = (FileSet) g.filesets.elementAt(fsi);
                        DirectoryScanner ds = fs.getDirectoryScanner(project);
                        String[] files = ds.getIncludedFiles();
                        for (int fid=0; fid < files.length; fid++) {
                            File n_file = new File(fs.getDir(project), files[fid]);
                            try {
                                long size = n_file.length ();
                                java.util.zip.ZipFile zip = new java.util.zip.ZipFile (n_file);
                                try {
                                    java.util.zip.ZipEntry entry = zip.getEntry ("Info/info.xml"); //NOI18N
                                    if (entry == null)
                                        throw new BuildException ("NBM " + n_file + " was malformed: no Info/info.xml", location);
                                    java.io.InputStream is = zip.getInputStream (entry);
                                    try {
                                        java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (is, "UTF-8")); //NOI18N
                                        String line = r.readLine ();
                                        if (! line.startsWith (" distribution fixed to  : \"" + newline + "\"", Project.MSG_VERBOSE);
                                                        line = newline;
                                                    }
                                                }
                                            }

                                            String docType = "") != -1) { //NOI18N
                                                        licenses.append ('\n'); //NOI18N
                                                        continue INFOXML;
                                                    }
                                                } while ((line = r.readLine ()) != null);
                                            } else {
                                                // Non-license line.
                                                pw.println (line);
                                            }
                                        }
                                    } finally {
                                        is.close ();
                                        pw.println ();
                                    }
                                } finally {
                                    zip.close ();
                                }
                            } catch (java.io.IOException ioe) {
                                throw new BuildException("Cannot access nbm file: " + n_file, ioe, location);
                            }
                        }
		    }
                    if ( ! g.name.equals("root")) { //NOI18N
                        pw.println (""); //NOI18N
                        pw.println ();
                    }
		}
		pw.print (licenses.toString ());
                if ( entityincludes.size() <= 0 ) {
                    pw.println (""); //NOI18N
                    pw.println ();
                }
                pw.flush ();
		pw.close ();
	    } finally {
                os.flush ();
		os.close ();
	    }
	} catch (java.io.IOException ioe) {
	    desc.delete ();
	    throw new BuildException ("Cannot create update description", ioe, location);
	}
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.