gov.nih.nlm.util
Class ObjectXMLSerializer

java.lang.Object
  extended byorg.xml.sax.helpers.DefaultHandler
      extended bygov.nih.nlm.util.ObjectXMLSerializer
All Implemented Interfaces:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler

public class ObjectXMLSerializer
extends DefaultHandler

This class is used to provide an XML serialization scheme for ANY java object. This class is thread-safe. It uses only local variables and some static HashMap. Any multi-threaded application can create one instance of this class and then safely call the toXML and fromXML methods in overlapping threads without worrying that it will cause conflicts.

Author:
MEME Group

Nested Class Summary
static interface ObjectXMLSerializer.NotPersistentIfTransient
          Objects should implement this if they are to be ignored by the serializer and are marked as transient members.
static interface ObjectXMLSerializer.Primitive
          Anything implementing this method should also have a static newInstance(String) method.
 
Field Summary
 HashMap objects_seen_read
           
 
Constructor Summary
ObjectXMLSerializer()
          Instantiates an empty ObjectXMLSerializer.
 
Method Summary
 void characters(char[] chars, int start, int length)
          Implementation of DefaultHandler API.
static Character CharacterValueOf(String data)
          The Character class does not have a valueOf(String) method, this method takes its place in reconstructing primitive values.
 void clear()
          Clears the various class fields for the next round.
protected  void clearObject()
          Clears the object stack.
static Date DateValueOf(String data)
          The Date classes do not have a valueOf(String) method, this method takes its place in reconstructing primitive values.
 void endElement(String namespace_uri, String simple_name, String qualified_name)
          Implementation of DefaultHandler API.
 Object forceNewInstance(Class c)
          This method is responsible for finding a constructor and forcing it to create an intitial object.
 Object fromXML(Reader reader)
          Creates an object from the XML representation indicated by the specified Reader.
 Object fromXML(Reader reader, boolean validating)
          Creates an object from the XML representation indicated by the specified Reader.
 Object fromXML(String file)
          Creates an object from the XML representation indicated by the specified file name.
 Object fromXML(String file, boolean validating)
          Creates an object from the XML representation indicated by the specified file name.
protected  Object getObject()
          Returns the top object on the object Stack.
static void main(String[] argv)
          The main method performs a self-QA test
 void startElement(String namespace_uri, String simple_name, String qualified_name, Attributes attrs)
          Handles opening tags.
static String StringValueOf(String data)
          The String classes do not have a valueOf(String) method, this method takes its place in reconstructing primitive values.
 String toXML(Object obj)
          Converts an object to an XML representation.
 String toXML(Object obj, String indent)
          Converts an object to an XML representation with the specified level of indenting.
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endDocument, endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

objects_seen_read

public HashMap objects_seen_read
Constructor Detail

ObjectXMLSerializer

public ObjectXMLSerializer()
Instantiates an empty ObjectXMLSerializer.

Method Detail

clear

public void clear()
Clears the various class fields for the next round. This should be called between subsequent serializations or de-serializations.


CharacterValueOf

public static Character CharacterValueOf(String data)
The Character class does not have a valueOf(String) method, this method takes its place in reconstructing primitive values.

Parameters:
data - a String
Returns:
the Character in the first position of the specified String

DateValueOf

public static Date DateValueOf(String data)
The Date classes do not have a valueOf(String) method, this method takes its place in reconstructing primitive values. Here, dates are represented as the string value of the long obtained by calling Date.getTime().

Parameters:
data - a String representation of a date
Returns:
the Date corresponding to that string

StringValueOf

public static String StringValueOf(String data)
The String classes do not have a valueOf(String) method, this method takes its place in reconstructing primitive values.

Parameters:
data - the input String
Returns:
the output String

toXML

public String toXML(Object obj)
             throws Exception
Converts an object to an XML representation.

Parameters:
obj - an object to convert
Returns:
a XML representation of the object
Throws:
Exception - if conversion fails

toXML

public String toXML(Object obj,
                    String indent)
             throws Exception
Converts an object to an XML representation with the specified level of indenting.

Parameters:
obj - an object to convert
indent - a String containing space characters
Returns:
a XML representation of the object
Throws:
Exception - if conversion fails

fromXML

public Object fromXML(String file)
               throws Exception
Creates an object from the XML representation indicated by the specified file name.

Parameters:
file - an XML file name
Returns:
the object represented by the file
Throws:
Exception - if conversion fails

fromXML

public Object fromXML(String file,
                      boolean validating)
               throws Exception
Creates an object from the XML representation indicated by the specified file name.

Parameters:
file - an XML file name
validating - a flag indicating whether or not a validating parser should be used
Returns:
the object represented by the file
Throws:
Exception - if conversion fails

fromXML

public Object fromXML(Reader reader)
               throws Exception
Creates an object from the XML representation indicated by the specified Reader.

Parameters:
reader - a Reader object
Returns:
the object represented by the file
Throws:
Exception - if conversion fails

fromXML

public Object fromXML(Reader reader,
                      boolean validating)
               throws Exception
Creates an object from the XML representation indicated by the specified Reader.

Parameters:
reader - an object Reader
validating - a flag indicating whether or not a validating parser should be used
Returns:
the object represented by the file
Throws:
Exception - if conversion fails

getObject

protected Object getObject()
                    throws SAXException
Returns the top object on the object Stack.

Returns:
the top object on the object Stack
Throws:
SAXException - if nothing is on the stack

clearObject

protected void clearObject()
Clears the object stack.


startElement

public void startElement(String namespace_uri,
                         String simple_name,
                         String qualified_name,
                         Attributes attrs)
                  throws SAXException
Handles opening tags.

Parameters:
namespace_uri - The namespace URI
simple_name - the simple name of the element
qualified_name - the fully qualified name of the allement
attrs - The element attributes
Throws:
SAXException - if anything goes wrong

characters

public void characters(char[] chars,
                       int start,
                       int length)
Implementation of DefaultHandler API.

Parameters:
chars - characters from within an XML tag
start - start index
length - number of chars in this segment

endElement

public void endElement(String namespace_uri,
                       String simple_name,
                       String qualified_name)
                throws SAXException
Implementation of DefaultHandler API.

Parameters:
namespace_uri - the namespace URI
simple_name - the simple name
qualified_name - the qualified name
Throws:
SAXException - if failed to parse object

forceNewInstance

public Object forceNewInstance(Class c)
                        throws Exception
This method is responsible for finding a constructor and forcing it to create an intitial object. It uses default values for the various constructor parameters. If no constructor could be found, or if calling the constructor raises an exception, then we should just bail.

Parameters:
c - An object Class to convert to XML.
Returns:
An object Object representation of converted document.
Throws:
Exception - if conversion fails.

main

public static void main(String[] argv)
The main method performs a self-QA test

Parameters:
argv - An array of argument.


Copyright ©2005