org.apache.tomcat.util.xml
Class XmlMapper

java.lang.Object
  extended byorg.xml.sax.HandlerBase
      extended byorg.apache.tomcat.util.xml.XmlMapper
All Implemented Interfaces:
org.xml.sax.DocumentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler, SaxContext

public class XmlMapper
extends org.xml.sax.HandlerBase
implements SaxContext

SAX Handler - it will read the XML and construct java objects

Author:
costin@dnt.ro

Constructor Summary
XmlMapper()
           
 
Method Summary
 XmlAction addChild(java.lang.String parentM, java.lang.String argType)
          For the last 2 objects in stack, create a parent-child relation by invokeing parent.parentM with the child as parameter ArgType is the parameter expected by addParent ( null use the current object type)
 void addRule(java.lang.String path, XmlAction action)
           
 void characters(char[] buf, int offset, int len)
           
 java.lang.Object currentObject()
           
static void dumpCP(java.lang.ClassLoader cl)
           
 void endDocument()
           
 void endElement(java.lang.String tag)
           
 org.xml.sax.AttributeList getAttributeList(int pos)
          Random access to attributes of a particular element.
 java.lang.String getBody()
          Body of the last tag.
 java.lang.ClassLoader getClassLoader()
           
 org.xml.sax.AttributeList getCurrentAttributes()
          Attributes of the current tag
 java.lang.String getCurrentElement()
          Current element
 int getDebug()
           
 java.util.Hashtable getEntities()
           
 org.xml.sax.Locator getLocator()
           
 XmlMapper getMapper()
           
 java.util.Stack getObjectStack()
          We maintain a stack to keep java objects that are generated as result of parsing.
 java.lang.Object getPropertySource()
           
 java.lang.Object getRoot()
          The root object is either set by caller before starting the parse or can be created using the first tag.
 java.lang.String getTag(int pos)
          Random Access a particular parent element XXX getElement() is a better name
 int getTagCount()
          Depth of the tag stack.
 boolean getValidating()
           
 java.lang.Object getVariable(java.lang.String name)
           
 void ignorableWhitespace(char[] buf, int offset, int len)
           
 void log(java.lang.String msg)
           
 XmlAction methodParam(int ord)
          Extract the method param from the body of the tag
 XmlAction methodParam(int ord, java.lang.String attrib)
          Extract the method param from a tag attribute
 XmlAction methodSetter(java.lang.String method, int paramC)
          If a tag matches, invoke a method on the current object.
 XmlAction methodSetter(java.lang.String method, int paramC, java.lang.String[] paramTypes)
          If a tag matches, invoke a method on the current object.
 void notationDecl(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
           
 XmlAction objectCreate(java.lang.String classN)
          Create an object using for a matching tag with the given class name
 XmlAction objectCreate(java.lang.String classN, java.lang.String attrib)
          Create an object using an attribute value as the class name If no attribute use classN as a default.
 XmlAction objectCreate(java.lang.String classN, java.lang.String attrib, java.lang.String[] pref)
          Create an object using an attribute value as the class name If no attribute use classN as a default.
 java.lang.Object popObject()
           
 XmlAction popStack()
          Pop the object stack
 java.lang.String positionToString()
           
 java.lang.Object previousObject()
           
 void processingInstruction(java.lang.String name, java.lang.String instruction)
           
 void pushObject(java.lang.Object o)
           
 java.lang.Object readXml(java.io.File xmlFile, java.lang.Object root)
          read an XML file, construct and return the object hierarchy
 java.lang.Object readXml(java.io.InputStream xmlFile, java.lang.Object root)
          read an XML input stream, construct and return the object hierarchy
 void registerDTDFile(java.lang.String publicId, java.lang.String dtdFile)
          Register the specified DTD with a local file.
 void registerDTDRes(java.lang.String publicId, java.lang.String dtdRes)
          Register the specified DTD to map to a resource in the classpath This must be called prior to the first call to readXml().
 java.lang.String replaceProperties(java.lang.String k)
           
 org.xml.sax.InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId)
          Resolve the requested external entity, replacing it by an internal DTD if one has been registered.
 void setClassLoader(java.lang.ClassLoader loader)
           
 void setDebug(int level)
           
 void setDocumentLocator(org.xml.sax.Locator locator)
           
 void setErrorHandler(org.xml.sax.ErrorHandler eh)
           
 XmlAction setParent(java.lang.String childM)
          For the last 2 objects in stack, create a parent-child and child.childM with parente as parameter
 XmlAction setParent(java.lang.String childM, java.lang.String argType)
          For the last 2 objects in stack, create a parent-child and child.childM with parent as parameter
 XmlAction setProperties()
          Set object properties using XML attributes
 void setPropertySource(java.lang.Object src)
           
 void setRoot(java.lang.Object o)
           
 void setValidating(boolean validating)
           
 XmlAction setVar(java.lang.String varName, java.lang.String attName)
          Set a variable varName using the value of an attribute
 XmlAction setVar(java.lang.String varName, java.lang.String nameAtt, java.lang.String valueAtt, boolean reset)
          Set a variable varName using the value of an attribute
 void setVariable(java.lang.String name, java.lang.Object value)
           
 void startDocument()
           
 void startElement(java.lang.String tag, org.xml.sax.AttributeList attributes)
           
 void unparsedEntityDecl(java.lang.String name, java.lang.String publicId, java.lang.String systemId, java.lang.String notationName)
           
 void useLocalLoader(boolean b)
           
 
