|
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 Rich Unger. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.xml.tree.nodes; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.URL; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.openide.ErrorManager; import org.openide.xml.EntityCatalog; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; /** * * @author Rich Unger */ public class DoctypeMapParser { static int indent = 0; /** * fills map with element entries from the xml file * returns the doctype public ID and URI namespaces. */ public static String[] parse(URL url, Map map, ClassLoader cl) { // Step 1: create a DocumentBuilderFactory and configure it DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); // No need to validate since developer is in control of the doc type map. dbf.setValidating(false); dbf.setIgnoringComments(true); dbf.setIgnoringElementContentWhitespace(true); // Step 2: create a DocumentBuilder that satisfies the constraints // specified by the DocumentBuilderFactory DocumentBuilder db = null; try { // !!! it can be terribly slow and memory intensive // always use SAX for data extraction db = dbf.newDocumentBuilder(); } catch( ParserConfigurationException ex ) { ex.printStackTrace(); return null; } // Set an ErrorHandler before parsing OutputStreamWriter errorWriter = new OutputStreamWriter(System.err); db.setErrorHandler( new MyErrorHandler(new PrintWriter(errorWriter, true))); // Set the entity resolver. Not really needed, since we're not validating, // but OK. db.setEntityResolver(EntityCatalog.getDefault()); // Step 3: parse the input file Document doc = null; try { doc = db.parse(url.toString()); } catch( SAXException ex ) { // programming error, customizations must be well tested throw new IllegalArgumentException("Customization description syntax error: " + ex); // NOI18N } catch( IOException ex ) { ex.printStackTrace(); return null; // Runtime error, ignore it } // Now down to business! Element root = doc.getDocumentElement(); String sDoctype = root.getAttribute("publicID"); // NOI18N String namespace = root.getAttribute("nsURI"); // NOI18N if (sDoctype == null && namespace == null) { // programming error, customizations must be well tested throw new IllegalArgumentException("Customization description syntax error: missing doctype attribute!"); // NOI18N } NodeList list = root.getChildNodes(); for( int i=0; i < list.getLength(); i++) { Node n = list.item(i); if (n != null && n instanceof Element) { Element element = (Element) n; element.normalize(); addElementToMap(element, map, cl); } } return new String[] {sDoctype, namespace}; } protected static void addElementToMap(Element element, Map map, ClassLoader cl) { String sName = element.getAttribute("name"); // NOI18N (local name) String sIcon = element.getAttribute("iconbase"); // NOI18N String sText = element.getAttribute("text"); // NOI18N NodeList list = element.getChildNodes(); Element customizerElement = null; for( int i=0; i < list.getLength(); i++) { Node n = list.item(i); if (n != null && n instanceof Element) { customizerElement = (Element) n; customizerElement.normalize(); break; } } // nothing is declared, so don't bother adding to the map if( customizerElement == null && sIcon.equals("") && sText.equals("") ) // NOI18N { return; } ElementInfo info = new ElementInfo(); if( !sIcon.equals("") ) { info.setIconBase(sIcon); } if( !sText.equals("") ) { info.setText(sText); } if( customizerElement != null ) { Class clazz = null; try { clazz = Class.forName( customizerElement.getAttribute("class"), // NOI18N true, cl ); } catch( Exception ex ) { ErrorManager.getDefault().notify(ex); return; } NodeList propertyList = customizerElement.getChildNodes(); for( int i=0; i < propertyList.getLength(); i++) { Node node = propertyList.item(i); node.normalize(); if (node.getNodeName().length() > 0 && node.getNodeName().equalsIgnoreCase("#text")) { // NOI18N customizerElement.removeChild(node); } } info.setCustomizerClass( clazz, customizerElement.getChildNodes() ); } map.put(sName, info); } // Error handler to report errors and warnings private static class MyErrorHandler implements ErrorHandler { /** Error handler output goes here */ private PrintWriter out; MyErrorHandler(PrintWriter out) { this.out = out; } /** * Returns a string describing parse exception details */ private String getParseExceptionInfo(SAXParseException spe) { String systemId = spe.getSystemId(); if (systemId == null) { systemId = "null"; // NOI18N } String info = "URI=" + systemId + // NOI18N " Line=" + spe.getLineNumber() + // NOI18N ": " + spe.getMessage(); // NOI18N return info; } // The following methods are standard SAX ErrorHandler methods. // See SAX documentation for more info. public void warning(SAXParseException spe) throws SAXException { out.println("Warning: " + getParseExceptionInfo(spe)); // NOI18N } public void error(SAXParseException spe) throws SAXException { String message = "Error: " + getParseExceptionInfo(spe); // NOI18N throw new SAXException(message); } public void fatalError(SAXParseException spe) throws SAXException { String message = "Fatal Error: " + getParseExceptionInfo(spe); // NOI18N throw new SAXException(message); } } } |
... 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.