saxdriver.java
来自「kaffe Java 解释器语言,源码,Java的子集系统,开放源代码」· Java 代码 · 共 1,273 行 · 第 1/3 页
JAVA
1,273 行
/* * Copyright (C) 1999-2001 David Brownell * * This file is part of GNU JAXP, a library. * * GNU JAXP is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * GNU JAXP 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 * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * As a special exception, if you link this library with other files to * produce an executable, this library does not by itself cause the * resulting executable to be covered by the GNU General Public License. * This exception does not however invalidate any other reasons why the * executable file might be covered by the GNU General Public License. *///// Copyright (c) 1998 by Microstar Software Ltd.// From Microstar's README (the entire original license)://// Separate statements also said it's in the public domain.// All modifications are distributed under the license// above (GPL with library exception).//// 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 gnu.xml.aelfred2;import java.io.*;import java.net.MalformedURLException;import java.net.URL;import java.util.Locale;import java.util.Stack;// maintaining 1.1 compatibility for now ... more portable, PJava, etc// Iterator, Hashmap and ArrayList ought to be fasterimport java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import org.xml.sax.*;import org.xml.sax.ext.*;import org.xml.sax.helpers.NamespaceSupport;/** * 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. 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 defaults to <em>true</em></td></tr> * <tr><td>(URL)/external-parameter-entities</td> * <td>Value defaults to <em>true</em></td></tr> * <tr><td>(URL)/is-standalone</td> * <td>(PRELIMINARY) Returns true iff the document's parsing * has started (some non-error event after <em>startDocument()</em> * was reported) and the document's standalone flag is set.</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)/lexical-handler/parameter-entities</td> * <td>Value is fixed at <em>true</em></td></tr> * <tr><td>(URL)/namespaces</td> * <td>Value defaults to <em>true</em></td></tr> * <tr><td>(URL)/resolve-dtd-uris</td> * <td>(PRELIMINARY) 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)/use-attributes2</td> * <td>(PRELIMINARY) Value is fixed at <em>true</em></td></tr> * <tr><td>(URL)/use-entity-resolver2</td> * <td>(PRELIMINARY) Value defaults to <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. </td></tr> * <tr><td>(URL)/lexical-handler</td> * <td>A lexical handler may be provided. </td></tr> * </table> * * <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 (version 1.2a from Microstar) * @author Updated by David Brownell <dbrownell@users.sourceforge.net> * @see org.xml.sax.Parser */final public class SAXDriver implements Locator, Attributes2, XMLReader, Parser, AttributeList{ private final DefaultHandler2 base = new DefaultHandler2 (); private XmlParser parser; private EntityResolver entityResolver = base; private EntityResolver2 resolver2 = null; 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 Stack entityStack = new Stack (); // could use just one vector (of object/struct): faster, smaller private Vector attributeNames = new Vector (); private Vector attributeNamespaces = new Vector (); private Vector attributeLocalNames = new Vector (); private Vector attributeValues = new Vector (); private boolean attributeSpecified [] = new boolean[10]; private boolean attributeDeclared [] = new boolean[10]; private boolean namespaces = true; private boolean xmlNames = false; private boolean extGE = true; private boolean extPE = true; private boolean resolveAll = true; private boolean useResolver2 = true; private int attributeCount = 0; private boolean attributes; private String nsTemp [] = new String [3]; private NamespaceSupport prefixStack; // // 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 ("AElfred2 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 == base) ? null : entityResolver; } /** * <b>SAX1, SAX2</b>: Set the entity resolver for this parser. * @param handler The object to receive entity events. */ public void setEntityResolver (EntityResolver resolver) { if (resolver instanceof EntityResolver2) resolver2 = (EntityResolver2) resolver; else resolver2 = null; if (resolver == null) resolver = base; entityResolver = resolver; } /** * <b>SAX2</b>: Returns the object used to process declarations related * to notations and unparsed entities. */ public DTDHandler getDTDHandler () { return (dtdHandler == base) ? null : 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 == base ? null : 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 == base ? null : 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 (); if (namespaces) prefixStack = new NamespaceSupport (); else if (!xmlNames) throw new IllegalStateException (); parser.setHandler (this); try { Reader r = source.getCharacterStream(); InputStream in = source.getByteStream(); parser.doParse (source.getSystemId (), source.getPublicId (), r, in, source.getEncoding ()); } catch (SAXException e) { throw e; } catch (IOException e) { throw e; } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new SAXParseException (e.getMessage (), this, e); } finally { contentHandler.endDocument (); entityStack.removeAllElements (); parser = null; prefixStack = null; } } } /** * <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 PROPERTY = "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, SAXNotSupportedException { if ((FEATURE + "validation").equals (featureId)) return false; // external entities (both types) are optionally included if ((FEATURE + "external-general-entities").equals (featureId)) return extGE; if ((FEATURE + "external-parameter-entities") .equals (featureId)) return extPE; // 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; // all PEs and GEs are reported if ((FEATURE + "lexical-handler/parameter-entities").equals (featureId)) return true; // always interns if ((FEATURE + "string-interning").equals (featureId)) return true; // EXTENSIONS 1.1 // always returns isSpecified info if ((FEATURE + "use-attributes2").equals (featureId)) return true; // meaningful between startDocument/endDocument if ((FEATURE + "is-standalone").equals (featureId)) { if (parser == null) throw new SAXNotSupportedException (featureId); return parser.isStandalone (); } // optionally don't absolutize URIs in declarations if ((FEATURE + "resolve-dtd-uris").equals (featureId)) return resolveAll; // optionally use resolver2 interface methods, if possible if ((FEATURE + "use-entity-resolver2").equals (featureId)) return useResolver2; throw new SAXNotRecognizedException (featureId); } // package private DeclHandler getDeclHandler () { return declHandler; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?