org.atzberger.application.selm_builder
Class XMLContentHandler

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by org.xml.sax.ext.DefaultHandler2
          extended by org.atzberger.application.selm_builder.XMLContentHandler
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler, org.xml.sax.ext.DeclHandler, org.xml.sax.ext.EntityResolver2, org.xml.sax.ext.LexicalHandler

public abstract class XMLContentHandler
extends org.xml.sax.ext.DefaultHandler2

XMLContentHandler provides a structured approach to building a data structure from XML content using SAX parser. Generally most of the data structures can be built in a top down approach where a parent gets created first and then children add themselves to the parent. This utility class facilitates building data structures in such a top down approach. The default content handler provided by SAX API is not conducive to building a data structure. So a programmer ends up in storing parent context of an element as a transient attribute of the handler, but this does not scale up. For every level of depth, a separate attribute needs to be created. This utility class provides parent element as the context while creating child element and hence allows building a data structure top down. This utility also provides the element context while reading CDATA and TEXT sections of an element. In order to make use of this feature, certain properties have to be set up as follows...


 SAXParser parser = ...
 XMLContentHandler handler = ... //an instance of a subclass of XMLContentHandler
 parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
 

In order to make use of XMLContentHandler, user has to subclass this and provide implementation for the following methods ...

 
 createElement, processText and processCDATA.
 

Author:
venkatm (http://www.javalobby.org/java/forums/t104100.html) This code is in the public domain, see above link for details.

Constructor Summary
XMLContentHandler()
           
 
Method Summary
 void characters(char[] ch, int start, int length)
           
protected abstract  XMLElement createElement(XMLElement parent, java.lang.String name, org.xml.sax.Attributes attributes)
          Subclasses provide an implementation for this.
 void endCDATA()
           
 void endElement(java.lang.String uri, java.lang.String localName, java.lang.String name)
           
protected  XMLElement newXMLElement(XMLElement parent, java.lang.Object value)
           
protected  XMLElement nullXMLElement(XMLElement parent)
          Give a call to this to create null valued XML elements.
protected abstract  void processCDATA(XMLElement parentElement, XMLElement element, java.lang.String str)
           
protected abstract  void processText(XMLElement parentElement, XMLElement element, java.lang.String name, java.lang.String str)
           
 void startCDATA()
           
 void startElement(java.lang.String uri, java.lang.String localName, java.lang.String name, org.xml.sax.Attributes attributes)
           
 
Methods inherited from class org.xml.sax.ext.DefaultHandler2
attributeDecl, comment, elementDecl, endDTD, endEntity, externalEntityDecl, getExternalSubset, internalEntityDecl, resolveEntity, resolveEntity, startDTD, startEntity
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endDocument, endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, processingInstruction, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLContentHandler

public XMLContentHandler()
Method Detail

startElement

public void startElement(java.lang.String uri,
                         java.lang.String localName,
                         java.lang.String name,
                         org.xml.sax.Attributes attributes)
                  throws org.xml.sax.SAXException
Specified by:
startElement in interface org.xml.sax.ContentHandler
Overrides:
startElement in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

createElement

protected abstract XMLElement createElement(XMLElement parent,
                                            java.lang.String name,
                                            org.xml.sax.Attributes attributes)
                                     throws java.lang.Exception
Subclasses provide an implementation for this.

Parameters:
parent - Represents the parent element created before. It is null for the root element
name - Represents the name of the XML element.
attributes - Represents the attributes of the XML element
Returns:
element.
Throws:
java.lang.Exception

newXMLElement

protected XMLElement newXMLElement(XMLElement parent,
                                   java.lang.Object value)

nullXMLElement

protected XMLElement nullXMLElement(XMLElement parent)
Give a call to this to create null valued XML elements. This would be helpful when an xml element represents just a value rather an object of its own. Very often primitive valued properties of an object are represented by an individual xml element.

Parameters:
parent -
Returns:
element

endElement

public void endElement(java.lang.String uri,
                       java.lang.String localName,
                       java.lang.String name)
                throws org.xml.sax.SAXException
Specified by:
endElement in interface org.xml.sax.ContentHandler
Overrides:
endElement in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

processText

protected abstract void processText(XMLElement parentElement,
                                    XMLElement element,
                                    java.lang.String name,
                                    java.lang.String str)
                             throws java.lang.Exception
Parameters:
parentElement - It is null when element represents root.
element -
name -
str -
Throws:
java.lang.Exception

endCDATA

public void endCDATA()
              throws org.xml.sax.SAXException
Specified by:
endCDATA in interface org.xml.sax.ext.LexicalHandler
Overrides:
endCDATA in class org.xml.sax.ext.DefaultHandler2
Throws:
org.xml.sax.SAXException

processCDATA

protected abstract void processCDATA(XMLElement parentElement,
                                     XMLElement element,
                                     java.lang.String str)
                              throws java.lang.Exception
Throws:
java.lang.Exception

startCDATA

public void startCDATA()
                throws org.xml.sax.SAXException
Specified by:
startCDATA in interface org.xml.sax.ext.LexicalHandler
Overrides:
startCDATA in class org.xml.sax.ext.DefaultHandler2
Throws:
org.xml.sax.SAXException

characters

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