org.xml.sax.helpers

Class ParserAdapter

Implemented Interfaces:
DocumentHandler, XMLReader

public class ParserAdapter
extends java.lang.Object
implements XMLReader, DocumentHandler

Adapt a SAX1 Parser as a SAX2 XMLReader.
This module, both source code and documentation, is in the Public Domain, and comes with NO WARRANTY.

This class wraps a SAX1 Parser and makes it act as a SAX2 XMLReader, with feature, property, and Namespace support. Note that it is not possible to report skippedEntity events, since SAX1 does not make that information available.

This adapter does not test for duplicate Namespace-qualified attribute names.

Version:
2.0r2pre

Author:
David Megginson, sax@megginson.com

Since:
SAX 2.0

See Also:
XMLReaderAdapter, XMLReader, Parser

Constructor Summary

ParserAdapter()
Construct a new parser adapter.
ParserAdapter(Parser parser)
Construct a new parser adapter.

Method Summary

void
characters(ch[] , int start, int length)
Adapt a SAX1 characters event.
void
endDocument()
Adapt a SAX1 end document event.
void
endElement(String qName)
Adapt a SAX1 end element event.
ContentHandler
getContentHandler()
Return the current content handler.
DTDHandler
getDTDHandler()
Return the current DTD handler.
EntityResolver
getEntityResolver()
Return the current entity resolver.
ErrorHandler
getErrorHandler()
Return the current error handler.
boolean
getFeature(String name)
Check a parser feature.
Object
getProperty(String name)
Get a parser property.
void
ignorableWhitespace(ch[] , int start, int length)
Adapt a SAX1 ignorable whitespace event.
void
parse(String systemId)
Parse an XML document.
void
parse(InputSource input)
Parse an XML document.
void
processingInstruction(String target, String data)
Adapt a SAX1 processing instruction event.
void
setContentHandler(ContentHandler handler)
Set the content handler.
void
setDTDHandler(DTDHandler handler)
Set the DTD handler.
void
setDocumentLocator(Locator locator)
Adapt a SAX1 document locator event.
void
setEntityResolver(EntityResolver resolver)
Set the entity resolver.
void
setErrorHandler(ErrorHandler handler)
Set the error handler.
void
setFeature(String name, boolean state)
Set a feature for the parser.
void
setProperty(String name, Object value)
Set a parser property.
void
startDocument()
Adapt a SAX1 start document event.
void
startElement(String qName, AttributeList qAtts)
Adapt a SAX1 startElement event.

Constructor Details

ParserAdapter

public ParserAdapter()
            throws SAXException
Construct a new parser adapter.

Use the "org.xml.sax.parser" property to locate the embedded SAX1 driver.

Throws:
SAXException - If the embedded driver cannot be instantiated or if the org.xml.sax.parser property is not specified.


ParserAdapter

public ParserAdapter(Parser parser)
Construct a new parser adapter.

Note that the embedded parser cannot be changed once the adapter is created; to embed a different parser, allocate a new ParserAdapter.

Parameters:
parser - The SAX1 parser to embed.

Method Details

characters

public void characters(ch[] ,
                       int start,
                       int length)
            throws SAXException
Adapt a SAX1 characters event.
Specified by:
characters in interface DocumentHandler

Parameters:
start - The starting position in the array.
length - The number of characters to use.

Throws:
SAXException - The client may raise a processing exception.

See Also:
DocumentHandler.characters(ch[],int,int)


endDocument

public void endDocument()
            throws SAXException
Adapt a SAX1 end document event.
Specified by:
endDocument in interface DocumentHandler

Throws:
SAXException - The client may raise a processing exception.

See Also:
DocumentHandler.endDocument()


endElement

public void endElement(String qName)
            throws SAXException
Adapt a SAX1 end element event.
Specified by:
endElement in interface DocumentHandler

Parameters:
qName - The qualified (prefixed) name.

Throws:
SAXException - The client may raise a processing exception.

See Also:
DocumentHandler.endElement(String)


getContentHandler

public ContentHandler getContentHandler()
Return the current content handler.
Specified by:
getContentHandler in interface XMLReader

Returns:
The current content handler, or null if none was supplied.

See Also:
XMLReader.getEntityResolver()


getDTDHandler

public DTDHandler getDTDHandler()
Return the current DTD handler.
Specified by:
getDTDHandler in interface XMLReader

Returns:
The current DTD handler, or null if none was supplied.

See Also:
XMLReader.getEntityResolver()


getEntityResolver

public EntityResolver getEntityResolver()
Return the current entity resolver.
Specified by:
getEntityResolver in interface XMLReader

Returns:
The current entity resolver, or null if none was supplied.

See Also:
XMLReader.getEntityResolver()


getErrorHandler

public ErrorHandler getErrorHandler()
Return the current error handler.
Specified by:
getErrorHandler in interface XMLReader

Returns:
The current error handler, or null if none was supplied.

See Also:
XMLReader.getEntityResolver()


getFeature

public boolean getFeature(String name)
            throws SAXNotRecognizedException,
                   SAXNotSupportedException
