parser2.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,790 行 · 第 1/5 页
JAVA
1,790 行
/* * $Id: Parser2.java,v 1.16 2001/09/29 04:17:47 edwingo Exp $ * * The Apache Software License, Version 1.1 * * * Copyright (c) 2000 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Crimson" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, Sun Microsystems, Inc., * http://www.sun.com. For more information on the Apache Software * Foundation, please see <http://www.apache.org/>. */package org.apache.crimson.parser;import java.io.InputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.Reader;import java.io.FileNotFoundException;import java.util.Enumeration;import java.util.Hashtable;import java.util.Locale;import java.util.Vector;import java.util.Properties;import java.security.AccessController;import java.security.PrivilegedAction;import java.security.PrivilegedExceptionAction;import org.xml.sax.*;import org.xml.sax.helpers.*;import org.xml.sax.ext.*;import org.apache.crimson.util.MessageCatalog;import org.apache.crimson.util.XmlChars;import org.apache.crimson.util.XmlNames;//// NOTE: when maintaining this code, take care to keep the message// catalogue(s) up to date!! It's important that the diagnostics// be informative.///** * This implements a fast non-validating SAX2 parser. This one always * processes external parsed entities, strictly adheres to the XML 1.0 * specification, and provides useful diagnostics. It supports an * optimization allowing faster processing of valid standalone XML * documents. For multi-language applications (such as web servers using * XML processing to create dynamic content), a method supports choosing a * locale for parser diagnostics which is both understood by the message * recipient and supported by the parser. * * <P> This conforms to the XML 1.0 specification. To configure an XML * processor which tests document conformance against XML Namespaces, * provide a <em>DtdEventListener</em> which examines declarations of * entities and notations, and have your document listener check other * constraints such as ensuring <em>xmlns*</em> attribute values properly * declare all namespace prefixes. (Only element and attribute names may * contain colons, and even then the name prefix before the colon must be * properly declared.) * * <P> SAX parsers produce a stream of parse events, which applications * process to create an object model which is specific to their tasks. * Applications which do not want to process event streams in that way * should use an API producing a standardized object model, such as the * W3C's <em>Document Object Model</em> (DOM). This parser supports * building fully conformant DOM <em>Document</em> objects, through * use of DtdEventListener extensions to SAX in conjunction with an * appropriate implementation of a SAX <em>DocumentHandler</em>. In * addition, it supports some features (exposing comments, CDATA sections, * and entity references) which are allowed by DOM but not required to * be reported by conformant XML processors. (As usual, the default * handler for parsing events other than fatal errors ignores them.) * * @see ValidatingParser * * @author David Brownell * @author Rajiv Mordani * @author Edwin Goei * @version $Revision: 1.16 $ */public class Parser2{ // stack of input entities being merged private InputEntity in; // temporaries reused during parsing private AttributesExImpl attTmp; private StringBuffer strTmp; private char nameTmp []; private NameCache nameCache; private char charTmp [] = new char [2]; private String[] namePartsTmp = new String[3]; // temporaries local to namespace attribute processing in elements private boolean seenNSDecl; private NamespaceSupport nsSupport; /** * nsAttTmp holds a list of namespace attributes used to check for * #REQUIRED when validating and (namespaces == true && prefixes == * false) */ private Vector nsAttTmp; // NOTE: odd heap behavior, at least with classic VM: if "strTmp" is // reused, LOTS of extra memory is consumed in some simple situations. // JVM bug filed; it's no longer a win to reuse it as much, in any case. // parsing modes private boolean isValidating = false; private boolean fastStandalone = false; private boolean isInAttribute = false; private boolean namespaces; // new in SAX2 private boolean prefixes; // new in SAX2 // temporary DTD parsing state private boolean inExternalPE; private boolean doLexicalPE; private boolean donePrologue; // info about the document private boolean isStandalone; private String rootElementName; // DTD state, used during parsing private boolean ignoreDeclarations; private SimpleHashtable elements = new SimpleHashtable (47); private SimpleHashtable params = new SimpleHashtable (7); // exposed to package-private subclass Hashtable notations = new Hashtable (7); SimpleHashtable entities = new SimpleHashtable (17); // stuff associated with SAX private ContentHandler contentHandler; private DTDHandler dtdHandler; private EntityResolver resolver; private ErrorHandler errHandler; private Locale locale; private Locator locator; // SAX2 extension API support private DeclHandler declHandler; private LexicalHandler lexicalHandler; private boolean disallowDoctypeDecl = false ; private String propertyEntityExpansionLimit = null; private String propertyDisallowDoctypeDecl = null ; //restricting entity expansions //set this value to zero, initially no entity is expanded private int entityExpansionCount = 0 ; //this can be set to any arbitrary value, it would be reset by the value obtained from system property. private int entityExpansionLimit = -1 ; private static final int DEFAULT_ENTITY_EXPANSION_LIMIT = 64000 ; // Compile time option: disable validation support for a better // fit in memory-critical environments (P-Java etc). Doing that // and removing the validating parser support saves (at this time) // about 15% in size. private static final boolean supportValidation = true; // string constants -- use these copies so "==" works // package private static final String strANY = "ANY"; static final String strEMPTY = "EMPTY"; // system properties static final String SYSTEM_PROPERTY_ENTITY_EXPANSION_LIMIT = "entityExpansionLimit" ; static final String SYSTEM_PROPERTY_DISALLOW_DOCTYPE_DECL = "disallowDoctypeDecl" ; static final boolean SECURITY_DEBUG = false ; //////////////////////////////////////////////////////////////// // // PARSER methods // //////////////////////////////////////////////////////////////// /** * Construct a SAX2 parser object */ public Parser2 () { locator = new DocLocator (); setHandlers (); //setSecuirtyConstraintValues() setSecurityConstraintValues() ; } /** * Set up the namespace related features for this parser. SAX2 specifies * these are read-only during a parse, read-write otherwise. */ void setNamespaceFeatures(boolean namespaces, boolean prefixes) { this.namespaces = namespaces; this.prefixes = prefixes; } void setEntityResolver(EntityResolver resolver) { this.resolver = resolver; } public void setDTDHandler(DTDHandler handler) { dtdHandler = handler; } void setContentHandler(ContentHandler handler) { contentHandler = handler; } void setErrorHandler (ErrorHandler handler) { errHandler = handler; } void setLexicalHandler (LexicalHandler handler) { lexicalHandler = handler; } void setDeclHandler (DeclHandler handler) { declHandler = handler; } // XXX Maybe we can remove some of these old locale methods /** * <b>SAX:</b> Used by applications to request locale for diagnostics. * * @param l The locale to use, or null to use system defaults * (which may include only message IDs). * @throws SAXException If no diagnostic messages are available * in that locale. */ public void setLocale (Locale l) throws SAXException { if (l != null && !messages.isLocaleSupported (l.toString ())) throw new SAXException (messages.getMessage (locale, "P-078", new Object [] { l })); locale = l; } /** Returns the diagnostic locale. */ public Locale getLocale () { return locale; } /** * Chooses a client locale to use for diagnostics, using the first * language specified in the list that is supported by this parser. * That locale is then set using <a href="#setLocale(java.util.Locale)"> * setLocale()</a>. Such a list could be provided by a variety of user * preference mechanisms, including the HTTP <em>Accept-Language</em> * header field. * * @see org.apache.crimson.util.MessageCatalog * * @param languages Array of language specifiers, ordered with the most * preferable one at the front. For example, "en-ca" then "fr-ca", * followed by "zh_CN". Both RFC 1766 and Java styles are supported. * @return The chosen locale, or null. */ public Locale chooseLocale (String languages []) throws SAXException { Locale l = messages.chooseLocale (languages); if (l != null) setLocale (l); return l; } /** <b>SAX:</b> Parse a document. */ public void parse (InputSource in) throws SAXException, IOException { init (); parseInternal (in); } /** * Setting this flag enables faster processing of valid standalone * documents: external DTD information is not processed, and no * attribute normalization or defaulting is done. This optimization * is only permitted in non-validating parsers; for validating * parsers, this mode is silently disabled. * * <P> For documents which are declared as standalone, but which are * not valid, a fatal error may be reported for references to externally * defined entities. That could happen in any nonvalidating parser which * did not read externally defined entities. Also, if any attribute * values need normalization or defaulting, it will not be done. */ public void setFastStandalone (boolean value) { fastStandalone = value && !isValidating; } /** * Returns true if standalone documents skip processing of * all external DTD information. */ public boolean isFastStandalone () { return fastStandalone; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?