org.openbp.common.io.xml
Class XMLDriver

java.lang.Object
  extended by org.openbp.common.io.xml.XMLDriver

public final class XMLDriver
extends java.lang.Object

The XML driver class implements a generic driver for XML serialzation/deserialization. It builds upon Castor XML/bean mapping support.

Author:
Heiko Erhardt

Constructor Summary
XMLDriver(java.lang.ClassLoader loader)
          Class loader constructor.
 
Method Summary
 java.lang.Object deserializeFile(java.lang.Class cls, java.lang.String fileName)
          Deserializes an object of known type from an input file.
 java.lang.Object deserializeFile(java.lang.String fileName)
          Deserializes an object of unknown type from an input file.
 java.lang.Object deserializeResource(java.lang.Class cls, org.springframework.core.io.Resource resource)
          Deserializes an object of known type from an input file.
 java.lang.Object deserializeResource(org.springframework.core.io.Resource resource)
          Deserializes an object of unknown type from an input file.
 java.lang.Object deserializeStream(java.lang.Class cls, java.io.InputStream in)
          Deserializes an object of known type from an input stream.
 java.lang.String getEncoding()
          Gets the encoding for XML I/O.
static XMLDriver getInstance()
          Gets the singleton instance of this class.
 java.lang.ClassLoader getLoader()
          Gets the class loader to user.
 org.exolab.castor.mapping.Mapping getMapping()
          Gets the the castor mapping table.
 boolean isPrettyPrint()
          Gets the flag if output should be pretty-printed.
 void loadMapping(java.lang.Class cls)
          Loads the XML mapping for the specified class.
 void loadMapping(java.io.File file)
          Loads a mapping from a mapping file.
 boolean loadMappings(java.lang.Class[] classes)
          Loads a set of XML mappings.
 boolean loadMappings(java.util.Iterator itClasses)
          Loads a set of XML mappings.
 void serialize(java.lang.Object o, org.w3c.dom.Node node)
          Serializes an object into a DOM node.
 void serialize(java.lang.Object o, java.io.OutputStream out)
          Serializes an object to an output stream.
 void serialize(java.lang.Object o, java.lang.String fileName)
          Serializes an object to an output file.
 void serializeObject(java.lang.Object o, java.io.ObjectOutputStream out)
          Serializes an object to an object output stream.
static void setDefaultClassLoader(java.lang.ClassLoader defaultClassLoaderArg)
          Sets the default class loader for all instances.
 void setEncoding(java.lang.String encoding)
          Sets the encoding for XML I/O.
 void setPrettyPrint(boolean prettyPrint)
          Sets the flag if output should be pretty-printed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLDriver

public XMLDriver(java.lang.ClassLoader loader)
Class loader constructor.

Parameters:
loader - Class loader to use for class access
Method Detail

getInstance

public static XMLDriver getInstance()
Gets the singleton instance of this class.


setDefaultClassLoader

public static void setDefaultClassLoader(java.lang.ClassLoader defaultClassLoaderArg)
Sets the default class loader for all instances.


getLoader

public java.lang.ClassLoader getLoader()
Gets the class loader to user.


serializeObject

public void serializeObject(java.lang.Object o,
                            java.io.ObjectOutputStream out)
                     throws java.io.IOException
Serializes an object to an object output stream. This method needs to be called from inside the writeObject method if an object should be serialized using the XML driver. Serialization is used for RMI in the first line.

Parameters:
o - Object to serialize
out - Object output stream to write to
Throws:
java.io.IOException - On i/o or xml/marshalling error

serialize

public void serialize(java.lang.Object o,
                      java.lang.String fileName)
               throws XMLDriverException
Serializes an object to an output file.

Parameters:
o - Object to serialize
fileName - Output file name
Throws:
XMLDriverException - On i/o or xml/marshalling error

serialize

public void serialize(java.lang.Object o,
                      java.io.OutputStream out)
               throws XMLDriverException
Serializes an object to an output stream.

Parameters:
o - Object to serialize
out - Output stream
Throws:
XMLDriverException - On i/o or xml/marshalling error

serialize

public void serialize(java.lang.Object o,
                      org.w3c.dom.Node node)
               throws XMLDriverException
Serializes an object into a DOM node.

Parameters:
o - Object to serialize
node - The DOM node to marshal into
Throws:
XMLDriverException - On xml/marshalling error

deserializeFile

public java.lang.Object deserializeFile(java.lang.String fileName)
                                 throws XMLDriverException
Deserializes an object of unknown type from an input file.

Parameters:
fileName - Input file name
Returns:
The deserialized object
Throws:
XMLDriverException - On i/o or xml/marshalling error

deserializeFile

public java.lang.Object deserializeFile(java.lang.Class cls,
                                        java.lang.String fileName)
                                 throws XMLDriverException
Deserializes an object of known type from an input file.

Parameters:
cls - Class of the object to deserialize
fileName - Input file name
Returns:
The deserialized object
Throws:
XMLDriverException - On i/o or xml/marshalling error

deserializeResource

public java.lang.Object deserializeResource(org.springframework.core.io.Resource resource)
                                     throws XMLDriverException
Deserializes an object of unknown type from an input file.

Parameters:
resource - Resource to open
Returns:
The deserialized object
Throws:
XMLDriverException - On i/o or xml/marshalling error

deserializeResource

public java.lang.Object deserializeResource(java.lang.Class cls,
                                            org.springframework.core.io.Resource resource)
                                     throws XMLDriverException
Deserializes an object of known type from an input file.

Parameters:
cls - Class of the object to deserialize
resource - Resource to open
Returns:
The deserialized object
Throws:
XMLDriverException - On i/o or xml/marshalling error

deserializeStream

public java.lang.Object deserializeStream(java.lang.Class cls,
                                          java.io.InputStream in)
                                   throws XMLDriverException
Deserializes an object of known type from an input stream.

Parameters:
cls - Class of the object to deserialize
in - Input stream
Returns:
The deserialized object
Throws:
XMLDriverException - On i/o or xml/marshalling error

getEncoding

public java.lang.String getEncoding()
Gets the encoding for XML I/O.


setEncoding

public void setEncoding(java.lang.String encoding)
Sets the encoding for XML I/O.


isPrettyPrint

public boolean isPrettyPrint()
Gets the flag if output should be pretty-printed.


setPrettyPrint

public void setPrettyPrint(boolean prettyPrint)
Sets the flag if output should be pretty-printed.


loadMappings

public boolean loadMappings(java.util.Iterator itClasses)
Loads a set of XML mappings. Does not generate a persistence exception, writes any errors to the log file instead.

Parameters:
itClasses - Iterator that contains the class objects (java.lang.Class) to load the mappings for
Returns:
true All mappings were loaded successfully.
false There were errors loading the mappings. See the log file for further details.

loadMappings

public boolean loadMappings(java.lang.Class[] classes)
Loads a set of XML mappings. Does not generate a persistence exception, writes any errors to the log file instead.

Parameters:
classes - Classes to load the mappings for
Returns:
true All mappings were loaded successfully.
false There were errors loading the mappings. See the log file for further details.

loadMapping

public void loadMapping(java.lang.Class cls)
                 throws XMLDriverException
Loads the XML mapping for the specified class. Checks the mapping table first. If no mapping is present yet, the method tries to read the mapping from the Castor mapping file with the same name as the class in the same directory as the class.

Parameters:
cls - Class to load the mapping for
Throws:
XMLDriverException - If no mapping file exists for this class or if the mapping file could not be loaded successfully

loadMapping

public void loadMapping(java.io.File file)
                 throws XMLDriverException
Loads a mapping from a mapping file. The mapping is just added to the mapping list of the xml driver. Since there is no class file associated with it, the class cache remains unchanged.

Parameters:
file - Mapping file to load
Throws:
XMLDriverException - If no mapping file exists for this class or if the mapping file could not be loaded successfully

getMapping

public org.exolab.castor.mapping.Mapping getMapping()
Gets the the castor mapping table.

Returns:
The castor mapping table


Copyright © 2011. All Rights Reserved.