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.
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 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
|
static java.lang.String |
DOCTYPE
the DOCTYPE for the serialization
|
static java.lang.String |
ROOT_NODE
the root node of the XML document
|
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 |
---|---|
void |
clear()
generates internally a new XML document and clears also the IgnoreList and
the mappings for the Read/Write-Methods
|
java.lang.Object |
fromXML(org.w3c.dom.Document document)
returns the given DOM document as an instance of the specified class
|
java.lang.String |
getRevision()
Returns the revision string.
|
java.lang.String |
getVersion()
returns the WEKA version with which the serialized object was created
|
static void |
main(java.lang.String[] args)
for testing only.
|
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.
|
short |
readShortFromXML(org.w3c.dom.Element node)
builds the primitive from the given DOM node.
|
XMLDocument |
toXML(java.lang.Object o)
extracts all accesible properties from the given object
|
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
|
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.
|
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 XMLSerialization() throws java.lang.Exception
java.lang.Exception
- if initialization failspublic void clear() throws java.lang.Exception
java.lang.Exception
- if something goes wrongpublic java.lang.String getVersion()
Version
public 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_ClassnameOverride
public XMLDocument toXML(java.lang.Object o) throws java.lang.Exception
o
- the object to turn into an XML representationjava.lang.Exception
- if XML generation 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 wrongpublic java.lang.Object readFromXML(org.w3c.dom.Element node) throws java.lang.Exception
node
- the associated XML nodejava.lang.Exception
- if instantiation 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.Exception
public java.lang.String getRevision()
getRevision
in interface RevisionHandler