⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parseradapter.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// ParserAdapter.java - adapt a SAX1 Parser to a SAX2 XMLReader.// http://www.saxproject.org// Written by David Megginson// NO WARRANTY!  This class is in the public domain.// $Id: ParserAdapter.java,v 1.12 2004/12/11 15:41:10 dog Exp $package org.xml.sax.helpers;import java.io.IOException;import java.util.Enumeration;import java.util.Vector;import org.xml.sax.Parser;	// deprecatedimport org.xml.sax.InputSource;import org.xml.sax.Locator;import org.xml.sax.AttributeList; // deprecatedimport org.xml.sax.EntityResolver;import org.xml.sax.DTDHandler;import org.xml.sax.DocumentHandler; // deprecatedimport org.xml.sax.ErrorHandler;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.XMLReader;import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;import org.xml.sax.SAXNotRecognizedException;import org.xml.sax.SAXNotSupportedException;/** * Adapt a SAX1 Parser as a SAX2 XMLReader. * * <blockquote> * <em>This module, both source code and documentation, is in the * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em> * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a> * for further information. * </blockquote> * * <p>This class wraps a SAX1 {@link org.xml.sax.Parser Parser} * and makes it act as a SAX2 {@link org.xml.sax.XMLReader XMLReader}, * with feature, property, and Namespace support.  Note * that it is not possible to report {@link org.xml.sax.ContentHandler#skippedEntity * skippedEntity} events, since SAX1 does not make that information available.</p> * * <p>This adapter does not test for duplicate Namespace-qualified * attribute names.</p> * * @since SAX 2.0 * @author David Megginson * @version 2.0.1 (sax2r2) * @see org.xml.sax.helpers.XMLReaderAdapter * @see org.xml.sax.XMLReader * @see org.xml.sax.Parser */public class ParserAdapter implements XMLReader, DocumentHandler{    ////////////////////////////////////////////////////////////////////    // Constructors.    ////////////////////////////////////////////////////////////////////    /**     * Construct a new parser adapter.     *     * <p>Use the "org.xml.sax.parser" property to locate the     * embedded SAX1 driver.</p>     *     * @exception SAXException If the embedded driver     *            cannot be instantiated or if the     *            org.xml.sax.parser property is not specified.     */    public ParserAdapter ()      throws SAXException    {	super();	String driver = System.getProperty("org.xml.sax.parser");	try {	    setup(ParserFactory.makeParser());	} catch (ClassNotFoundException e1) {	    throw new		SAXException("Cannot find SAX1 driver class " +			     driver, e1);	} catch (IllegalAccessException e2) {	    throw new		SAXException("SAX1 driver class " +			     driver +			     " found but cannot be loaded", e2);	} catch (InstantiationException e3) {	    throw new		SAXException("SAX1 driver class " +			     driver +			     " loaded but cannot be instantiated", e3);	} catch (ClassCastException e4) {	    throw new		SAXException("SAX1 driver class " +			     driver +			     " does not implement org.xml.sax.Parser");	} catch (NullPointerException e5) {	    throw new 		SAXException("System property org.xml.sax.parser not specified");	}    }    /**     * Construct a new parser adapter.     *     * <p>Note that the embedded parser cannot be changed once the     * adapter is created; to embed a different parser, allocate     * a new ParserAdapter.</p>     *     * @param parser The SAX1 parser to embed.     * @exception java.lang.NullPointerException If the parser parameter     *            is null.     */    public ParserAdapter (Parser parser)    {	super();	setup(parser);    }    /**     * Internal setup method.     *     * @param parser The embedded parser.     * @exception java.lang.NullPointerException If the parser parameter     *            is null.     */    private void setup (Parser parser)    {	if (parser == null) {	    throw new		NullPointerException("Parser argument must not be null");	}	this.parser = parser;	atts = new AttributesImpl();	nsSupport = new NamespaceSupport();	attAdapter = new AttributeListAdapter();    }    ////////////////////////////////////////////////////////////////////    // Implementation of org.xml.sax.XMLReader.    ////////////////////////////////////////////////////////////////////    //    // Internal constants for the sake of convenience.    //    private final static String FEATURES = "http://xml.org/sax/features/";    private final static String NAMESPACES = FEATURES + "namespaces";    private final static String NAMESPACE_PREFIXES = FEATURES + "namespace-prefixes";    private final static String XMLNS_URIs = FEATURES + "xmlns-uris";    /**     * Set a feature flag for the parser.     *     * <p>The only features recognized are namespaces and      * namespace-prefixes.</p>     *     * @param name The feature name, as a complete URI.     * @param value The requested feature value.     * @exception SAXNotRecognizedException If the feature     *            can't be assigned or retrieved.     * @exception SAXNotSupportedException If the feature     *            can't be assigned that value.     * @see org.xml.sax.XMLReader#setFeature     */    public void setFeature (String name, boolean value)	throws SAXNotRecognizedException, SAXNotSupportedException    {	if (name.equals(NAMESPACES)) {	    checkNotParsing("feature", name);	    namespaces = value;	    if (!namespaces && !prefixes) {		prefixes = true;	    }	} else if (name.equals(NAMESPACE_PREFIXES)) {	    checkNotParsing("feature", name);	    prefixes = value;	    if (!prefixes && !namespaces) {		namespaces = true;	    }	} else if (name.equals(XMLNS_URIs)) {	    checkNotParsing("feature", name);	    uris = value;	} else {	    throw new SAXNotRecognizedException("Feature: " + name);	}    }    /**     * Check a parser feature flag.     *     * <p>The only features recognized are namespaces and      * namespace-prefixes.</p>     *     * @param name The feature name, as a complete URI.     * @return The current feature value.     * @exception SAXNotRecognizedException If the feature     *            value can't be assigned or retrieved.     * @exception SAXNotSupportedException If the     *            feature is not currently readable.     * @see org.xml.sax.XMLReader#setFeature     */    public boolean getFeature (String name)	throws SAXNotRecognizedException, SAXNotSupportedException    {	if (name.equals(NAMESPACES)) {	    return namespaces;	} else if (name.equals(NAMESPACE_PREFIXES)) {	    return prefixes;	} else if (name.equals(XMLNS_URIs)) {	    return uris;	} else {	    throw new SAXNotRecognizedException("Feature: " + name);	}    }    /**     * Set a parser property.     *     * <p>No properties are currently recognized.</p>     *     * @param name The property name.     * @param value The property value.     * @exception SAXNotRecognizedException If the property     *            value can't be assigned or retrieved.     * @exception SAXNotSupportedException If the property     *            can't be assigned that value.     * @see org.xml.sax.XMLReader#setProperty     */    public void setProperty (String name, Object value)	throws SAXNotRecognizedException, SAXNotSupportedException    {	throw new SAXNotRecognizedException("Property: " + name);    }    /**     * Get a parser property.     *     * <p>No properties are currently recognized.</p>     *     * @param name The property name.     * @return The property value.     * @exception SAXNotRecognizedException If the property     *            value can't be assigned or retrieved.     * @exception SAXNotSupportedException If the property     *            value is not currently readable.     * @see org.xml.sax.XMLReader#getProperty     */    public Object getProperty (String name)	throws SAXNotRecognizedException, SAXNotSupportedException    {	throw new SAXNotRecognizedException("Property: " + name);    }    /**     * Set the entity resolver.     *     * @param resolver The new entity resolver.     * @see org.xml.sax.XMLReader#setEntityResolver     */    public void setEntityResolver (EntityResolver resolver)    {	entityResolver = resolver;    }    /**     * Return the current entity resolver.     *     * @return The current entity resolver, or null if none was supplied.     * @see org.xml.sax.XMLReader#getEntityResolver     */    public EntityResolver getEntityResolver ()    {	return entityResolver;    }    /**     * Set the DTD handler.     *     * @param handler the new DTD handler     * @see org.xml.sax.XMLReader#setEntityResolver     */    public void setDTDHandler (DTDHandler handler)    {	dtdHandler = handler;    }    /**     * Return the current DTD handler.     *     * @return the current DTD handler, or null if none was supplied     * @see org.xml.sax.XMLReader#getEntityResolver     */    public DTDHandler getDTDHandler ()    {	return dtdHandler;    }    /**     * Set the content handler.     *     * @param handler the new content handler     * @see org.xml.sax.XMLReader#setEntityResolver     */    public void setContentHandler (ContentHandler handler)    {	contentHandler = handler;    }    /**     * Return the current content handler.     *     * @return The current content handler, or null if none was supplied.     * @see org.xml.sax.XMLReader#getEntityResolver     */    public ContentHandler getContentHandler ()    {	return contentHandler;    }    /**     * Set the error handler.     *     * @param handler The new error handler.     * @see org.xml.sax.XMLReader#setEntityResolver     */    public void setErrorHandler (ErrorHandler handler)    {	errorHandler = handler;    }    /**     * Return the current error handler.     *     * @return The current error handler, or null if none was supplied.     * @see org.xml.sax.XMLReader#getEntityResolver     */    public ErrorHandler getErrorHandler ()    {	return errorHandler;    }    /**     * Parse an XML document.     *     * @param systemId The absolute URL of the document.     * @exception java.io.IOException If there is a problem reading     *            the raw content of the document.     * @exception SAXException If there is a problem     *            processing the document.     * @see #parse(org.xml.sax.InputSource)     * @see org.xml.sax.Parser#parse(java.lang.String)     */    public void parse (String systemId)	throws IOException, SAXException    {	parse(new InputSource(systemId));    }    /**     * Parse an XML document.     *     * @param input An input source for the document.     * @exception java.io.IOException If there is a problem reading     *            the raw content of the document.     * @exception SAXException If there is a problem     *            processing the document.     * @see #parse(java.lang.String)     * @see org.xml.sax.Parser#parse(org.xml.sax.InputSource)     */    public void parse (InputSource input)	throws IOException, SAXException    {	if (parsing) {	    throw new SAXException("Parser is already in use");	}	setupParser();	parsing = true;	try {	    parser.parse(input);	} finally {	    parsing = false;	}	parsing = false;    }    ////////////////////////////////////////////////////////////////////    // Implementation of org.xml.sax.DocumentHandler.    ////////////////////////////////////////////////////////////////////    /**     * Adapter implementation method; do not call.     * Adapt a SAX1 document locator event.     *     * @param locator A document locator.     * @see org.xml.sax.ContentHandler#setDocumentLocator     */    public void setDocumentLocator (Locator locator)    {	this.locator = locator;	if (contentHandler != null) {	    contentHandler.setDocumentLocator(locator);	}    }    /**     * Adapter implementation method; do not call.     * Adapt a SAX1 start document event.     *     * @exception SAXException The client may raise a     *            processing exception.     * @see org.xml.sax.DocumentHandler#startDocument     */    public void startDocument ()	throws SAXException    {	if (contentHandler != null) {	    contentHandler.startDocument();	}    }    /**     * Adapter implementation method; do not call.     * Adapt a SAX1 end document event.     *     * @exception SAXException The client may raise a     *            processing exception.     * @see org.xml.sax.DocumentHandler#endDocument     */    public void endDocument ()	throws SAXException    {	if (contentHandler != null) {	    contentHandler.endDocument();	}    }    /**     * Adapter implementation method; do not call.     * Adapt a SAX1 startElement event.     *     * <p>If necessary, perform Namespace processing.</p>     *     * @param qName The qualified (prefixed) name.     * @param qAtts The XML attribute list (with qnames).     * @exception SAXException The client may raise a     *            processing exception.     */    public void startElement (String qName, AttributeList qAtts)	throws SAXException    {				// These are exceptions from the				// first pass; they should be				// ignored if there's a second pass,				// but reported otherwise.	Vector exceptions = null;				// If we're not doing Namespace				// processing, dispatch this quickly.	if (!namespaces) {	    if (contentHandler != null) {		attAdapter.setAttributeList(qAtts);		contentHandler.startElement("", "", qName.intern(),					    attAdapter);	    }	    return;	}				// OK, we're doing Namespace processing.	nsSupport.pushContext();	int length = qAtts.getLength();					// First pass:  handle NS decls	for (int i = 0; i < length; i++) {	    String attQName = qAtts.getName(i);	    if (!attQName.startsWith("xmlns"))		continue;				// Could be a declaration...	    String prefix;	    int n = attQName.indexOf(':');	    			// xmlns=...	    if (n == -1 && attQName.length () == 5) {		prefix = "";	    } else if (n != 5) {		// XML namespaces spec doesn't discuss "xmlnsf:oo"		// (and similarly named) attributes ... at most, warn		continue;	    } else 		// xmlns:foo=...		prefix = attQName.substring(n+1);

⌨️ 快捷键说明

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