org.opennms.bb.common.utils
Class MultiplexContentHandler

java.lang.Object
  |
  +--org.opennms.bb.common.utils.MultiplexContentHandler
All Implemented Interfaces:
org.xml.sax.ContentHandler

public final class MultiplexContentHandler
extends Object
implements org.xml.sax.ContentHandler

This class is designed to allow for multiple ContentHandler object to process the same input stream. If a single stream can contain multiple documents that can be identified by the first element in the document, then this handler will choose the appropiate document and forward all calls to the selected document.

The only caveat is that several calls can occur prior to the first element and those calls will be forwarded to all handlers until the first start element is processed. For more information see the ContentHandler interface in the Xerces 1.1.3 documentation.

Version:
$Revision: 1.5 $
Author:
Brian Weaver, OpenNMS

Inner Class Summary
private static class MultiplexContentHandler.ElementInfo
          The element info tag that associates a tag name and a namespace, if any.
 
Field Summary
private  org.xml.sax.ContentHandler m_curDocHandler
          The content handler that is processing the current document.
private  Map m_handlers
          The map used to track ElementInfo to ContentHandler
 
Constructor Summary
MultiplexContentHandler()
          Creates a new instance of the class that can be used to multiplex between different content handlers based on the initial tag.
 
Method Summary
 void characters(char[] data, int offset, int length)
          Called when character data is encountered.
 Object deregister(String localName)
          Removes the registration for a content handler based on the top level tag for the handler.
 Object deregister(String localName, String uri)
          Removes the registration for of a content handler for the specific namespace and name.
 void endDocument()
          Called when the end of the document is reached.
 void endElement(String uri, String localName, String qName)
          Called when the end tag for an element is reached.
 void endPrefixMapping(String prefix)
          Called when a prefix namespace URI ending.
 void ignorableWhitespace(char[] data, int offset, int length)
          Called when ignorable whtespace characters are encountered.
 void processingInstruction(String target, String data)
          Called when a processing instructions are encountered.
 void register(String localName, org.xml.sax.ContentHandler handler)
          Registers a new content handler for the tag.
 void register(String localName, String uri, org.xml.sax.ContentHandler handler)
          Registers a new content handler for the tag and namespace.
 void setDocumentLocator(org.xml.sax.Locator locator)
          Sets the document locator in the current content handler.
 void skippedEntity(String name)
          Receives notification of a skipped entity.
 void startDocument()
          Called when a new document is started.
 void startElement(String uri, String localName, String qName, org.xml.sax.Attributes attributes)
          Called for each new element encountered in the document.
 void startPrefixMapping(String prefix, String uri)
          Called when a new namespace URI is being mapped.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

m_handlers

private Map m_handlers
The map used to track ElementInfo to ContentHandler

m_curDocHandler

private org.xml.sax.ContentHandler m_curDocHandler
The content handler that is processing the current document. This will be null after startDocument() until the first call to startElement() at which point it must not be null.
Constructor Detail

MultiplexContentHandler

public MultiplexContentHandler()
Creates a new instance of the class that can be used to multiplex between different content handlers based on the initial tag. The mapping can be based on the tag name, or tag name/namespace combinations.
Method Detail

register

public void register(String localName,
                     org.xml.sax.ContentHandler handler)
Registers a new content handler for the tag. If the tag combination already existed, then they are replace by the new content handler.
Parameters:
localName - The toplevel tag for the handler
handler - The handler for the tag.
See Also:
Map.put(java.lang.Object, java.lang.Object)

register

public void register(String localName,
                     String uri,
                     org.xml.sax.ContentHandler handler)
Registers a new content handler for the tag and namespace. If the tag/namespace combination already existed, then they are replace by the new content handler.
Parameters:
localName - The toplevel tag for the handler
uri - The namespace of the tag.
handler - The handler for the tag.
See Also:
Map.put(java.lang.Object, java.lang.Object)

deregister

public Object deregister(String localName)
Removes the registration for a content handler based on the top level tag for the handler.
Parameters:
localName - The toplevel tag fo the handler.
See Also:
Map.remove(java.lang.Object)

deregister

public Object deregister(String localName,
                         String uri)
Removes the registration for of a content handler for the specific namespace and name.
Parameters:
localName - The toplevel tag name
uri - The namespace of the tag
See Also:
Map.remove(java.lang.Object)

