com.wutka.jox
Class JOXBeanDOM

java.lang.Object
  |
  +--com.wutka.jox.JOXBeanOutput
        |
        +--com.wutka.jox.JOXBeanDOM

public class JOXBeanDOM
extends com.wutka.jox.JOXBeanOutput

Converts a Java Bean into an XML Document. If you don't supply a Document object, this class uses the DocumentBuilder to create a new document. For some tools like the Xalan XSL processor, you must use a specific Document implementation (the Xerces implementation). If you need to use Xalan, create your own Document using the Xerces API and pass it to one of the JOXBeanDOM constructors.


Field Summary
protected  java.util.Hashtable attrTranslation
           
protected static java.util.Hashtable beanCache
           
protected  java.util.Hashtable childTranslation
           
protected static java.text.DateFormat dateFormat
          The date formatter used to write out dates
protected  org.w3c.dom.Document document
           
protected  com.wutka.dtd.DTD dtd
           
protected static java.lang.Object[] getParams
          The dummy parameter block for invoking the getters for the bean
protected  java.util.Hashtable nameTranslation
           
protected  com.wutka.jox.JOXOutput outputStream
           
static int WRITE_AS_ATTRIBUTE
           
static int WRITE_AS_ELEMENT
           
static int WRITE_AS_INDEXED_ELEMENT
           
protected  boolean writeAttributes
           
 
Constructor Summary
JOXBeanDOM()
          Creates a JOXBeanDOM that stores simple properties as attributes
JOXBeanDOM(boolean writeAttributesFlag)
          Creates a JOXBeanDOM that doesn't use a DTD
JOXBeanDOM(org.w3c.dom.Document aDocument)
          Creates a JOXBeanDOM that stores simple properties as attributes
JOXBeanDOM(org.w3c.dom.Document aDocument, boolean writeAttributesFlag)
          Creates a JOXBeanDOM that doesn't use a DTD
JOXBeanDOM(org.w3c.dom.Document aDocument, com.wutka.dtd.DTD aDTD)
          Creates a JOXBeanDOM that uses a DTD to determine which attributes to store and what the format of the names should be.
JOXBeanDOM(org.w3c.dom.Document aDocument, java.lang.String dtdURL)
          Creates a JOXBeanDOM that uses a DTD to determine which attributes to store and what the format of the names should be.
JOXBeanDOM(com.wutka.dtd.DTD aDTD)
          Creates a JOXBeanDOM that uses a DTD to determine which attributes to store and what the format of the names should be.
JOXBeanDOM(java.lang.String dtdURL)
          Creates a JOXBeanDOM that uses a DTD to determine which attributes to store and what the format of the names should be.
 
Method Summary
 org.w3c.dom.Document beanToDocument(java.lang.String rootName, java.lang.Object ob)
          Stores a Java bean in an XML document (this is the main method for JOXBeanDOM).
protected  void createDocument()
          Creates a new Document using a DocumentBuilder
 void expandChildElements(com.wutka.dtd.DTDContainer children, java.util.Hashtable childTransTable)
           
 java.lang.String getTranslatedChildName(java.lang.String elementName, java.lang.String childName)
           
 java.lang.String getTranslatedEntityName(java.lang.String name)
          Finds the correct format of a name as given by the DTD.
protected  com.wutka.dtd.DTD readDTD(java.lang.String dtdURL)
           
