|
What this is
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.core.windows.persistence; import org.netbeans.core.windows.Debug; import org.openide.ErrorManager; import org.openide.filesystems.FileLock; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; 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.*; /** * Handle loading/saving of TopComponent reference in Group configuration data. * * @author Marek Slama */ class TCGroupParser { public static final String INSTANCE_DTD_ID_2_0 = "-//NetBeans//DTD Top Component in Group Properties 2.0//EN"; // NOI18N private static final boolean DEBUG = Debug.isLoggable(TCGroupParser.class); /** Unique id from file name */ private String tc_id; /** Module parent folder */ private FileObject moduleParentFolder; /** Local parent folder */ private FileObject localParentFolder; private InternalConfig internalConfig; /** true if wstcgrp file is present in module folder */ private boolean inModuleFolder; /** true if wstcgrp file is present in local folder */ private boolean inLocalFolder; public TCGroupParser(String tc_id) { this.tc_id = tc_id; } /** Load tcgroup configuration. */ TCGroupConfig load () throws IOException { if (DEBUG) Debug.log(TCGroupParser.class, "load ENTER" + " tcGrp:" + tc_id); TCGroupConfig tcGroupCfg = new TCGroupConfig(); PropertyHandler propertyHandler = new PropertyHandler(); InternalConfig internalCfg = getInternalConfig(); internalCfg.clear(); propertyHandler.readData(tcGroupCfg, internalCfg); if (DEBUG) Debug.log(TCGroupParser.class, "load LEAVE" + " tcGrp:" + tc_id); return tcGroupCfg; } /** Save tcGroup configuration. */ void save (TCGroupConfig tcGroupCfg) throws IOException { if (DEBUG) Debug.log(TCGroupParser.class, "save ENTER" + " tcGrp:" + tc_id); PropertyHandler propertyHandler = new PropertyHandler(); InternalConfig internalCfg = getInternalConfig(); propertyHandler.writeData(tcGroupCfg, internalCfg); if (DEBUG) Debug.log(TCGroupParser.class, "save LEAVE" + " tcGrp:" + tc_id); } 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; } boolean isInModuleFolder () { return inModuleFolder; } void setInModuleFolder (boolean inModuleFolder) { this.inModuleFolder = inModuleFolder; } boolean isInLocalFolder () { return inLocalFolder; } void setInLocalFolder (boolean inLocalFolder) { this.inLocalFolder = inLocalFolder; } void setModuleParentFolder (FileObject moduleParentFolder) { this.moduleParentFolder = moduleParentFolder; } void setLocalParentFolder (FileObject localParentFolder) { this.localParentFolder = localParentFolder; } void log (String s) { Debug.log(TCGroupParser.class, s); } private final class PropertyHandler extends DefaultHandler { /** tcRef manager configuration data */ private TCGroupConfig tcGroupConfig = 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 tcGroupConfigFO; if (isInLocalFolder()) { //log("-- TCGroupParser.getConfigFOInput" + " looking for LOCAL"); tcGroupConfigFO = localParentFolder.getFileObject (TCGroupParser.this.getName(), PersistenceManager.TCGROUP_EXT); } else if (isInModuleFolder()) { //log("-- TCGroupParser.getConfigFOInput" + " looking for MODULE"); tcGroupConfigFO = moduleParentFolder.getFileObject (TCGroupParser.this.getName(), PersistenceManager.TCGROUP_EXT); } else { //XXX should not happen tcGroupConfigFO = null; } //log("-- TCGroupParser.getConfigFOInput" + " tcGroupConfigFO:" + tcGroupConfigFO); return tcGroupConfigFO; } private FileObject getConfigFOOutput () throws IOException { FileObject tcGroupConfigFO; tcGroupConfigFO = localParentFolder.getFileObject (TCGroupParser.this.getName(), PersistenceManager.TCGROUP_EXT); if (tcGroupConfigFO != null) { //log("-- TCGroupParser.getConfigFOOutput" + " tcGroupConfigFO LOCAL:" + tcGroupConfigFO); return tcGroupConfigFO; } else { StringBuffer buffer = new StringBuffer(); buffer.append(TCGroupParser.this.getName()); buffer.append('.'); buffer.append(PersistenceManager.TCGROUP_EXT); //XXX should be improved localParentFolder can be null tcGroupConfigFO = FileUtil.createData(localParentFolder, buffer.toString()); //log("-- TCGroupParser.getConfigFOOutput" + " LOCAL not found CREATE"); return tcGroupConfigFO; } } /** Reads tcRef configuration data from XML file. Data are returned in output params. */ void readData (TCGroupConfig tcGroupCfg, InternalConfig internalCfg) throws IOException { tcGroupConfig = tcGroupCfg; internalConfig = internalCfg; FileObject cfgFOInput = getConfigFOInput(); if (cfgFOInput == null) { throw new FileNotFoundException("[WinSys] Missing TCGroup configuration file:" // NOI18N + TCGroupParser.this.getName()); } try { synchronized (RW_LOCK) { //DUMP BEGIN /*InputStream is = cfgFOInput.getInputStream(); byte [] arr = new byte [is.available()]; is.read(arr); log("DUMP TCGroup: " + TCGroupParser.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(TCGroupParser.class, "EXC_TCGroupParse", cfgFOInput); IOException ioe = new IOException(msg); ErrorManager.getDefault().annotate(ioe, exc); throw ioe; } tcGroupCfg = tcGroupConfig; internalCfg = internalConfig; tcGroupConfig = null; internalConfig = null; } public void startElement (String nameSpace, String name, String qname, Attributes attrs) throws SAXException { if ("tc-group".equals(qname)) { // NOI18N handleTCGroup(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 ("tc-id".equals(qname)) { // NOI18N handleTcId(attrs); } else if ("open-close-behavior".equals(qname)) { // NOI18N handleOpenCloseBehavior(attrs); } } else { log("-- TCGroupParser.startElement PARSING OLD"); //Parse version < 2.0 } } 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-group" */ private void handleTCGroup (Attributes attrs) { String version = attrs.getValue("version"); // NOI18N if (version != null) { internalConfig.specVersion = new SpecificationVersion(version); } else { ErrorManager.getDefault().log(ErrorManager.WARNING, "[WinSys.TCGroupParser.handleTCGroup]" // NOI18N + " Warning: Missing attribute \"version\" of element \"tc-group\"."); // NOI18N internalConfig.specVersion = new SpecificationVersion("2.0"); // NOI18N } } /** 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 |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
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.