📄 saxbuilder.java
字号:
/*--
$Id: SAXBuilder.java,v 1.92 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.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.util.*;
import org.jdom.*;
import org.xml.sax.*;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Builds a JDOM document from files, streams, readers, URLs, or a SAX {@link
* org.xml.sax.InputSource} instance using a SAX parser. The builder uses a
* third-party SAX parser (chosen by JAXP by default, or you can choose
* manually) to handle the parsing duties and simply listens to the SAX events
* to construct a document. Details which SAX does not provide, such as
* whitespace outside the root element, are not represented in the JDOM
* document. Information about SAX can be found at <a
* href="http://www.saxproject.org">http://www.saxproject.org</a>.
* <p>
* Known issues: Relative paths for a {@link DocType} or {@link EntityRef} may
* be converted by the SAX parser into absolute paths.
*
* @version $Revision: 1.92 $, $Date: 2007/11/10 05:29:00 $
* @author Jason Hunter
* @author Brett McLaughlin
* @author Dan Schaffer
* @author Philip Nelson
* @author Alex Rosen
*/
public class SAXBuilder {
private static final String CVS_ID =
"@(#) $RCSfile: SAXBuilder.java,v $ $Revision: 1.92 $ $Date: 2007/11/10 05:29:00 $ $Name: jdom_1_1 $";
/**
* Default parser class to use. This is used when no other parser
* is given and JAXP isn't available.
*/
private static final String DEFAULT_SAX_DRIVER =
"org.apache.xerces.parsers.SAXParser";
/** Whether validation should occur */
private boolean validate;
/** Whether expansion of entities should occur */
private boolean expand = true;
/** Adapter class to use */
private String saxDriverClass;
/** ErrorHandler class to use */
private ErrorHandler saxErrorHandler = null;
/** EntityResolver class to use */
private EntityResolver saxEntityResolver = null;
/** DTDHandler class to use */
private DTDHandler saxDTDHandler = null;
/** XMLFilter instance to use */
private XMLFilter saxXMLFilter = null;
/** The factory for creating new JDOM objects */
private JDOMFactory factory = new DefaultJDOMFactory();
/** Whether to ignore ignorable whitespace */
private boolean ignoringWhite = false;
/** Whether to ignore all whitespace content */
private boolean ignoringBoundaryWhite = false;
/** User-specified features to be set on the SAX parser */
private HashMap features = new HashMap(5);
/** User-specified properties to be set on the SAX parser */
private HashMap properties = new HashMap(5);
/**
* Whether parser reuse is allowed.
* <p>Default: <code>true</code></p>
*/
private boolean reuseParser = true;
/** The current SAX parser, if parser reuse has been activated. */
private XMLReader saxParser = null;
/**
* Creates a new SAXBuilder which will attempt to first locate
* a parser via JAXP, then will try to use a set of default
* SAX Drivers. The underlying parser will not validate.
*/
public SAXBuilder() {
this(false);
}
/**
* Creates a new SAXBuilder which will attempt to first locate
* a parser via JAXP, then will try to use a set of default
* SAX Drivers. The underlying parser will validate or not
* according to the given parameter.
*
* @param validate <code>boolean</code> indicating if
* validation should occur.
*/
public SAXBuilder(boolean validate) {
this.validate = validate;
}
/**
* Creates a new SAXBuilder using the specified SAX parser.
* The underlying parser will not validate.
*
* @param saxDriverClass <code>String</code> name of SAX Driver
* to use for parsing.
*/
public SAXBuilder(String saxDriverClass) {
this(saxDriverClass, false);
}
/**
* Creates a new SAXBuilder using the specified SAX parser.
* The underlying parser will validate or not
* according to the given parameter.
*
* @param saxDriverClass <code>String</code> name of SAX Driver
* to use for parsing.
* @param validate <code>boolean</code> indicating if
* validation should occur.
*/
public SAXBuilder(String saxDriverClass, boolean validate) {
this.saxDriverClass = saxDriverClass;
this.validate = validate;
}
/**
* Returns the driver class assigned in the constructor, or null if none.
*
* @return the driver class assigned in the constructor
*/
public String getDriverClass() {
return saxDriverClass;
}
/**
* Returns the current {@link org.jdom.JDOMFactory} in use.
* @return the factory in use
*/
public JDOMFactory getFactory() {
return factory;
}
/**
* This sets a custom JDOMFactory for the builder. Use this to build
* the tree with your own subclasses of the JDOM classes.
*
* @param factory <code>JDOMFactory</code> to use
*/
public void setFactory(JDOMFactory factory) {
this.factory = factory;
}
/**
* Returns whether validation is to be performed during the build.
*
* @return whether validation is to be performed during the build
*/
public boolean getValidation() {
return validate;
}
/**
* This sets validation for the builder.
*
* @param validate <code>boolean</code> indicating whether validation
* should occur.
*/
public void setValidation(boolean validate) {
this.validate = validate;
}
/**
* Returns the {@link ErrorHandler} assigned, or null if none.
* @return the ErrorHandler assigned, or null if none
*/
public ErrorHandler getErrorHandler() {
return saxErrorHandler;
}
/**
* This sets custom ErrorHandler for the <code>Builder</code>.
*
* @param errorHandler <code>ErrorHandler</code>
*/
public void setErrorHandler(ErrorHandler errorHandler) {
saxErrorHandler = errorHandler;
}
/**
* Returns the {@link EntityResolver} assigned, or null if none.
*
* @return the EntityResolver assigned
*/
public EntityResolver getEntityResolver() {
return saxEntityResolver;
}
/**
* This sets custom EntityResolver for the <code>Builder</code>.
*
* @param entityResolver <code>EntityResolver</code>
*/
public void setEntityResolver(EntityResolver entityResolver) {
saxEntityResolver = entityResolver;
}
/**
* Returns the {@link DTDHandler} assigned, or null if none.
*
* @return the DTDHandler assigned
*/
public DTDHandler getDTDHandler() {
return saxDTDHandler;
}
/**
* This sets custom DTDHandler for the <code>Builder</code>.
*
* @param dtdHandler <code>DTDHandler</code>
*/
public void setDTDHandler(DTDHandler dtdHandler) {
saxDTDHandler = dtdHandler;
}
/**
* Returns the {@link XMLFilter} used during parsing, or null if none.
*
* @return the XMLFilter used during parsing
*/
public XMLFilter getXMLFilter() {
return saxXMLFilter;
}
/**
* This sets a custom {@link org.xml.sax.XMLFilter} for the builder.
*
* @param xmlFilter the filter to use
*/
public void setXMLFilter(XMLFilter xmlFilter) {
saxXMLFilter = xmlFilter;
}
/**
* Returns whether element content whitespace is to be ignored during the
* build.
*
* @return whether element content whitespace is to be ignored during the
* build
*/
public boolean getIgnoringElementContentWhitespace() {
return ignoringWhite;
}
/**
* 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;
}
/**
* 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;
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -