com.interstarinc.xmedius.sendfax
Class XMLBuilder.MetaDocHandler

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by com.interstarinc.xmedius.sendfax.XMLBuilder.MetaDocHandler
All Implemented Interfaces:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler
Enclosing class:
XMLBuilder

public class XMLBuilder.MetaDocHandler
extends DefaultHandler

This class defines the SAX parser callback methods.
It creates a DOM tree with text nodes values picked up from a java beans graph according to meta-attributes found in a meta-document.

Author:
André Morin
See Also:
The XMLBuilder documentation

Field Summary
private  DOMImplementation impl
          The DOMImplementation to be used.
private  List metaAttList
          The meta-attributes names retrieved from the processing instruction meta-att-list.
private  Object model
          The model from which to pick up the DOM tree text node values.
private  boolean nameSpaceAware
          Is the meta-document namespace aware?
private  Node parent
          The parent node of a newly created element.
private  String skippedElement
          Holds the meta-document element name to be skipped.
static String XMB_NAME_SPACE
          A namespace aware meta-document must declare this namespace URI on its root element and qualified all meta-attributes.
private  Document xmlDoc
          The document object model tree produced by the XMLBuilder.
 
Constructor Summary
XMLBuilder.MetaDocHandler(Object model)
           
 
Method Summary
private  void addAttributes(Element element, Attributes atts)
          Adds attributes to an element created in the startElement method.
private  void addNodes(Node aListItem, String currentIndex)
          This method adds a node element that maps to a model list entry.
private  Attributes createAttributes(NamedNodeMap map, String currentIndex)
          Creates the attributes to be passed to startElement.
 void endDocument()
           
 void endElement(String nameSpaceUri, String localName, String qName)
          This callback method adds a text node value to an element.
private  String getAttributeValue(Attributes atts, String attName)
          Returns an element meta-attribute value, whether or not the meta-document is namespace aware.
private  String getAttributeValue(Element elem, String attName)
          Returns an element meta-attribute value, whether or not the meta-document is namespace aware.
 Document getXmlDoc()
           
private  boolean isInstructionDataValid(String data)
          This methods validates a processing instruction value.
 void processingInstruction(String target, String data)
          This callback method handles the processing instructions.
private  void removeMetaAttributes(Element element)
          Whenever the parser reaches the end of the meta-document, this method is called to remove all meta-attributes from the created DOM tree.
 void startDocument()
          This callback is used to create the DOM implementor.
 void startElement(String nameSpaceUri, String localName, String qName, Attributes atts)
          This callback method creates the document and its elements.
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
characters, endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, resolveEntity, setDocumentLocator, skippedEntity, startPrefixMapping, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

XMB_NAME_SPACE

public static final String XMB_NAME_SPACE
A namespace aware meta-document must declare this namespace URI on its root element and qualified all meta-attributes.

See Also:
Constant Field Values

impl

private DOMImplementation impl
The DOMImplementation to be used.


xmlDoc

private Document xmlDoc
The document object model tree produced by the XMLBuilder.


parent

private Node parent
The parent node of a newly created element.


skippedElement

private String skippedElement
Holds the meta-document element name to be skipped.


model

private Object model
The model from which to pick up the DOM tree text node values.


metaAttList

private List metaAttList
The meta-attributes names retrieved from the processing instruction meta-att-list.

See Also:
processingInstruction(String, String), isInstructionDataValid(String)

nameSpaceAware

private boolean nameSpaceAware
Is the meta-document namespace aware?

Constructor Detail

XMLBuilder.MetaDocHandler

public XMLBuilder.MetaDocHandler(Object model)
Parameters:
model - The java beans graph used to set text node values.
Method Detail

getXmlDoc

public Document getXmlDoc()
Returns:
The DOM tree generated while parsing the meta-document.

startDocument

public void startDocument()
                   throws SAXException
This callback is used to create the DOM implementor.

Specified by:
startDocument in interface ContentHandler
Overrides:
startDocument in class DefaultHandler
Throws:
SAXException - if the DOM implementor cannot be created.
See Also:
DocumentBuilderFactory, DocumentBuilder

startElement

public void startElement(String nameSpaceUri,
                         String localName,
                         String qName,
                         Attributes atts)
                  throws SAXException
This callback method creates the document and its elements.

