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-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.nbbuild;

import java.io.*;
import java.util.*;

import org.w3c.dom.*;
import org.xml.sax.InputSource;

import org.apache.tools.ant.BuildException;

/** This class represents module updates tracking
 *
 * @author  akemr
 */
class ModuleTracking {
    private static final String ELEMENT_MODULE_TRACKING = "tracking"; // NOI18N
    private static final String ELEMENT_MODULE = "module"; // NOI18N
    private static final String ATTR_MODULE_NAME = "name"; // NOI18N
    private static final String ELEMENT_FILE = "file"; // NOI18N
    private static final String ATTR_FILE_NAME = "name"; // NOI18N
    private static final String ATTR_MODULE_PATH = "path"; // NOI18N
    
    /** Platform dependent file name separator */
    private static final String FILE_SEPARATOR = System.getProperty ("file.separator");                

    /** The name of the file */
    public static final String TRACKING_FILE = "module_tracking.xml"; // NOI18N
    
    private boolean pError = false;
    
    private File trackingFile = null;
    
    private String nbPath = null;
    private Tracking tracking = null;
   
    // for generating xml in build process
    public ModuleTracking(String nbPath) {
        this.nbPath = nbPath;
        File directory = new File( nbPath );
        if (!directory.exists()) {
            directory.mkdirs();
        }
        trackingFile = new File(directory, TRACKING_FILE);
        read();
    }
    
/*    public Module addNewModule( String name ) {
         = new Tracking();
        module.setName( name );
        return module;
    }*/
    
    public void putModule(String name, String path, String[] files) {
        Module module = tracking.getModule(name);
        if (module == null) {
            module = new Module();
            module.setName(name);
            module.setPath(path);
            tracking.addModule(module);
        }
        module.putFiles( files );
    }
    
    public Iterator getFilesForModule(String name) {
        Module module = tracking.getModule(name);
        if (module == null) return null;
        String[] files = new String[module.getFiles().size()];
        return module.getFiles().iterator();
    }
    
    void write( ) {
        Document document = XMLUtil.createDocument(ELEMENT_MODULE_TRACKING);
        Element e_module_tracking = document.getDocumentElement();
        Iterator it2 = tracking.getModules().values().iterator();
        while ( it2.hasNext() ) {
            Module mod = (Module)it2.next();
            Element e_module = document.createElement(ELEMENT_MODULE);
            e_module.setAttribute(ATTR_MODULE_NAME, mod.getName());
            e_module.setAttribute(ATTR_MODULE_PATH, mod.getPath());
            e_module_tracking.appendChild( e_module );
            Iterator it3 = mod.getFiles().iterator();
            while ( it3.hasNext() ) {
                String file = (String)it3.next();
                Element e_file = document.createElement(ELEMENT_FILE);
                e_file.setAttribute(ATTR_FILE_NAME, file);
                e_module.appendChild( e_file );
            }
        }
        
        //document.getDocumentElement().normalize();
        try {
	    OutputStream os = new FileOutputStream(trackingFile);
            XMLUtil.write(document, os);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            trackingFile.delete();
            throw new BuildException("Could not write update tracking file", e);
        }        
    }

    /** Scan through org.w3c.dom.Document document. */
    private void read() {
        /** org.w3c.dom.Document document */
        org.w3c.dom.Document document;
        if (trackingFile.exists()) {
            InputStream is;
            try {
                is = new FileInputStream( trackingFile );
                
            InputSource xmlInputSource = new InputSource( is );
            document = XMLUtil.parse( xmlInputSource, false, false, new ErrorCatcher(), null );
            if (is != null)
                is.close();
            }
            catch ( org.xml.sax.SAXException e ) {
                System.out.println("Bad module_tracking" ); // NOI18N
                e.printStackTrace();
                return;
            }
            catch ( java.io.IOException e ) {
                System.out.println("Missing module_tracking" ); // NOI18N
                e.printStackTrace();
                return;
            }
            
            org.w3c.dom.Element element = document.getDocumentElement();
            if ((element != null) && element.getTagName().equals(ELEMENT_MODULE_TRACKING)) {
                scanElement_module_tracking(element);
            }
        }
        if (tracking == null)
            tracking = new Tracking();
    }
    
