mls.common
Class TranslatingXmlObject

java.lang.Object
  |
  +--mls.common.XmlObject
        |
        +--mls.common.TranslatingXmlObject
Direct Known Subclasses:
MlsBusinessObject

public class TranslatingXmlObject
extends XmlObject

An XML aware object that can translate from XML to Java and back


Field Summary
private static java.lang.String EMPTY
           
private static boolean NULL_BOOLEAN
           
private static byte NULL_BYTE
           
private static char NULL_CHAR
           
private static double NULL_DOUBLE
           
private static float NULL_FLOAT
           
private static int NULL_INTEGER
           
private static long NULL_LONG
           
private static short NULL_SHORT
           
private static java.lang.String NULL_STRING
           
 
Fields inherited from class mls.common.XmlObject
INDENT
 
Constructor Summary
TranslatingXmlObject()
           
 
Method Summary
private static void addCloseTag(java.lang.StringBuffer xmlDoc, java.lang.String tag, int level)
          Add close tag to an initialized document
private static void addOpenTag(java.lang.StringBuffer xmlDoc, java.lang.String tag, int level)
          Add open tag to an initialized document
private static void addString(java.lang.StringBuffer xmlDoc, java.lang.String str, int level)
          Add a string element using level to determine indenting Do not add tags as these are assumed to exist Note this is only used by buildEnum
private static void addString(java.lang.StringBuffer xmlDoc, java.lang.String tag, java.lang.String str, int level)
          Add a string element with CDATA quoting to an initialized document Use level to determine indenting
private static void build(java.lang.StringBuffer sb, java.lang.Object ob, int level)
          Build XML document from the specified object This routine is recursive to handle nesting; the recursion terminates primitive elements (or strings) are encountered level specifies indentation within the XML document
private static void buildArrayPrimitive(java.lang.StringBuffer sb, java.lang.Object array, int index, java.lang.Class arrayComponentClass, int level)
          Build a primitive element from an item in an array
private static void buildEnum(java.lang.StringBuffer sb, java.lang.Object ob, java.lang.Class thisClass, int level)
          Build an enumerated type XML string
private static void buildPrimitive(java.lang.StringBuffer sb, java.lang.Object ob, java.lang.reflect.Field field, java.lang.Class fieldType, int level)
          Build a primitive element from the field in an object
private static java.lang.String classNameToXmlTag(java.lang.String className)
          Convert a class name to an xml tag name
private static java.lang.Object extract(java.lang.Class thisClass, org.w3c.dom.Element el)
          Extract an instance of the specified class from the supplied DOM element This routine is recursive to handle nesting; the recursion terminates when primitive elements (or strings) are encountered
private static void extractArrayPrimitive(java.lang.Object array, int index, java.lang.Class arrayComponentClass, org.w3c.dom.Element el)
          Extract a primitive element's value and populate the indexed item in an array
private static java.lang.Object extractEnum(java.lang.Class thisClass, org.w3c.dom.Element el)
          Extract a value for an enumerated type
private static void extractPrimitive(java.lang.Object ob, java.lang.reflect.Field field, java.lang.Class fieldType, org.w3c.dom.Element el)
          Extract a primitive element's values and populate a field in an object
private static java.lang.StringBuffer initDoc()
          Initialize a document as a standalone document
private static boolean isPrimitive(java.lang.Class fieldType)
          Return true if this is a Java primitive or String class
static java.lang.String object2Xml(java.lang.Object ob)
          Build an xml document string from the specified object
static java.lang.Object xml2Object(java.lang.String fileName, java.lang.String className)
          Extract an object from the specified xml document file
 
Methods inherited from class mls.common.XmlObject
dump, dumpElement, getChildren, getChildren, getElement, getElement, getElement, getElementText, getParser, indent, narrow, nElements, parse, parseString
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

EMPTY

private static final java.lang.String EMPTY

NULL_INTEGER

private static final int NULL_INTEGER

NULL_SHORT

private static final short NULL_SHORT

NULL_BYTE

private static final byte NULL_BYTE

NULL_LONG

private static final long NULL_LONG

NULL_FLOAT

private static final float NULL_FLOAT

NULL_DOUBLE

private static final double NULL_DOUBLE

NULL_CHAR

private static final char NULL_CHAR

NULL_BOOLEAN

private static final boolean NULL_BOOLEAN

NULL_STRING

private static final java.lang.String NULL_STRING
Constructor Detail

TranslatingXmlObject

public TranslatingXmlObject()
Method Detail

object2Xml

public static java.lang.String object2Xml(java.lang.Object ob)
Build an xml document string from the specified object
Parameters:
ob - the Object to convert to XML
Returns:
an XML string

initDoc

private static java.lang.StringBuffer initDoc()
Initialize a document as a standalone document
Returns:
an initial XML document string

addString

private static void addString(java.lang.StringBuffer xmlDoc,
                              java.lang.String tag,
                              java.lang.String str,
                              int level)
