FileDocCategorySizeDatePackage
XMLReaderAdapter.javaAPI DocJava SE 5 API14046Fri Aug 26 14:58:44 BST 2005org.xml.sax.helpers

XMLReaderAdapter

public class XMLReaderAdapter extends Object implements ContentHandler, Parser
Adapt a SAX2 XMLReader as a SAX1 Parser.
This module, both source code and documentation, is in the Public Domain, and comes with NO WARRANTY. See http://www.saxproject.org for further information.

This class wraps a SAX2 {@link org.xml.sax.XMLReader XMLReader} and makes it act as a SAX1 {@link org.xml.sax.Parser Parser}. The XMLReader must support a true value for the http://xml.org/sax/features/namespace-prefixes property or parsing will fail with a {@link org.xml.sax.SAXException SAXException}; if the XMLReader supports a false value for the http://xml.org/sax/features/namespaces property, that will also be used to improve efficiency.

since
SAX 2.0
author
David Megginson
version
2.0.1 (sax2r2)
see
org.xml.sax.Parser
see
org.xml.sax.XMLReader

Fields Summary
XMLReader
xmlReader
DocumentHandler
documentHandler
AttributesAdapter
qAtts
Constructors Summary
public XMLReaderAdapter()
Create a new adapter.

Use the "org.xml.sax.driver" property to locate the SAX2 driver to embed.

exception
org.xml.sax.SAXException If the embedded driver cannot be instantiated or if the org.xml.sax.driver property is not specified.

	setup(XMLReaderFactory.createXMLReader());
    
public XMLReaderAdapter(XMLReader xmlReader)
Create a new adapter.

Create a new adapter, wrapped around a SAX2 XMLReader. The adapter will make the XMLReader act like a SAX1 Parser.

param
xmlReader The SAX2 XMLReader to wrap.
exception
java.lang.NullPointerException If the argument is null.

	setup(xmlReader);
    
Methods Summary
public voidcharacters(char[] ch, int start, int length)
Adapt a SAX2 characters event.

param
ch An array of characters.
param
start The starting position in the array.
param
length The number of characters to use.
exception
org.xml.sax.SAXException The client may raise a processing exception.
see
org.xml.sax.ContentHandler#characters

	if (documentHandler != null)
	    documentHandler.characters(ch, start, length);
    
public voidendDocument()
End document event.

exception
org.xml.sax.SAXException The client may raise a processing exception.
see
org.xml.sax.ContentHandler#endDocument

	if (documentHandler != null)
	    documentHandler.endDocument();
    
public voidendElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
Adapt a SAX2 end element event.

param
uri The Namespace URI.
param
localName The Namespace local name.
param
qName The qualified (prefixed) name.
exception
org.xml.sax.SAXException The client may raise a processing exception.
see
org.xml.sax.ContentHandler#endElement

	if (documentHandler != null)
	    documentHandler.endElement(qName);
    
public voidendPrefixMapping(java.lang.String prefix)
Adapt a SAX2 end prefix mapping event.

param
prefix The prefix being mapped.
see
org.xml.sax.ContentHandler#endPrefixMapping

    
public voidignorableWhitespace(char[] ch, int start, int length)
Adapt a SAX2 ignorable whitespace event.

param
ch An array of characters.
param
start The starting position in the array.
param
length The number of characters to use.
exception
org.xml.sax.SAXException The client may raise a processing exception.
see
org.xml.sax.ContentHandler#ignorableWhitespace

	if (documentHandler != null)
	    documentHandler.ignorableWhitespace(ch, start, length);
    
public voidparse(org.xml.sax.InputSource input)
Parse the document.

This method will throw an exception if the embedded XMLReader does not support the http://xml.org/sax/features/namespace-prefixes property.

param
input An input source for the document.
exception
java.io.IOException If there is a problem reading the raw content of the document.
exception
org.xml.sax.SAXException If there is a problem processing the document.
see
#parse(java.lang.String)
see
org.xml.sax.Parser#parse(org.xml.sax.InputSource)

	setupXMLReader();
	xmlReader.parse(input);
    
public voidparse(java.lang.String systemId)
Parse the document.

This method will throw an exception if the embedded XMLReader does not support the http://xml.org/sax/features/namespace-prefixes property.

