📄 saxhandler.java
字号:
/*--
$Id: SAXHandler.java,v 1.73 2007/11/10 05:29:00 jhunter Exp $
Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
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 disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The name "JDOM" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact <request_AT_jdom_DOT_org>.
4. Products derived from this software may not be called "JDOM", nor
may "JDOM" appear in their name, without prior written permission
from the JDOM Project Management <request_AT_jdom_DOT_org>.
In addition, we request (but do not require) that you include in the
end-user documentation provided with the redistribution and/or in the
software itself an acknowledgement equivalent to the following:
"This product includes software developed by the
JDOM Project (http://www.jdom.org/)."
Alternatively, the acknowledgment may be graphical using the logos
available at http://www.jdom.org/images/logos.
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 JDOM AUTHORS OR THE PROJECT
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 JDOM Project and was originally
created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
on the JDOM Project, please see <http://www.jdom.org/>.
*/
package org.jdom.input;
import java.util.*;
import org.jdom.*;
import org.xml.sax.*;
import org.xml.sax.ext.*;
import org.xml.sax.helpers.*;
/**
* A support class for {@link SAXBuilder}.
*
* @version $Revision: 1.73 $, $Date: 2007/11/10 05:29:00 $
* @author Brett McLaughlin
* @author Jason Hunter
* @author Philip Nelson
* @author Bradley S. Huffman
* @author phil@triloggroup.com
*/
public class SAXHandler extends DefaultHandler implements LexicalHandler,
DeclHandler,
DTDHandler {
private static final String CVS_ID =
"@(#) $RCSfile: SAXHandler.java,v $ $Revision: 1.73 $ $Date: 2007/11/10 05:29:00 $ $Name: jdom_1_1 $";
/** Hash table to map SAX attribute type names to JDOM attribute types. */
private static final Map attrNameToTypeMap = new HashMap(13);
/** <code>Document</code> object being built */
private Document document;
/** <code>Element</code> object being built */
private Element currentElement;
/** Indicator of where in the document we are */
private boolean atRoot;
/** Indicator of whether we are in the DocType. Note that the DTD consists
* of both the internal subset (inside the <!DOCTYPE> tag) and the
* external subset (in a separate .dtd file). */
private boolean inDTD = false;
/** Indicator of whether we are in the internal subset */
private boolean inInternalSubset = false;
/** Indicator of whether we previously were in a CDATA */
private boolean previousCDATA = false;
/** Indicator of whether we are in a CDATA */
private boolean inCDATA = false;
/** Indicator of whether we should expand entities */
private boolean expand = true;
/** Indicator of whether we are actively suppressing (non-expanding) a
current entity */
private boolean suppress = false;
/** How many nested entities we're currently within */
private int entityDepth = 0; // XXX may not be necessary anymore?
/** Temporary holder for namespaces that have been declared with
* startPrefixMapping, but are not yet available on the element */
private List declaredNamespaces;
/** Temporary holder for the internal subset */
private StringBuffer internalSubset = new StringBuffer();
/** Temporary holder for Text and CDATA */
private TextBuffer textBuffer = new TextBuffer();
/** The external entities defined in this document */
private Map externalEntities;
/** The JDOMFactory used for JDOM object creation */
private JDOMFactory factory;
/** Whether to ignore ignorable whitespace */
private boolean ignoringWhite = false;
/** Whether to ignore text containing all whitespace */
private boolean ignoringBoundaryWhite = false;
/** The SAX Locator object provided by the parser */
private Locator locator;
/**
* Class initializer: Populate a table to translate SAX attribute
* type names into JDOM attribute type value (integer).
* <p>
* <b>Note that all the mappings defined below are compliant with
* the SAX 2.0 specification exception for "ENUMERATION" with is
* specific to Crimson 1.1.X and Xerces 2.0.0-betaX which report
* attributes of enumerated types with a type "ENUMERATION"
* instead of the expected "NMTOKEN".
* </p>
* <p>
* Note also that Xerces 1.4.X is not SAX 2.0 compliant either
* but handling its case requires
* {@link #getAttributeType specific code}.
* </p>
*/
static {
attrNameToTypeMap.put("CDATA",
new Integer(Attribute.CDATA_TYPE));
attrNameToTypeMap.put("ID",
new Integer(Attribute.ID_TYPE));
attrNameToTypeMap.put("IDREF",
new Integer(Attribute.IDREF_TYPE));
attrNameToTypeMap.put("IDREFS",
new Integer(Attribute.IDREFS_TYPE));
attrNameToTypeMap.put("ENTITY",
new Integer(Attribute.ENTITY_TYPE));
attrNameToTypeMap.put("ENTITIES",
new Integer(Attribute.ENTITIES_TYPE));
attrNameToTypeMap.put("NMTOKEN",
new Integer(Attribute.NMTOKEN_TYPE));
attrNameToTypeMap.put("NMTOKENS",
new Integer(Attribute.NMTOKENS_TYPE));
attrNameToTypeMap.put("NOTATION",
new Integer(Attribute.NOTATION_TYPE));
attrNameToTypeMap.put("ENUMERATION",
new Integer(Attribute.ENUMERATED_TYPE));
}
/**
* This will create a new <code>SAXHandler</code> that listens to SAX
* events and creates a JDOM Document. The objects will be constructed
* using the default factory.
*/
public SAXHandler() {
this(null);
}
/**
* This will create a new <code>SAXHandler</code> that listens to SAX
* events and creates a JDOM Document. The objects will be constructed
* using the provided factory.
*
* @param factory <code>JDOMFactory</code> to be used for constructing
* objects
*/
public SAXHandler(JDOMFactory factory) {
if (factory != null) {
this.factory = factory;
} else {
this.factory = new DefaultJDOMFactory();
}
atRoot = true;
declaredNamespaces = new ArrayList();
externalEntities = new HashMap();
document = this.factory.document(null);
}
/**
* Pushes an element onto the tree under construction. Allows subclasses
* to put content under a dummy root element which is useful for building
* content that would otherwise be a non-well formed document.
*
* @param element root element under which content will be built
*/
protected void pushElement(Element element) {
if (atRoot) {
document.setRootElement(element); // XXX should we use a factory call?
atRoot = false;
}
else {
factory.addContent(currentElement, element);
}
currentElement = element;
}
/**
* Returns the document. Should be called after parsing is complete.
*
* @return <code>Document</code> - Document that was built
*/
public Document getDocument() {
return document;
}
/**
* Returns the factory used for constructing objects.
*
* @return <code>JDOMFactory</code> - the factory used for
* constructing objects.
*
* @see #SAXHandler(org.jdom.JDOMFactory)
*/
public JDOMFactory getFactory() {
return factory;
}
/**
* This sets whether or not to expand entities during the build.
* A true means to expand entities as normal content. A false means to
* leave entities unexpanded as <code>EntityRef</code> objects. The
* default is true.
*
* @param expand <code>boolean</code> indicating whether entity expansion
* should occur.
*/
public void setExpandEntities(boolean expand) {
this.expand = expand;
}
/**
* Returns whether or not entities will be expanded during the
* build.
*
* @return <code>boolean</code> - whether entity expansion
* will occur during build.
*
* @see #setExpandEntities
*/
public boolean getExpandEntities() {
return expand;
}
/**
* Specifies whether or not the parser should elminate whitespace in
* element content (sometimes known as "ignorable whitespace") when
* building the document. Only whitespace which is contained within
* element content that has an element only content model will be
* eliminated (see XML Rec 3.2.1). For this setting to take effect
* requires that validation be turned on. The default value of this
* setting is <code>false</code>.
*
* @param ignoringWhite Whether to ignore ignorable whitespace
*/
public void setIgnoringElementContentWhitespace(boolean ignoringWhite) {
this.ignoringWhite = ignoringWhite;
}
/**
* Specifies whether or not the parser should elminate text() nodes
* containing only whitespace when building the document. See
* {@link SAXBuilder#setIgnoringBoundaryWhitespace(boolean)}.
*
* @param ignoringBoundaryWhite Whether to ignore only whitespace content
*/
public void setIgnoringBoundaryWhitespace(boolean ignoringBoundaryWhite) {
this.ignoringBoundaryWhite = ignoringBoundaryWhite;
}
/**
* Returns whether or not the parser will elminate element content
* containing only whitespace.
*
* @return <code>boolean</code> - whether only whitespace content will
* be ignored during build.
*
* @see #setIgnoringBoundaryWhitespace
*/
public boolean getIgnoringBoundaryWhitespace() {
return ignoringBoundaryWhite;
}
/**
* Returns whether or not the parser will elminate whitespace in
* element content (sometimes known as "ignorable whitespace") when
* building the document.
*
* @return <code>boolean</code> - whether ignorable whitespace will
* be ignored during build.
*
* @see #setIgnoringElementContentWhitespace
*/
public boolean getIgnoringElementContentWhitespace() {
return ignoringWhite;
}
public void startDocument() {
if (locator != null) {
document.setBaseURI(locator.getSystemId());
}
}
/**
* This is called when the parser encounters an external entity
* declaration.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -