org.jdom.input
Class SAXBuilder

java.lang.Object
  |
  +--org.jdom.input.SAXBuilder

public class SAXBuilder
extends java.lang.Object

SAXBuilder builds a JDOM tree using SAX. Information about SAX can be found at http://www.megginson.com/SAX.

Known issues: Relative paths for a DocType or EntityRef may be converted by the SAX parser into absolute paths

Version:
$Revision: 1.65 $, $Date: 2002/04/11 06:19:10 $
Author:
Jason Hunter, Brett McLaughlin, Dan Schaffer, Philip Nelson, Alex Rosen

Field Summary
protected  JDOMFactory factory
          The factory for creating new JDOM objects
 
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.
protected  java.net.URL fileToURL(java.io.File f)
          Imitation of File.toURL(), a JDK 1.2 method, reimplemented here to work with JDK 1.1.
 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)
           
 void setFeature(java.lang.String name, boolean value)
           This sets a feature on the SAX parser.
 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 setValidation(boolean validate)
           This sets validation for the builder.
 void setXMLFilter(org.xml.sax.XMLFilter xmlFilter)
           This sets 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

factory

protected JDOMFactory factory
The factory for creating new JDOM objects
Constructor Detail

SAXBuilder

public 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. The underlying parser will not validate.


SAXBuilder

public 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. The underlying parser will validate or not according to the given parameter.

Parameters:
validate - boolean indicating if validation should occur.

SAXBuilder

public SAXBuilder(java.lang.String saxDriverClass)

Creates a new SAXBuilder using the specified SAX parser. The underlying parser will not validate.

Parameters:
saxDriverClass - String name of SAX Driver to use for parsing.

SAXBuilder

public SAXBuilder(java.lang.String saxDriverClass,
                  boolean validate)

Creates a new SAXBuilder using the specified SAX parser. The underlying parser will validate or not according to the given parameter.

Parameters:
saxDriverClass - String name of SAX Driver to use for parsing.
validate - boolean indicating if validation should occur.
Method Detail

setFactory

public void setFactory(JDOMFactory factory)

setValidation

public void setValidation(boolean validate)

This sets validation for the builder.

Parameters:
validate - boolean indicating whether validation should occur.

setErrorHandler

public void setErrorHandler(org.xml.sax.ErrorHandler errorHandler)

This sets custom ErrorHandler for the Builder.

Parameters:
errorHandler - ErrorHandler

setEntityResolver

public void setEntityResolver(org.xml.sax.EntityResolver entityResolver)

This sets custom EntityResolver for the Builder.

Parameters:
entityResolver - EntityResolver

setDTDHandler

public void setDTDHandler(org.xml.sax.DTDHandler dtdHandler)

This sets custom DTDHandler for the Builder.

Parameters:
dtdHandler - DTDHandler

setXMLFilter

public void setXMLFilter(org.xml.sax.XMLFilter xmlFilter)

This sets custom XMLFilter for the Builder.

Parameters:
xmlFilter - XMLFilter

setIgnoringElementContentWhitespace

public 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. 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 false.

Parameters:
ignoringWhite - Whether to ignore ignorable whitespace

setFeature

public void setFeature(java.lang.String name,
                       boolean value)

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.

Parameters:
name - The feature name, which is a fully-qualified URI.
value - The requested state of the feature (true or false).

setProperty

public void setProperty(java.lang.String name,
                        java.lang.Object value)

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.

Parameters:
name - The property name, which is a fully-qualified URI.
value - The requested value for the property.

build

public Document build(org.xml.sax.InputSource in)
               throws JDOMException,
                      java.io.IOException

This builds a document from the supplied input source.

Parameters:
in - InputSource to read from.
Returns:
Document - resultant Document object.
Throws:
JDOMException - when errors occur in parsing.
java.io.IOException - when an I/O error prevents a document from being fully parsed.

createContentHandler

protected SAXHandler createContentHandler()

This creates the SAXHandler that will be used to build the Document.

Returns:
SAXHandler - resultant SAXHandler object.

configureContentHandler

protected void configureContentHandler(SAXHandler contentHandler)

This configures the SAXHandler that will be used to build the Document.

The default implementation simply passes through some configuration settings that were set on the SAXBuilder: setExpandEntities() and setIgnoringElementContentWhitespace().


createParser

protected org.xml.sax.XMLReader createParser()
                                      throws JDOMException

This creates the XMLReader to be used for reading the XML document.

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.

Returns:
XMLReader - resultant XMLReader object.

configureParser

protected void configureParser(org.xml.sax.XMLReader parser,
                               SAXHandler contentHandler)
                        throws JDOMException

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.


build

public Document build(java.io.InputStream in)
               throws JDOMException,
                      java.io.IOException

This builds a document from the supplied input stream.

Parameters:
in - InputStream to read from.
Returns:
Document - resultant Document object.
Throws:
JDOMException - when errors occur in parsing.
java.io.IOException - when an I/O error prevents a document from being fully parsed.

build

public Document build(java.io.File file)
               throws JDOMException,
                      java.io.IOException

This builds a document from the supplied filename.

Parameters:
file - File to read from.
Returns:
Document - resultant Document object.
Throws:
JDOMException - when errors occur in parsing.
java.io.IOException - when an I/O error prevents a document from being fully parsed.

build

public Document build(java.net.URL url)
               throws JDOMException,
                      java.io.IOException

This builds a document from the supplied URL.

Parameters:
url - URL to read from.
Returns:
Document - resultant Document object.
Throws:
JDOMException - when errors occur in parsing.
java.io.IOException - when an I/O error prevents a document from being fully parsed.

build

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.

Parameters:
in - InputStream to read from.
systemId - base for resolving relative URIs
Returns:
Document - resultant Document object.
Throws:
JDOMException - when errors occur in parsing.
java.io.IOException - when an I/O error prevents a document from being fully parsed.

build

public Document build(java.io.Reader characterStream)
               throws JDOMException,
                      java.io.IOException

This builds a document from the supplied Reader.

Parameters:
in - Reader to read from.
Returns:
Document - resultant Document object.
Throws:
JDOMException - when errors occur in parsing.
java.io.IOException - when an I/O error prevents a document from being fully parsed.

build

public Document build(java.io.Reader characterStream,
                      java.lang.String SystemId)
               throws JDOMException,
                      java.io.IOException

This builds a document from the supplied Reader.

Parameters:
in - Reader to read from.
systemId - base for resolving relative URIs
Returns:
Document - resultant Document object.
Throws:
JDOMException - when errors occur in parsing.
java.io.IOException - when an I/O error prevents a document from being fully parsed.

build

public Document build(java.lang.String systemId)
               throws JDOMException,
                      java.io.IOException

This builds a document from the supplied URI.

Parameters:
systemId - URI for the input
Returns:
Document - resultant Document object.
Throws:
JDOMException - when errors occur in parsing.
java.io.IOException - when an I/O error prevents a document from being fully parsed.

fileToURL

protected java.net.URL fileToURL(java.io.File f)
                          throws java.net.MalformedURLException
Imitation of File.toURL(), a JDK 1.2 method, reimplemented here to work with JDK 1.1.
Parameters:
f - the file to convert
Returns:
the file path converted to a file: URL
See Also:
File

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 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

Parameters:
expand - boolean indicating whether entity expansion should occur.


Copyright © 2002 Jason Hunter, Brett McLaughlin. All Rights Reserved.