org.progeeks.util.xml
Class XmlReader

java.lang.Object
  extended by org.progeeks.util.xml.XmlReader
Direct Known Subclasses:
ObjectXmlReader, SimpleXmlReader, XmlSignatureReader

public class XmlReader
extends java.lang.Object

Provides base support for reading XML files into objects as defined by subclass implementations. This class acts as an adatper between the SAX HandlerBase class and slightly more subclass friendly version. This class is designed to work with JAXP 1.0, ie: SAX1... I don't want to break compatability yet so using a newer version of JAXP will work, but will generate deprecation warnings.

This base class implementation also provides a simple kind of tag handler support that allows subclasses to simple register the appropriate handler objects for reading certain tag types.

Since:
1.0
Version:
$Revision: 1.18 $
Author:
Paul Speed

Nested Class Summary
protected  class XmlReader.XmlHandler
          HandlerBase implementation that interacts with the outer class.
 
Constructor Summary
XmlReader()
           
 
Method Summary
protected  void addTagText(java.lang.String name, java.lang.String newText, java.lang.StringBuffer accumulatedText, java.lang.Object object)
          Called when new text is parsed within a tag body.
protected  XmlException createXmlException(java.lang.String message)
          Creates a properly "located" XmlException using the getLocation() method.
protected  XmlException createXmlException(java.lang.String message, java.lang.Throwable cause)
          Creates a properly "located" XmlException using the getLocation() method.
protected  void endTag(java.lang.String name, java.lang.String text, java.lang.Object object)
          Called when the end of a tag is reached.
static java.util.Map getAttributesAsMap(org.xml.sax.Attributes atts)
          Converts the specified Attributes object to a Map.
protected  java.lang.Object getCurrentObject()
          Returns the last object that was created during a startTag() method.
protected  org.xml.sax.Locator getDocumentLocator()
          Returns a locator object that can be used to determine information about the current file position.
protected  boolean getIgnoreTagCase()
          Returns the tag name case sensitivity policy for looking up tag handlers.
protected  java.lang.String getLocation()
          Returns the current location as a formatted string.
protected  java.lang.Object getPreviousObject()
          Returns the object that was created two calls startTag() ago.
protected  java.lang.Object getRootObject()
          Returns the root object on the stack.
protected  int getStackSize()
          Returns the current tag object stack size.
protected  TagReader getTagReader(java.lang.String tagName)
           
 java.lang.Object readObject(java.io.Reader in)
          Implemented by subclasses to provide read support.
protected  void readXml(org.w3c.dom.Document document)
          Called by subclasses to read XML in from the supplied document.
protected  void readXml(org.w3c.dom.Document document, javax.xml.transform.Transformer transformer)
          Called by subclasses to read XML in from the supplied document after applying the given transformation.
protected  void readXml(java.io.Reader in)
          Called by the subclass implementation to load data from the specified reader.
protected  void registerHandler(java.lang.String tagName, TagReader tagReader)
          Registers the specified handler object to handle tags of the specified tag name.
protected  void replaceCurrentObject(java.lang.Object object)
          Replaces the last object that was create during a startTag().
protected  void reset()
          Resets the internal data structures before reading another object.
protected  org.xml.sax.InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId)
           
protected  void setDefaultHandler(TagReader defaultHandler)
          Sets a default handler that will be used when a better handler cannot be found.
protected  void setIgnoreTagCase(boolean bool)
          Sets the tag name case sensitivity policy for looking up tag handlers.
protected  java.lang.Object startTag(java.lang.String name, org.xml.sax.Attributes atts)
          Called at the beginning of a new tag.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlReader

public XmlReader()
Method Detail

readObject

public java.lang.Object readObject(java.io.Reader in)
                            throws java.io.IOException
Implemented by subclasses to provide read support. This is an optional operation since some xml reader implementations will only read from a single reader. Eventually we may want to break this out into a ReusableXmlReader interface or something.

Throws:
java.io.IOException

reset

protected void reset()
Resets the internal data structures before reading another object. This should be called by readObject().


setIgnoreTagCase

protected void setIgnoreTagCase(boolean bool)
Sets the tag name case sensitivity policy for looking up tag handlers. True means that case is ignored when looking up handlers.


getIgnoreTagCase

protected boolean getIgnoreTagCase()
Returns the tag name case sensitivity policy for looking up tag handlers.


setDefaultHandler

protected void setDefaultHandler(TagReader defaultHandler)
Sets a default handler that will be used when a better handler cannot be found.


registerHandler

protected void registerHandler(java.lang.String tagName,
                               TagReader tagReader)
Registers the specified handler object to handle tags of the specified tag name.


getTagReader

protected TagReader getTagReader(java.lang.String tagName)

startTag

protected java.lang.Object startTag(java.lang.String name,
                                    org.xml.sax.Attributes atts)
                             throws org.xml.sax.SAXException
Called at the beginning of a new tag. The implementation class can use the supplied data to instantiate an object and return it. After this method, the returned object will be the new value returned by getCurrentObject(). The default implementation of this method searches for the tag handler object associated with the specified tag name and uses that to process the attribute list.

Throws:
org.xml.sax.SAXException

addTagText

protected void addTagText(java.lang.String name,
                          java.lang.String newText,
                          java.lang.StringBuffer accumulatedText,
                          java.lang.Object object)
                   throws org.xml.sax.SAXException
Called when new text is parsed within a tag body.

Throws:
org.xml.sax.SAXException

endTag

protected void endTag(java.lang.String name,
                      java.lang.String text,
                      java.lang.Object object)
               throws org.xml.sax.SAXException
Called when the end of a tag is reached.

Throws:
org.xml.sax.SAXException

getDocumentLocator

protected org.xml.sax.Locator getDocumentLocator()
Returns a locator object that can be used to determine information about the current file position.


getCurrentObject

protected java.lang.Object getCurrentObject()
Returns the last object that was created during a startTag() method.


replaceCurrentObject

protected void replaceCurrentObject(java.lang.Object object)
Replaces the last object that was create during a startTag().


getPreviousObject

protected java.lang.Object getPreviousObject()
Returns the object that was created two calls startTag() ago. This should be the parent of the current object.


getStackSize

protected int getStackSize()
Returns the current tag object stack size.


getRootObject

protected java.lang.Object getRootObject()
Returns the root object on the stack.


getLocation

protected java.lang.String getLocation()
Returns the current location as a formatted string.


createXmlException

protected XmlException createXmlException(java.lang.String message)
Creates a properly "located" XmlException using the getLocation() method.


createXmlException

protected XmlException createXmlException(java.lang.String message,
                                          java.lang.Throwable cause)
Creates a properly "located" XmlException using the getLocation() method.


getAttributesAsMap

public static java.util.Map getAttributesAsMap(org.xml.sax.Attributes atts)
Converts the specified Attributes object to a Map. This is mostly to allow easy insertion into exceptions and log messages since some information is actually lost.


readXml

protected void readXml(java.io.Reader in)
                throws java.io.IOException,
                       XmlException
Called by the subclass implementation to load data from the specified reader.

Throws:
java.io.IOException
XmlException

readXml

protected void readXml(org.w3c.dom.Document document)
                throws XmlException
Called by subclasses to read XML in from the supplied document.

Throws:
XmlException

readXml

protected void readXml(org.w3c.dom.Document document,
                       javax.xml.transform.Transformer transformer)
                throws XmlException
Called by subclasses to read XML in from the supplied document after applying the given transformation.

Throws:
XmlException

resolveEntity

protected org.xml.sax.InputSource resolveEntity(java.lang.String publicId,
                                                java.lang.String systemId)
                                         throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException


Copyright © 2002-2003 Paul Speed. All Rights Reserved.