public class XMLSerialization extends java.lang.Object implements RevisionHandler
The generic approach of writing data as XML can be overriden by adding custom
methods for reading/writing in a derived class (cf. m_Properties, m_CustomMethods).
Custom read and write methods must have the same signature (and also be
public!) as the readFromXML and
writeToXML methods. Methods that apply to the naming rule
read + property name are added automatically to the list of
methods by the method
XMLSerializationMethodHandler.addMethods(...).
Other properties that are not conform the bean set/get-methods have to be
processed manually in a derived class (cf.
readPostProcess(Object), writePostProcess(Object)).
For a complete XML serialization/deserialization have a look at the
KOML class.
If a stored class has a constructor that takes a String to initialize (e.g. String or Double) then the content of the tag will used for the constructor, e.g. from
<object name="name" class="String" primitive="no">Smith</object>"Smith" will be used to instantiate a String object as constructor argument.
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
ATT_ARRAY
the tag whether array or not (yes/no)
|
static java.lang.String |
ATT_ARRAY_DEFAULT
default value for attribute ATT_ARRAY
|
static java.lang.String |
ATT_CLASS
the tag for the class
|
static java.lang.String |
ATT_NAME
the tag for the name
|
static java.lang.String |
ATT_NULL
the tag whether null or not (yes/no)
|
static java.lang.String |
ATT_NULL_DEFAULT
default value for attribute ATT_NULL
|
static java.lang.String |
ATT_PRIMITIVE
the tag whether primitive or not (yes/no)
|
static java.lang.String |
ATT_PRIMITIVE_DEFAULT
default value for attribute ATT_PRIMITIVE
|
static java.lang.String |
ATT_VERSION
the version attribute
|
protected static boolean |
DEBUG
for debugging purposes only
|
static java.lang.String |
DOCTYPE
the DOCTYPE for the serialization
|
protected java.util.Hashtable<java.lang.Class<?>,java.lang.String> |
m_ClassnameOverride
for overriding class names (Class <-> Classname (String))
|
protected org.w3c.dom.Element |
m_CurrentNode
the node that is currently processed, in case of writing the parent node
(something might go wrong writing the new child) and in case of reading the
actual node that is tried to process
|
protected XMLSerializationMethodHandler |
m_CustomMethods
for handling custom read/write methods
|
protected XMLDocument |
m_Document
the XMLDocument that performs the transformation to and fro XML
|
protected PropertyHandler |
m_Properties
for handling properties (ignored/allowed)
|
protected boolean |
m_suppressWarnings
true to suppress warnings about loading newer/older versions etc.
|
static java.lang.String |
ROOT_NODE
the root node of the XML document
|
static java.util.List<java.lang.String> |
SUPPRESS_PROPERTY_WARNINGS
List of fully qualified property names to suppress any warning messages for
|
static java.lang.String |
TAG_OBJECT
the tag for an object
|
static java.lang.String |
VAL_NO
the value "no" for the primitive and array attribute
|
static java.lang.String |
VAL_ROOT
the value of the name for the root node
|
static java.lang.String |
VAL_YES
the value "yes" for the primitive and array attribute
|
| Constructor and Description |
|---|
XMLSerialization()
initializes the serialization
|
| Modifier and Type | Method and Description |
|---|---|
protected org.w3c.dom.Element |
addElement(org.w3c.dom.Element parent,
java.lang.String name,
java.lang.String classname,
boolean primitive)
appends a new node to the parent with the given parameters (a non-array)
|
protected org.w3c.dom.Element |
addElement(org.w3c.dom.Element parent,
java.lang.String name,
java.lang.String classname,
boolean primitive,
int array)
appends a new node to the parent with the given parameters
|
protected org.w3c.dom.Element |
addElement(org.w3c.dom.Element parent,
java.lang.String name,
java.lang.String classname,
boolean primitive,
int array,
boolean isnull)
appends a new node to the parent with the given parameters
|
protected java.lang.String |
booleanToString(boolean b)
returns either
VAL_YES or VAL_NO depending on the
value of b |
void |
clear()
generates internally a new XML document and clears also the IgnoreList and
the mappings for the Read/Write-Methods
|
protected java.lang.Class<?> |
determineClass(java.lang.String name)
returns the associated class for the given name
|
protected java.beans.PropertyDescriptor |
determineDescriptor(java.lang.String className,
java.lang.String displayName)
returns a property descriptor if possible, otherwise
null |
java.lang.Object |
fromXML(org.w3c.dom.Document document)
returns the given DOM document as an instance of the specified class
|
protected int[] |
getArrayDimensions(org.w3c.dom.Element node)
returns an array with the dimensions of the array stored in XML
|
protected java.beans.PropertyDescriptor |
getDescriptorByName(java.lang.Object o,
java.lang.String name)
returns a descriptor for a given objet by providing the name
|
protected java.util.Hashtable<java.lang.String,java.beans.PropertyDescriptor> |
getDescriptors(java.lang.Object o)
returns a hashtable with PropertyDescriptors that have "get" and "set"
methods indexed by the property name.
|
protected java.lang.String |
getPath(org.w3c.dom.Element node)
returns the path of the "name" attribute from the root down to this node
(including it).
|
protected java.lang.Object |
getPrimitive(org.w3c.dom.Element node)
returns an Object representing the primitive described by the given node.
|
java.lang.String |
getRevision()
Returns the revision string.
|
java.lang.String |
getVersion()
returns the WEKA version with which the serialized object was created
|
protected java.lang.Object |
invokeReadFromXML(org.w3c.dom.Element node)
either invokes a custom method to read a specific property/class or the
standard method
readFromXML(Element) |
protected org.w3c.dom.Element |
invokeWriteToXML(org.w3c.dom.Element parent,
java.lang.Object o,
java.lang.String name)
either invokes a custom method to write a specific property/class or the
standard method
writeToXML(Element,Object,String) |
protected boolean |
isPrimitiveArray(java.lang.Class<?> c)
checks whether the innermost class is a primitive class (handles
multi-dimensional arrays)
|
static void |
main(java.lang.String[] args)
for testing only. if the first argument is a filename with ".xml" as
extension it tries to generate an instance from the XML description and
does a
toString() of the generated object. |
protected java.lang.String |
overrideClassname(java.lang.Object o)
if the class of the given object (or one of its ancestors) is stored in the
classname override hashtable, then the override name is returned otherwise
the classname of the given object.
|
protected java.lang.String |
overrideClassname(java.lang.String classname)
if the given classname is stored in the classname override hashtable, then
the override name is returned otherwise the given classname.
|
java.lang.Object |
read(java.io.File file)
parses the given file and returns a DOM document
|
java.lang.Object |
read(java.io.InputStream stream)
parses the given stream and returns a DOM document
|
java.lang.Object |
read(java.io.Reader reader)
parses the given reader and returns a DOM document
|
java.lang.Object |
read(java.lang.String xml)
parses the given XML string (can be XML or a filename) and returns an
Object generated from the representation
|
boolean |
readBooleanFromXML(org.w3c.dom.Element node)
builds the primitive from the given DOM node.
|
byte |
readByteFromXML(org.w3c.dom.Element node)
builds the primitive from the given DOM node.
|
char |
readCharFromXML(org.w3c.dom.Element node)
builds the primitive from the given DOM node.
|
double |
readDoubleFromXML(org.w3c.dom.Element node)
builds the primitive from the given DOM node.
|
float |
readFloatFromXML(org.w3c.dom.Element node)
builds the primitive from the given DOM node.
|
java.lang.Object |
readFromXML(org.w3c.dom.Element node)
builds the object from the given DOM node.
|
java.lang.Object |
readFromXML(java.lang.Object o,
java.lang.String name,
org.w3c.dom.Element child)
adds the specific node to the object via a set method
|
int |
readIntFromXML(org.w3c.dom.Element node)
builds the primitive from the given DOM node.
|
long |
readLongFromXML(org.w3c.dom.Element node)
builds the primitive from the given DOM node.
|
protected java.lang.Object |
readPostProcess(java.lang.Object o)
additional post-processing can happen in derived classes after reading from
XML. right now it only returns the object as it is.
|
protected org.w3c.dom.Document |
readPreProcess(org.w3c.dom.Document document)
additional pre-processing can happen in derived classes before the actual
reading from XML (working on the raw XML). right now it does nothing with
the document.
|
short |
readShortFromXML(org.w3c.dom.Element node)
builds the primitive from the given DOM node.
|
void |
setSuppressWarnings(boolean suppress)
Set whether to suppress warning messages or not
|
protected boolean |
stringToBoolean(java.lang.String s)
turns the given string into a boolean, if a positive number is given, then
zero is considered FALSE, every other number TRUE; the empty string is also
considered being FALSE
|
XMLDocument |
toXML(java.lang.Object o)
extracts all accesible properties from the given object
|
protected void |
trace(java.lang.Throwable t,
java.lang.String msg)
used for debugging purposes, i.e. only if DEBUG is set to true. needs a
newly generated Throwable instance to get the method/line from
|
void |
write(java.io.File file,
java.lang.Object o)
writes the given object into the file
|
void |
write(java.io.OutputStream stream,
java.lang.Object o)
writes the given object into the stream
|
void |
write(java.lang.String file,
java.lang.Object o)
writes the given object into the file
|
void |
write(java.io.Writer writer,
java.lang.Object o)
writes the given object into the writer
|
protected org.w3c.dom.Element |
writeBooleanToXML(org.w3c.dom.Element parent,
boolean o,
java.lang.String name)
adds the given primitive to the DOM structure.
|
protected org.w3c.dom.Element |
writeByteToXML(org.w3c.dom.Element parent,
byte o,
java.lang.String name)
adds the given primitive to the DOM structure.
|
protected org.w3c.dom.Element |
writeCharToXML(org.w3c.dom.Element parent,
char o,
java.lang.String name)
adds the given primitive to the DOM structure.
|
protected org.w3c.dom.Element |
writeDoubleToXML(org.w3c.dom.Element parent,
double o,
java.lang.String name)
adds the given primitive to the DOM structure.
|
protected org.w3c.dom.Element |
writeFloatToXML(org.w3c.dom.Element parent,
float o,
java.lang.String name)
adds the given primitive to the DOM structure.
|
protected org.w3c.dom.Element |
writeIntToXML(org.w3c.dom.Element parent,
int o,
java.lang.String name)
adds the given primitive to the DOM structure.
|
protected org.w3c.dom.Element |
writeLongToXML(org.w3c.dom.Element parent,
long o,
java.lang.String name)
adds the given primitive to the DOM structure.
|
protected void |
writePostProcess(java.lang.Object o)
enables derived classes to add other properties to the DOM tree, e.g. ones
that do not apply to the get/set convention of beans. only implemented with
empty method body.
|
protected java.lang.Object |
writePreProcess(java.lang.Object o)
enables derived classes to due some pre-processing on the objects, that's
about to be serialized.
|
protected org.w3c.dom.Element |
writeShortToXML(org.w3c.dom.Element parent,
short o,
java.lang.String name)
adds the given primitive to the DOM structure.
|
org.w3c.dom.Element |
writeToXML(org.w3c.dom.Element parent,
java.lang.Object o,
java.lang.String name)
adds the given Object to a DOM structure.
|
protected static boolean DEBUG
protected org.w3c.dom.Element m_CurrentNode
public static final java.lang.String TAG_OBJECT
public static final java.lang.String ATT_VERSION
public static final java.lang.String ATT_NAME
public static final java.lang.String ATT_CLASS
public static final java.lang.String ATT_PRIMITIVE
public static final java.lang.String ATT_ARRAY
public static final java.lang.String ATT_NULL
public static final java.lang.String VAL_YES
public static final java.lang.String VAL_NO
public static final java.lang.String VAL_ROOT
public static final java.lang.String ROOT_NODE
public static final java.lang.String ATT_PRIMITIVE_DEFAULT
ATT_PRIMITIVE,
Constant Field Valuespublic static final java.lang.String ATT_ARRAY_DEFAULT
ATT_ARRAY,
Constant Field Valuespublic static final java.lang.String ATT_NULL_DEFAULT
ATT_NULL,
Constant Field Valuespublic static final java.lang.String DOCTYPE
public static final java.util.List<java.lang.String> SUPPRESS_PROPERTY_WARNINGS
protected XMLDocument m_Document
protected PropertyHandler m_Properties
protected XMLSerializationMethodHandler m_CustomMethods
protected java.util.Hashtable<java.lang.Class<?>,java.lang.String> m_ClassnameOverride
overrideClassname(Object)protected boolean m_suppressWarnings
public XMLSerialization()
throws java.lang.Exception
java.lang.Exception - if initialization failspublic void setSuppressWarnings(boolean suppress)
suppress - true to suppress warningsprotected void trace(java.lang.Throwable t,
java.lang.String msg)
t - a throwable instance, generated in the calling methodmsg - a message to pringDEBUGpublic void clear()
throws java.lang.Exception
java.lang.Exception - if something goes wrongpublic java.lang.String getVersion()
Versionprotected java.util.Hashtable<java.lang.String,java.beans.PropertyDescriptor> getDescriptors(java.lang.Object o)
throws java.lang.Exception
o - the object to retrieve the descriptors fromjava.lang.Exception - if the introspection failsPropertyDescriptorprotected java.lang.String getPath(org.w3c.dom.Element node)
node - the node to get the path forprotected java.lang.String booleanToString(boolean b)
VAL_YES or VAL_NO depending on the
value of bb - the boolean to turn into a stringprotected boolean stringToBoolean(java.lang.String s)
s - the string to turn into a booleanprotected org.w3c.dom.Element addElement(org.w3c.dom.Element parent,
java.lang.String name,
java.lang.String classname,
boolean primitive)
parent - the parent of this node. if it is null the
document root element is usedname - the name of the nodeclassname - the classname for this nodeprimitive - whether it is a primitve data type or not (i.e. an object)protected org.w3c.dom.Element addElement(org.w3c.dom.Element parent,
java.lang.String name,
java.lang.String classname,
boolean primitive,
int array)
parent - the parent of this node. if it is null the
document root element is usedname - the name of the nodeclassname - the classname for this nodeprimitive - whether it is a primitve data type or not (i.e. an object)array - the dimensions of the array (0 if not an array)protected org.w3c.dom.Element addElement(org.w3c.dom.Element parent,
java.lang.String name,
java.lang.String classname,
boolean primitive,
int array,
boolean isnull)
parent - the parent of this node. if it is null the
document root element is usedname - the name of the nodeclassname - the classname for this nodeprimitive - whether it is a primitve data type or not (i.e. an object)array - the dimensions of the array (0 if not an array)isnull - whether it is nullprotected java.lang.String overrideClassname(java.lang.Object o)
o - the object to check for overriding its classnamem_ClassnameOverrideprotected java.lang.String overrideClassname(java.lang.String classname)
overrideClassname(Object) does this method only
look for exact name matches. The other method checks whether the class of
the given object is a subclass of any of the stored overrides.classname - the classname to check for overridingm_ClassnameOverride,
overrideClassname(Object)protected java.beans.PropertyDescriptor determineDescriptor(java.lang.String className,
java.lang.String displayName)
nullclassName - the name of the class to get the descriptor fordisplayName - the name of the propertynullprotected org.w3c.dom.Element writeBooleanToXML(org.w3c.dom.Element parent,
boolean o,
java.lang.String name)
throws java.lang.Exception
parent - the parent of this object, e.g. the class this object is a
member ofo - the primitive to describe in XMLname - the name of the primitivejava.lang.Exception - if the DOM creation failsprotected org.w3c.dom.Element writeByteToXML(org.w3c.dom.Element parent,
byte o,
java.lang.String name)
throws java.lang.Exception
parent - the parent of this object, e.g. the class this object is a
member ofo - the primitive to describe in XMLname - the name of the primitivejava.lang.Exception - if the DOM creation failsprotected org.w3c.dom.Element writeCharToXML(org.w3c.dom.Element parent,
char o,
java.lang.String name)
throws java.lang.Exception
parent - the parent of this object, e.g. the class this object is a
member ofo - the primitive to describe in XMLname - the name of the primitivejava.lang.Exception - if the DOM creation failsprotected org.w3c.dom.Element writeDoubleToXML(org.w3c.dom.Element parent,
double o,
java.lang.String name)
throws java.lang.Exception
parent - the parent of this object, e.g. the class this object is a
member ofo - the primitive to describe in XMLname - the name of the primitivejava.lang.Exception - if the DOM creation failsprotected org.w3c.dom.Element writeFloatToXML(org.w3c.dom.Element parent,
float o,
java.lang.String name)
throws java.lang.Exception
parent - the parent of this object, e.g. the class this object is a
member ofo - the primitive to describe in XMLname - the name of the primitivejava.lang.Exception - if the DOM creation failsprotected org.w3c.dom.Element writeIntToXML(org.w3c.dom.Element parent,
int o,
java.lang.String name)
throws java.lang.Exception
parent - the parent of this object, e.g. the class this object is a
member ofo - the primitive to describe in XMLname - the name of the primitivejava.lang.Exception - if the DOM creation failsprotected org.w3c.dom.Element writeLongToXML(org.w3c.dom.Element parent,
long o,
java.lang.String name)
throws java.lang.Exception
parent - the parent of this object, e.g. the class this object is a
member ofo - the primitive to describe in XMLname - the name of the primitivejava.lang.Exception - if the DOM creation failsprotected org.w3c.dom.Element writeShortToXML(org.w3c.dom.Element parent,
short o,
java.lang.String name)
throws java.lang.Exception
parent - the parent of this object, e.g. the class this object is a
member ofo - the primitive to describe in XMLname - the name of the primitivejava.lang.Exception - if the DOM creation failsprotected boolean isPrimitiveArray(java.lang.Class<?> c)
c - the array class to inspectpublic org.w3c.dom.Element writeToXML(org.w3c.dom.Element parent,
java.lang.Object o,
java.lang.String name)
throws java.lang.Exception
overrideClassname(Object) is not invoked in case
of arrays, since the array class could be a superclass, whereas the
elements of the array can be specialized subclasses. In case of an array
the method overrideClassname(String) is invoked, which
searches for an exact match of the classname in the override hashtable.parent - the parent of this object, e.g. the class this object is a
member ofo - the Object to describe in XMLname - the name of the objectjava.lang.Exception - if the DOM creation failsoverrideClassname(Object),
overrideClassname(String),
m_ClassnameOverrideprotected org.w3c.dom.Element invokeWriteToXML(org.w3c.dom.Element parent,
java.lang.Object o,
java.lang.String name)
throws java.lang.Exception
writeToXML(Element,Object,String)parent - the parent XML nodeo - the object's content will be added as children to the given parent
nodename - the name of the objectjava.lang.Exception - if invocation or turning into XML failsprotected java.lang.Object writePreProcess(java.lang.Object o)
throws java.lang.Exception
o - the object that is serialized into XMLjava.lang.Exception - if post-processing failsprotected void writePostProcess(java.lang.Object o)
throws java.lang.Exception
o - the object that is serialized into XMLjava.lang.Exception - if post-processing failspublic XMLDocument toXML(java.lang.Object o) throws java.lang.Exception
o - the object to turn into an XML representationjava.lang.Exception - if XML generation failsprotected java.beans.PropertyDescriptor getDescriptorByName(java.lang.Object o,
java.lang.String name)
throws java.lang.Exception
o - the object the get the descriptor forname - the display name of the descriptornulljava.lang.Exception - if introsepction failsprotected java.lang.Class<?> determineClass(java.lang.String name)
throws java.lang.Exception
name - the name of the class to return a Class object forjava.lang.Exception - if it class retrieval failsprotected java.lang.Object getPrimitive(org.w3c.dom.Element node)
throws java.lang.Exception
node - the node to return the value as "primitive" objectjava.lang.Exception - if the instantiation of the array fails or any of the
String conversions failspublic boolean readBooleanFromXML(org.w3c.dom.Element node)
throws java.lang.Exception
node - the associated XML nodejava.lang.Exception - if instantiation failspublic byte readByteFromXML(org.w3c.dom.Element node)
throws java.lang.Exception
node - the associated XML nodejava.lang.Exception - if instantiation failspublic char readCharFromXML(org.w3c.dom.Element node)
throws java.lang.Exception
node - the associated XML nodejava.lang.Exception - if instantiation failspublic double readDoubleFromXML(org.w3c.dom.Element node)
throws java.lang.Exception
node - the associated XML nodejava.lang.Exception - if instantiation failspublic float readFloatFromXML(org.w3c.dom.Element node)
throws java.lang.Exception
node - the associated XML nodejava.lang.Exception - if instantiation failspublic int readIntFromXML(org.w3c.dom.Element node)
throws java.lang.Exception
node - the associated XML nodejava.lang.Exception - if instantiation failspublic long readLongFromXML(org.w3c.dom.Element node)
throws java.lang.Exception
node - the associated XML nodejava.lang.Exception - if instantiation failspublic short readShortFromXML(org.w3c.dom.Element node)
throws java.lang.Exception
node - the associated XML nodejava.lang.Exception - if instantiation failspublic java.lang.Object readFromXML(java.lang.Object o,
java.lang.String name,
org.w3c.dom.Element child)
throws java.lang.Exception
o - the object to set a propertyname - the name of the object for which to set a property (only for
information reasons)child - the value of the property to addjava.lang.Exception - if something goes wrongprotected int[] getArrayDimensions(org.w3c.dom.Element node)
node - the node to determine the dimensions forpublic java.lang.Object readFromXML(org.w3c.dom.Element node)
throws java.lang.Exception
node - the associated XML nodejava.lang.Exception - if instantiation failsprotected java.lang.Object invokeReadFromXML(org.w3c.dom.Element node)
throws java.lang.Exception
readFromXML(Element)node - the associated XML nodejava.lang.Exception - if instantiation failsprotected org.w3c.dom.Document readPreProcess(org.w3c.dom.Document document)
throws java.lang.Exception
document - the document to pre-processjava.lang.Exception - if post-processing failsprotected java.lang.Object readPostProcess(java.lang.Object o)
throws java.lang.Exception
o - the object to perform some additional processing onjava.lang.Exception - if post-processing failspublic java.lang.Object fromXML(org.w3c.dom.Document document)
throws java.lang.Exception
document - the parsed DOM document representing the objectjava.lang.Exception - if object instantiation failspublic java.lang.Object read(java.lang.String xml)
throws java.lang.Exception
xml - the xml to parse (if "
java.lang.Exception - if something goes wrong with the parsingpublic java.lang.Object read(java.io.File file)
throws java.lang.Exception
file - the XML file to parsejava.lang.Exception - if something goes wrong with the parsingpublic java.lang.Object read(java.io.InputStream stream)
throws java.lang.Exception
stream - the XML stream to parsejava.lang.Exception - if something goes wrong with the parsingpublic java.lang.Object read(java.io.Reader reader)
throws java.lang.Exception
reader - the XML reader to parsejava.lang.Exception - if something goes wrong with the parsingpublic void write(java.lang.String file,
java.lang.Object o)
throws java.lang.Exception
file - the filename to write too - the object to serialize as XMLjava.lang.Exception - if something goes wrong with the parsingpublic void write(java.io.File file,
java.lang.Object o)
throws java.lang.Exception
file - the filename to write too - the object to serialize as XMLjava.lang.Exception - if something goes wrong with the parsingpublic void write(java.io.OutputStream stream,
java.lang.Object o)
throws java.lang.Exception
stream - the filename to write too - the object to serialize as XMLjava.lang.Exception - if something goes wrong with the parsingpublic void write(java.io.Writer writer,
java.lang.Object o)
throws java.lang.Exception
writer - the filename to write too - the object to serialize as XMLjava.lang.Exception - if something goes wrong with the parsingpublic static void main(java.lang.String[] args)
throws java.lang.Exception
toString() of the generated object.java.lang.Exceptionpublic java.lang.String getRevision()
getRevision in interface RevisionHandler