abstractsaxparser.java

来自「JAVA 所有包」· Java 代码 · 共 1,706 行 · 第 1/5 页

JAVA
1,706
字号
/* * Copyright 2001-2005 The Apache Software Foundation. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.sun.org.apache.xerces.internal.parsers;import java.io.IOException;import java.util.Locale;import com.sun.org.apache.xerces.internal.impl.Constants;import com.sun.org.apache.xerces.internal.xs.PSVIProvider;import com.sun.org.apache.xerces.internal.util.EntityResolverWrapper;import com.sun.org.apache.xerces.internal.util.EntityResolver2Wrapper;import com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper;import com.sun.org.apache.xerces.internal.util.SAXMessageFormatter;import com.sun.org.apache.xerces.internal.util.SymbolHash;import com.sun.org.apache.xerces.internal.util.XMLSymbols;import com.sun.org.apache.xerces.internal.xni.Augmentations;import com.sun.org.apache.xerces.internal.xni.NamespaceContext;import com.sun.org.apache.xerces.internal.xni.QName;import com.sun.org.apache.xerces.internal.xni.XMLAttributes;import com.sun.org.apache.xerces.internal.xni.XMLLocator;import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;import com.sun.org.apache.xerces.internal.xni.XMLString;import com.sun.org.apache.xerces.internal.xni.XNIException;import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;import com.sun.org.apache.xerces.internal.xni.parser.XMLParseException;import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration;import com.sun.org.apache.xerces.internal.xs.AttributePSVI;import com.sun.org.apache.xerces.internal.xs.ElementPSVI;import org.xml.sax.AttributeList;import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;import org.xml.sax.DTDHandler;import org.xml.sax.DocumentHandler;import org.xml.sax.EntityResolver;import org.xml.sax.ErrorHandler;import org.xml.sax.InputSource;import org.xml.sax.Locator;import org.xml.sax.Parser;import org.xml.sax.SAXException;import org.xml.sax.SAXNotRecognizedException;import org.xml.sax.SAXNotSupportedException;import org.xml.sax.SAXParseException;import org.xml.sax.XMLReader;import org.xml.sax.ext.Attributes2;import org.xml.sax.ext.DeclHandler;import org.xml.sax.ext.EntityResolver2;import org.xml.sax.ext.LexicalHandler;import org.xml.sax.ext.Locator2;import org.xml.sax.helpers.LocatorImpl;/** * This is the base class of all SAX parsers. It implements both the * SAX1 and SAX2 parser functionality, while the actual pipeline is * defined in the parser configuration. * * @author Arnaud Le Hors, IBM * @author Andy Clark, IBM * * @version $Id: AbstractSAXParser.java,v 1.2.6.1 2005/09/06 12:51:15 sunithareddy Exp $ */public abstract class AbstractSAXParser    extends AbstractXMLDocumentParser    implements PSVIProvider, // PSVI               Parser, XMLReader // SAX1, SAX2{    //    // Constants    //    // features    /** Feature identifier: namespaces. */    protected static final String NAMESPACES =        Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE;    /** Feature identifier: namespace prefixes. */    protected static final String NAMESPACE_PREFIXES =        Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE;    /** Feature id: string interning. */    protected static final String STRING_INTERNING =        Constants.SAX_FEATURE_PREFIX + Constants.STRING_INTERNING_FEATURE;        /** Feature identifier: allow notation and unparsed entity events to be sent out of order. */    // this is not meant to be a recognized feature, but we need it here to use    // if it is already a recognized feature for the pipeline    protected static final String ALLOW_UE_AND_NOTATION_EVENTS =        Constants.SAX_FEATURE_PREFIX + Constants.ALLOW_DTD_EVENTS_AFTER_ENDDTD_FEATURE;    /** Recognized features. */    private static final String[] RECOGNIZED_FEATURES = {        NAMESPACES,        NAMESPACE_PREFIXES,        STRING_INTERNING,    };    // properties    /** Property id: lexical handler. */    protected static final String LEXICAL_HANDLER =         Constants.SAX_PROPERTY_PREFIX + Constants.LEXICAL_HANDLER_PROPERTY;    /** Property id: declaration handler. */    protected static final String DECLARATION_HANDLER =        Constants.SAX_PROPERTY_PREFIX + Constants.DECLARATION_HANDLER_PROPERTY;    /** Property id: DOM node. */    protected static final String DOM_NODE =         Constants.SAX_PROPERTY_PREFIX + Constants.DOM_NODE_PROPERTY;    /** Recognized properties. */    private static final String[] RECOGNIZED_PROPERTIES = {        LEXICAL_HANDLER,        DECLARATION_HANDLER,        DOM_NODE,    };    //    // Data    //    // features    /** Namespaces. */    protected boolean fNamespaces;    /** Namespace prefixes. */    protected boolean fNamespacePrefixes = false;        /** Lexical handler parameter entities. */    protected boolean fLexicalHandlerParameterEntities = true;        /** Standalone document declaration. */    protected boolean fStandalone;        /** Resolve DTD URIs. */    protected boolean fResolveDTDURIs = true;        /** Use EntityResolver2. */    protected boolean fUseEntityResolver2 = true;        /**      * XMLNS URIs: Namespace declarations in the      * http://www.w3.org/2000/xmlns/ namespace.     */    protected boolean fXMLNSURIs = false;    // parser handlers    /** Content handler. */    protected ContentHandler fContentHandler;    /** Document handler. */    protected DocumentHandler fDocumentHandler;        /** Namespace context */    protected NamespaceContext fNamespaceContext;    /** DTD handler. */    protected org.xml.sax.DTDHandler fDTDHandler;    /** Decl handler. */    protected DeclHandler fDeclHandler;    /** Lexical handler. */    protected LexicalHandler fLexicalHandler;    protected QName fQName = new QName();    // state    /**     * True if a parse is in progress. This state is needed because     * some features/properties cannot be set while parsing (e.g.     * validation and namespaces).     */    protected boolean fParseInProgress = false;    // track the version of the document being parsed    protected String fVersion;    // temp vars    private final AttributesProxy fAttributesProxy = new AttributesProxy();    private Augmentations fAugmentations = null;    // temporary buffer for sending normalized values    // REVISIT: what should be the size of the buffer?    private static final int BUFFER_SIZE = 20;    private char[] fCharBuffer =  new char[BUFFER_SIZE];    // allows us to keep track of whether an attribute has    // been declared twice, so that we can avoid exposing the    // second declaration to any registered DeclHandler    protected SymbolHash fDeclaredAttrs = null;    //    // Constructors    //    /** Default constructor. */    protected AbstractSAXParser(XMLParserConfiguration config) {        super(config);        config.addRecognizedFeatures(RECOGNIZED_FEATURES);        config.addRecognizedProperties(RECOGNIZED_PROPERTIES);        try {            config.setFeature(ALLOW_UE_AND_NOTATION_EVENTS, false);        }        catch (XMLConfigurationException e) {            // it wasn't a recognized feature, so we don't worry about it        }    } // <init>(XMLParserConfiguration)    //    // XMLDocumentHandler methods    //    /**     * The start of the document.     *     * @param locator The document locator, or null if the document     *                 location cannot be reported during the parsing     *                 of this document. However, it is <em>strongly</em>     *                 recommended that a locator be supplied that can     *                 at least report the system identifier of the     *                 document.     * @param encoding The auto-detected IANA encoding name of the entity     *                 stream. This value will be null in those situations     *                 where the entity encoding is not auto-detected (e.g.     *                 internal entities or a document entity that is     *                 parsed from a java.io.Reader).     * @param namespaceContext     *                 The namespace context in effect at the     *                 start of this document.     *                 This object represents the current context.     *                 Implementors of this class are responsible     *                 for copying the namespace bindings from the     *                 the current context (and its parent contexts)     *                 if that information is important.     * @param augs     Additional information that may include infoset augmentations     *     * @throws XNIException Thrown by handler to signal an error.     */    public void startDocument(XMLLocator locator, String encoding,                               NamespaceContext namespaceContext, Augmentations augs)        throws XNIException {                fNamespaceContext = namespaceContext;        try {            // SAX1            if (fDocumentHandler != null) {                if (locator != null) {                    fDocumentHandler.setDocumentLocator(new LocatorProxy(locator));                }                fDocumentHandler.startDocument();            }            // SAX2            if (fContentHandler != null) {                if (locator != null) {                    fContentHandler.setDocumentLocator(new LocatorProxy(locator));                }                fContentHandler.startDocument();            }        }        catch (SAXException e) {            throw new XNIException(e);        }    } // startDocument(locator,encoding,augs)    /**     * Notifies of the presence of an XMLDecl line in the document. If     * present, this method will be called immediately following the     * startDocument call.     *      * @param version    The XML version.     * @param encoding   The IANA encoding name of the document, or null if     *                   not specified.     * @param standalone The standalone value, or null if not specified.     * @param augs   Additional information that may include infoset augmentations     *     * @throws XNIException Thrown by handler to signal an error.     */    public void xmlDecl(String version, String encoding, String standalone, Augmentations augs)        throws XNIException {        // the version need only be set once; if        // document's XML 1.0|1.1, that's how it'll stay        fVersion = version;        fStandalone = "yes".equals(standalone);    } // xmlDecl(String,String,String)    /**     * Notifies of the presence of the DOCTYPE line in the document.     *     * @param rootElement The name of the root element.     * @param publicId    The public identifier if an external DTD or null     *                    if the external DTD is specified using SYSTEM.     * @param systemId    The system identifier if an external DTD, null     *                    otherwise.     * @param augs     Additional information that may include infoset augmentations     *     * @throws XNIException Thrown by handler to signal an error.     */    public void doctypeDecl(String rootElement,                            String publicId, String systemId, Augmentations augs)        throws XNIException {        fInDTD = true;        try {            // SAX2 extension            if (fLexicalHandler != null) {                fLexicalHandler.startDTD(rootElement, publicId, systemId);            }        }        catch (SAXException e) {            throw new XNIException(e);        }        // is there a DeclHandler?        if(fDeclHandler != null) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?