Methods inherited from class org.xml.sax.HandlerBase
error, fatalError, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlMapper

public XmlMapper()
Method Detail

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
Specified by:
setDocumentLocator in interface org.xml.sax.DocumentHandler

getLocator

public org.xml.sax.Locator getLocator()

startDocument

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

endDocument

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

startElement

public void startElement(java.lang.String tag,
                         org.xml.sax.AttributeList attributes)
                  throws org.xml.sax.SAXException
Specified by:
startElement in interface org.xml.sax.DocumentHandler
Throws:
org.xml.sax.SAXException

positionToString

public java.lang.String positionToString()

endElement

public void endElement(java.lang.String tag)
                throws org.xml.sax.SAXException
Specified by:
endElement in interface org.xml.sax.DocumentHandler
Throws:
org.xml.sax.SAXException

characters

public void characters(char[] buf,
                       int offset,
                       int len)
                throws org.xml.sax.SAXException
Specified by:
characters in interface org.xml.sax.DocumentHandler
Throws:
org.xml.sax.SAXException

ignorableWhitespace

public void ignorableWhitespace(char[] buf,
                                int offset,
                                int len)
                         throws org.xml.sax.SAXException
Specified by:
ignorableWhitespace in interface org.xml.sax.DocumentHandler
Throws:
org.xml.sax.SAXException

processingInstruction

public void processingInstruction(java.lang.String name,
                                  java.lang.String instruction)
                           throws org.xml.sax.SAXException
Specified by:
processingInstruction in interface org.xml.sax.DocumentHandler
Throws:
org.xml.sax.SAXException

getAttributeList

public org.xml.sax.AttributeList getAttributeList(int pos)
Description copied from interface: SaxContext
Random access to attributes of a particular element.

Specified by:
getAttributeList in interface SaxContext

getCurrentAttributes

public org.xml.sax.AttributeList getCurrentAttributes()
Description copied from interface: SaxContext
Attributes of the current tag

Specified by:
getCurrentAttributes in interface SaxContext

getTagCount

public int getTagCount()
Description copied from interface: SaxContext
Depth of the tag stack. XXX getElementDepth() ?

Specified by:
getTagCount in interface SaxContext

getTag

public java.lang.String getTag(int pos)
Description copied from interface: SaxContext
Random Access a particular parent element XXX getElement() is a better name

Specified by:
getTag in interface SaxContext

getCurrentElement

public java.lang.String getCurrentElement()
Description copied from interface: SaxContext
Current element

Specified by:
getCurrentElement in interface SaxContext

getBody

public java.lang.String getBody()
Description copied from interface: SaxContext
Body of the last tag.

Specified by:
getBody in interface SaxContext

getObjectStack

public java.util.Stack getObjectStack()
Description copied from interface: SaxContext
We maintain a stack to keep java objects that are generated as result of parsing. You can either use the stack ( which is very powerfull construct !), or use the root object and navigation in the result tree.

Specified by:
getObjectStack in interface SaxContext

popObject

public java.lang.Object popObject()
Specified by:
popObject in interface SaxContext

currentObject

public java.lang.Object currentObject()
Specified by:
currentObject in interface SaxContext

previousObject

public java.lang.Object previousObject()
Specified by:
previousObject in interface SaxContext

pushObject

public void pushObject(java.lang.Object o)
Specified by:
pushObject in interface SaxContext

getRoot

public java.lang.Object getRoot()
Description copied from interface: SaxContext
The root object is either set by caller before starting the parse or can be created using the first tag. It is used to set object in the result graph by navigation ( using root and a path). Please use the stack, it's much faster and better.

Specified by:
getRoot in interface SaxContext

setRoot

public void setRoot(java.lang.Object o)

setPropertySource

public void setPropertySource(java.lang.Object src)

getPropertySource

public java.lang.Object getPropertySource()

replaceProperties

public java.lang.String replaceProperties(java.lang.String k)

setDebug

public void setDebug(int level)

getDebug

public int getDebug()
Specified by:
getDebug in interface SaxContext

setValidating

public void setValidating(boolean validating)

getValidating

public boolean getValidating()

setVariable

public void setVariable(java.lang.String name,
                        java.lang.Object value)
Specified by:
setVariable in interface SaxContext

getVariable

public java.lang.Object getVariable(java.lang.String name)
Specified by:
getVariable in interface SaxContext

getMapper

