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.core.windows.persistence.convert;

import org.netbeans.core.projects.SessionManager;
import org.netbeans.core.windows.Debug;
import org.netbeans.core.windows.persistence.InternalConfig;
import org.netbeans.core.windows.persistence.PersistenceManager;
import org.openide.ErrorManager;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileSystem;
import org.openide.modules.SpecificationVersion;
import org.openide.util.NbBundle;
import org.openide.xml.XMLUtil;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Handle loading/saving of TopComponent reference in Mode configuration data.
 *
 * @author Marek Slama
 */

class TCRefParserOld {
    
    public static final String INSTANCE_DTD_ID_1_0
    = "-//NetBeans//DTD Top Component in Mode Properties 1.0//EN"; // NOI18N
    public static final String INSTANCE_DTD_ID_2_0
    = "-//NetBeans//DTD Top Component in Mode Properties 2.0//EN"; // NOI18N
    
    /** Unique id from file name */
    private String tc_id;
    
    /** Module parent folder */
    private FileObject moduleParentFolder;
    
    private PropertyHandler propertyHandler;
    
    private InternalConfig internalConfig;
    
    public TCRefParserOld (String tc_id) {
        this.tc_id = tc_id;
    }
    
    /** Load tcref configuration. */
    TCRefConfigOld load () throws IOException {
        //log("load ENTER" + " tcRef:" + tc_id);
        TCRefConfigOld tcRefCfg = new TCRefConfigOld();
        if (propertyHandler == null) {
            propertyHandler = new PropertyHandler();
        }
        InternalConfig internalCfg = getInternalConfig();
        internalCfg.clear();
        propertyHandler.readData(tcRefCfg, internalCfg);
        //log("load LEAVE" + " tcRef:" + tc_id);
        return tcRefCfg;
    }
    
    String getName () {
        return tc_id;
    }
    
    /** Getter for internal configuration data.
     * @return instance of internal configuration data
     */
    InternalConfig getInternalConfig () {
        if (internalConfig == null) {
            internalConfig = new InternalConfig();
        }
        return internalConfig;
    }
    
    void setModuleParentFolder (FileObject moduleParentFolder) {
        this.moduleParentFolder = moduleParentFolder;
    }
    
    void log (String s) {
        Debug.log(TCRefParserOld.class, s);
    }
    
    private final class PropertyHandler extends DefaultHandler {
        
        /** tcRef manager configuration data */
        private TCRefConfigOld tcRefConfig = null;
        
        /** internal configuration data */
        private InternalConfig internalConfig = null;
        
        /** xml parser */
        private XMLReader parser;
        
        /** Lock to prevent mixing readData and writeData */
        private final Object RW_LOCK = new Object();
        
        public PropertyHandler () {
        }
        
        private FileObject getConfigFOInput () {
            FileObject fo = moduleParentFolder.getFileObject
                (TCRefParserOld.this.getName(), PersistenceManager.TCREF_EXT);
            
            log("getConfigFOInput" + " fo:" + fo);
            
            FileObject tcRefConfigFO = null;
            if (fo != null) {
                //Look for file at install layer
                SessionManager sm = SessionManager.getDefault();
                FileSystem installLayer = sm.getLayer(SessionManager.LAYER_INSTALL);
                tcRefConfigFO = installLayer.findResource(fo.getPath());
            }
            
            log("getConfigFOInput" + " tcRefConfigFO:" + tcRefConfigFO);
            
            return tcRefConfigFO;
        }

        /** 
         Reads tcRef configuration data from XML file. 
         Data are returned in output params.
         */
        void readData (TCRefConfigOld tcRefCfg, InternalConfig internalCfg)
        throws IOException {
            tcRefConfig = tcRefCfg;
            internalConfig = internalCfg;
            
            FileObject cfgFOInput = getConfigFOInput();
            if (cfgFOInput == null) {
                throw new FileNotFoundException("[WinSys] Missing TCRef configuration file:" // NOI18N
                + TCRefParserOld.this.getName());
            }
            try {
                synchronized (RW_LOCK) {
                    //DUMP BEGIN
                    /*InputStream is = cfgFOInput.getInputStream();
                    byte [] arr = new byte [is.available()];
                    is.read(arr);
                    log("DUMP TCRef: " + TCRefParserOld.this.getName());
                    String s = new String(arr);
                    log(s);*/
                    //DUMP END
                    
                    getXMLParser().parse(new InputSource(cfgFOInput.getInputStream()));
                }
            } catch (SAXException exc) {
                //Turn into annotated IOException
                String msg = NbBundle.getMessage(TCRefParserOld.class,
                    "EXC_TCRefParse", cfgFOInput);
                IOException ioe = new IOException(msg);
                ErrorManager.getDefault().annotate(ioe, exc);
                throw ioe;
            }
            
            tcRefConfig.initialized = true;
            
            tcRefCfg = tcRefConfig;
            internalCfg = internalConfig;
            
            tcRefConfig = null;
            internalConfig = null;
        }
        
