XML technologies / XML / XML in a Nutshell / SAX reference / The package org.xml.sax.helpers
(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)
The package org.xml.sax.helpers contains helper classes for the SAX core, including factories creating instances of various org.xml.sax interfaces and default implementations to subclass for your own needs.
The AttributesImpl class
AttributesImpl is a default implementation of the interface Attributes for SAX parsers. Beyond the methods of the interface Attributes, it lets you manipulate and reuse attribute lists. Inside startElement() you can freeze a list for later use or build a new object Attributes to pass through a SAX filter:
package org.xml.sax.helpers;
public class AttributesImpl implements Attributes {
public AttributesImpl( );
public AttributesImpl(Attributes atts);
public int getLength( );
public String getURI(int index);
public String getLocalName(int index);
public String getQName(int index);
public String getType(int index);
public String getValue(int index);
public int getIndex(String uri,String localName);
public int getIndex(String qualifiedName);
public String getType(String uri, String localName);
public String getType(String qualifiedName);
public String getValue(String uri, String localName);
public String getValue(String qualifiedName);
public void clear( );
public void setAttributes(Attributes atts);
public void addAttribute(String uri, String localName,
String qualifiedName, String type, String value);
public void setAttribute(int index, String uri, String localName,
String qualifiedName, String type, String value);
public void removeAttribute(int index)
public void setURI(int index, String
uri) public void setLocalName(int index, String localName)
public void setQName(int index, String qualifiedName);
public void setType(int index, String type);
public void setValue(int index, String value);
}
The DefaultHandler class
A DefaultHandler is a helper implementing the interfaces EntityResolver, DTDHandler, ContentHandler and ErrorHandler, mostly with no-op methods. Subclass it and override only what you need. You can instead ignore it and implement an interface directly. It is analogous to AWT adapter classes such as MouseAdapter or WindowAdapter:
package org.xml.sax.helpers;
public class DefaultHandler implements EntityResolver, DTDHandler,
ContentHandler, ErrorHandler {
// Standardimplementierung des Interfaces EntityResolver public InputSource
resolveEntity(String publicID, String systemID)
throws SAXException {
return null;
}
// Standardimplementierung des Interfaces DTDHandler
public void notationDecl(String name,
String publicID, String systemID) throws SAXException {}
public void unparsedEntityDecl(String
name, String publicID, String
systemID, String notationName) throws SAXException{}
// Standardimplementierung des Interfaces ContentHandler
public void setDocumentLocator(Locator locator) {}
public void startDocument( ) throws SAXException {}
public void endDocument( ) throws SAXException {}
public void startPrefixMapping(String prefix, String uri)
throws SAXException {}
public void endPrefixMapping(String prefix) throws SAXException {}
public void startElement(String uri, String localName,
String qualifiedName, Attributes attributes) throws SAXException {}
public void endElement(String uri, String localName,
String qualifiedName) throws SAXException {}
public void characters(char[] text, int
start, int length) throws SAXException {}
public void ignorableWhitespace(char[]
whitespace, int start, int length)
throws SAXException {} public void processingInstruction(String
target, String data)
throws SAXException {}
public void skippedEntity(String name) throws SAXException {}
// Standardimplementierung des Interfaces ErrorHandler
public void warning(SAXParseException ex) throws SAXException {}
public void error(SAXParseException ex) throws SAXException {}
public void fatalError(SAXParseException ex) throws SAXException {
throw ex;
}
}
The LocatorImpl class
Another default implementation is LocatorImpl, implementing the interface Locator for parser authors. Direct use is rare. Besides constructors, it defines methods set for the PUBLIC identifier, SYSTEM identifier, line and column numbers, read by methods get from Locator:
package org.xml.sax.helpers;
public class LocatorImpl implements Locator {
public LocatorImpl();
public LocatorImpl(Locator locator);
public String getPublicId();
public String getSystemId();
public int getLineNumber();
public int getColumnNumber();
public void setPublicId(String publicID);
public void setSystemId(String systemID);
public void setLineNumber(int lineNumber);
public void setColumnNumber(int columnNumber);
}
The NamespaceSupport class
The class NamespaceSupport helps parser authors handle namespaces. Push a new context onto the stack at the start of each element’s namespace mappings and pop it at the end. For each call to the method startPrefixMapping(), call declarePrefix() again to add to the object NamespaceSupport a new prefix. To associate a prefix with a URI, call the method getPrefix(). The empty string represents the default namespace. Getters then reveal the URI for any prefix and vice versa anywhere in the document. To reuse an object NamespaceSupport across documents, call reset() on switching documents.
package org.xml.sax.helpers;
public class NamespaceSupport {
public final static String XMLNS="http://www.w3.org/XML/1998/namespace"
public NamespaceSupport();
public void reset();
public void pushContext();
public void popContext();
public boolean declarePrefix(String prefix, String uri);
public String[] processName(String qualifiedName, String[] parts,
boolean isAttribute);
public String getURI(String prefix);
public Enumeration getPrefixes();
public String getPrefix(String uri);
public Enumeration getPrefixes(String uri);
public Enumeration getDeclaredPrefixes();
}
The ParserAdapter class
The class ParserAdapter uses the adapter pattern to turn a SAX1 parser, an instance of org.xml.sax.Parser, into a SAX2 parser, an instance of org.xml.sax.XMLReader. As SAX2 spreads, this class may become obsolete. Some SAX2 features are unavailable when adapting SAX1 with ParserAdapter; for example, no information about skipped entities. Most features and properties, even normally mandatory basics, are unsupported.
package org.xml.sax.helpers;
public class ParserAdapter implements XMLReader, DocumentHandler {
public ParserAdapter() throws SAXException;
public ParserAdapter(Parser parser);
// Standardimplementierung von org.xml.sax.XMLReader
public void setFeature(String name, boolean state)
throws SAXNotRecognizedException, SAXNotSupportedException;
public boolean getFeature(String name)
throws SAXNotRecognizedException, SAXNotSupportedException;
public void setProperty(String name, Object value)
throws SAXNotRecognizedException, SAXNotSupportedException;
public Object getProperty(String name)
throws SAXNotRecognizedException, SAXNotSupportedException;
public void setEntityResolver(EntityResolver resolver);
public EntityResolver getEntityResolver();
public void setDTDHandler(DTDHandler handler);
public DTDHandler getDTDHandler();
public void setContentHandler(ContentHandler handler);
public ContentHandler getContentHandler();
public void setErrorHandler(ErrorHandler handler);
public ErrorHandler getErrorHandler();
public void parse(String systemID) throws IOException, SAXException;
public void parse(InputSource input) throws IOException, SAXException;
// Standardimplementierung von org.xml.sax.DocumentHandler
public void setDocumentLocator(Locator locator);
public void startDocument() throws SAXException;
public void endDocument() throws SAXException;
public void startElement(String qualifiedName,
AttributeList qualifiedAttributes) throws SAXException;
public void endElement(String qualifiedName) throws SAXException;
public void characters(char[] text, int start, int length)
throws SAXException;public void ignorableWhitespace(char[]
text, int start, int length)
throws SAXException;
public void processingInstruction(String target, String data)
throws SAXException;
}
The XMLFilterImpl class
The class XMLFilterImpl helps implement XML filters. Put an instance between an XMLReader and the application’s EventHandler. It receives events from the XMLReader and forwards them unchanged. Subclass it and override selected methods for the desired events. Chain a XMLReader to a filter by passing the XMLReader to its constructor. Then call the method parse() of the filter rather than that of the XMLReader:
package org.xml.sax.helpers;
public class XMLFilterImpl implements XMLFilter, EntityResolver,
DTDHandler, ContentHandler, ErrorHandler {
public XMLFilterImpl();
public XMLFilterImpl(XMLReader parent);
// Implementierung von org.xml.sax.XMLFilter
public voidsetParent(XMLReader parent);
public XMLReader getParent();
// Implementierung von org.xml.sax.XMLReader
public void setFeature(String name, boolean state)
throws SAXNotRecognizedException, SAXNotSupportedException;
public boolean getFeature(String name)
throws SAXNotRecognizedException, SAXNotSupportedException;
public void setProperty(String name, Object value)
throws SAXNotRecognizedException, SAXNotSupportedException;
public Object getProperty(String name)
throws SAXNotRecognizedException, SAXNotSupportedException;
public void setEntityResolver(EntityResolver resolver);
public EntityResolver getEntityResolver();
public void setDTDHandler(DTDHandler handler);
public DTDHandler getDTDHandler();
public void setContentHandler(ContentHandler handler);
public ContentHandler getContentHandler();
public void setErrorHandler(ErrorHandler handler);
public ErrorHandler getErrorHandler();
public void parse(InputSource input) throws SAXException, IOException;
public void parse(String systemID) throws SAXException, IOException
// Implementierung von org.xml.sax.EntityResolver
public InputSource resolveEntity(String publicID, String systemID)
throws SAXException, IOException;
// Implementierung von org.xml.sax.DTDHandler
public void notationDecl(String name,
String publicID, String systemID) throws SAXException;
public void unparsedEntityDecl(String name, String publicID,
String systemID, String notationName) throws SAXException;
// Implementierung von org.xml.sax.ContentHandler
public void setDocumentLocator(Locator locator);
public void startDocument() throws SAXException;
public void endDocument() throws SAXException;
public void startPrefixMapping(String prefix, String uri)
throws SAXException;
public void endPrefixMapping(String prefix) throws SAXException;
public void startElement(String namespaceURI, String localName,
String qualifiedName, Attributes atts) throws SAXException;
public void endElement(String namespaceURI, String localName,
String qualifiedName) throws SAXException;
public void characters(char[] text, int start, int length)
throws SAXException;
public void ignorableWhitespace(char[] text, int start, int length)
throws SAXException;
public void processingInstruction(String target, String data)
throws SAXException;public void skippedEntity(String name) throws SAXException;
// Implementierung von org.xml.sax.ErrorHandler
public void warning(SAXParseException ex) throws SAXException;
public void error(SAXParseException ex) throws SAXException;
public void fatalError(SAXParseException ex) throws SAXException;
}
The XMLReaderAdapter class
An XMLReaderAdapter is the counterpart of a ParserAdapter: It uses the adapter pattern to turn an XMLReader from SAX2 into a Parser from SAX1, letting older SAX1 applications use modern SAX2 parsers:
package org.xml.sax.helpers;
public class XMLReaderAdapter implements Parser, ContentHandler {
public XMLReaderAdapter() throws SAXException;
public XMLReaderAdapter(XMLReader reader);
// Implementierung von org.xml.sax.Parser
public void setLocale(Locale locale) throws SAXException;
public void setEntityResolver(EntityResolver resolver);
public void setDTDHandler(DTDHandler handler);
public void setDocumentHandler(DocumentHandler handler);
public void setErrorHandler(ErrorHandler handler);
public void parse(String systemID) throws IOException, SAXException;
public void parse(InputSource input) throws IOException, SAXException
// Implementierung von org.xml.sax.ContentHandler
public void setDocumentLocator(Locator locator);
public void startDocument() throws SAXException;
public void endDocument() throws SAXException;
public void startPrefixMapping(String prefix, String uri) throws SAXException;
public void endPrefixMapping(String
prefix) throws SAXException;public void
startElement(String namespaceURI, String localName,
String qualifiedName, Attributes atts) throws SAXException;
public void endElement(String
namespaceURI, String localName, String qualifiedName) throws SAXException;
public void characters(char[] text, int
start, int length) throws SAXException;
public void ignorableWhitespace(char[]
text, int start, int length) throws SAXException;
public void processingInstruction(String
target, String data) throws SAXException;
public void skippedEntity(String name) throws SAXException;
}
The XMLReaderFactory class
The class XMLReaderFactory creates an instance of XMLReader independently of the parser. The no-argument method createXMLReader() reads the system property org.xml.sax.driver, containing a class name implementing XMLReader. For a particular parser, pass the class name of XMLReader to the other method createXMLReader(), taking one argument. Use a fully qualified name such as org.apache.xerces.parsers.SAXParser:
package org.xml.sax.helpers;
public final class XMLReaderFactory {
public static XMLReader createXMLReader() throws SAXException;
public static XMLReader createXMLReader(String className)
throws SAXException;
}
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML in a Nutshell” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de