protected  void setupNameTranslation()
           
 void storeBasicType(java.lang.String xmlTagName, java.lang.String propName, java.lang.Object propValue, int writeType, org.w3c.dom.Element parentElement)
          Writes out a basic type including the opening and closing tags.
 void storeDate(java.lang.String xmlTagName, java.lang.String propName, java.lang.Object propValue, int writeType, org.w3c.dom.Element parentElement)
          Writes out a date including the opening and closing tags
 void storeIndexedProperty(java.lang.String xmlTagName, java.lang.Object ob, java.beans.IndexedPropertyDescriptor prop, org.w3c.dom.Element parentElement)
          Stores an indexed property (an array of properties)
 org.w3c.dom.Element storeObject(java.lang.String rootName, java.lang.Object ob)
          Stores a bean in an XML element and returns the element
 void storeObjectFields(java.lang.String xmlTagName, java.lang.Object ob, int writeType, org.w3c.dom.Element parentElement)
          Stores the properties of a bean as DOM Elements
 void storeProperty(java.lang.String xmlTagName, java.lang.Object ob, java.beans.PropertyDescriptor prop, int writeType, org.w3c.dom.Element parentElement)
          Stores a non-indexed property
 void storePropertyValue(java.lang.String xmlTagName, java.lang.Object propValue, java.beans.PropertyDescriptor prop, int writeType, org.w3c.dom.Element parentElement)
          Stores a property value based on the property type
 void storeString(java.lang.String xmlTagName, java.lang.String propName, java.lang.Object propValue, int writeType, org.w3c.dom.Element parentElement)
          Writes out a string, using CDATA if the string contains < or &
 java.lang.String stripName(java.lang.String name)
          Converts a name to lower case and removes any '-' or '_' characters
 boolean writeAsAttribute(java.lang.String elementName, java.lang.String attributeName)
          Returns true if a tag name for a particular object should be written as an attribute
 void writeBasicType(java.lang.String xmlTagName, java.lang.String propName, java.lang.Object propValue, int writeType)
          Writes out a basic type including the opening and closing tags.
 void writeDate(java.lang.String xmlTagName, java.lang.String propName, java.lang.Object propValue, int writeType)
          Writes out a date including the opening and closing tags
 void writeIndexedProperty(java.lang.String xmlTagName, java.lang.Object ob, java.beans.IndexedPropertyDescriptor prop)
          Writes out an indexed property (an array of properties)
 void writeObject(java.lang.String rootName, java.lang.Object ob)
          Writes out an object as XML
 void writeObjectFields(java.lang.String xmlTagName, java.lang.Object ob, int writeType)
          Writes out the properties of a bean as XML
 void writeProperty(java.lang.String xmlTagName, java.lang.Object ob, java.beans.PropertyDescriptor prop, int writeType)
          Writes out a non-indexed property
 void writePropertyValue(java.lang.String xmlTagName, java.lang.Object propValue, java.beans.PropertyDescriptor prop, int writeType)
          Writes out a property value based on the property type
 void writeString(java.lang.String xmlTagName, java.lang.String propName, java.lang.Object propValue, int writeType)
          Writes out a string, using CDATA if the string contains < or &
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

document

protected org.w3c.dom.Document document

beanCache

protected static java.util.Hashtable beanCache

WRITE_AS_ATTRIBUTE

public static final int WRITE_AS_ATTRIBUTE

WRITE_AS_ELEMENT

public static final int WRITE_AS_ELEMENT

WRITE_AS_INDEXED_ELEMENT

public static final int WRITE_AS_INDEXED_ELEMENT

dateFormat

protected static java.text.DateFormat dateFormat
The date formatter used to write out dates

getParams

protected static java.lang.Object[] getParams
The dummy parameter block for invoking the getters for the bean

nameTranslation

protected java.util.Hashtable nameTranslation

attrTranslation

protected java.util.Hashtable attrTranslation

childTranslation

protected java.util.Hashtable childTranslation

dtd

protected com.wutka.dtd.DTD dtd

outputStream

protected com.wutka.jox.JOXOutput outputStream

writeAttributes

protected boolean writeAttributes
Constructor Detail

JOXBeanDOM

public JOXBeanDOM()
           throws java.io.IOException
Creates a JOXBeanDOM that stores simple properties as attributes

JOXBeanDOM

public JOXBeanDOM(org.w3c.dom.Document aDocument)
           throws java.io.IOException
Creates a JOXBeanDOM that stores simple properties as attributes
Parameters:
aDocument - The document to store the bean into

JOXBeanDOM

public JOXBeanDOM(java.lang.String dtdURL)
           throws java.io.IOException
Creates a JOXBeanDOM that uses a DTD to determine which attributes to store and what the format of the names should be.
Parameters:
dtdURL - The URL of the DTD

JOXBeanDOM

public JOXBeanDOM(com.wutka.dtd.DTD aDTD)
           throws java.io.IOException
Creates a JOXBeanDOM that uses a DTD to determine which attributes to store and what the format of the names should be.
Parameters:
dtdURL - The URL of the DTD

JOXBeanDOM

public JOXBeanDOM(org.w3c.dom.Document aDocument,
                  java.lang.String dtdURL)
           throws java.io.IOException
Creates a JOXBeanDOM that uses a DTD to determine which attributes to store and what the format of the names should be.
Parameters:
dtdURL - The URL of the DTD
aDocument - The XML document the bean will be stored in

JOXBeanDOM

public JOXBeanDOM(org.w3c.dom.Document aDocument,
                  com.wutka.dtd.DTD aDTD)
Creates a JOXBeanDOM that uses a DTD to determine which attributes to store and what the format of the names should be.
Parameters:
dtdURL - The URL of the DTD
aDocument - The XML document the bean will be stored in

JOXBeanDOM

public JOXBeanDOM(boolean writeAttributesFlag)
           throws java.io.IOException
Creates a JOXBeanDOM that doesn't use a DTD
Parameters:
writeAttributesFlag - True if simple properties should be stored as attributes

JOXBeanDOM

public JOXBeanDOM(org.w3c.dom.Document aDocument,
                  boolean writeAttributesFlag)
           throws java.io.IOException