public XmlMapper getMapper()
Specified by:
getMapper in interface SaxContext

setErrorHandler

public void setErrorHandler(org.xml.sax.ErrorHandler eh)

useLocalLoader

public void useLocalLoader(boolean b)

log

public void log(java.lang.String msg)
Specified by:
log in interface SaxContext

readXml

public java.lang.Object readXml(java.io.File xmlFile,
                                java.lang.Object root)
                         throws java.lang.Exception
read an XML file, construct and return the object hierarchy

Throws:
java.lang.Exception

readXml

public java.lang.Object readXml(java.io.InputStream xmlFile,
                                java.lang.Object root)
                         throws java.lang.Exception
read an XML input stream, construct and return the object hierarchy

Throws:
java.lang.Exception

registerDTDFile

public void registerDTDFile(java.lang.String publicId,
                            java.lang.String dtdFile)
Register the specified DTD with a local file. This must be called prior to the first call to readXml().

Parameters:
publicId - Public identifier of the DTD to be resolved
dtdFile - The local file name to use for reading this DTD

registerDTDRes

public void registerDTDRes(java.lang.String publicId,
                           java.lang.String dtdRes)
Register the specified DTD to map to a resource in the classpath This must be called prior to the first call to readXml().

Parameters:
publicId - Public identifier of the DTD to be resolved
dtdRes - local resource name, to be used with getResource()

getEntities

public java.util.Hashtable getEntities()

addRule

public void addRule(java.lang.String path,
                    XmlAction action)

resolveEntity

public org.xml.sax.InputSource resolveEntity(java.lang.String publicId,
                                             java.lang.String systemId)
                                      throws org.xml.sax.SAXException
Resolve the requested external entity, replacing it by an internal DTD if one has been registered.

Specified by:
resolveEntity in interface org.xml.sax.EntityResolver
Parameters:
publicId - Public identifier of the entity being referenced
systemId - System identifier of the entity being referenced
Throws:
org.xml.sax.SAXException - if a parsing error occurs

dumpCP

public static void dumpCP(java.lang.ClassLoader cl)

notationDecl

public void notationDecl(java.lang.String name,
                         java.lang.String publicId,
                         java.lang.String systemId)
Specified by:
notationDecl in interface org.xml.sax.DTDHandler

unparsedEntityDecl

public void unparsedEntityDecl(java.lang.String name,
                               java.lang.String publicId,
                               java.lang.String systemId,
                               java.lang.String notationName)
Specified by:
unparsedEntityDecl in interface org.xml.sax.DTDHandler

objectCreate

public XmlAction objectCreate(java.lang.String classN)
Create an object using for a matching tag with the given class name


objectCreate

public XmlAction objectCreate(java.lang.String classN,
                              java.lang.String attrib)
Create an object using an attribute value as the class name If no attribute use classN as a default.


objectCreate

public XmlAction objectCreate(java.lang.String classN,
                              java.lang.String attrib,
                              java.lang.String[] pref)
Create an object using an attribute value as the class name If no attribute use classN as a default. If the class name has no ".", use the third parameter as prefix


setProperties

public XmlAction setProperties()
Set object properties using XML attributes


setVar

public XmlAction setVar(java.lang.String varName,
                        java.lang.String attName)
Set a variable varName using the value of an attribute


setVar

public XmlAction setVar(java.lang.String varName,
                        java.lang.String nameAtt,
                        java.lang.String valueAtt,
                        boolean reset)
Set a variable varName using the value of an attribute


setParent

public XmlAction setParent(java.lang.String childM)
For the last 2 objects in stack, create a parent-child and child.childM with parente as parameter


setParent

public XmlAction setParent(java.lang.String childM,
                           java.lang.String argType)
For the last 2 objects in stack, create a parent-child and child.childM with parent as parameter


addChild

public XmlAction addChild(java.lang.String parentM,
                          java.lang.String argType)
For the last 2 objects in stack, create a parent-child relation by invokeing parent.parentM with the child as parameter ArgType is the parameter expected by addParent ( null use the current object type)


methodSetter

public XmlAction methodSetter(java.lang.String method,
                              int paramC)
If a tag matches, invoke a method on the current object. Parameters can be extracted from sub-elements of the current tag.


methodSetter

public XmlAction methodSetter(java.lang.String method,
                              int paramC,
                              java.lang.String[] paramTypes)
If a tag matches, invoke a method on the current object. Parameters can be extracted from sub-elements of the current tag.


methodParam

public XmlAction methodParam(int ord)
Extract the method param from the body of the tag


methodParam

public XmlAction methodParam(int ord,
                             java.lang.String attrib)
Extract the method param from a tag attribute


popStack

public XmlAction popStack()
Pop the object stack


getClassLoader

public java.lang.ClassLoader getClassLoader()
Specified by:
getClassLoader in interface SaxContext

setClassLoader

public void setClassLoader(java.lang.ClassLoader loader)


Copyright ? 2001 Apache Software Foundation. All Rights Reserved.