public class XMLSerialization extends 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.
KOML
,
fromXML(Document)
,
toXML(Object)
,
m_Properties
,
m_CustomMethods
,
readPostProcess(Object)
,
writePostProcess(Object)
,
readFromXML(Element)
,
writeToXML(Element, Object, String)
Modifier and Type | Field and Description |
---|---|
static String |
ATT_ARRAY
the tag whether array or not (yes/no)
|
static String |
ATT_ARRAY_DEFAULT
default value for attribute ATT_ARRAY
|
static String |
ATT_CLASS
the tag for the class
|
static String |
ATT_NAME
the tag for the name
|
static String |
ATT_NULL
the tag whether null or not (yes/no)
|
static String |
ATT_NULL_DEFAULT
default value for attribute ATT_NULL
|
static String |
ATT_PRIMITIVE
the tag whether primitive or not (yes/no)
|
static String |
ATT_PRIMITIVE_DEFAULT
default value for attribute ATT_PRIMITIVE
|
static String |
ATT_VERSION
the version attribute
|
protected static boolean |
DEBUG
for debugging purposes only
|
static String |
DOCTYPE
the DOCTYPE for the serialization
|
protected Hashtable |
m_ClassnameOverride
for overriding class names (Class <-> Classname (String))
|
protected 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)
|
static String |
ROOT_NODE
the root node of the XML document
|
static String |
TAG_OBJECT
the tag for an object
|
static String |
VAL_NO
the value "no" for the primitive and array attribute
|
static String |
VAL_ROOT
the value of the name for the root node
|
static 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 Element |
addElement(Element parent,
String name,
String classname,
boolean primitive)
appends a new node to the parent with the given parameters (a non-array)
|
protected Element |
addElement(Element parent,
String name,
String classname,
boolean primitive,
int array)
appends a new node to the parent with the given parameters
|
protected Element |
addElement(Element parent,
String name,
String classname,
boolean primitive,
int array,
boolean isnull)
appends a new node to the parent with the given parameters
|
protected 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 Class |
determineClass(String name)
returns the associated class for the given name
|
protected PropertyDescriptor |
determineDescriptor(String className,
String displayName)
returns a property descriptor if possible, otherwise
null |
Object |
fromXML(Document document)
returns the given DOM document as an instance of the specified class
|
protected int[] |
getArrayDimensions(Element node)
returns an array with the dimensions of the array stored in XML
|
protected PropertyDescriptor |
getDescriptorByName(Object o,
String name)
returns a descriptor for a given objet by providing the name
|
protected Hashtable |
getDescriptors(Object o)
returns a hashtable with PropertyDescriptors that have "get" and "set"
methods indexed by the property name.
|
protected String |
getPath(Element node)
returns the path of the "name" attribute from the root down to this node
(including it).
|
protected Object |
getPrimitive(Element node)
returns an Object representing the primitive described by the given node.
|
String |
getRevision()
Returns the revision string.
|
String |
getVersion()
returns the WEKA version with which the serialized object was created
|
protected Object |
invokeReadFromXML(Element node)
either invokes a custom method to read a specific property/class or the standard
method
readFromXML(Element) |
protected Element |
invokeWriteToXML(Element parent,
Object o,
String name)
either invokes a custom method to write a specific property/class or the standard
method
writeToXML(Element,Object,String) |
protected boolean |
isPrimitiveArray(Class c)
checks whether the innermost class is a primitive class (handles
multi-dimensional arrays)
|
static void |
main(String[] args)
for testing only.
|
protected String |
overrideClassname(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 String |
overrideClassname(String classname)
if the given classname is stored in the classname override hashtable,
then the override name is returned otherwise the given classname.
|
Object |
read(File file)
parses the given file and returns a DOM document
|
Object |
read(InputStream stream)
parses the given stream and returns a DOM document
|
Object |
read(Reader reader)
parses the given reader and returns a DOM document
|
Object |
read(String xml)
parses the given XML string (can be XML or a filename) and returns an
Object generated from the representation
|
boolean |
readBooleanFromXML(Element node)
builds the primitive from the given DOM node.
|
byte |
readByteFromXML(Element node)
builds the primitive from the given DOM node.
|
char |
readCharFromXML(Element node)
builds the primitive from the given DOM node.
|
double |
readDoubleFromXML(Element node)
builds the primitive from the given DOM node.
|
float |
readFloatFromXML(Element node)
builds the primitive from the given DOM node.
|
Object |
readFromXML(Element node)
builds the object from the given DOM node.
|
Object |
readFromXML(Object o,
String name,
Element child)
adds the specific node to the object via a set method
|
int |
readIntFromXML(Element node)
builds the primitive from the given DOM node.
|
long |
readLongFromXML(Element node)
builds the primitive from the given DOM node.
|
protected Object |
readPostProcess(Object o)
additional post-processing can happen in derived classes after reading
from XML.
|
protected Document |
readPreProcess(Document document)
additional pre-processing can happen in derived classes before the
actual reading from XML (working on the raw XML).
|
short |
readShortFromXML(Element node)
builds the primitive from the given DOM node.
|
protected boolean |
stringToBoolean(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(Object o)
extracts all accesible properties from the given object
|
protected void |
trace(Throwable t,
String msg)
used for debugging purposes, i.e.
|
void |
write(File file,
Object o)
writes the given object into the file
|
void |
write(OutputStream stream,
Object o)
writes the given object into the stream
|
void |
write(String file,
Object o)
writes the given object into the file
|
void |
write(Writer writer,
Object o)
writes the given object into the writer
|
protected Element |
writeBooleanToXML(Element parent,
boolean o,
String name)
adds the given primitive to the DOM structure.
|
protected Element |
writeByteToXML(Element parent,
byte o,
String name)
adds the given primitive to the DOM structure.
|
protected Element |
writeCharToXML(Element parent,
char o,
String name)
adds the given primitive to the DOM structure.
|
protected Element |
writeDoubleToXML(Element parent,
double o,
String name)
adds the given primitive to the DOM structure.
|
protected Element |
writeFloatToXML(Element parent,
float o,
String name)
adds the given primitive to the DOM structure.
|
protected Element |
writeIntToXML(Element parent,
int o,
String name)
adds the given primitive to the DOM structure.
|
protected Element |
writeLongToXML(Element parent,
long o,
String name)
adds the given primitive to the DOM structure.
|
protected void |
writePostProcess(Object o)
enables derived classes to add other properties to the DOM tree, e.g.
|
protected Object |
writePreProcess(Object o)
enables derived classes to due some pre-processing on the objects, that's
about to be serialized.
|
protected Element |
writeShortToXML(Element parent,
short o,
String name)
adds the given primitive to the DOM structure.
|
Element |
writeToXML(Element parent,
Object o,
String name)
adds the given Object to a DOM structure.
|
protected static boolean DEBUG
protected Element m_CurrentNode
public static final String TAG_OBJECT
public static final String ATT_VERSION
public static final String ATT_NAME
public static final String ATT_CLASS
public static final String ATT_PRIMITIVE
public static final String ATT_ARRAY
public static final String ATT_NULL
public static final String VAL_YES
public static final String VAL_NO
public static final String VAL_ROOT
public static final String ROOT_NODE
public static final String ATT_PRIMITIVE_DEFAULT
ATT_PRIMITIVE
,
Constant Field Valuespublic static final String ATT_ARRAY_DEFAULT
ATT_ARRAY
,
Constant Field Valuespublic static final String ATT_NULL_DEFAULT
ATT_NULL
,
Constant Field Valuespublic static final String DOCTYPE
protected XMLDocument m_Document
protected PropertyHandler m_Properties
protected XMLSerializationMethodHandler m_CustomMethods
protected Hashtable m_ClassnameOverride
overrideClassname(Object)
protected void trace(Throwable t, String msg)
t
- a throwable instance, generated in the calling methodmsg
- a message to pringDEBUG
public void clear() throws Exception
Exception
- if something goes wrongpublic String getVersion()
Version
protected Hashtable getDescriptors(Object o) throws Exception
o
- the object to retrieve the descriptors fromException
- if the introspection failsPropertyDescriptor
protected String getPath(Element node)
node
- the node to get the path forprotected String booleanToString(boolean b)
VAL_YES
or VAL_NO
depending
on the value of b
b
- the boolean to turn into a stringprotected boolean stringToBoolean(String s)
s
- the string to turn into a booleanprotected Element addElement(Element parent, String name, 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 Element addElement(Element parent, String name, 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 Element addElement(Element parent, String name, 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 String overrideClassname(Object o)
o
- the object to check for overriding its classnamem_ClassnameOverride
protected String overrideClassname(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 PropertyDescriptor determineDescriptor(String className, String displayName)
null
className
- the name of the class to get the descriptor fordisplayName
- the name of the propertynull
protected Element writeBooleanToXML(Element parent, boolean o, String name) throws 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 primitiveException
- if the DOM creation failsprotected Element writeByteToXML(Element parent, byte o, String name) throws 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 primitiveException
- if the DOM creation failsprotected Element writeCharToXML(Element parent, char o, String name) throws 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 primitiveException
- if the DOM creation failsprotected Element writeDoubleToXML(Element parent, double o, String name) throws 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 primitiveException
- if the DOM creation failsprotected Element writeFloatToXML(Element parent, float o, String name) throws 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 primitiveException
- if the DOM creation failsprotected Element writeIntToXML(Element parent, int o, String name) throws 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 primitiveException
- if the DOM creation failsprotected Element writeLongToXML(Element parent, long o, String name) throws 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 primitiveException
- if the DOM creation failsprotected Element writeShortToXML(Element parent, short o, String name) throws 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 primitiveException
- if the DOM creation failsprotected boolean isPrimitiveArray(Class c)
c
- the array class to inspectpublic Element writeToXML(Element parent, Object o, String name) throws 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 objectException
- if the DOM creation failsoverrideClassname(Object)
,
overrideClassname(String)
,
m_ClassnameOverride
protected Element invokeWriteToXML(Element parent, Object o, String name) throws 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 objectException
- if invocation or turning into XML failsprotected Object writePreProcess(Object o) throws Exception
o
- the object that is serialized into XMLException
- if post-processing failsprotected void writePostProcess(Object o) throws Exception
o
- the object that is serialized into XMLException
- if post-processing failspublic XMLDocument toXML(Object o) throws Exception
o
- the object to turn into an XML representationException
- if XML generation failsprotected PropertyDescriptor getDescriptorByName(Object o, String name) throws Exception
o
- the object the get the descriptor forname
- the display name of the descriptornull
Exception
- if introsepction failsprotected Class determineClass(String name) throws Exception
name
- the name of the class to return a Class object forException
- if it class retrieval failsprotected Object getPrimitive(Element node) throws Exception
node
- the node to return the value as "primitive" objectException
- if the instantiation of the array fails or any of the
String conversions failspublic boolean readBooleanFromXML(Element node) throws Exception
node
- the associated XML nodeException
- if instantiation failspublic byte readByteFromXML(Element node) throws Exception
node
- the associated XML nodeException
- if instantiation failspublic char readCharFromXML(Element node) throws Exception
node
- the associated XML nodeException
- if instantiation failspublic double readDoubleFromXML(Element node) throws Exception
node
- the associated XML nodeException
- if instantiation failspublic float readFloatFromXML(Element node) throws Exception
node
- the associated XML nodeException
- if instantiation failspublic int readIntFromXML(Element node) throws Exception
node
- the associated XML nodeException
- if instantiation failspublic long readLongFromXML(Element node) throws Exception
node
- the associated XML nodeException
- if instantiation failspublic short readShortFromXML(Element node) throws Exception
node
- the associated XML nodeException
- if instantiation failspublic Object readFromXML(Object o, String name, Element child) throws 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 addException
- if something goes wrongprotected int[] getArrayDimensions(Element node)
node
- the node to determine the dimensions forpublic Object readFromXML(Element node) throws Exception
node
- the associated XML nodeException
- if instantiation failsprotected Object invokeReadFromXML(Element node) throws Exception
readFromXML(Element)
node
- the associated XML nodeException
- if instantiation failsprotected Document readPreProcess(Document document) throws Exception
document
- the document to pre-processException
- if post-processing failsprotected Object readPostProcess(Object o) throws Exception
o
- the object to perform some additional processing onException
- if post-processing failspublic Object fromXML(Document document) throws Exception
document
- the parsed DOM document representing the objectException
- if object instantiation failspublic Object read(String xml) throws Exception
xml
- the xml to parse (if "
Exception
- if something goes wrong with the parsingpublic Object read(File file) throws Exception
file
- the XML file to parseException
- if something goes wrong with the parsingpublic Object read(InputStream stream) throws Exception
stream
- the XML stream to parseException
- if something goes wrong with the parsingpublic Object read(Reader reader) throws Exception
reader
- the XML reader to parseException
- if something goes wrong with the parsingpublic void write(String file, Object o) throws Exception
file
- the filename to write too
- the object to serialize as XMLException
- if something goes wrong with the parsingpublic void write(File file, Object o) throws Exception
file
- the filename to write too
- the object to serialize as XMLException
- if something goes wrong with the parsingpublic void write(OutputStream stream, Object o) throws Exception
stream
- the filename to write too
- the object to serialize as XMLException
- if something goes wrong with the parsingpublic void write(Writer writer, Object o) throws Exception
writer
- the filename to write too
- the object to serialize as XMLException
- if something goes wrong with the parsingpublic static void main(String[] args) throws Exception
toString()
of the generated object.Exception
public String getRevision()
getRevision
in interface RevisionHandler
Copyright © 2015 University of Waikato, Hamilton, NZ. All rights reserved.