Creates a JOXBeanDOM that doesn't use a DTD
Parameters:
aDocument - The XML document the bean will be stored in
writeAttributesFlag - True if simple properties should be stored as attributes
Method Detail

createDocument

protected void createDocument()
                       throws java.io.IOException
Creates a new Document using a DocumentBuilder

beanToDocument

public org.w3c.dom.Document beanToDocument(java.lang.String rootName,
                                           java.lang.Object ob)
                                    throws java.io.IOException
Stores a Java bean in an XML document (this is the main method for JOXBeanDOM).
Parameters:
rootName - The name of the document's root tag
ob - The bean to store in the document

storeObject

public org.w3c.dom.Element storeObject(java.lang.String rootName,
                                       java.lang.Object ob)
                                throws java.io.IOException
Stores a bean in an XML element and returns the element
Parameters:
rootName - The name of the element's tag
ob - The bean to store in the element

storeObjectFields

public void storeObjectFields(java.lang.String xmlTagName,
                              java.lang.Object ob,
                              int writeType,
                              org.w3c.dom.Element parentElement)
                       throws java.io.IOException
Stores the properties of a bean as DOM Elements
Parameters:
xmlTagName - The XML tag name of the bean being written
ob - The bean whose properties will be written
writeType - The kind of fields being written (attribute or element)
Throws:
java.io.IOException - If there is an error writing the object

storeProperty

public void storeProperty(java.lang.String xmlTagName,
                          java.lang.Object ob,
                          java.beans.PropertyDescriptor prop,
                          int writeType,
                          org.w3c.dom.Element parentElement)
                   throws java.io.IOException
Stores a non-indexed property
Parameters:
xmlTagName - The XML tag name of the bean being written
ob - The object containing the property
prop - The property descriptor of the property
writeType - Should we try to write this property as an attribute? If not, don't write the object if writeAttributes is true and this object can be written as an attribute
Throws:
java.io.IOException - If there is an error writing out the property

storeIndexedProperty

public void storeIndexedProperty(java.lang.String xmlTagName,
                                 java.lang.Object ob,
                                 java.beans.IndexedPropertyDescriptor prop,
                                 org.w3c.dom.Element parentElement)
                          throws java.io.IOException
Stores an indexed property (an array of properties)
Parameters:
xmlTagName - The XML tag name of the bean being written
ob - The object containing the property
prop - The descriptor for the property as an attribute? If not, don't write the object if writeAttributes is true and this object can be written as an attribute
Throws:
java.io.IOException - If there is an error writing the properties

storePropertyValue

public void storePropertyValue(java.lang.String xmlTagName,
                               java.lang.Object propValue,
                               java.beans.PropertyDescriptor prop,
                               int writeType,
                               org.w3c.dom.Element parentElement)
                        throws java.io.IOException
Stores a property value based on the property type
Parameters:
xmlTagName - The XML tag name of the bean being written
propValue - The value to write
prop - The property descriptor of the property
writeType - Should we try to write this property as an attribute? If not, don't write the object if writeAttributes is true and this object can be written as an attribute
Throws:
java.io.IOException - If there is an error while writing the property

storeBasicType

public void storeBasicType(java.lang.String xmlTagName,
                           java.lang.String propName,
                           java.lang.Object propValue,
                           int writeType,
                           org.w3c.dom.Element parentElement)
                    throws java.io.IOException
Writes out a basic type including the opening and closing tags.
Parameters:
xmlTagName - The XML tag name of the bean being written
propName - The name of the property to write
propValue - The value of the property
Throws:
java.io.IOException - If there is an error while writing the property

storeString

public void storeString(java.lang.String xmlTagName,
                        java.lang.String propName,
                        java.lang.Object propValue,
                        int writeType,
                        org.w3c.dom.Element parentElement)
                 throws java.io.IOException
Writes out a string, using CDATA if the string contains < or &
Parameters:
xmlTagName - The XML tag name of the bean being written
propName - The name of the property to write
propValue - The value of the property
outputStream - The output stream or writer where the value is to be written
Throws:
java.io.IOException - If there is an error while writing the property

storeDate

public void storeDate(java.lang.String xmlTagName,
                      java.lang.String propName,
                      java.lang.Object propValue,
                      int writeType,
                      org.w3c.dom.Element parentElement)
               throws java.io.IOException
Writes out a date including the opening and closing tags
Parameters:
xmlTagName - The XML tag name of the bean being written
propName - The name of the property to write
propValue - The value of the property
outputStream - The output stream or writer where the value is to be written
Throws:
java.io.IOException - If there is an error while writing the property

readDTD

protected com.wutka.dtd.DTD readDTD(java.lang.String dtdURL)
                             throws java.io.IOException

writeObject

