org.apache.ws.jaxme.util
Class Configurator

java.lang.Object
  extended by org.apache.ws.jaxme.util.Configurator
All Implemented Interfaces:
NamespaceResolver, org.xml.sax.ContentHandler

public class Configurator
extends java.lang.Object
implements org.xml.sax.ContentHandler, NamespaceResolver

The Configurator is an idea borrowed by the Ant project. It is a SAX2 handler that reads a config file which is represented by a hierarchy of Java beans. For example:

   <outerBean foo="true" bar="Quite right">
     <innerBean whatever="57">
     </innerBean
   </outerBean>
 
The example would create an object outerBean and call its methods setFoo(boolean) and setBar(String) to process the attributes. It would also create a bean innerBean by calling the outerBeans method createInnerBean(). Finally the innerBean is configured by calling setWhatever(int).

Version:
$Id$
Author:
Jochen Wiedmann

Constructor Summary
Configurator()
           
 
Method Summary
 void characters(char[] ch, int start, int length)
          Handles atomic child elements by invoking their method addText(String pText).
 void endDocument()
           
 void endElement(java.lang.String namespaceURI, java.lang.String qName, java.lang.String localName)
          Terminates parsing the current bean by calling its finish() method, if any.
 void endPrefixMapping(java.lang.String pPrefix)
           
 java.lang.Object getBeanFactory()
          Returns the bean factory, creating the outermost element.
 org.xml.sax.Locator getDocumentLocator()
          Returns the Locator being used in error messages.
protected  java.lang.String getMethodNameFor(java.lang.String pPrefix, java.lang.String pName)
          Given a prefix and a name, creates a method name matching the prefix and the name.
 java.lang.String[] getNamespaces()
          Returns the namespaces handled by the configurator.
 java.lang.Object getResult()
          Returns the parsed result bean.
 java.lang.Object getRootObject()
          An alternative to using the bean factory.
 void ignorableWhitespace(char[] ch, int start, int length)
           
protected  java.lang.Object invokeMethod(java.lang.String pMethodName, java.lang.Object pBean, java.lang.Class[] pSignature, java.lang.Object[] pArgs)
           
protected  boolean isNamespaceMatching(java.lang.String pNamespace)
          Returns whether a namespace is matching the configured namespace.
 void processingInstruction(java.lang.String target, java.lang.String data)
           
 boolean processName(java.lang.String pName, java.lang.String[] parts)
          Splits the XML name pName into its namespace URI, qualified name and local name, which are stored into the array parts: parts[0]: namespace URI; empty if no namespace is used parts[1]: local name, with the prefix removed parts[2]: qualified name (same as pName)
 void setBeanFactory(java.lang.Object pFactory)
          Sets the bean factory, creating the outermost element.
 void setDocumentLocator(org.xml.sax.Locator pLocator)
          Sets the Locator being used in error messages.
 void setNamespace(java.lang.String pNamespace)
          Sets the namespace handled by the configurator.
 void setNamespaces(java.lang.String[] pNamespaces)
          Sets the namespaces handled by the configurator.
 void setRootObject(java.lang.Object pRootObject)
          An alternative to using the bean factory.
 void skippedEntity(java.lang.String name)
           
 void startDocument()
           
 void startElement(java.lang.String pNamespaceURI, java.lang.String pQName, java.lang.String pLocalName, org.xml.sax.Attributes pAttr)
          Creates a new bean, matching the element name pLocalName.
 void startPrefixMapping(java.lang.String pPrefix, java.lang.String pURI)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Configurator

public Configurator()
Method Detail

setNamespace

public void setNamespace(java.lang.String pNamespace)

Sets the namespace handled by the configurator. Defaults to no namespace. Shortcut for setNamespace(new String[]{pNamespace}).

Parameters:
pNamespace - The namespace being set

setNamespaces

public void setNamespaces(java.lang.String[] pNamespaces)

Sets the namespaces handled by the configurator. Defaults to no namespace.

Parameters:
pNamespaces - The namespaces being set

getNamespaces

public java.lang.String[] getNamespaces()

Returns the namespaces handled by the configurator. Defaults to no namespace.


setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator pLocator)

Sets the Locator being used in error messages.

Specified by:
setDocumentLocator in interface org.xml.sax.ContentHandler

getDocumentLocator

public org.xml.sax.Locator getDocumentLocator()

Returns the Locator being used in error messages.


setBeanFactory

public void setBeanFactory(java.lang.Object pFactory)

Sets the bean factory, creating the outermost element. The bean factory must have a matching createElementName() method, with ElementName being the element name of the document element.


getBeanFactory

public java.lang.Object getBeanFactory()

Returns the bean factory, creating the outermost element. The bean factory must have a matching createElementName() method, with ElementName being the element name of the document element.


setRootObject

public void setRootObject(java.lang.Object pRootObject)

An alternative to using the bean factory. This object is used as the root object, regardless of its name.


getRootObject

public java.lang.Object getRootObject()

An alternative to using the bean factory. This object is used as the root object, regardless of its name.


startDocument

public void startDocument()
                   throws org.xml.sax.SAXException
Specified by:
startDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
Specified by:
endDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

startPrefixMapping

public void startPrefixMapping(java.lang.String pPrefix,
                               java.lang.String pURI)
                        throws org.xml.sax.SAXException
Specified by:
startPrefixMapping in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

endPrefixMapping

public void endPrefixMapping(java.lang.String pPrefix)
                      throws org.xml.sax.SAXException
Specified by:
endPrefixMapping in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

isNamespaceMatching

protected boolean isNamespaceMatching(java.lang.String pNamespace)

Returns whether a namespace is matching the configured namespace.


getMethodNameFor

protected java.lang.String getMethodNameFor(java.lang.String pPrefix,
                                            java.lang.String pName)

Given a prefix and a name, creates a method name matching the prefix and the name.


startElement

public void startElement(java.lang.String pNamespaceURI,
                         java.lang.String pQName,
                         java.lang.String pLocalName,
                         org.xml.sax.Attributes pAttr)
                  throws org.xml.sax.SAXException

Creates a new bean, matching the element name pLocalName. If this is the outermost bean, calls the bean factorys createBeanName() method, otherwise calls the current beans createBeanName() method, with beanName being the value of the pLocalName parameter.

Specified by:
startElement in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

invokeMethod

protected java.lang.Object invokeMethod(java.lang.String pMethodName,
                                        java.lang.Object pBean,
                                        java.lang.Class[] pSignature,
                                        java.lang.Object[] pArgs)
                                 throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

endElement

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

Terminates parsing the current bean by calling its finish() method, if any.

Specified by:
endElement in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

characters

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

Handles atomic child elements by invoking their method addText(String pText). Note that it may happen, that this method is invoked multiple times, if the parser splits a piece of text into multiple SAX events.

Specified by:
characters in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
                         throws org.xml.sax.SAXException
Specified by:
ignorableWhitespace in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

processingInstruction

public void processingInstruction(java.lang.String target,
                                  java.lang.String data)
                           throws org.xml.sax.SAXException
Specified by:
processingInstruction in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

skippedEntity

public void skippedEntity(java.lang.String name)
                   throws org.xml.sax.SAXException
Specified by:
skippedEntity in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

getResult

public java.lang.Object getResult()
Returns the parsed result bean.


processName

public boolean processName(java.lang.String pName,
                           java.lang.String[] parts)
Description copied from interface: NamespaceResolver

Splits the XML name pName into its namespace URI, qualified name and local name, which are stored into the array parts:

Specified by:
processName in interface NamespaceResolver
Returns:
True, if the namespace prefix of pName was successfully resolved. False otherwise.