Add a string element with CDATA quoting to an initialized document Use level to determine indenting
Parameters:
xmlDoc - IN OUT the string buffer to which this will be added
tag - the element tag to add
str - the element's contained text
level - the level of indentation to apply

addString

private static void addString(java.lang.StringBuffer xmlDoc,
                              java.lang.String str,
                              int level)
Add a string element using level to determine indenting Do not add tags as these are assumed to exist Note this is only used by buildEnum
Parameters:
xmlDoc - IN OUT the string buffer to which this will be added
str - the text to add
level - the level of indentation to apply

addOpenTag

private static void addOpenTag(java.lang.StringBuffer xmlDoc,
                               java.lang.String tag,
                               int level)
Add open tag to an initialized document
Parameters:
xmlDoc - IN OUT the string buffer to which this will be added
tag - the element tag to add
level - the level of indentation to apply

addCloseTag

private static void addCloseTag(java.lang.StringBuffer xmlDoc,
                                java.lang.String tag,
                                int level)
Add close tag to an initialized document
Parameters:
xmlDoc - IN OUT the string buffer to which this will be added
tag - the element tag to add
level - the level of indentation to apply

build

private static void build(java.lang.StringBuffer sb,
                          java.lang.Object ob,
                          int level)
Build XML document from the specified object This routine is recursive to handle nesting; the recursion terminates primitive elements (or strings) are encountered level specifies indentation within the XML document
Parameters:
sb - IN OUT the string buffer in which the object will be built
ob - The object to convert to an XML string
level - the level of indentation to apply

buildArrayPrimitive

private static void buildArrayPrimitive(java.lang.StringBuffer sb,
                                        java.lang.Object array,
                                        int index,
                                        java.lang.Class arrayComponentClass,
                                        int level)
Build a primitive element from an item in an array
Parameters:
sb - IN OUT the string buffer in which the object will be built
array - the array containing the primitive
index - the index of this element in the array
arrayComponentClass - the class of this array's components
level - the level of indentation to apply

buildPrimitive

private static void buildPrimitive(java.lang.StringBuffer sb,
                                   java.lang.Object ob,
                                   java.lang.reflect.Field field,
                                   java.lang.Class fieldType,
                                   int level)
Build a primitive element from the field in an object
Parameters:
sb - IN OUT the string buffer in which the object will be built
on - the object containing this primitive
field - the field containing this primitive
fieldType - the class of this field
level - the level of indentation to apply

buildEnum

private static void buildEnum(java.lang.StringBuffer sb,
                              java.lang.Object ob,
                              java.lang.Class thisClass,
                              int level)
Build an enumerated type XML string
Parameters:
sb - IN OUT the string buffer in which the object will be built
ob - the object containing this enumeration
thisClass - the class of this enumeration
level - the level of indentation to apply

classNameToXmlTag

private static java.lang.String classNameToXmlTag(java.lang.String className)
Convert a class name to an xml tag name
Parameters:
className - the class name to convert
Returns:
an xml tag made from the last component in the class name

isPrimitive

private static boolean isPrimitive(java.lang.Class fieldType)
Return true if this is a Java primitive or String class
Parameters:
fieldType - The type of field to evaluate
Returns:
true if this field is a known primitive including String

xml2Object

public static java.lang.Object xml2Object(java.lang.String fileName,
                                          java.lang.String className)
                                   throws java.lang.ClassNotFoundException
Extract an object from the specified xml document file
Parameters:
fileName - the file containing an XML document
className - the full Java class name (including package) of the object corresponding to the root of the XML document
Returns:
an object built from the specified file

extract

private static java.lang.Object extract(java.lang.Class thisClass,
                                        org.w3c.dom.Element el)
Extract an instance of the specified class from the supplied DOM element This routine is recursive to handle nesting; the recursion terminates when primitive elements (or strings) are encountered
Parameters:
thisClass - the Java class that corresponds to the DOM element
el - a DOM element containing a tag that corresponds to thisClass
Returns:
an extracted object

extractArrayPrimitive

private static void extractArrayPrimitive(java.lang.Object array,
                                          int index,
                                          java.lang.Class arrayComponentClass,
                                          org.w3c.dom.Element el)
Extract a primitive element's value and populate the indexed item in an array
Parameters:
array - the array which will contain the primitive
index - the index of this element in the array
arrayComponentClass - the class of this array's components
el - the element from which this will be extracted

extractPrimitive

private static void extractPrimitive(java.lang.Object ob,
                                     java.lang.reflect.Field field,
                                     java.lang.Class fieldType,
                                     org.w3c.dom.Element el)
Extract a primitive element's values and populate a field in an object
Parameters:
ob - the object which will contain the primitive
field - teh field in the object which will contain the primitive
fieldType - the class of this field
el - the element from which this will be extracted

extractEnum

private static java.lang.Object extractEnum(java.lang.Class thisClass,
                                            org.w3c.dom.Element el)
Extract a value for an enumerated type
Parameters:
thisClass - the class of this enumeration
el - the element from which this will be extracted
Returns:
the extracted object