public void writeObject(java.lang.String rootName,
                        java.lang.Object ob)
                 throws java.io.IOException
Writes out an object as XML
Parameters:
rootName - The tag name for this object (if this object is a property of a bean, the tag name will be the property name)
ob - The object to write out
Throws:
java.io.IOException - If there is an error while writing the object

writeObjectFields

public void writeObjectFields(java.lang.String xmlTagName,
                              java.lang.Object ob,
                              int writeType)
                       throws java.io.IOException
Writes out the properties of a bean as XML
Parameters:
xmlTagName - The XML tag name of the bean being written
ob - The bean whose properties will be written
writeType - The kind of fields being written (attribute or element)
Throws:
java.io.IOException - If there is an error writing the object

writeProperty

public void writeProperty(java.lang.String xmlTagName,
                          java.lang.Object ob,
                          java.beans.PropertyDescriptor prop,
                          int writeType)
                   throws java.io.IOException
Writes out a non-indexed property
Parameters:
xmlTagName - The XML tag name of the bean being written
ob - The object containing the property
prop - The property descriptor of the property
writeType - Should we try to write this property as an attribute? If not, don't write the object if writeAttributes is true and this object can be written as an attribute
Throws:
java.io.IOException - If there is an error writing out the property

writeIndexedProperty

public void writeIndexedProperty(java.lang.String xmlTagName,
                                 java.lang.Object ob,
                                 java.beans.IndexedPropertyDescriptor prop)
                          throws java.io.IOException
Writes out an indexed property (an array of properties)
Parameters:
xmlTagName - The XML tag name of the bean being written
ob - The object containing the property
prop - The descriptor for the property as an attribute? If not, don't write the object if writeAttributes is true and this object can be written as an attribute
Throws:
java.io.IOException - If there is an error writing the properties

writePropertyValue

public void writePropertyValue(java.lang.String xmlTagName,
                               java.lang.Object propValue,
                               java.beans.PropertyDescriptor prop,
                               int writeType)
                        throws java.io.IOException
Writes out a property value based on the property type
Parameters:
xmlTagName - The XML tag name of the bean being written
propValue - The value to write
prop - The property descriptor of the property
writeType - Should we try to write this property as an attribute? If not, don't write the object if writeAttributes is true and this object can be written as an attribute
Throws:
java.io.IOException - If there is an error while writing the property

writeBasicType

public void writeBasicType(java.lang.String xmlTagName,
                           java.lang.String propName,
                           java.lang.Object propValue,
                           int writeType)
                    throws java.io.IOException
Writes out a basic type including the opening and closing tags.
Parameters:
xmlTagName - The XML tag name of the bean being written
propName - The name of the property to write
propValue - The value of the property
Throws:
java.io.IOException - If there is an error while writing the property

writeString

public void writeString(java.lang.String xmlTagName,
                        java.lang.String propName,
                        java.lang.Object propValue,
                        int writeType)
                 throws java.io.IOException
Writes out a string, using CDATA if the string contains < or &
Parameters:
xmlTagName - The XML tag name of the bean being written
propName - The name of the property to write
propValue - The value of the property
outputStream - The output stream or writer where the value is to be written
Throws:
java.io.IOException - If there is an error while writing the property

writeDate

public void writeDate(java.lang.String xmlTagName,
                      java.lang.String propName,
                      java.lang.Object propValue,
                      int writeType)
               throws java.io.IOException
Writes out a date including the opening and closing tags
Parameters:
xmlTagName - The XML tag name of the bean being written
propName - The name of the property to write
propValue - The value of the property
outputStream - The output stream or writer where the value is to be written
Throws:
java.io.IOException - If there is an error while writing the property

stripName

public java.lang.String stripName(java.lang.String name)
Converts a name to lower case and removes any '-' or '_' characters
Parameters:
name - The name to strip
Returns:
The stripped version of the name

setupNameTranslation

protected void setupNameTranslation()

expandChildElements

public void expandChildElements(com.wutka.dtd.DTDContainer children,
                                java.util.Hashtable childTransTable)

getTranslatedEntityName

public java.lang.String getTranslatedEntityName(java.lang.String name)
Finds the correct format of a name as given by the DTD. If there is no DTD, this method returns the original name. If there is a DTD but no match, this method returns null.
Parameters:
The - name to translate
Returns:
The translated name or null if no translation exists

getTranslatedChildName

public java.lang.String getTranslatedChildName(java.lang.String elementName,
                                               java.lang.String childName)

writeAsAttribute

public boolean writeAsAttribute(java.lang.String elementName,
                                java.lang.String attributeName)
Returns true if a tag name for a particular object should be written as an attribute
Parameters:
elementName - The element whose tag is being written
attributeName - The potential attribute name
Returns:
True if the tag should be written as an attribute