📄 saxdriver.java
字号:
/* * Copyright (c) 1999-2000 by David Brownell. All Rights Reserved. * * This program is open source software; you may use, copy, modify, and * redistribute it under the terms of the LICENSE with which it was * originally distributed. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * LICENSE for more details. *///// Copyright (c) 1998 by Microstar Software Ltd.// From Microstar's README (the entire original license)://// AElfred is free for both commercial and non-commercial use and// redistribution, provided that Microstar's copyright and disclaimer are// retained intact. You are free to modify AElfred for your own use and// to redistribute AElfred with your modifications, provided that the// modifications are clearly documented.//// This program is distributed in the hope that it will be useful, but// WITHOUT ANY WARRANTY; without even the implied warranty of// merchantability or fitness for a particular purpose. Please use it AT// YOUR OWN RISK.//package org.dom4j.io.aelfred;import org.xml.sax.*;import org.xml.sax.ext.DeclHandler;import org.xml.sax.ext.LexicalHandler;import org.xml.sax.helpers.NamespaceSupport;import java.io.IOException;import java.io.InputStreamReader;import java.util.*;// $Id: SAXDriver.java,v 1.2 2003/06/10 16:18:34 per_nyfelt Exp $/** * An enhanced SAX2 version of Microstar's Ælfred XML parser. * The enhancements primarily relate to significant improvements in * conformance to the XML specification, and SAX2 support. Performance * has been improved. However, the Ælfred proprietary APIs are * no longer public. See the package level documentation for more * information. * * <table border="1" width='100%' cellpadding='3' cellspacing='0'> * <tr bgcolor='#ccccff'> * <th><font size='+1'>Name</font></th> * <th><font size='+1'>Notes</font></th></tr> * * <tr><td colspan=2><center><em>Features ... URL prefix is * <b>http://xml.org/sax/features/</b></em></center></td></tr> * * <tr><td>(URL)/external-general-entities</td> * <td>Value is fixed at <em>true</em></td></tr> * <tr><td>(URL)/external-parameter-entities</td> * <td>Value is fixed at <em>true</em></td></tr> * <tr><td>(URL)/namespace-prefixes</td> * <td>Value defaults to <em>false</em> (but XML 1.0 names are * always reported)</td></tr> * <tr><td>(URL)/namespaces</td> * <td>Value defaults to <em>true</em></td></tr> * <tr><td>(URL)/string-interning</td> * <td>Value is fixed at <em>true</em></td></tr> * <tr><td>(URL)/validation</td> * <td>Value is fixed at <em>false</em></td></tr> * * <tr><td colspan=2><center><em>Handler Properties ... URL prefix is * <b>http://xml.org/sax/properties/</b></em></center></td></tr> * * <tr><td>(URL)/declaration-handler</td> * <td>A declaration handler may be provided. Declaration of general * entities is exposed, but not parameter entities; none of the entity * names reported here will begin with "%". </td></tr> * <tr><td>(URL)/lexical-handler</td> * <td>A lexical handler may be provided. Entity boundaries and * comments are not exposed; only CDATA sections and the start/end of * the DTD (the internal subset is not detectible). </td></tr> * </table> * * <p> Note that the declaration handler doesn't suffice for showing all * the logical structure * of the DTD; it doesn't expose the name of the root element, or the values * that are permitted in a NOTATIONS attribute. (The former is exposed as * lexical data, and SAX2 beta doesn't expose the latter.) * * <p> Although support for several features and properties is "built in" * to this parser, it support all others by storing the assigned values * and returning them. * * <p>This parser currently implements the SAX1 Parser API, but * it may not continue to do so in the future. * * @author Written by David Megginson <dmeggins@microstar.com> * (version 1.2a from Microstar) * @author Updated by David Brownell <david-b@pacbell.net> * @version $Date: 2003/06/10 16:18:34 $ * @see org.xml.sax.Parser */final public class SAXDriver implements Locator, Attributes, XMLReader, Parser, AttributeList{ private final DefaultHandler base = new DefaultHandler (); private XmlParser parser; private EntityResolver entityResolver = base; private ContentHandler contentHandler = base; private DTDHandler dtdHandler = base; private ErrorHandler errorHandler = base; private DeclHandler declHandler = base; private LexicalHandler lexicalHandler = base; private String elementName = null; private ArrayList entityStack = new ArrayList (); private ArrayList attributeNames = new ArrayList (); private ArrayList attributeNamespaces = new ArrayList (); private ArrayList attributeLocalNames = new ArrayList (); private ArrayList attributeValues = new ArrayList (); private boolean namespaces = true; private boolean xmlNames = false; private boolean nspending = false; // indicates an attribute was read before its // namespace declaration private int attributeCount = 0; private String nsTemp [] = new String [3]; private NamespaceSupport prefixStack = new NamespaceSupport (); private HashMap features; private HashMap properties; // // Constructor. // /** Constructs a SAX Parser. */ public SAXDriver () {} // // Implementation of org.xml.sax.Parser. // /** * <b>SAX1</b>: Sets the locale used for diagnostics; currently, * only locales using the English language are supported. * @param locale The locale for which diagnostics will be generated */ public void setLocale (Locale locale) throws SAXException { if ("en".equals (locale.getLanguage ())) return ; throw new SAXException ("AElfred only supports English locales."); } /** * <b>SAX2</b>: Returns the object used when resolving external * entities during parsing (both general and parameter entities). */ public EntityResolver getEntityResolver () { return entityResolver; } /** * <b>SAX1, SAX2</b>: Set the entity resolver for this parser. * @param resolver The object to receive entity events. */ public void setEntityResolver (EntityResolver resolver) { if (resolver == null) resolver = base; this.entityResolver = resolver; } /** * <b>SAX2</b>: Returns the object used to process declarations related * to notations and unparsed entities. */ public DTDHandler getDTDHandler () { return dtdHandler; } /** * <b>SAX1, SAX2</b>: Set the DTD handler for this parser. * @param handler The object to receive DTD events. */ public void setDTDHandler (DTDHandler handler) { if (handler == null) handler = base; this.dtdHandler = handler; } /** * <b>SAX1</b>: Set the document handler for this parser. If a * content handler was set, this document handler will supplant it. * The parser is set to report all XML 1.0 names rather than to * filter out "xmlns" attributes (the "namespace-prefixes" feature * is set to true). * * @deprecated SAX2 programs should use the XMLReader interface * and a ContentHandler. * * @param handler The object to receive document events. */ public void setDocumentHandler (DocumentHandler handler) { contentHandler = new Adapter (handler); xmlNames = true; } /** * <b>SAX2</b>: Returns the object used to report the logical * content of an XML document. */ public ContentHandler getContentHandler () { return contentHandler; } /** * <b>SAX2</b>: Assigns the object used to report the logical * content of an XML document. If a document handler was set, * this content handler will supplant it (but XML 1.0 style name * reporting may remain enabled). */ public void setContentHandler (ContentHandler handler) { if (handler == null) handler = base; contentHandler = handler; } /** * <b>SAX1, SAX2</b>: Set the error handler for this parser. * @param handler The object to receive error events. */ public void setErrorHandler (ErrorHandler handler) { if (handler == null) handler = base; this.errorHandler = handler; } /** * <b>SAX2</b>: Returns the object used to receive callbacks for XML * errors of all levels (fatal, nonfatal, warning); this is never null; */ public ErrorHandler getErrorHandler () { return errorHandler; } /** * <b>SAX1, SAX2</b>: Auxiliary API to parse an XML document, used mostly * when no URI is available. * If you want anything useful to happen, you should set * at least one type of handler. * @param source The XML input source. Don't set 'encoding' unless * you know for a fact that it's correct. * @see #setEntityResolver * @see #setDTDHandler * @see #setContentHandler * @see #setErrorHandler * @exception SAXException The handlers may throw any SAXException, * and the parser normally throws SAXParseException objects. * @exception IOException IOExceptions are normally through through * the parser if there are problems reading the source document. */ public void parse (InputSource source) throws SAXException, IOException { synchronized (base) { parser = new XmlParser (); parser.setHandler (this); try { String systemId = source.getSystemId (); // duplicate first entry, in case startDocument handler // needs to use Locator.getSystemId(), before entities // start to get reported by the parser if (systemId != null) entityStack.add (systemId); else entityStack.add ("illegal:unknown system ID"); parser.doParse (systemId, source.getPublicId (), source.getCharacterStream (), source.getByteStream (), source.getEncoding ()); } catch (SAXException e) { throw e; } catch (IOException e) { throw e; } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new SAXException (e.getMessage (), e); } finally { contentHandler.endDocument (); entityStack.clear (); } } } /** * <b>SAX1, SAX2</b>: Preferred API to parse an XML document, using a * system identifier (URI). */ public void parse (String systemId) throws SAXException, IOException { parse (new InputSource (systemId)); } // // Implementation of SAX2 "XMLReader" interface // static final String FEATURE = "http://xml.org/sax/features/"; static final String HANDLER = "http://xml.org/sax/properties/"; /** * <b>SAX2</b>: Tells the value of the specified feature flag. * * @exception SAXNotRecognizedException thrown if the feature flag * is neither built in, nor yet assigned. */ public boolean getFeature (String featureId) throws SAXNotRecognizedException { if ((FEATURE + "validation").equals (featureId)) return false; // external entities (both types) are currently always included if ((FEATURE + "external-general-entities").equals (featureId) || (FEATURE + "external-parameter-entities") .equals (featureId)) return true; // element/attribute names are as written in document; no mangling if ((FEATURE + "namespace-prefixes").equals (featureId)) return xmlNames; // report element/attribute namespaces? if ((FEATURE + "namespaces").equals (featureId)) return namespaces; // XXX always provides a locator ... removed in beta // always interns if ((FEATURE + "string-interning").equals (featureId)) return true; if (features != null && features.containsKey (featureId)) return ((Boolean)features.get (featureId)).booleanValue (); throw new SAXNotRecognizedException (featureId); } /** * <b>SAX2</b>: Returns the specified property. * * @exception SAXNotRecognizedException thrown if the property value * is neither built in, nor yet stored. */ public Object getProperty (String propertyId) throws SAXNotRecognizedException { if ((HANDLER + "declaration-handler").equals (propertyId)) return declHandler; if ((HANDLER + "lexical-handler").equals (propertyId)) return lexicalHandler; if (properties != null && properties.containsKey (propertyId)) return properties.get (propertyId); // unknown properties throw new SAXNotRecognizedException (propertyId); } /** * <b>SAX2</b>: Sets the state of feature flags in this parser. Some * built-in feature flags are mutable; all flags not built-in are * motable. */ public void setFeature (String featureId, boolean state) throws SAXNotRecognizedException, SAXNotSupportedException { boolean value; try { // Features with a defined value, we just change it if we can. value = getFeature (featureId); if (state == value) return; if ((FEATURE + "namespace-prefixes").equals (featureId)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -