com.ls.util.xml
Class XMLHandler

java.lang.Object
  |
  +--com.ls.util.xml.XMLHandler
All Implemented Interfaces:
org.xml.sax.DocumentHandler, IXMLHandler
Direct Known Subclasses:
OrderedXMLHandler

public class XMLHandler
extends Object
implements org.xml.sax.DocumentHandler, IXMLHandler

Creates XML documents from a Map or to parse a XML document into a Map

Version:
$Revision: 1.5 $
Author:
Last modified by $Author: OHittmeyer $

Field Summary
protected  StringBuffer elementValue
          the value of an element
 
Constructor Summary
XMLHandler()
          creates a Parser instance and sets its handler to this object
 
Method Summary
 String buildString(String documentType, Map xmlHash)
          creates a XML document from a map
 void characters(char[] ch, int start, int length)
          Any character within an element node will be interpreted as the value of this element node.
protected  Map createElementHash()
          Creates a map.
 com.sun.xml.tree.XmlDocument createXMLdocument(String documentType, Map xmlHash)
          creates a XML document from a map
 void endDocument()
          If the document is finished there should be only one map left in the list.
 void endElement(String elementName)
          If the Parser receives the end tag of an element, he will remove it from the list of stuff to parse.
 ReferenceIdentityHashtable getAttributes()
          returns the list of attributes (which was generated while parsing)
 void ignorableWhitespace(char[] ch, int start, int length)
          empty implementation: method is not needed for the parsing of XML streams but belongs to the interface DocumentHandler
 Map parse(org.xml.sax.InputSource xmlSource)
          Parses a XML structure as read from the given InputSource and returns a map view on it.
 Map parse(Reader reader)
          Parses a XML structure as read from the given Reader and returns a map view on it.
 Map parse(String xmlString)
          Parses a XML structure as read from the given String and returns a map view on it.
 void processingInstruction(String target, String data)
          empty implementation: method is not needed for the parsing of XML streams but belongs to the interface DocumentHandler
 void setDocumentLocator(org.xml.sax.Locator locator)
          empty implementation: method is not needed for the parsing of XML streams but belongs to the interface DocumentHandler
 void startDocument()
          receives notification of the beginning of a document and creates a new list to store the element nodes
 void startElement(String elementName, org.xml.sax.AttributeList attributeList)
          If the Parser receives the start tag of an element, a new map will be added to the list, containing the attributes of this element node.
 void write(Writer writer, String documentType, Map xmlHash)
          creates a XML document from a map and writes it to the specified Writer
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

elementValue

protected StringBuffer elementValue
the value of an element
Constructor Detail

XMLHandler

public XMLHandler()
creates a Parser instance and sets its handler to this object
Method Detail

getAttributes

public ReferenceIdentityHashtable getAttributes()
returns the list of attributes (which was generated while parsing)
Returns:
reference to a container holding all attribute lists as key value pairs, where the keys are the object references stored in the content hash. The values are objects of type AttributeList of the org.xml.sax.AttributeList package containing all attributes of the corresponding tag.

createElementHash

protected Map createElementHash()
Creates a map.
Purpose of this method is to allow child classes using different map implementations.
Returns:
empty map (HashMap in this implementation)

createXMLdocument

public com.sun.xml.tree.XmlDocument createXMLdocument(String documentType,
                                                      Map xmlHash)
creates a XML document from a map
Parameters:
documentType - specifies the document type (for example: MESSAGE or CONFIG)
xmlHash - XML data stored in a map
Returns:
XML document built from xmlHash

buildString

public String buildString(String documentType,
                          Map xmlHash)
                   throws XMLException
creates a XML document from a map
Parameters:
xmlHash - XML data stored in a hash (keys will become tag names, values the tag values)
documentType - specifies the document type (for example: "MESSAGE" or "CONTENT")
Returns:
XML document built from given documentType and xmlHash
Throws:
XMLException - If the conversion to XML fails

write

public void write(Writer writer,
                  String documentType,
                  Map xmlHash)
           throws XMLException
creates a XML document from a map and writes it to the specified Writer
Parameters:
writer - Writer to wich the built XML string should be written
documentType - specifies the document type (message type)
xmlHash - XML data stored in a hash
Throws:
XMLException - If the XML string was not successfully written to the writer

parse

public Map parse(org.xml.sax.InputSource xmlSource)
          throws XMLException,
                 IllegalArgumentException,
                 ProgrammingException
Parses a XML structure as read from the given InputSource and returns a map view on it.
Specified by:
parse in interface IXMLHandler
Parameters:
xmlSource - the source from where to get the XML structure
Returns:
Map
Throws:
XMLException - If the parsing process fails
IllegalArgumentException - If the given XML input source is null
ProgrammingException - If this.xmlParser is null

parse

public Map parse(Reader reader)
          throws XMLException,
                 IllegalArgumentException,
                 ProgrammingException
Parses a XML structure as read from the given Reader and returns a map view on it.
Specified by:
parse in interface IXMLHandler
Parameters:
reader - the source from where to get the XML structure
Returns:
Map
Throws:
XMLException - If the parsing process fails
IllegalArgumentException - If the given reader is null
ProgrammingException - If this.xmlParser is null

parse

public Map parse(String xmlString)
          throws XMLException,
                 IllegalArgumentException,
                 ProgrammingException
Parses a XML structure as read from the given String and returns a map view on it.
Specified by:
parse in interface IXMLHandler
Parameters:
xmlString - the source from where to get the XML structure
Returns:
Map
Throws:
XMLException - If the parsing process fails
IllegalArgumentException - If the given XML string is null
ProgrammingException - If this.xmlParser is null

startElement

public void startElement(String elementName,
                         org.xml.sax.AttributeList attributeList)
                  throws ProgrammingException
If the Parser receives the start tag of an element, a new map will be added to the list, containing the attributes of this element node.
Specified by:
startElement in interface org.xml.sax.DocumentHandler
Parameters:
elementName - element type name
attributeList - NOT USED (according to interface: attributes attached to the element, if any)
Throws:
ProgrammingException - If this.xmlList is null

characters

public void characters(char[] ch,
                       int start,
                       int length)
Any character within an element node will be interpreted as the value of this element node.
Specified by:
characters in interface org.xml.sax.DocumentHandler
Parameters:
ch - characters from the XML document
start - start position in the array
length - number of characters to read from the array

endElement

public void endElement(String elementName)
If the Parser receives the end tag of an element, he will remove it from the list of stuff to parse. If the element has sub elements, those will be parsed first.
Specified by:
endElement in interface org.xml.sax.DocumentHandler
Parameters:
elementName - element type name

startDocument

public void startDocument()
receives notification of the beginning of a document and creates a new list to store the element nodes
Specified by:
startDocument in interface org.xml.sax.DocumentHandler

endDocument

public void endDocument()
If the document is finished there should be only one map left in the list. This map contains the whole XML string. To return it in the parse method, the xmlHash is set to the last element of the list.
Specified by:
endDocument in interface org.xml.sax.DocumentHandler

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
empty implementation: method is not needed for the parsing of XML streams but belongs to the interface DocumentHandler
Specified by:
ignorableWhitespace in interface org.xml.sax.DocumentHandler
Parameters:
ch - char[] containing the whitespace characters
start - starting point of the array from where to identify whitespace characters
length - number of characters specifying the whitespace

processingInstruction

public void processingInstruction(String target,
                                  String data)
empty implementation: method is not needed for the parsing of XML streams but belongs to the interface DocumentHandler
Specified by:
processingInstruction in interface org.xml.sax.DocumentHandler
Parameters:
target - the target for calling a processing instruction
data - a string representing the data

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
empty implementation: method is not needed for the parsing of XML streams but belongs to the interface DocumentHandler
Specified by:
setDocumentLocator in interface org.xml.sax.DocumentHandler
Parameters:
locator - object that can return the location of any SAX document event