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

📄 domparser.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* DomParser.java --    Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package gnu.xml.util;import java.util.Enumeration;import java.util.Locale;import org.xml.sax.*;import org.xml.sax.helpers.AttributesImpl;import org.xml.sax.helpers.NamespaceSupport;import org.xml.sax.ext.DeclHandler;import org.xml.sax.ext.DefaultHandler2;import org.xml.sax.ext.LexicalHandler;import org.w3c.dom.*;/** * This parser emits SAX2 parsing events as it traverses a DOM tree, using * any conformant implementation of DOM.  It exposes all SAX1 features, * and the following SAX2 features and properties (as * identified by standard URIs which are not fully provided here).  Note * that if a Level 1 DOM implementation is given, then this behaves as if * namespaces were disabled, and namespace prefixes were enabled.  </p> * * <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>false (does no parsing)</td></tr> * <tr><td>(URL)/external-parameter-entities</td> *	<td>false (does no parsing)</td></tr> * <tr><td>(URL)/namespaces</td> *	<td>Value is fixed at <em>true</em></td></tr> * <tr><td>(URL)/namespace-prefixes</td> *	<td>Value is settable, defaulting to <em>false</em> *	(<code>xmlns</code> attributes hidden, and names aren't prefixed) *	</td></tr> * <tr><td>(URL)/string-interning</td> *	<td>Value is fixed at <em>false</em> (DOM provides no *	guarantees as to interning)</td></tr> * <tr><td>(URL)/validation</td> *	<td>false (does no parsing)</td></tr> * <tr><td>(URL)/lexical-handler/parameter-entities</td> *	<td>false (DOM doesn't do parameter entities)</td></tr> * * <tr><td colspan=2><center><em>Properties ... URL prefix is * <b>http://xml.org/sax/properties/</b></em></center></td></tr> * * * <tr><td>(URL)/dom-node</td> *	<td>This property may be set before parsing to hold a DOM *	<em>Document</em> node; any arguments given to <em>parse</em> *	methods are ignored.  When retrieved *	during a parse, this value contains the "current" DOM node. *	</td></tr> * <tr><td>(URL)/declaration-handler</td> *	<td>A declaration handler may be provided.  Declaration of external *	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.  While the start and end of *	any external subset are reported, expansion of other parameter *	entities (e.g. inside attribute list declarations) is not exposed. *	Expansion of general entities within attributes is also not exposed *	(see below).</td></tr> * </table> * * <P> The consequences of modifying a DOM document tree as it is being walked * by this "parser" are unspecified; don't do it! </P> * * @author David Brownell */final public class DomParser implements XMLReader{    // Stuff used internally to route events correctly    private DefaultHandler2	defaultHandler = new DefaultHandler2 ();    // per-parse SAX stuff    private ContentHandler	contentHandler = defaultHandler;    private DTDHandler		dtdHandler = defaultHandler;    private DeclHandler		declHandler = defaultHandler;    private LexicalHandler	lexicalHandler = defaultHandler;    // shared context    private ErrorHandler	errHandler = defaultHandler;    private EntityResolver	resolver = defaultHandler;    private Locale		locale = Locale.getDefault ();    // parser state    private Node		start;    private Node		current;    private boolean		isL2;    private boolean		showNamespaces = true;    private boolean		showXML1_0 = false;    private NamespaceSupport	prefixStack = new NamespaceSupport ();    private boolean		isDocument;    /**     * Constructs an unitialized <b>SAX2</b> parser.     */    public DomParser () {    }     /**     * Constructs an <b>SAX2</b> parser initialized to traverse the specified     * DOM tree.  If the node is a document, the startDocument() and     * endDocument() calls bracket the calls exposing children.     */    public DomParser (Node node) {	setStart (node);    }     // stuff that most components in an application should be sharing:    // resolver and error locale.    /**     * <b>SAX2</b>: Returns the object used when resolving external     * entities during parsing (both general and parameter entities).     */    public EntityResolver getEntityResolver ()    {	return resolver;    }    /**     * <b>SAX1</b>: Provides an object which may be used when resolving external     * entities during parsing (both general and parameter entities).     */    public void setEntityResolver (EntityResolver resolver)    {	if (resolver == null)	    resolver = defaultHandler;	this.resolver = resolver;    }    /**     * <b>SAX1</b>: Identifies the locale which the parser should use for the     * diagnostics it provides.     *     * @exception SAXException as defined in the specification for     *	<em>org.xml.sax.Parser.setLocale()</em>     */    public void setLocale (Locale locale)    throws SAXException    {	if (locale == null)	    locale = Locale.getDefault ();	this.locale = locale;    }        // different modules will tend to handle error handling the same,    // but it may not be the same through the whole app    /**     * <b>SAX2</b>: Returns the object used to receive callbacks for XML     * errors of all levels (fatal, nonfatal, warning).     */    public ErrorHandler getErrorHandler ()    {	return errHandler;    }    /**     * <b>SAX1</b>: Provides an object which receives callbacks for XML errors     * of all levels (fatal, nonfatal, warning).     */    public void setErrorHandler (ErrorHandler handler)    {	if (handler == null)	    handler = defaultHandler;	errHandler = handler;    }    // stuff different parts of a module will handle differently    /**     * <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.     */    public void setContentHandler (ContentHandler handler)    {	if (handler == null)	    handler = defaultHandler;	contentHandler = handler;    }    /**     * <b>SAX2</b>: Returns the object used to process declarations related     * to notations and unparsed entities.     */    public DTDHandler getDTDHandler ()    {	return dtdHandler;    }    /**     * <b>SAX1</b>: Provides an object which may be used to intercept     * declarations related to notations and unparsed entities.     */    public void setDTDHandler (DTDHandler handler)    {	if (handler == null)	    handler = defaultHandler;	dtdHandler = handler;    }    /**     * <b>SAX1</b>:  Parses the previously provided DOM document (the     * input parameter is ignored).  When this returns, that same     * document may be parsed again without needing a "reset".     *     * @param uri ignored (pass an empty string)     * @exception SAXException as defined in the specification for     *	<em>org.xml.sax.Parser.parse()</em>     */    public void parse (String uri) throws SAXException    {	parse ();    }    /**     * <b>SAX1</b>:  Parses the previously provided DOM document (the     * input parameter is ignored).  When this returns, that same     * document may be parsed again without needing a "reset".     *     * @param input ignored     * @exception SAXException as defined in the specification for     *	<em>org.xml.sax.Parser.parse()</em>     */    public void parse (InputSource input) throws SAXException    {	parse ();    }    private void parse () throws SAXException    {	try {	    walk ();	} finally {	    if (isDocument)		contentHandler.endDocument ();	    current = null;	    prefixStack.reset ();	}    }    private boolean getIsL2 (Node node)    {	DOMImplementation	impl;	Document		doc;	if (node instanceof Document)	    doc = (Document) node;	else	    doc = node.getOwnerDocument ();	if (doc == null)	    throw new RuntimeException ("? unowned node - L2 DTD ?");	impl = doc.getImplementation ();	return impl.hasFeature ("XML", "2.0");    }    private static final String FEATURES = "http://xml.org/sax/features/";    private static final String HANDLERS = "http://xml.org/sax/properties/";    /**     * <b>SAX2</b>: Tells whether this parser supports the specified feature.     */    public boolean getFeature (String name)    throws SAXNotRecognizedException, SAXNotSupportedException    {	// basically, none are relevant -- they relate more to	// parsing than to walking a "parse tree".		// FIXME: DOM feature to expose interning?	if ((FEATURES + "validation").equals (name)		|| (FEATURES + "external-general-entities")		    .equals (name)		|| (FEATURES + "external-parameter-entities")		    .equals (name)		|| (FEATURES + "string-interning").equals (name)		)	    return false;    	if ((FEATURES + "namespaces").equals (name))	    return showNamespaces;	if ((FEATURES + "namespace-prefixes").equals (name))	    return showXML1_0;	throw new SAXNotRecognizedException (name);    }    /**     * <b>SAX2</b>:  Returns the specified property.  At this time only     * the declaration and lexical handlers, and current the "DOM" node,     * are supported.     */    public Object getProperty (String name)    throws SAXNotRecognizedException, SAXNotSupportedException    {	if ((HANDLERS + "declaration-handler").equals (name))	    return declHandler == defaultHandler ? null : declHandler;	if ((HANDLERS + "lexical-handler").equals (name))	    return lexicalHandler == defaultHandler ? null : lexicalHandler;	if ((HANDLERS + "dom-node").equals (name))	    return current;	// unknown properties	throw new SAXNotRecognizedException (name);    }    /**     * <b>SAX2</b>:  Sets the state of features supported in this parser.     * Only the namespace support features are mutable.     */    public void setFeature (String name, boolean state)    throws SAXNotRecognizedException, SAXNotSupportedException    {	if (current != null)	    throw new IllegalStateException ("feature change midparse");	boolean value = getFeature (name);	if (value == state)	    return;	if ((FEATURES + "namespaces").equals (name)) {	    if (!showXML1_0 && state == false)		throw new SAXNotSupportedException ("Illegal namespace "			+ "processing configuration");	    showNamespaces = state;	    return;	}	if ((FEATURES + "namespace-prefixes").equals (name)) {	    if (!showNamespaces && state == false)		throw new SAXNotSupportedException ("Illegal namespace "			+ "processing configuration");	    showXML1_0 = state;	    return;	}	throw new SAXNotSupportedException (name);    }    /**     * <b>SAX2</b>:  Assigns the specified property.  At this time only

⌨️ 快捷键说明

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