Specified by:
startElement in interface ContentHandler
Overrides:
startElement in class DefaultHandler
Parameters:
nameSpaceUri - The element namespace uri, if the meta-document is namespace aware.
localName - The element local name, if the meta-document is namespace aware.
qName - The qualified element name.
atts - The element attributes
Throws:
SAXException
See Also:
addAttributes(Element, Attributes), getAttributeValue(Attributes, String), PropertyUtils.getProperty(java.lang.Object, java.lang.String)

getAttributeValue

private String getAttributeValue(Attributes atts,
                                 String attName)
Returns an element meta-attribute value, whether or not the meta-document is namespace aware.

Parameters:
atts - The element attributes.
attName - The meta-attribute to look up in atts.
Returns:
The meta-attribute value if present, null otherwise.

getAttributeValue

private String getAttributeValue(Element elem,
                                 String attName)
Returns an element meta-attribute value, whether or not the meta-document is namespace aware.

Parameters:
elem - The element to be queried.
attName - The meta-attribute to look up in elem.
Returns:
The meta-attribute value if present, otherwise an empty string.

endElement

public void endElement(String nameSpaceUri,
                       String localName,
                       String qName)
                throws SAXException
This callback method adds a text node value to an element.
The text node value is retrieved from the model.

Specified by:
endElement in interface ContentHandler
Overrides:
endElement in class DefaultHandler
Parameters:
nameSpaceUri - The element namespace uri, if the meta-document is namespace aware.
localName - The element local name, if the meta-document is namespace aware.
qName - The qualified element name.
Throws:
SAXException
See Also:
getAttributeValue(Element, String), addNodes(Node, String), PropertyUtils.getProperty(java.lang.Object, java.lang.String)

addNodes

private void addNodes(Node aListItem,
                      String currentIndex)
               throws SAXException
This method adds a node element that maps to a model list entry.
It uses recursion to process aListItem and its children.
This method calls startElement and then, calls itself for each aListItem children. Whenever backtracking occurs this method calls endElement.

Parameters:
aListItem - The meta-document element that maps to a model list entry.
currentIndex - The current model list entry.
Throws:
SAXException
See Also:
startElement(String, String, String, Attributes), endElement(String, String, String), createAttributes(NamedNodeMap, String)

createAttributes

private Attributes createAttributes(NamedNodeMap map,
                                    String currentIndex)
Creates the attributes to be passed to startElement.
This method is called when processing indexed model properties.

Parameters:
map - The element attributes map used to creates the attribute list.
currentIndex - This parameter is used to update the meta-attribute index when iterating over an indexed model property.
Returns:
The attribute list to be passed to startElement
See Also:
startElement(String, String, String, Attributes), addNodes(Node, String)

endDocument

public void endDocument()
                 throws SAXException
Specified by:
endDocument in interface ContentHandler
Overrides:
endDocument in class DefaultHandler
Throws:
SAXException
See Also:
removeMetaAttributes(Element)

removeMetaAttributes

private void removeMetaAttributes(Element element)
                           throws SAXException
Whenever the parser reaches the end of the meta-document, this method is called to remove all meta-attributes from the created DOM tree.

Parameters:
element - The created document root element.
Throws:
SAXException
See Also:
endDocument()

addAttributes

private void addAttributes(Element element,
                           Attributes atts)
                    throws SAXException
Adds attributes to an element created in the startElement method.
The element inherits the attributes, including the meta-attributes, passed to the startElement method. The meta-attributes are also added in order to handle the indexed model properties. They will be removed from the document whenever the parsing is completed.

Parameters:
element - The element created in the startElement method.
atts - The attributes list used to create the element's attributes.
Throws:
SAXException
See Also:
startElement(String, String, String, Attributes), removeMetaAttributes(Element)

processingInstruction

public void processingInstruction(String target,
                                  String data)
                           throws SAXException
This callback method handles the processing instructions.
Whenever the processing instruction is meta-att-list, this method validates its value. If the value is valid, this method initializes the instance variable metaAttList from the processing instruction value. Otherwise, it throws a SAXException

Specified by:
processingInstruction in interface ContentHandler
Overrides:
processingInstruction in class DefaultHandler
Parameters:
target - The processing instruction name.
data - The processing instruction value.
Throws:
SAXException

isInstructionDataValid

private boolean isInstructionDataValid(String data)
This methods validates a processing instruction value.

Parameters:
data - A processing instruction value to be validated.
Returns:
true if the processing instruction value is valid, false otherwise.