Class SAXBuilder
- All Implemented Interfaces:
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:
- Author:
- Jason Hunter, Brett McLaughlin, Dan Schaffer, Philip Nelson, Alex Rosen, Rolf Lear
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new JAXP-based SAXBuilder.SAXBuilder(boolean validate) Deprecated.SAXBuilder(String saxDriverClass) Deprecated.SAXBuilder(String saxDriverClass, boolean validate) Deprecated.SAXBuilder(XMLReaderJDOMFactory readersouce) Creates a new SAXBuilder with the specified XMLReaderJDOMFactory.SAXBuilder(XMLReaderJDOMFactory xmlreaderfactory, SAXHandlerFactory handlerfactory, JDOMFactory jdomfactory) Creates a new SAXBuilder. -
Method Summary
Modifier and TypeMethodDescriptionThis builds a document from the supplied filename.build(InputStream in) This builds a document from the supplied input stream.build(InputStream in, String systemId) This builds a document from the supplied input stream.This builds a document from the supplied Reader.This builds a document from the supplied Reader.This builds a document from the supplied URI.This builds a document from the supplied URL.build(InputSource in) This builds a document from the supplied input source.This method builds a new and reusableSAXEngine.protected voidconfigureParser(XMLReader parser, SAXHandler contentHandler) This configures the XMLReader to be used for reading the XML document.protected XMLReaderAllow overriding classes access to the Parser before it is used in a SAXBuilderEngine.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 anXMLReaderSAX2Factoryinstance.Returns theDTDHandlerassigned, or null if the assignedSAXHandlerwill be used for DTD SAX events.Returns theEntityResolverassigned, or null if none.Returns theErrorHandlerassigned, or null if none.booleanReturns whether or not entities are being expanded into normal text content.Deprecated.as it is replaced bygetJDOMFactory()booleanReturns whether or not the parser will eliminate element content containing only whitespace.booleanReturns whether element content whitespace is to be ignored during the build.Returns the currentJDOMFactoryin use.booleanReturns whether the contained SAX parser instance is reused across multiple parses.Get the SAXHandlerFactory used to supply SAXHandlers to this SAXBuilder.booleanDeprecated.in lieu ofisValidating()Returns theXMLFilterused during parsing, or null if none.Get the current XMLReader factory.booleanReturns whether validation is to be performed during the build.voidsetDTDHandler(DTDHandler dtdHandler) This sets custom DTDHandler for theBuilder.voidsetEntityResolver(EntityResolver entityResolver) This sets custom EntityResolver for theBuilder.voidsetErrorHandler(ErrorHandler errorHandler) This sets custom ErrorHandler for the Builder.voidsetExpandEntities(boolean expand) This sets whether or not to expand entities for the builder.voidsetFactory(JDOMFactory factory) Deprecated.as it is replaced bysetJDOMFactory(JDOMFactory)voidsetFastReconfigure(boolean fastReconfigure) Deprecated.All reused Parsers are now fast-reconfigured.voidsetFeature(String name, boolean value) This sets a feature on the SAX parser.voidsetIgnoringBoundaryWhitespace(boolean ignoringBoundaryWhite) Specifies whether or not the parser should eliminate boundary whitespace, a term that indicates whitespace-only text between element tags.voidsetIgnoringElementContentWhitespace(boolean ignoringWhite) Specifies whether or not the parser should eliminate whitespace in element content (sometimes known as "ignorable whitespace") when building the document.voidsetJDOMFactory(JDOMFactory factory) This sets a custom JDOMFactory for the builder.voidsetProperty(String name, Object value) This sets a property on the SAX parser.voidsetReuseParser(boolean reuseParser) Specifies whether this builder will reuse the same SAX parser when performing subsequent parses or allocate a new parser for each parse.voidsetSAXHandlerFactory(SAXHandlerFactory factory) Set the SAXHandlerFactory to be used by this SAXBuilder.voidsetValidation(boolean validate) Deprecated.voidsetXMLFilter(XMLFilter xmlFilter) This sets a customXMLFilterfor the builder.voidSet the current XMLReader factory.
-
Constructor Details
-
SAXBuilder
public SAXBuilder()Creates a new JAXP-based SAXBuilder. The underlying parser will not validate.- See Also:
-
SAXBuilder
Deprecated.useSAXBuilder(XMLReaderJDOMFactory)with eitherXMLReaders.DTDVALIDATINGorXMLReaders.NONVALIDATINGCreates a new JAXP-based SAXBuilder. The underlying parser will validate (using DTD) or not according to the given parameter. If you want Schema validation then use SAXBuilder(XMLReaders.XSDVALIDATOR)- Parameters:
validate-booleanindicating if DTD validation should occur.- See Also:
-
SAXBuilder
Deprecated.Creates a new SAXBuilder using the specified SAX parser. The underlying parser will not validate.- Parameters:
saxDriverClass-Stringname of SAX Driver to use for parsing.- See Also:
-
SAXBuilder
Deprecated.Creates a new SAXBuilder using the specified SAX2.0 parser source. The underlying parser will validate or not according to the given parameter.- Parameters:
saxDriverClass-Stringname of SAX Driver to use for parsing.validate-booleanindicating if validation should occur.- See Also:
-
SAXBuilder
Creates a new SAXBuilder with the specified XMLReaderJDOMFactory.- Parameters:
readersouce- theXMLReaderJDOMFactorythat supplies XMLReaders. If the value is null then a Non-Validating JAXP-based SAX2.0 parser will be used.- See Also:
-
SAXBuilder
public SAXBuilder(XMLReaderJDOMFactory xmlreaderfactory, SAXHandlerFactory handlerfactory, JDOMFactory jdomfactory) Creates a new SAXBuilder. This is the base constructor for all other SAXBuilder constructors: they all find a way to create a JDOMXMLReaderFactory and then call this constructor with that factory, and theDefaultSAXHandlerFactoryandDefaultJDOMFactory.- Parameters:
xmlreaderfactory- aXMLReaderJDOMFactorythat creates XMLReaders. Specify null for the default.handlerfactory- aSAXHandlerFactorythat creates SAXHandlers Specify null for the default.jdomfactory- aJDOMFactorythat creates JDOM Content. Specify null for the default.- See Also:
-
-
Method Details
-
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 anXMLReaderSAX2Factoryinstance.Returns the driver class assigned in the constructor, or null if none. The driver class is only available if a SAX2 source was specified. This method is available for backward-compatibility with JDOM 1.x- Returns:
- the driver class assigned in the constructor
-
getFactory
Deprecated.as it is replaced bygetJDOMFactory()Returns the currentJDOMFactoryin use.- Returns:
- the factory in use
-
getJDOMFactory
Returns the currentJDOMFactoryin use.- Specified by:
getJDOMFactoryin interfaceSAXEngine- Returns:
- the factory in use
-
setFactory
Deprecated.as it is replaced bysetJDOMFactory(JDOMFactory)This sets a custom JDOMFactory for the builder. Use this to build the tree with your own subclasses of the JDOM classes.- Parameters:
factory-JDOMFactoryto use
-
setJDOMFactory
This sets a custom JDOMFactory for the builder. Use this to build the tree with your own subclasses of the JDOM classes.- Parameters:
factory-JDOMFactoryto use
-
getXMLReaderFactory
Get the current XMLReader factory.- Returns:
- the current JDOMXMLReaderFactory
-
setXMLReaderFactory
Set the current XMLReader factory.- Parameters:
rfac- the JDOMXMLReaderFactory to set. A null rfac will indicate the defaultXMLReaders.NONVALIDATING
-
getSAXHandlerFactory
Get the SAXHandlerFactory used to supply SAXHandlers to this SAXBuilder.- Returns:
- the current SAXHandlerFactory (never null).
-
setSAXHandlerFactory
Set the SAXHandlerFactory to be used by this SAXBuilder.- Parameters:
factory- the required SAXHandlerFactory. A null input factory will request theDefaultSAXHandlerFactory.
-
getValidation
Deprecated.in lieu ofisValidating()Returns whether validation is to be performed during the build.- Returns:
- whether validation is to be performed during the build
-
isValidating
public boolean isValidating()Returns whether validation is to be performed during the build.- Specified by:
isValidatingin interfaceSAXEngine- Returns:
- whether validation is to be performed during the build
-
setValidation
Deprecated.This sets validation for the builder.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, andsetXMLReaderFactory(XMLReaders.NONVALIDATING)
for false.- Parameters:
validate-booleanindicating whether validation should occur.- See Also:
-
getErrorHandler
Returns theErrorHandlerassigned, or null if none. When the SAXBuilder parses a document it will always have an ErrorHandler but it will be an instance ofBuilderErrorHandlerunless you specify a different ErrorHandler insetErrorHandler(ErrorHandler). In other words, a null return value from here indicates a default will be used.- Specified by:
getErrorHandlerin interfaceSAXEngine- Returns:
- the ErrorHandler assigned, or null if SAXBuilder will create a default ErrorHandler when needed.
-
setErrorHandler
This sets custom ErrorHandler for the Builder. Setting a null value will indicate SAXBuilder should create a default ErrorHandler when needed.- Parameters:
errorHandler-ErrorHandler
-
getEntityResolver
Returns theEntityResolverassigned, or null if none.- Specified by:
getEntityResolverin interfaceSAXEngine- Returns:
- the EntityResolver assigned
-
setEntityResolver
This sets custom EntityResolver for theBuilder.- Parameters:
entityResolver-EntityResolver
-
getDTDHandler
Returns theDTDHandlerassigned, or null if the assignedSAXHandlerwill be used for DTD SAX events.- Specified by:
getDTDHandlerin interfaceSAXEngine- Returns:
- the DTDHandler assigned
-
setDTDHandler
This sets custom DTDHandler for theBuilder. Setting a null value indicates that SAXBuilder should use the assigned SAXHandler for DTD processing.- Parameters:
dtdHandler-DTDHandler
-
getXMLFilter
Returns theXMLFilterused during parsing, or null if none.- Returns:
- the XMLFilter used during parsing
-
setXMLFilter
This sets a customXMLFilterfor 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.
- Parameters:
xmlFilter- the XMLFilter to use
-
getIgnoringElementContentWhitespace
public boolean getIgnoringElementContentWhitespace()Returns whether element content whitespace is to be ignored during the build.- Specified by:
getIgnoringElementContentWhitespacein interfaceSAXEngine- Returns:
- whether element content whitespace is to be ignored during the build
-
setIgnoringElementContentWhitespace
public 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. 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 isfalse.- Parameters:
ignoringWhite- Whether to ignore ignorable whitespace
-
getIgnoringBoundaryWhitespace
public boolean getIgnoringBoundaryWhitespace()Returns whether or not the parser will eliminate element content containing only whitespace.- Specified by:
getIgnoringBoundaryWhitespacein interfaceSAXEngine- Returns:
boolean- whether only whitespace content will be ignored during build.- See Also:
-
setIgnoringBoundaryWhitespace
public void setIgnoringBoundaryWhitespace(boolean ignoringBoundaryWhite) Specifies whether or not the parser should eliminate boundary whitespace, a term that indicates whitespace-only text between element tags. This feature is a lot likesetIgnoringElementContentWhitespace(boolean)but this feature is more aggressive and doesn't require validation be turned on. ThesetIgnoringElementContentWhitespace(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 isfalse.- Parameters:
ignoringBoundaryWhite- Whether to ignore whitespace-only text nodes
-
getExpandEntities
public boolean getExpandEntities()Returns whether or not entities are being expanded into normal text content.- Specified by:
getExpandEntitiesin interfaceSAXEngine- Returns:
- whether entities are being expanded
-
setExpandEntities
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
EntityRefobjects. 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
- Parameters:
expand-booleanindicating whether entity expansion should occur.
-
getReuseParser
public boolean getReuseParser()Returns whether the contained SAX parser instance is reused across multiple parses. The default is true.- Returns:
- whether the contained SAX parser instance is reused across multiple parses
-
setReuseParser
public 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. The default value of this setting istrue(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.
- Parameters:
reuseParser- Whether to reuse the SAX parser.
-
setFastReconfigure
Deprecated.All reused Parsers are now fast-reconfigured. No need to set it.Specifies whether this builder will do fast reconfiguration of the underlying SAX parser when reuseParser is true. This improves performance in cases where SAXBuilders are reused and lots of small documents are frequently parsed. This avoids attempting to set features on the SAX parser each time build() is called which result in SaxNotRecognizedExceptions. This should ONLY be set for builders where this specific case is an issue. The default value of this setting isfalse(no fast reconfiguration). If reuseParser is false, calling this has no effect.- Parameters:
fastReconfigure- Whether to do a fast reconfiguration of the parser
-
setFeature
This sets a feature on the SAX parser. See the SAX documentation for more information.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 usesXMLReaderJDOMFactoryinstances to provide XMLReader instances. If you require special configuration on your XMLReader you should consider extending or implementing an XMLReaderJDOMFactory in theorg.jdom2.input.saxpackage.- Parameters:
name- The feature name, which is a fully-qualified URI.value- The requested state of the feature (true or false).
-
setProperty
This sets a property on the SAX parser. See the SAX documentation for more information.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 usesXMLReaderJDOMFactoryinstances to provide XMLReader instances. If you require special configuration on your XMLReader you should consider extending or implementing an XMLReaderJDOMFactory in theorg.jdom2.input.saxpackage.- Parameters:
name- The property name, which is a fully-qualified URI.value- The requested value for the property.
-
buildEngine
This method builds a new and reusableSAXEngine. 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.
- Returns:
- a
SAXEnginerepresenting the current state of the current SAXBuilder settings. - Throws:
JDOMException- if there is any problem initialising the engine.
-
createParser
Allow overriding classes access to the Parser before it is used in a SAXBuilderEngine.- Returns:
- a XMLReader parser.
- Throws:
JDOMException- if there is a problem
-
configureParser
This configures the XMLReader to be used for reading the XML document.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.- Parameters:
parser- the XMLReader to configure.contentHandler- The SAXHandler to use for the XMLReader- Throws:
JDOMException- if configuration fails.
-
build
This builds a document from the supplied input source.- Specified by:
buildin interfaceSAXEngine- Parameters:
in-InputSourceto read from- Returns:
Documentresultant Document object- Throws:
JDOMException- when errors occur in parsingIOException- when an I/O error prevents a document from being fully parsed
-
build
This builds a document from the supplied input stream.
- Specified by:
buildin interfaceSAXEngine- Parameters:
in-InputStreamto read from- Returns:
Documentresultant Document object- Throws:
JDOMException- when errors occur in parsingIOException- when an I/O error prevents a document from being fully parsed.
-
build
This builds a document from the supplied filename.
- Specified by:
buildin interfaceSAXEngine- Parameters:
file-Fileto read from- Returns:
Documentresultant Document object- Throws:
JDOMException- when errors occur in parsingIOException- when an I/O error prevents a document from being fully parsed
-
build
This builds a document from the supplied URL.
- Specified by:
buildin interfaceSAXEngine- Parameters:
url-URLto read from.- Returns:
Document- resultant Document object.- Throws:
JDOMException- when errors occur in parsingIOException- when an I/O error prevents a document from being fully parsed.
-
build
This builds a document from the supplied input stream.
- Specified by:
buildin interfaceSAXEngine- Parameters:
in-InputStreamto read from.systemId- base for resolving relative URIs- Returns:
Documentresultant Document object- Throws:
JDOMException- when errors occur in parsingIOException- when an I/O error prevents a document from being fully parsed
-
build
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.
- Specified by:
buildin interfaceSAXEngine- Parameters:
characterStream-Readerto read from- Returns:
Documentresultant Document object- Throws:
JDOMException- when errors occur in parsingIOException- when an I/O error prevents a document from being fully parsed
-
build
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.
- Specified by:
buildin interfaceSAXEngine- Parameters:
characterStream-Readerto read from.systemId- base for resolving relative URIs- Returns:
Documentresultant Document object- Throws:
JDOMException- when errors occur in parsingIOException- when an I/O error prevents a document from being fully parsed
-
build
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.
- Right:
....build("path/to/file.xml"); - Right:
....build("http://my.example.com/xmlfile"); - Wrong:
....build("<root>datainvalid input: '<'/root>");
....build(new StringReader("<root>datainvalid input: '<'/root>"));- Specified by:
buildin interfaceSAXEngine- Parameters:
systemId- URI for the input- Returns:
Documentresultant Document object- Throws:
JDOMException- when errors occur in parsingIOException- when an I/O error prevents a document from being fully parsed
- Right:
-
SAXBuilder(XMLReaderJDOMFactory)with eitherXMLReaders.DTDVALIDATINGorXMLReaders.NONVALIDATING