public class SAXBuilder extends java.lang.Object implements SAXEngine
SAXbuilder uses a third-party SAX parser (chosen by JAXP by default, or you can configure it manually) to handle the parsing duties and uses an instance of a SAXHandler to listen to the SAX events in order to construct a document with JDOM content using a JDOMFactory. Information about SAX can be found at http://www.saxproject.org.
For a complete description of how SAXBuilder is used, and how to customise
the process you should look at the org.jdom2.input.sax
package
documentation.
JDOM users needing to customise the SAX parsing process have traditionally
sub-classed this SAXBuilder class. In JDOM2 this should never be necessary.
Please read the full documentation of this class, SAXHandler
,
SAXHandlerFactory
, JDOMFactory
, and the package documentation
for org.jdom2.input.sax
before overriding this class. Future versions
of JDOM2 may make this class 'final'. I you feel you have a good reason to
subclass SAXBuilder please mention it on jdom-interest mailing list
so that SAXBuilder can be extended or adapted to handle your use-case.
Neither SAXBuilder nor anything derived from SAXBuilder is thread-safe. You
must ensure that SAXBuilder is used in a single thread, or that sufficient
locking is in place to ensure that SAXBuilder is not concurrently accessed.
See the special note on buildEngine()
.
Known issues:
org.jdom2.input.sax
Constructor and Description |
---|
SAXBuilder()
Creates a new JAXP-based SAXBuilder.
|
SAXBuilder(boolean validate)
Deprecated.
use
SAXBuilder(XMLReaderJDOMFactory) with
either XMLReaders.DTDVALIDATING
or XMLReaders.NONVALIDATING |
SAXBuilder(java.lang.String saxDriverClass)
|
SAXBuilder(java.lang.String saxDriverClass,
boolean validate)
|
SAXBuilder(XMLReaderJDOMFactory readersouce)
Creates a new SAXBuilder with the specified XMLReaderJDOMFactory.
|
SAXBuilder(XMLReaderJDOMFactory xmlreaderfactory,
SAXHandlerFactory handlerfactory,
JDOMFactory jdomfactory)
Creates a new SAXBuilder.
|
Modifier and Type | Method and Description |
---|---|
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.
|
SAXEngine |
buildEngine()
This method builds a new and reusable
SAXEngine . |
protected void |
configureParser(org.xml.sax.XMLReader parser,
SAXHandler contentHandler)
This configures the XMLReader to be used for reading the XML document.
|
protected org.xml.sax.XMLReader |
createParser()
Allow overriding classes access to the Parser before it is used in a
SAXBuilderEngine.
|
java.lang.String |
getDriverClass()
Deprecated.
as the driver class is only available in limited situations
and anyway it had to be supplied in a constructor as either a
direct value or as an
XMLReaderSAX2Factory instance. |
org.xml.sax.DTDHandler |
getDTDHandler()
Returns the
DTDHandler assigned, or null if the assigned
SAXHandler will be used for DTD SAX events. |
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()
Deprecated.
as it is replaced by
getJDOMFactory() |
boolean |
getIgnoringBoundaryWhitespace()
Returns whether or not the parser will eliminate element content
containing only whitespace.
|
boolean |
getIgnoringElementContentWhitespace()
Returns whether element content whitespace is to be ignored during the
build.
|
JDOMFactory |
getJDOMFactory()
Returns the current
JDOMFactory in use. |
boolean |
getReuseParser()
Returns whether the contained SAX parser instance is reused across
multiple parses.
|
SAXHandlerFactory |
getSAXHandlerFactory()
Get the SAXHandlerFactory used to supply SAXHandlers to this SAXBuilder.
|
boolean |
getValidation()
Deprecated.
in lieu of
isValidating() |
org.xml.sax.XMLFilter |
getXMLFilter()
Returns the
XMLFilter used during parsing, or null if none. |
XMLReaderJDOMFactory |
getXMLReaderFactory()
Get the current XMLReader factory.
|
boolean |
isValidating()
Returns whether validation is to be performed during the build.
|
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)
Deprecated.
as it is replaced by
setJDOMFactory(JDOMFactory) |
void |
setFastReconfigure(boolean fastReconfigure)
Deprecated.
All reused Parsers are now fast-reconfigured. No need to set
it.
|
void |
setFeature(java.lang.String name,
boolean value)
This sets a feature on the SAX parser.
|
void |
setIgnoringBoundaryWhitespace(boolean ignoringBoundaryWhite)
Specifies whether or not the parser should eliminate boundary whitespace,
a term that indicates whitespace-only text between element tags.
|
void |
setIgnoringElementContentWhitespace(boolean ignoringWhite)
Specifies whether or not the parser should eliminate whitespace in
element content (sometimes known as "ignorable whitespace") when building
the document.
|
void |
setJDOMFactory(JDOMFactory factory)
This sets a custom JDOMFactory for the builder.
|
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 will reuse the same SAX parser when
performing subsequent parses or allocate a new parser for each parse.
|
void |
setSAXHandlerFactory(SAXHandlerFactory factory)
Set the SAXHandlerFactory to be used by this SAXBuilder.
|
void |
setValidation(boolean validate)
Deprecated.
|
void |
setXMLFilter(org.xml.sax.XMLFilter xmlFilter)
This sets a custom
XMLFilter for the builder. |
void |
setXMLReaderFactory(XMLReaderJDOMFactory rfac)
Set the current XMLReader factory.
|
public SAXBuilder()
@Deprecated public SAXBuilder(boolean validate)
SAXBuilder(XMLReaderJDOMFactory)
with
either XMLReaders.DTDVALIDATING
or XMLReaders.NONVALIDATING
validate
- boolean
indicating if DTD validation should occur.SAXBuilder(XMLReaderJDOMFactory, SAXHandlerFactory,
JDOMFactory)
,
XMLReaders.NONVALIDATING
,
XMLReaders.DTDVALIDATING
,
DefaultSAXHandlerFactory
,
DefaultJDOMFactory
,
for important details on SAXBuilder
@Deprecated public SAXBuilder(java.lang.String saxDriverClass)
SAXBuilder(XMLReaderJDOMFactory)
with
XMLReaderSAX2Factory.XMLReaderSAX2Factory(boolean, String)
saxDriverClass
- String
name of SAX Driver to use for parsing.SAXBuilder(XMLReaderJDOMFactory, SAXHandlerFactory,
JDOMFactory)
,
XMLReaderSAX2Factory
,
DefaultSAXHandlerFactory
,
DefaultJDOMFactory
,
for important details on SAXBuilder
@Deprecated public SAXBuilder(java.lang.String saxDriverClass, boolean validate)
SAXBuilder(XMLReaderJDOMFactory)
with
XMLReaderSAX2Factory.XMLReaderSAX2Factory(boolean, String)
saxDriverClass
- String
name of SAX Driver to use for parsing.validate
- boolean
indicating if validation should occur.SAXBuilder(XMLReaderJDOMFactory, SAXHandlerFactory,
JDOMFactory)
,
XMLReaderSAX2Factory
,
DefaultSAXHandlerFactory
,
DefaultJDOMFactory
,
for important details on SAXBuilder
public SAXBuilder(XMLReaderJDOMFactory readersouce)
readersouce
- the XMLReaderJDOMFactory
that supplies XMLReaders. If the
value is null then a Non-Validating JAXP-based SAX2.0 parser will
be used.SAXBuilder(XMLReaderJDOMFactory, SAXHandlerFactory,
JDOMFactory)
,
XMLReaderJDOMFactory
,
XMLReaders.NONVALIDATING
,
DefaultSAXHandlerFactory
,
DefaultJDOMFactory
,
for important details on SAXBuilder
public SAXBuilder(XMLReaderJDOMFactory xmlreaderfactory, SAXHandlerFactory handlerfactory, JDOMFactory jdomfactory)
DefaultSAXHandlerFactory
and DefaultJDOMFactory
.
xmlreaderfactory
- a XMLReaderJDOMFactory
that creates XMLReaders. Specify
null for the default.handlerfactory
- a SAXHandlerFactory
that creates SAXHandlers Specify null
for the default.jdomfactory
- a JDOMFactory
that creates JDOM Content. Specify null for
the default.XMLReaderJDOMFactory
,
XMLReaders.NONVALIDATING
,
SAXHandlerFactory
,
DefaultSAXHandlerFactory
,
JDOMFactory
,
DefaultJDOMFactory
,
for important details on SAXBuilder
@Deprecated public java.lang.String getDriverClass()
XMLReaderSAX2Factory
instance.@Deprecated public JDOMFactory getFactory()
getJDOMFactory()
JDOMFactory
in use.public JDOMFactory getJDOMFactory()
JDOMFactory
in use.getJDOMFactory
in interface SAXEngine
@Deprecated public void setFactory(JDOMFactory factory)
setJDOMFactory(JDOMFactory)
factory
- JDOMFactory
to usepublic void setJDOMFactory(JDOMFactory factory)
factory
- JDOMFactory
to usepublic XMLReaderJDOMFactory getXMLReaderFactory()
public void setXMLReaderFactory(XMLReaderJDOMFactory rfac)
rfac
- the JDOMXMLReaderFactory to set. A null rfac will indicate the
default XMLReaders.NONVALIDATING
public SAXHandlerFactory getSAXHandlerFactory()
public void setSAXHandlerFactory(SAXHandlerFactory factory)
factory
- the required SAXHandlerFactory. A null input factory will request
the DefaultSAXHandlerFactory
.@Deprecated public boolean getValidation()
isValidating()
public boolean isValidating()
isValidating
in interface SAXEngine
@Deprecated public void setValidation(boolean validate)
setXMLReaderFactory(XMLReaderJDOMFactory)
Do Not Use
JDOM2 introduces the concept of XMLReader factories. The XMLReader is
what determines the type of validation. A simple boolean is not enough to
indicate what sort of validation is required. The
setXMLReaderFactory(XMLReaderJDOMFactory)
method provides a
means to be more specific about validation.
For backward compatibility this method has been retained, but its use is discouraged. It does make some logical choices though. The code is equivalent to:
setXMLReaderFactory(XMLReaders.DTDVALIDATING)for true, and
setXMLReaderFactory(XMLReaders.NONVALIDATING)for false.
validate
- boolean
indicating whether validation should occur.setXMLReaderFactory(XMLReaderJDOMFactory)
,
XMLReaders.NONVALIDATING
,
XMLReaders.DTDVALIDATING
public org.xml.sax.ErrorHandler getErrorHandler()
ErrorHandler
assigned, or null if none. When the
SAXBuilder parses a document it will always have an ErrorHandler but it
will be an instance of BuilderErrorHandler
unless you specify a
different ErrorHandler in setErrorHandler(ErrorHandler)
. In
other words, a null return value from here indicates a default will be
used.getErrorHandler
in interface SAXEngine
public void setErrorHandler(org.xml.sax.ErrorHandler errorHandler)
errorHandler
- ErrorHandler
public org.xml.sax.EntityResolver getEntityResolver()
EntityResolver
assigned, or null if none.getEntityResolver
in interface SAXEngine
public void setEntityResolver(org.xml.sax.EntityResolver entityResolver)
Builder
.entityResolver
- EntityResolver
public org.xml.sax.DTDHandler getDTDHandler()
DTDHandler
assigned, or null if the assigned
SAXHandler
will be used for DTD SAX events.getDTDHandler
in interface SAXEngine
public void setDTDHandler(org.xml.sax.DTDHandler dtdHandler)
Builder
. Setting a null
value indicates that SAXBuilder should use the assigned SAXHandler for
DTD processing.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.
Care should be taken to ensure that the specified xmlFilter is reentrant and thread-safe.
SAXBuilder will set this instance as the parent instance for all XMLReaders that may be created, and these may (depending on SAXBuilder usage) be accessed concurrently. It is the responsibility of the JDOM user to ensure that if the XMLFilter is not thread-safe then neither the SAXBuilder nor any of its SAXEngines are accessed concurrently.
xmlFilter
- the XMLFilter to usepublic boolean getIgnoringElementContentWhitespace()
getIgnoringElementContentWhitespace
in interface SAXEngine
public void setIgnoringElementContentWhitespace(boolean ignoringWhite)
false
.ignoringWhite
- Whether to ignore ignorable whitespacepublic boolean getIgnoringBoundaryWhitespace()
getIgnoringBoundaryWhitespace
in interface SAXEngine
boolean
- whether only whitespace content will be
ignored during build.setIgnoringBoundaryWhitespace(boolean)
public void setIgnoringBoundaryWhitespace(boolean ignoringBoundaryWhite)
setIgnoringElementContentWhitespace(boolean)
but this feature is
more aggressive and doesn't require validation be turned on. The
setIgnoringElementContentWhitespace(boolean)
call impacts the
SAX parse process while this method impacts the JDOM build process, so it
can be beneficial to turn both on for efficiency. For implementation
efficiency, this method actually removes all whitespace-only text()
nodes. That can, in some cases (like between an element tag and a
comment) include whitespace that isn't just boundary whitespace. The
default is false
.ignoringBoundaryWhite
- Whether to ignore whitespace-only text nodespublic boolean getExpandEntities()
getExpandEntities
in interface SAXEngine
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 incorrectly reported if this flag is turned off, resulting in entities appearing 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.public boolean getReuseParser()
public void setReuseParser(boolean reuseParser)
true
(parser reuse).
Note: SAX parser instances are not thread safe (they are not even reentrant), and nor are SAXBuilder instances. Setting parser reuse does not imply the parser is thread-safe.
reuseParser
- Whether to reuse the SAX parser.@Deprecated public void setFastReconfigure(boolean fastReconfigure)
false
(no fast reconfiguration). If reuseParser is false,
calling this has no effect.fastReconfigure
- Whether to do a fast reconfiguration of the parserpublic 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.
JDOM usesXMLReaderJDOMFactory
instances to provide XMLReader
instances. If you require special configuration on your XMLReader you
should consider extending or implementing an XMLReaderJDOMFactory in the
org.jdom2.input.sax
package.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.
JDOM usesXMLReaderJDOMFactory
instances to provide XMLReader
instances. If you require special configuration on your XMLReader you
should consider extending or implementing an XMLReaderJDOMFactory in the
org.jdom2.input.sax
package.name
- The property name, which is a fully-qualified URI.value
- The requested value for the property.public SAXEngine buildEngine() throws JDOMException
SAXEngine
.
Each time this method is called a new instance of a SAXEngine will be
returned.
This method is used internally by the various SAXBuilder.build(*) methods (if any configuration has changed) but can also be used as a mechanism for creating SAXEngines to be used in parsing pools or other optimised structures.
SAXEngine
representing the current state of the
current SAXBuilder settings.JDOMException
- if there is any problem initialising the engine.protected org.xml.sax.XMLReader createParser() throws JDOMException
JDOMException
- if there is a problemprotected 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.
parser
- the XMLReader to configure.contentHandler
- The SAXHandler to use for the XMLReaderJDOMException
- if configuration fails.public Document build(org.xml.sax.InputSource in) throws JDOMException, java.io.IOException
build
in interface SAXEngine
in
- InputSource
to read fromDocument
resultant Document objectJDOMException
- when errors occur in parsingjava.io.IOException
- when an I/O error prevents a document from being fully parsedpublic Document build(java.io.InputStream in) throws JDOMException, java.io.IOException
This builds a document from the supplied input stream.
build
in interface SAXEngine
in
- InputStream
to read fromDocument
resultant Document objectJDOMException
- when errors occur in parsingjava.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.
build
in interface SAXEngine
file
- File
to read fromDocument
resultant Document objectJDOMException
- when errors occur in parsingjava.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.
build
in interface SAXEngine
url
- URL
to read from.Document
- resultant Document object.JDOMException
- when errors occur in parsingjava.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.
build
in interface SAXEngine
in
- InputStream
to read from.systemId
- base for resolving relative URIsDocument
resultant Document objectJDOMException
- when errors occur in parsingjava.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.
build
in interface SAXEngine
characterStream
- Reader
to read fromDocument
resultant Document objectJDOMException
- when errors occur in parsingjava.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.
build
in interface SAXEngine
characterStream
- Reader
to read from.systemId
- base for resolving relative URIsDocument
resultant Document objectJDOMException
- when errors occur in parsingjava.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. The URI is typically a file name, or a URL. Do not use this method for parsing XML content that is in a Java String variable.
....build("path/to/file.xml");
....build("http://my.example.com/xmlfile");
....build("<root>data</root>");
....build(new StringReader("<root>data</root>"));
build
in interface SAXEngine
systemId
- URI for the inputDocument
resultant Document objectJDOMException
- when errors occur in parsingjava.io.IOException
- when an I/O error prevents a document from being fully parsedCopyright © 2021 Jason Hunter, Brett McLaughlin. All Rights Reserved.