startDocument

public void startDocument()
                   throws org.xml.sax.SAXException
Called when a new document is started.
Specified by:
startDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException - Thrown if a content handler throws the exception.

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
Called when the end of the document is reached.
Specified by:
endDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException - Thrown if a no content handler is currently defined, or the content handler throws the exception.

startElement

public void startElement(String uri,
                         String localName,
                         String qName,
                         org.xml.sax.Attributes attributes)
                  throws org.xml.sax.SAXException
Called for each new element encountered in the document.
Specified by:
startElement in interface org.xml.sax.ContentHandler
Parameters:
uri - The namespace URI for this tag.
localName - The local, non-qualified tag that is starting
qName - The fully qualified namespace tag.
attributes - The attributes for the element.
Throws:
org.xml.sax.SAXException - Thrown if a no content handler is currently defined, the content handler throws the exception, or there is no content handler found for the new document.

endElement

public void endElement(String uri,
                       String localName,
                       String qName)
                throws org.xml.sax.SAXException
Called when the end tag for an element is reached.
Specified by:
endElement in interface org.xml.sax.ContentHandler
Parameters:
uri - The namespace URI for this tag.
localName - The local, non-qualified tag that is starting
qName - The fully qualified namespace tag.
Throws:
org.xml.sax.SAXException - Thrown if a no content handler is currently defined, or the content handler throws the exception.

startPrefixMapping

public void startPrefixMapping(String prefix,
                               String uri)
                        throws org.xml.sax.SAXException
Called when a new namespace URI is being mapped. If no content handler is currently defined then all content handlers are informed. This is necessary since the prefix mapping call can occur prior to the first call to startElement()
Specified by:
startPrefixMapping in interface org.xml.sax.ContentHandler
Parameters:
prefix - The prefix to map to the URI.
uri - The namespace URI.
Throws:
org.xml.sax.SAXException - Thrown if a no content handler is currently defined, or the content handler throws the exception.

endPrefixMapping

public void endPrefixMapping(String prefix)
                      throws org.xml.sax.SAXException
Called when a prefix namespace URI ending.
Specified by:
endPrefixMapping in interface org.xml.sax.ContentHandler
Parameters:
prefix - The prefix that is ending.
Throws:
org.xml.sax.SAXException - Thrown if a no content handler is currently defined, or the content handler throws the exception.

processingInstruction

public void processingInstruction(String target,
                                  String data)
                           throws org.xml.sax.SAXException
Called when a processing instructions are encountered.
Specified by:
processingInstruction in interface org.xml.sax.ContentHandler
Parameters:
target - Processing target
data - The processing data.
Throws:
org.xml.sax.SAXException - Thrown if a no content handler is currently defined, or the content handler throws the exception.

skippedEntity

public void skippedEntity(String name)
                   throws org.xml.sax.SAXException
Receives notification of a skipped entity.
Specified by:
skippedEntity in interface org.xml.sax.ContentHandler
Parameters:
name - The skipped entity.
Throws:
org.xml.sax.SAXException - Thrown if a no content handler is currently defined, or the content handler throws the exception.

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
Sets the document locator in the current content handler. If no content handler is currently defined then all registered content handlers are informed.
Specified by:
setDocumentLocator in interface org.xml.sax.ContentHandler
Parameters:
locator - The document locator.

ignorableWhitespace

public void ignorableWhitespace(char[] data,
                                int offset,
                                int length)
                         throws org.xml.sax.SAXException
Called when ignorable whtespace characters are encountered.
Specified by:
ignorableWhitespace in interface org.xml.sax.ContentHandler
Parameters:
data - The buffer containing the data.
offset - The offset of the data in the buffer.
length - The length of data in the buffer.
Throws:
org.xml.sax.SAXException - Thrown if no content handler is registered or the content handler encounters an error.

characters

public void characters(char[] data,
                       int offset,
                       int length)
                throws org.xml.sax.SAXException
Called when character data is encountered.
Specified by:
characters in interface org.xml.sax.ContentHandler
Parameters:
data - The buffer containing the data.
offset - The offset of the data in the buffer.
length - The length of data in the buffer.
Throws:
org.xml.sax.SAXException - Thrown if there is no content handler registered or the content handler encounters an error.