org.sadun.util.xml.configuration
Class DispatcherHandler

java.lang.Object
  extended byorg.xml.sax.helpers.DefaultHandler
      extended byorg.sadun.util.xml.configuration.DispatcherHandler
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

public abstract class DispatcherHandler
extends org.xml.sax.helpers.DefaultHandler

A SAX handler which dispatches events to subclasses implementing the Configurator interface.

Dispatching can be set by tree depth level (using setDispatchLevel()) or by namespaceURI/tag combination (using setToDispatch(java.lang.String, java.lang.String).

Configurator classes are located automatically in a package whose name is set at construction and their name must match a transformation the element tag obtained by the following rules:

The class works via a current configurator (initially null).

Whenever a dispatching occurs, the Configurator class found in the package name given at constructor is instantiated and control is sent to that instance, which becomes the current configurator.

When the level/element for which the dispatch was enabled is terminated, the method configuratorReady() (which must be implemented by the class extending this one) is invoked, and the previous configurator is reinstated as "current".

Otherwise, the current configurator (if any) keeps receiving the SAX events.

Author:
Cristiano Sadun

Constructor Summary
protected DispatcherHandler(Setup setup, java.lang.String packageName, java.lang.String postfix)
          Constructor for DispatcherHandler.
protected DispatcherHandler(java.lang.String packageName)
          Constructor for DispatcherHandler.
protected DispatcherHandler(java.lang.String packageName, java.lang.String postfix)
           
 
Method Summary
 void characters(char[] ch, int start, int length)
           
protected abstract  void configuratorReady(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, Configurator configurator)
           
 void endDocument()
           
 void endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName)
           
 void endPrefixMapping(java.lang.String prefix)
           
 void error(org.xml.sax.SAXParseException exception)
           
 void fatalError(org.xml.sax.SAXParseException exception)
           
 Setup getSetupObject()
          Returns the setupObject.
 void ignorableWhitespace(char[] ch, int start, int length)
           
 void notationDecl(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
           
 void processingInstruction(java.lang.String target, java.lang.String data)
           
 org.xml.sax.InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId)
           
protected  void setDispatchLevel(int level)
          Indicates that every element at a given depth level in the XML tree is associated to a Configurator.
protected  void setDispatchLevel(int level, boolean toDispatch)
          Sets wether or not every element at a given depth level in the XML tree is associated to a Configurator.
 void setDocumentLocator(org.xml.sax.Locator locator)
           
 void setSetupObject(Setup setupObject)
          Sets the setupObject.
protected  void setToDispatch(java.lang.String namespaceURI, java.lang.String localName)
          Indicate that the given namespace URI/local name combination is associated to a Configurator.
 void skippedEntity(java.lang.String name)
           
 void startDocument()
           
 void startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts)
          Check whether the namespace/tag are associated to a configurator or we're at a dispatch level.
 void startPrefixMapping(java.lang.String prefix, java.lang.String uri)
           
 void unparsedEntityDecl(java.lang.String name, java.lang.String publicId, java.lang.String systemId, java.lang.String notationName)
           
 void warning(org.xml.sax.SAXParseException exception)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DispatcherHandler

protected DispatcherHandler(Setup setup,
                            java.lang.String packageName,
                            java.lang.String postfix)
Constructor for DispatcherHandler. Indicates that the Configurator classes will be searched for in the given package (see class description).

Parameters:
packageName - the name of the package where to attempt to locate the necessary Configurator classes.
postfix - a fixed postfix which will be added to the Configurator class name (see class description).

DispatcherHandler

protected DispatcherHandler(java.lang.String packageName,
                            java.lang.String postfix)

DispatcherHandler

protected DispatcherHandler(java.lang.String packageName)
Constructor for DispatcherHandler. Indicates that the Configurator classes will be searched for in the given package (see class description).

Parameters:
packageName - the name of the package where to attempt to locate the necessary Configurator classes.
Method Detail

setToDispatch

protected void setToDispatch(java.lang.String namespaceURI,
                             java.lang.String localName)
Indicate that the given namespace URI/local name combination is associated to a Configurator.


setDispatchLevel

protected void setDispatchLevel(int level,
                                boolean toDispatch)
Sets wether or not every element at a given depth level in the XML tree is associated to a Configurator.

Parameters:
level - the depth level in the XML tree

setDispatchLevel

protected void setDispatchLevel(int level)
Indicates that every element at a given depth level in the XML tree is associated to a Configurator.

Parameters:
level - the depth level in the XML tree

startElement

public final void startElement(java.lang.String namespaceURI,
                               java.lang.String localName,
                               java.lang.String qName,
                               org.xml.sax.Attributes atts)
                        throws org.xml.sax.SAXException
Check whether the namespace/tag are associated to a configurator or we're at a dispatch level.

If yes, instantiate a new associated Configurator. and invoke its startElement() method.

If no, invoke the startElement() method of the current Configurator (if any).

Throws:
org.xml.sax.SAXException

characters

public final void characters(char[] ch,
                             int start,
                             int length)
                      throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

endElement

public final void endElement(java.lang.String namespaceURI,
                             java.lang.String localName,
                             java.lang.String qName)
                      throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

configuratorReady

protected abstract void configuratorReady(java.lang.String namespaceURI,
                                          java.lang.String localName,
                                          java.lang.String qName,
                                          Configurator configurator)
                                   throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.endDocument()

endPrefixMapping

public void endPrefixMapping(java.lang.String prefix)
                      throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.endPrefixMapping(String)

error

public void error(org.xml.sax.SAXParseException exception)
           throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
ErrorHandler.error(SAXParseException)

fatalError

public void fatalError(org.xml.sax.SAXParseException exception)
                throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
ErrorHandler.fatalError(SAXParseException)

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
                         throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.ignorableWhitespace(char[], int, int)

notationDecl

public void notationDecl(java.lang.String name,
                         java.lang.String publicId,
                         java.lang.String systemId)
                  throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
DTDHandler.notationDecl(String, String, String)

processingInstruction

public void processingInstruction(java.lang.String target,
                                  java.lang.String data)
                           throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.processingInstruction(String, String)

resolveEntity

public org.xml.sax.InputSource resolveEntity(java.lang.String publicId,
                                             java.lang.String systemId)
                                      throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
EntityResolver.resolveEntity(String, String)

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
See Also:
ContentHandler.setDocumentLocator(Locator)

skippedEntity

public void skippedEntity(java.lang.String name)
                   throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.skippedEntity(String)

startDocument

public void startDocument()
                   throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.startDocument()

startPrefixMapping

public void startPrefixMapping(java.lang.String prefix,
                               java.lang.String uri)
                        throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.startPrefixMapping(String, String)

unparsedEntityDecl

public void unparsedEntityDecl(java.lang.String name,
                               java.lang.String publicId,
                               java.lang.String systemId,
                               java.lang.String notationName)
                        throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
DTDHandler.unparsedEntityDecl(String, String, String, String)

warning

public void warning(org.xml.sax.SAXParseException exception)
             throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
ErrorHandler.warning(SAXParseException)

getSetupObject

public Setup getSetupObject()
Returns the setupObject.

Returns:
Setup

setSetupObject

public void setSetupObject(Setup setupObject)
Sets the setupObject.

Parameters:
setupObject - The setupObject to set