java.lang.ObjectSAXBuilder
public class SAXBuilder
Builds a JDOM document from files, streams, readers, URLs, or a 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 http://www.saxproject.org.
Known issues: Relative paths for a DocType
or EntityRef
may
be converted by the SAX parser into absolute paths.
Field Summary | |
---|---|
private static java.lang.String |
CVS_ID
|
private static java.lang.String |
DEFAULT_SAX_DRIVER
Default parser class to use. |
private boolean |
expand
Whether expansion of entities should occur |
private JDOMFactory |
factory
The factory for creating new JDOM objects |
private java.util.HashMap |
features
User-specified features to be set on the SAX parser |
private boolean |
ignoringWhite
Whether to ignore ignorable whitespace |
private java.util.HashMap |
properties
User-specified properties to be set on the SAX parser |
private boolean |
reuseParser
Whether parser reuse is allowed. |
private java.lang.String |
saxDriverClass
Adapter class to use |
private org.xml.sax.DTDHandler |
saxDTDHandler
DTDHandler class to use |
private org.xml.sax.EntityResolver |
saxEntityResolver
EntityResolver class to use |
private org.xml.sax.ErrorHandler |
saxErrorHandler
ErrorHandler class to use |
private org.xml.sax.XMLReader |
saxParser
The current SAX parser, if parser reuse has been activated. |
private org.xml.sax.XMLFilter |
saxXMLFilter
XMLFilter instance to use |
private boolean |
validate
Whether validation should occur |
Constructor Summary | |
---|---|
SAXBuilder()
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. |
|
SAXBuilder(boolean validate)
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. |
|
SAXBuilder(java.lang.String saxDriverClass)
Creates a new SAXBuilder using the specified SAX parser. |
|
SAXBuilder(java.lang.String saxDriverClass,
boolean validate)
Creates a new SAXBuilder using the specified SAX parser. |
Method Summary | |
---|---|
Document |
build(java.io.File file)
This builds a document from the supplied filename. |
Document |
build(org.xml.sax.InputSource in)
This builds a document from the supplied input source. |
Document |
build(java.io.InputStream in)
This builds a document from the supplied input stream. |
Document |
build(java.io.InputStream in,
java.lang.String systemId)
This builds a document from the supplied input stream. |
Document |
build(java.io.Reader characterStream)
This builds a document from the supplied Reader. |
Document |
build(java.io.Reader characterStream,
java.lang.String systemId)
This builds a document from the supplied Reader. |
Document |
build(java.lang.String systemId)
This builds a document from the supplied URI. |
Document |
build(java.net.URL url)
This builds a document from the supplied URL. |
protected void |
configureContentHandler(SAXHandler contentHandler)
This configures the SAXHandler that will be used to build the Document. |
protected void |
configureParser(org.xml.sax.XMLReader parser,
SAXHandler contentHandler)
This configures the XMLReader to be used for reading the XML document. |
protected SAXHandler |
createContentHandler()
This creates the SAXHandler that will be used to build the Document. |
protected org.xml.sax.XMLReader |
createParser()
This creates the XMLReader to be used for reading the XML document. |
private static java.net.URL |
fileToURL(java.io.File file)
Custom File.toUrl() implementation to handle special chars in file names |
java.lang.String |
getDriverClass()
Returns the driver class assigned in the constructor, or null if none. |
org.xml.sax.DTDHandler |
getDTDHandler()
Returns the DTDHandler assigned, or null if none. |
org.xml.sax.EntityResolver |
getEntityResolver()
Returns the EntityResolver assigned, or null if none. |
org.xml.sax.ErrorHandler |
getErrorHandler()
Returns the ErrorHandler assigned, or null if none. |
boolean |
getExpandEntities()
Returns whether or not entities are being expanded into normal text content. |
JDOMFactory |
getFactory()
Returns the current JDOMFactory in use. |
boolean |
getIgnoringElementContentWhitespace()
Returns whether element content whitespace is to be ignored during the build. |
boolean |
getReuseParser()
Returns whether the contained SAX parser instance is reused across multiple parses. |
boolean |
getValidation()
Returns whether validation is to be performed during the build. |
org.xml.sax.XMLFilter |
getXMLFilter()
Returns the XMLFilter used during parsing, or null if none. |
private void |
internalSetFeature(org.xml.sax.XMLReader parser,
java.lang.String feature,
boolean value,
java.lang.String displayName)
Tries to set a feature on the parser. |
private void |
internalSetProperty(org.xml.sax.XMLReader parser,
java.lang.String property,
java.lang.Object value,
java.lang.String displayName)
Tries to set a property on the parser. |
void |
setDTDHandler(org.xml.sax.DTDHandler dtdHandler)
This sets custom DTDHandler for the Builder . |
void |
setEntityResolver(org.xml.sax.EntityResolver entityResolver)
This sets custom EntityResolver for the Builder . |
void |
setErrorHandler(org.xml.sax.ErrorHandler errorHandler)
This sets custom ErrorHandler for the Builder . |
void |
setExpandEntities(boolean expand)
This sets whether or not to expand entities for the builder. |
void |
setFactory(JDOMFactory factory)
This sets a custom JDOMFactory for the builder. |
void |
setFeature(java.lang.String name,
boolean value)
This sets a feature on the SAX parser. |
private void |
setFeaturesAndProperties(org.xml.sax.XMLReader parser,
boolean coreFeatures)
|
void |
setIgnoringElementContentWhitespace(boolean ignoringWhite)
Specifies whether or not the parser should elminate whitespace in element content (sometimes known as "ignorable whitespace") when building the document. |
void |
setProperty(java.lang.String name,
java.lang.Object value)
This sets a property on the SAX parser. |
void |
setReuseParser(boolean reuseParser)
Specifies whether this builder shall reuse the same SAX parser when performing subsequent parses or allocate a new parser for each parse. |
void |
setValidation(boolean validate)
This sets validation for the builder. |
void |
setXMLFilter(org.xml.sax.XMLFilter xmlFilter)
This sets a custom XMLFilter for the builder. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final java.lang.String CVS_ID
private static final java.lang.String DEFAULT_SAX_DRIVER
private boolean validate
private boolean expand
private java.lang.String saxDriverClass
private org.xml.sax.ErrorHandler saxErrorHandler
private org.xml.sax.EntityResolver saxEntityResolver
private org.xml.sax.DTDHandler saxDTDHandler
private org.xml.sax.XMLFilter saxXMLFilter
private JDOMFactory factory
private boolean ignoringWhite
private java.util.HashMap features
private java.util.HashMap properties
private boolean reuseParser
Default: true
private org.xml.sax.XMLReader saxParser
Constructor Detail |
---|
public SAXBuilder()
public SAXBuilder(boolean validate)
validate
- boolean
indicating if
validation should occur.public SAXBuilder(java.lang.String saxDriverClass)
saxDriverClass
- String
name of SAX Driver
to use for parsing.public SAXBuilder(java.lang.String saxDriverClass, boolean validate)
saxDriverClass
- String
name of SAX Driver
to use for parsing.validate
- boolean
indicating if
validation should occur.Method Detail |
---|
public java.lang.String getDriverClass()
public JDOMFactory getFactory()
JDOMFactory
in use.
public void setFactory(JDOMFactory factory)
factory
- JDOMFactory
to usepublic boolean getValidation()
public void setValidation(boolean validate)
validate
- boolean
indicating whether validation
should occur.public org.xml.sax.ErrorHandler getErrorHandler()
ErrorHandler
assigned, or null if none.
public void setErrorHandler(org.xml.sax.ErrorHandler errorHandler)
Builder
.
errorHandler
- ErrorHandler
public org.xml.sax.EntityResolver getEntityResolver()
EntityResolver
assigned, or null if none.
public void setEntityResolver(org.xml.sax.EntityResolver entityResolver)
Builder
.
entityResolver
- EntityResolver
public org.xml.sax.DTDHandler getDTDHandler()
DTDHandler
assigned, or null if none.
public void setDTDHandler(org.xml.sax.DTDHandler dtdHandler)
Builder
.
dtdHandler
- DTDHandler
public org.xml.sax.XMLFilter getXMLFilter()
XMLFilter
used during parsing, or null if none.
public void setXMLFilter(org.xml.sax.XMLFilter xmlFilter)
XMLFilter
for the builder.
xmlFilter
- the filter to usepublic boolean getIgnoringElementContentWhitespace()
public void setIgnoringElementContentWhitespace(boolean ignoringWhite)
false
.
ignoringWhite
- Whether to ignore ignorable whitespacepublic boolean getReuseParser()
public void setReuseParser(boolean reuseParser)
true
(parser reuse).
Note: As SAX parser instances are not thread safe, the parser reuse feature should not be used with SAXBuilder instances shared among threads.
reuseParser
- Whether to reuse the SAX parser.public void setFeature(java.lang.String name, boolean value)
NOTE: SAXBuilder requires that some particular features of the SAX parser be set up in certain ways for it to work properly. The list of such features may change in the future. Therefore, the use of this method may cause parsing to break, and even if it doesn't break anything today it might break parsing in a future JDOM version, because what JDOM parsers require may change over time. Use with caution.
name
- The feature name, which is a fully-qualified URI.value
- The requested state of the feature (true or false).public void setProperty(java.lang.String name, java.lang.Object value)
NOTE: SAXBuilder requires that some particular properties of the SAX parser be set up in certain ways for it to work properly. The list of such properties may change in the future. Therefore, the use of this method may cause parsing to break, and even if it doesn't break anything today it might break parsing in a future JDOM version, because what JDOM parsers require may change over time. Use with caution.
name
- The property name, which is a fully-qualified URI.value
- The requested value for the property.public Document build(org.xml.sax.InputSource in) throws JDOMException, java.io.IOException
in
- InputSource
to read from
Document
resultant Document object
JDOMException
- when errors occur in parsing
java.io.IOException
- when an I/O error prevents a document
from being fully parsedprotected SAXHandler createContentHandler()
SAXHandler
- resultant SAXHandler object.protected void configureContentHandler(SAXHandler contentHandler)
The default implementation simply passes through some configuration settings that were set on the SAXBuilder: setExpandEntities() and setIgnoringElementContentWhitespace().
protected org.xml.sax.XMLReader createParser() throws JDOMException
The default behavior is to (1) use the saxDriverClass, if it has been set, (2) try to obtain a parser from JAXP, if it is available, and (3) if all else fails, use a hard-coded default parser (currently the Xerces parser). Subclasses may override this method to determine the parser to use in a different way.
XMLReader
- resultant XMLReader object.
JDOMException
protected void configureParser(org.xml.sax.XMLReader parser, SAXHandler contentHandler) throws JDOMException
The default implementation sets various options on the given XMLReader,
such as validation, DTD resolution, entity handlers, etc., according
to the options that were set (e.g. via setEntityResolver
)
and set various SAX properties and features that are required for JDOM
internals. These features may change in future releases, so change this
behavior at your own risk.
JDOMException
private void setFeaturesAndProperties(org.xml.sax.XMLReader parser, boolean coreFeatures) throws JDOMException
JDOMException
private void internalSetFeature(org.xml.sax.XMLReader parser, java.lang.String feature, boolean value, java.lang.String displayName) throws JDOMException
JDOMException
private void internalSetProperty(org.xml.sax.XMLReader parser, java.lang.String property, java.lang.Object value, java.lang.String displayName) throws JDOMException
Tries to set a property on the parser. If the property cannot be set, throws a JDOMException describing the problem.
JDOMException
public Document build(java.io.InputStream in) throws JDOMException, java.io.IOException
This builds a document from the supplied input stream.
in
- InputStream
to read from
Document
resultant Document object
JDOMException
- when errors occur in parsing
java.io.IOException
- when an I/O error prevents a document
from being fully parsed.public Document build(java.io.File file) throws JDOMException, java.io.IOException
This builds a document from the supplied filename.
file
- File
to read from
Document
resultant Document object
JDOMException
- when errors occur in parsing
java.io.IOException
- when an I/O error prevents a document
from being fully parsedpublic Document build(java.net.URL url) throws JDOMException, java.io.IOException
This builds a document from the supplied URL.
url
- URL
to read from.
Document
- resultant Document object.
JDOMException
- when errors occur in parsing
java.io.IOException
- when an I/O error prevents a document
from being fully parsed.public Document build(java.io.InputStream in, java.lang.String systemId) throws JDOMException, java.io.IOException
This builds a document from the supplied input stream.
in
- InputStream
to read from.systemId
- base for resolving relative URIs
Document
resultant Document object
JDOMException
- when errors occur in parsing
java.io.IOException
- when an I/O error prevents a document
from being fully parsedpublic Document build(java.io.Reader characterStream) throws JDOMException, java.io.IOException
This builds a document from the supplied Reader. It's the programmer's responsibility to make sure the reader matches the encoding of the file. It's often easier and safer to use an InputStream rather than a Reader, and to let the parser auto-detect the encoding from the XML declaration.
characterStream
- Reader
to read from
Document
resultant Document object
JDOMException
- when errors occur in parsing
java.io.IOException
- when an I/O error prevents a document
from being fully parsedpublic Document build(java.io.Reader characterStream, java.lang.String systemId) throws JDOMException, java.io.IOException
This builds a document from the supplied Reader. It's the programmer's responsibility to make sure the reader matches the encoding of the file. It's often easier and safer to use an InputStream rather than a Reader, and to let the parser auto-detect the encoding from the XML declaration.
characterStream
- Reader
to read from.systemId
- base for resolving relative URIs
Document
resultant Document object
JDOMException
- when errors occur in parsing
java.io.IOException
- when an I/O error prevents a document
from being fully parsedpublic Document build(java.lang.String systemId) throws JDOMException, java.io.IOException
This builds a document from the supplied URI.
systemId
- URI for the input
Document
resultant Document object
JDOMException
- when errors occur in parsing
java.io.IOException
- when an I/O error prevents a document
from being fully parsedprivate static java.net.URL fileToURL(java.io.File file) throws java.net.MalformedURLException
file
- file object whose path will be converted
java.net.MalformedURLException
- if there's a problem constructing a URLpublic boolean getExpandEntities()
public void setExpandEntities(boolean expand)
This sets whether or not to expand entities for the builder.
A true means to expand entities as normal content. A false means to
leave entities unexpanded as EntityRef
objects. The
default is true.
When this setting is false, the internal DTD subset is retained; when this setting is true, the internal DTD subset is not retained.
Note that Xerces (at least up to 1.4.4) has a bug where entities in attribute values will be misreported if this flag is turned off, resulting in entities to appear within element content. When turning entity expansion off either avoid entities in attribute values, or use another parser like Crimson. http://nagoya.apache.org/bugzilla/show_bug.cgi?id=6111
expand
- boolean
indicating whether entity expansion
should occur.