        public void startElement (String nameSpace, String name, String qname, Attributes attrs) 
        throws SAXException {
            if ("tc-ref".equals(qname)) { // NOI18N
                handleTCRef(attrs);
            } else if (internalConfig.specVersion.compareTo(new SpecificationVersion("2.0")) == 0) { // NOI18N
                //Parse version 2.0
                if ("module".equals(qname)) { // NOI18N
                    handleModule(attrs);
                } else if ("properties".equals(qname)) { // NOI18N
                    handleProperties(attrs);
                }
            } else {
                //log("startElement PARSING OLD tcRef:" + TCRefParserOld.this.getName()
                //+ " qname:" + qname);
                //Parse version < 2.0
                if ("tc-ref".equals(qname)) { // NOI18N
                    handleTCRef(attrs);
                } else if ("ui-type".equals(qname)) { // NOI18N
                    handleUIType(attrs);
                } else if ("module".equals(qname)) { // NOI18N
                    handleModule(attrs);
                }
            }
        }

        public void error(SAXParseException ex) throws SAXException  {
            throw ex;
        }

        public void fatalError(SAXParseException ex) throws SAXException {
            throw ex;
        }

        public void warning(SAXParseException ex) throws SAXException {
            // ignore
        }
        
        /** Reads element "tc-ref" */
        private void handleTCRef (Attributes attrs) {
            String version = attrs.getValue("version"); // NOI18N
            if (version != null) {
                internalConfig.specVersion = new SpecificationVersion(version);
            } else {
                ErrorManager.getDefault().log(ErrorManager.WARNING,
                "[WinSys.TCRefParserOld.handleTCRef]" // NOI18N
                + " Warning: Missing attribute \"version\" of element \"tc-ref\"."); // NOI18N
                internalConfig.specVersion = new SpecificationVersion("1.0"); // NOI18N
            }
            //Before version 2.0 tc_id was attribute of tc-ref element
            //so we must read it directly here.
            if (internalConfig.specVersion.compareTo(new SpecificationVersion("2.0")) < 0) { // NOI18N
                String tc_id = attrs.getValue("id"); // NOI18N
                if (tc_id != null) {
                    tcRefConfig.tc_id = tc_id;
                } else {
                    ErrorManager.getDefault().log(ErrorManager.WARNING,
                    "[WinSys.TCRefParserOld.handleTCRef]" // NOI18N
                    + " Warning: Missing required attribute \"id\" of element \"tc-ref\"."); // NOI18N
                }
            }
        }
        