Check a parser feature.

The only features supported are namespaces and namespace-prefixes.

Specified by:
getFeature in interface XMLReader

Parameters:
name - The feature name, as a complete URI.

Returns:
The current feature state.

Throws:
SAXNotRecognizedException - If the feature name is not known.
SAXNotSupportedException - If querying the feature state is not supported.

See Also:
XMLReader.setFeature(String,boolean)


getProperty

public Object getProperty(String name)
            throws SAXNotRecognizedException,
                   SAXNotSupportedException
Get a parser property.

No special properties are currently supported.

Specified by:
getProperty in interface XMLReader

Parameters:
name - The property name.

Returns:
The property value.

Throws:
SAXNotRecognizedException - If the feature name is not known.
SAXNotSupportedException - If the feature state is not supported.

See Also:
XMLReader.getProperty(String)


ignorableWhitespace

public void ignorableWhitespace(ch[] ,
                                int start,
                                int length)
            throws SAXException
Adapt a SAX1 ignorable whitespace event.
Specified by:
ignorableWhitespace in interface DocumentHandler

Parameters:
start - The starting position in the array.
length - The number of characters to use.

Throws:
SAXException - The client may raise a processing exception.

See Also:
DocumentHandler.ignorableWhitespace(ch[],int,int)


parse

public void parse(String systemId)
            throws IOException,
                   SAXException
Parse an XML document.
Specified by:
parse in interface XMLReader

Parameters:
systemId - The absolute URL of the document.

Throws:
SAXException - If there is a problem processing the document.

See Also:
parse(InputSource), org.xml.sax.Parser.parse(java.lang.String)


parse

public void parse(InputSource input)
            throws IOException,
                   SAXException
Parse an XML document.
Specified by:
parse in interface XMLReader

Parameters:
input - An input source for the document.

Throws:
SAXException - If there is a problem processing the document.

See Also:
parse(java.lang.String), Parser.parse(InputSource)


processingInstruction

public void processingInstruction(String target,
                                  String data)
            throws SAXException
Adapt a SAX1 processing instruction event.
Specified by:
processingInstruction in interface DocumentHandler

Parameters:
target - The processing instruction target.
data - The remainder of the processing instruction

Throws:
SAXException - The client may raise a processing exception.

See Also:
DocumentHandler.processingInstruction(String,String)


setContentHandler

public void setContentHandler(ContentHandler handler)
Set the content handler.
Specified by:
setContentHandler in interface XMLReader

Parameters:

See Also:
XMLReader.setEntityResolver(EntityResolver)


setDTDHandler

public void setDTDHandler(DTDHandler handler)
Set the DTD handler.
Specified by:
setDTDHandler in interface XMLReader

Parameters:

See Also:
XMLReader.setEntityResolver(EntityResolver)


setDocumentLocator

public void setDocumentLocator(Locator locator)
Adapt a SAX1 document locator event.
Specified by:
setDocumentLocator in interface DocumentHandler

Parameters:
locator - A document locator.

See Also:
ContentHandler.setDocumentLocator(Locator)


setEntityResolver

public void setEntityResolver(EntityResolver resolver)
Set the entity resolver.
Specified by:
setEntityResolver in interface XMLReader

Parameters:
resolver - The new entity resolver.

See Also:
XMLReader.setEntityResolver(EntityResolver)


setErrorHandler

public void setErrorHandler(ErrorHandler handler)
Set the error handler.
Specified by:
setErrorHandler in interface XMLReader

Parameters:

See Also:
XMLReader.setEntityResolver(EntityResolver)


setFeature

public void setFeature(String name,
                       boolean state)
            throws SAXNotRecognizedException,
                   SAXNotSupportedException
Set a feature for the parser.

The only features supported are namespaces and namespace-prefixes.

Specified by:
setFeature in interface XMLReader

Parameters:
name - The feature name, as a complete URI.
state - The requested feature state.

Throws:
SAXNotRecognizedException - If the feature name is not known.
SAXNotSupportedException - If the feature state is not supported.

See Also:
XMLReader.setFeature(String,boolean)


setProperty

public void setProperty(String name,
                        Object value)
            throws SAXNotRecognizedException,
                   SAXNotSupportedException
Set a parser property.

No special properties are currently supported.

Specified by:
setProperty in interface XMLReader

Parameters:
name - The property name.
value - The property value.

Throws:
SAXNotRecognizedException - If the feature name is not known.
SAXNotSupportedException - If the feature state is not supported.

See Also:
XMLReader.setProperty(String,Object)


startDocument

public void startDocument()
            throws SAXException
Adapt a SAX1 start document event.
Specified by:
startDocument in interface DocumentHandler

Throws:
SAXException - The client may raise a processing exception.

See Also:
DocumentHandler.startDocument()


startElement

public void startElement(String qName,
                         AttributeList qAtts)
            throws SAXException
Adapt a SAX1 startElement event.

If necessary, perform Namespace processing.

Specified by:
startElement in interface DocumentHandler

Parameters:
qName - The qualified (prefixed) name.
qAtts - The XML 1.0 attribute list (with qnames).