    /** Scan through org.w3c.dom.Element named module. */
    void scanElement_module_tracking(org.w3c.dom.Element element) { // 
        tracking = new Tracking();        
        org.w3c.dom.NodeList nodes = element.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            org.w3c.dom.Node node = nodes.item(i);
            if ( node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE ) {
                org.w3c.dom.Element nodeElement = (org.w3c.dom.Element)node;
                if (nodeElement.getTagName().equals(ELEMENT_MODULE)) {
                    scanElement_module(nodeElement, tracking);
                }
            }
        }
    }
    
    /** Scan through org.w3c.dom.Element named module. */
    void scanElement_module(org.w3c.dom.Element element, Tracking tracking) { // 
        Module module = new Module();        
        org.w3c.dom.NamedNodeMap attrs = element.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            org.w3c.dom.Attr attr = (org.w3c.dom.Attr)attrs.item(i);
            if (attr.getName().equals(ATTR_MODULE_NAME)) { // 
                module.setName( attr.getValue() );
            }
            if (attr.getName().equals(ATTR_MODULE_PATH)) { // 
                module.setPath( attr.getValue() );
            }
        }
        org.w3c.dom.NodeList nodes = element.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            org.w3c.dom.Node node = nodes.item(i);
            if ( node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE ) {
                org.w3c.dom.Element nodeElement = (org.w3c.dom.Element)node;
                if (nodeElement.getTagName().equals(ELEMENT_FILE)) {
                    scanElement_file(nodeElement, module);
                }
            }
        }
        tracking.addModule(module);
    }
    
    /** Scan through org.w3c.dom.Element named file. */
    void scanElement_file(org.w3c.dom.Element element, Module module) { // 
        String file = null;
        org.w3c.dom.NamedNodeMap attrs = element.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            org.w3c.dom.Attr attr = (org.w3c.dom.Attr)attrs.item(i);
            if (attr.getName().equals(ATTR_FILE_NAME)) { // 
                file = attr.getValue();
            }
        }
        module.addFile (file);
    }

    public class Tracking extends Object {        
         private Hashtable modules = new Hashtable();
         /** Getter for property files.
         * @return Value of property files.
         */
        Hashtable getModules() {
            return modules;
        }
        
        /** Setter for property files.
         * @param files New value of property files.
         */
        void setModules(Hashtable modules) {
            this.modules = modules;
        }
        
        public void addModule( Module module ) {
            modules.put( module.getName(), module );
        }
        
        public Module getModule( String name ) {
            return (Module) modules.get(name);
        }
    }
    
    public class Module extends Object {        
        /** Holds value of property name. */
        private String name;
        
        /** Holds value of property path. */
        private String path;
                
        /** Holds value of property files. */
        private List files = new ArrayList();
        
        /** Getter for property name */
        String getName() {
            return name;
        }
        
        /** Setter for property name */
        void setName(String name) {
            this.name = name;
        }
        
        /** Getter for property path */
        String getPath() {
            return path;
        }
        
        /** Setter for property path */
        void setPath(String path) {
            this.path = path;
        }
        
         /** Getter for property files.
         * @return Value of property files.
         */
        List getFiles() {
            return files;
        }
        
        /** Setter for property files.
         * @param files New value of property files.
         */
        void setFiles(List files) {
            this.files = files;
        }
        
        public void addFile( String filename) {
            files.add( filename );
        }
        
        public void putFiles( String[] list ) {
            for (int i=0; i < list.length; i++) {
                if (!files.contains(list[i])) {
                    files.add(list[i]);
                }
            }
        }
    }
    
    class ErrorCatcher implements org.xml.sax.ErrorHandler {
        private void message (String level, org.xml.sax.SAXParseException e) {
            pError = true;
        }

        public void error (org.xml.sax.SAXParseException e) {
            // normally a validity error
            pError = true;
        }

        public void warning (org.xml.sax.SAXParseException e) {
            //parseFailed = true;
        }

        public void fatalError (org.xml.sax.SAXParseException e) {
            pError = true;
        }
    }
    
}
... 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.