        /** Reads element "ui-type" */
        private void handleUIType (Attributes attrs) throws SAXException {
            String type = attrs.getValue("type"); // NOI18N
            String constrMdi = ""; // NOI18N
            String constrSdi = ""; // NOI18N
            String constrAny = ""; // NOI18N
            boolean openedMdi = false, openedSdi = false, openedAny = false;
            boolean mdi = false, sdi = false, any = false;
            if (type != null) {
                if ("mdi".equals(type)) {
                    mdi = true;
                    String state = attrs.getValue("state"); // NOI18N
                    if ("opened".equals(state)) {
                        openedMdi = true;
                    }
                    String constr = attrs.getValue("constraint"); // NOI18N
                    if ("center".equals(constr)) {
                        constrMdi = ImportManager.CENTER;
                    } else if ("left".equals(constr)) {
                        constrMdi = ImportManager.LEFT;
                    } else if ("right".equals(constr)) {
                        constrMdi = ImportManager.RIGHT;
                    } else if ("top".equals(constr)) {
                        constrMdi = ImportManager.TOP;
                    } else if ("bottom".equals(constr)) {
                        constrMdi = ImportManager.BOTTOM;
                    } else {
                        constrMdi = ImportManager.CENTER;
                    }
                } else if ("sdi".equals(type)) {
                    sdi = true;
                    String state = attrs.getValue("state"); // NOI18N
                    if ("opened".equals(state)) {
                        openedSdi = true;
                    }
                    String constr = attrs.getValue("constraint"); // NOI18N
                    if ("center".equals(constr)) {
                        constrSdi = ImportManager.CENTER;
                    } else if ("left".equals(constr)) {
                        constrSdi = ImportManager.LEFT;
                    } else if ("right".equals(constr)) {
                        constrSdi = ImportManager.RIGHT;
                    } else if ("top".equals(constr)) {
                        constrSdi = ImportManager.TOP;
                    } else if ("bottom".equals(constr)) {
                        constrSdi = ImportManager.BOTTOM;
                    } else {
                        constrSdi = ImportManager.CENTER;
                    }
                } else if ("any".equals(type)) {
                    any = true;
                    String state = attrs.getValue("state"); // NOI18N
                    if ("opened".equals(state)) {
                        openedAny = true;
                    }
                    String constr = attrs.getValue("constraint"); // NOI18N
                    if ("center".equals(constr)) {
                        constrAny = ImportManager.CENTER;
                    } else if ("left".equals(constr)) {
                        constrAny = ImportManager.LEFT;
                    } else if ("right".equals(constr)) {
                        constrAny = ImportManager.RIGHT;
                    } else if ("top".equals(constr)) {
                        constrAny = ImportManager.TOP;
                    } else if ("bottom".equals(constr)) {
                        constrAny = ImportManager.BOTTOM;
                    } else {
                        constrAny = ImportManager.CENTER;
                    }
                } else {
                    ErrorManager.getDefault().log(ErrorManager.WARNING,
                    "[WinSys.TCRefParserOld.handleUIType]" // NOI18N
                    + " Warning: Invalid value of attribute \"type\"" // NOI18N
                    + " of element \"ui-type\"."); // NOI18N
                    throw new SAXException("Invalid attribute value"); // NOI18N
                }
            } else {
                ErrorManager.getDefault().log(ErrorManager.WARNING,
                "[WinSys.TCRefParserOld.handleUIType]" // NOI18N
                + " Warning: Missing required attribute \"type\"" // NOI18N
                + " of element \"ui-type\"."); // NOI18N
                throw new SAXException("Missing required attribute"); // NOI18N
            }
            if (mdi) {
                tcRefConfig.opened = openedMdi;
                tcRefConfig.constraint = constrMdi;
            } else if (any) {
                tcRefConfig.opened = openedAny;
                tcRefConfig.constraint = constrAny;
            } else {
                tcRefConfig.opened = false;
                tcRefConfig.constraint = ImportManager.CENTER;
            }
        }
        
        /** Reads element "module" and updates mode config content */
        private void handleModule (Attributes attrs) {
            String moduleCodeName = attrs.getValue("name"); // NOI18N
            //Parse code name
            internalConfig.moduleCodeNameBase = null;
            internalConfig.moduleCodeNameRelease = null;
            internalConfig.moduleSpecificationVersion = null;
            if (moduleCodeName != null) {
                int i = moduleCodeName.indexOf('/');
                if (i != -1) {
                    internalConfig.moduleCodeNameBase = moduleCodeName.substring(0, i);
                    internalConfig.moduleCodeNameRelease = moduleCodeName.substring(i + 1);
                    checkReleaseCode(internalConfig);
                } else {
                    internalConfig.moduleCodeNameBase = moduleCodeName;
                }
                internalConfig.moduleSpecificationVersion = attrs.getValue("spec"); // NOI18N
            }
        }

        /** Checks validity of moduleCodeNameRelease field. 
         * Helper method. */
        private void checkReleaseCode (InternalConfig internalConfig) {
            // #24844. Repair the wrongly saved "null" string
            // as release number.
            if("null".equals(internalConfig.moduleCodeNameRelease)) { // NOI18N
                ErrorManager.getDefault().notify(
                    ErrorManager.INFORMATIONAL,
                    new IllegalStateException(
                        "Module release code was saved as null string" // NOI18N
                        + " for module "  + internalConfig.moduleCodeNameBase // NOI18N
                        + "! Repairing.") // NOI18N
                );
                internalConfig.moduleCodeNameRelease = null;
            }
        }
        