param
systemId The absolute URL of the document.
exception
java.io.IOException If there is a problem reading the raw content of the document.
exception
org.xml.sax.SAXException If there is a problem processing the document.
see
#parse(org.xml.sax.InputSource)
see
org.xml.sax.Parser#parse(java.lang.String)

	parse(new InputSource(systemId));
    
public voidprocessingInstruction(java.lang.String target, java.lang.String data)
Adapt a SAX2 processing instruction event.

param
target The processing instruction target.
param
data The remainder of the processing instruction
exception
org.xml.sax.SAXException The client may raise a processing exception.
see
org.xml.sax.ContentHandler#processingInstruction

	if (documentHandler != null)
	    documentHandler.processingInstruction(target, data);
    
public voidsetDTDHandler(org.xml.sax.DTDHandler handler)
Register the DTD event handler.

param
handler The new DTD event handler.
see
org.xml.sax.Parser#setDTDHandler

	xmlReader.setDTDHandler(handler);
    
public voidsetDocumentHandler(org.xml.sax.DocumentHandler handler)
Register the SAX1 document event handler.

Note that the SAX1 document handler has no Namespace support.

param
handler The new SAX1 document event handler.
see
org.xml.sax.Parser#setDocumentHandler

	documentHandler = handler;
    
public voidsetDocumentLocator(org.xml.sax.Locator locator)
Set a document locator.

param
locator The document locator.
see
org.xml.sax.ContentHandler#setDocumentLocator

	if (documentHandler != null)
	    documentHandler.setDocumentLocator(locator);
    
public voidsetEntityResolver(org.xml.sax.EntityResolver resolver)
Register the entity resolver.

param
resolver The new resolver.
see
org.xml.sax.Parser#setEntityResolver

	xmlReader.setEntityResolver(resolver);
    
public voidsetErrorHandler(org.xml.sax.ErrorHandler handler)
Register the error event handler.

param
handler The new error event handler.
see
org.xml.sax.Parser#setErrorHandler

	xmlReader.setErrorHandler(handler);
    
public voidsetLocale(java.util.Locale locale)
Set the locale for error reporting.

This is not supported in SAX2, and will always throw an exception.

param
locale the locale for error reporting.
see
org.xml.sax.Parser#setLocale
exception
org.xml.sax.SAXException Thrown unless overridden.

	throw new SAXNotSupportedException("setLocale not supported");
    
private voidsetup(org.xml.sax.XMLReader xmlReader)
Internal setup.

param
xmlReader The embedded XMLReader.

	if (xmlReader == null) {
	    throw new NullPointerException("XMLReader must not be null");
	}
	this.xmlReader = xmlReader;
	qAtts = new AttributesAdapter();
    
private voidsetupXMLReader()
Set up the XML reader.

	xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
	try {
	    xmlReader.setFeature("http://xml.org/sax/features/namespaces",
	                         false);
	} catch (SAXException e) {
	    // NO OP: it's just extra information, and we can ignore it
	}
	xmlReader.setContentHandler(this);
    
public voidskippedEntity(java.lang.String name)
Adapt a SAX2 skipped entity event.

param
name The name of the skipped entity.
see
org.xml.sax.ContentHandler#skippedEntity
exception
org.xml.sax.SAXException Throwable by subclasses.

    
public voidstartDocument()
Start document event.

exception
org.xml.sax.SAXException The client may raise a processing exception.
see
org.xml.sax.ContentHandler#startDocument

	if (documentHandler != null)
	    documentHandler.startDocument();
    
public voidstartElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts)
Adapt a SAX2 start element event.

param
uri The Namespace URI.
param
localName The Namespace local name.
param
qName The qualified (prefixed) name.
param
atts The SAX2 attributes.
exception
org.xml.sax.SAXException The client may raise a processing exception.
see
org.xml.sax.ContentHandler#endDocument

	if (documentHandler != null) {
	    qAtts.setAttributes(atts);
	    documentHandler.startElement(qName, qAtts);
	}
    
public voidstartPrefixMapping(java.lang.String prefix, java.lang.String uri)
Adapt a SAX2 start prefix mapping event.

param
prefix The prefix being mapped.
param
uri The Namespace URI being mapped to.
see
org.xml.sax.ContentHandler#startPrefixMapping