        /** Reads element "properties" */
        private void handleProperties (Attributes attrs) throws SAXException {
            String tc_id = attrs.getValue("id"); // NOI18N
            if (tc_id != null) {
                tcRefConfig.tc_id = tc_id;
                if (!tc_id.equals(TCRefParserOld.this.getName())) {
                    ErrorManager.getDefault().log(ErrorManager.WARNING,
                    "[WinSys.TCRefParserOld.handleProperties]" // NOI18N
                    + " Error: Value of attribute \"id\" of element \"properties\"" // NOI18N
                    + " and configuration file name must be the same."); // NOI18N
                    throw new SAXException("Invalid attribute value"); // NOI18N
                }
            } else {
                ErrorManager.getDefault().log(ErrorManager.WARNING,
                "[WinSys.TCRefParserOld.handleProperties]" // NOI18N
                + " Error: Missing required attribute \"id\" of element \"properties\"."); // NOI18N
                throw new SAXException("Missing required attribute"); // NOI18N
            }
            
            String state = attrs.getValue("state"); // NOI18N;
            if (state != null) {
                if ("opened".equals(state)) { // NOI18N
                    tcRefConfig.opened = true;
                } else if ("closed".equals(state)) { // NOI18N
                    tcRefConfig.opened = false;
                } else {
                    ErrorManager.getDefault().log(ErrorManager.WARNING,
                    "[WinSys.TCRefParserOld.handleProperties]" // NOI18N
                    + " Warning: Invalid value of attribute \"state\" of element \"properties\"."); // NOI18N
                    tcRefConfig.opened = false;
                }
            } else {
                ErrorManager.getDefault().log(ErrorManager.WARNING,
                "[WinSys.TCRefParserOld.handleProperties]" // NOI18N
                + " Warning: Missing required attribute \"state\" of element \"properties\"."); // NOI18N
                tcRefConfig.opened = false;
            }
        }
        
        public void endDocument() throws org.xml.sax.SAXException {
        }
        
        public void ignorableWhitespace(char[] values, int param, int param2) 
        throws org.xml.sax.SAXException {
        }
        
        public void endElement(java.lang.String str, java.lang.String str1, java.lang.String str2) 
        throws org.xml.sax.SAXException {
        }
        
        public void skippedEntity(java.lang.String str) 
        throws org.xml.sax.SAXException {
        }
        
        public void processingInstruction(java.lang.String str, java.lang.String str1) 
        throws org.xml.sax.SAXException {
        }
                
        public void endPrefixMapping(java.lang.String str) 
        throws org.xml.sax.SAXException {
        }
        
        public void startPrefixMapping(java.lang.String str, java.lang.String str1) 
        throws org.xml.sax.SAXException {
        }
        
        public void characters(char[] values, int param, int param2) 
        throws org.xml.sax.SAXException {
        }
        
        public void setDocumentLocator(org.xml.sax.Locator locator) {
        }
        
        public void startDocument() throws org.xml.sax.SAXException {
        }
        
        /** @return Newly created parser with set content handler, errror handler
         * and entity resolver
         */
        private XMLReader getXMLParser () throws SAXException {
            if (parser == null) {
                // get non validating, not namespace aware parser
                parser = XMLUtil.createXMLReader();
                parser.setContentHandler(this);
                parser.setErrorHandler(this);
                parser.setEntityResolver(this);
            }
            return parser;
        }

        /** Implementation of entity resolver. Points to the local DTD
         * for our public ID */
        public InputSource resolveEntity (String publicId, String systemId)
        throws SAXException {
            if (INSTANCE_DTD_ID_1_0.equals(publicId)
             || INSTANCE_DTD_ID_2_0.equals(publicId)) {
                InputStream is = new ByteArrayInputStream(new byte[0]);
                //getClass().getResourceAsStream(INSTANCE_DTD_LOCAL);
//                if (is == null) {
//                    throw new IllegalStateException ("Entity cannot be resolved."); // NOI18N
//                }
                return new InputSource(is);
            }
            return null; // i.e. follow advice of systemID
        }
        
        void log (String s) {
            Debug.log(PropertyHandler.class, s);
        }
    }
    
}
... 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.