Class XMLSerialization
- java.lang.Object
-
- weka.core.xml.XMLSerialization
-
- All Implemented Interfaces:
RevisionHandler
- Direct Known Subclasses:
XMLBasicSerialization
public class XMLSerialization extends Object implements RevisionHandler
With this class objects can be serialized to XML instead into a binary format. It uses introspection (cf. beans) to retrieve the data from the given object, i.e. it can only access beans-conform fields automatically.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 bepublic!) as thereadFromXMLandwriteToXMLmethods. Methods that apply to the naming ruleread + property nameare added automatically to the list of methods by the methodXMLSerializationMethodHandler.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
KOMLclass.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.- Version:
- $Revision$
- Author:
- FracPete (fracpete at waikato dot ac dot nz)
- See Also:
KOML,fromXML(Document),toXML(Object),m_Properties,m_CustomMethods,readPostProcess(Object),writePostProcess(Object),readFromXML(Element),writeToXML(Element, Object, String)
-
-
Field Summary
Fields Modifier and Type Field Description static StringATT_ARRAYthe tag whether array or not (yes/no)static StringATT_ARRAY_DEFAULTdefault value for attribute ATT_ARRAYstatic StringATT_CLASSthe tag for the classstatic StringATT_NAMEthe tag for the namestatic StringATT_NULLthe tag whether null or not (yes/no)static StringATT_NULL_DEFAULTdefault value for attribute ATT_NULLstatic StringATT_PRIMITIVEthe tag whether primitive or not (yes/no)static StringATT_PRIMITIVE_DEFAULTdefault value for attribute ATT_PRIMITIVEstatic StringATT_VERSIONthe version attributestatic StringDOCTYPEthe DOCTYPE for the serializationstatic StringROOT_NODEthe root node of the XML documentstatic List<String>SUPPRESS_PROPERTY_WARNINGSList of fully qualified property names to suppress any warning messages forstatic StringTAG_OBJECTthe tag for an objectstatic StringVAL_NOthe value "no" for the primitive and array attributestatic StringVAL_ROOTthe value of the name for the root nodestatic StringVAL_YESthe value "yes" for the primitive and array attribute
-
Constructor Summary
Constructors Constructor Description XMLSerialization()initializes the serialization
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()generates internally a new XML document and clears also the IgnoreList and the mappings for the Read/Write-MethodsObjectfromXML(Document document)returns the given DOM document as an instance of the specified classStringgetRevision()Returns the revision string.StringgetVersion()returns the WEKA version with which the serialized object was createdstatic voidmain(String[] args)for testing only.Objectread(File file)parses the given file and returns a DOM documentObjectread(InputStream stream)parses the given stream and returns a DOM documentObjectread(Reader reader)parses the given reader and returns a DOM documentObjectread(String xml)parses the given XML string (can be XML or a filename) and returns an Object generated from the representationbooleanreadBooleanFromXML(Element node)builds the primitive from the given DOM node.bytereadByteFromXML(Element node)builds the primitive from the given DOM node.charreadCharFromXML(Element node)builds the primitive from the given DOM node.doublereadDoubleFromXML(Element node)builds the primitive from the given DOM node.floatreadFloatFromXML(Element node)builds the primitive from the given DOM node.ObjectreadFromXML(Object o, String name, Element child)adds the specific node to the object via a set methodObjectreadFromXML(Element node)builds the object from the given DOM node.intreadIntFromXML(Element node)builds the primitive from the given DOM node.longreadLongFromXML(Element node)builds the primitive from the given DOM node.shortreadShortFromXML(Element node)builds the primitive from the given DOM node.voidsetSuppressWarnings(boolean suppress)Set whether to suppress warning messages or notXMLDocumenttoXML(Object o)extracts all accesible properties from the given objectvoidwrite(File file, Object o)writes the given object into the filevoidwrite(OutputStream stream, Object o)writes the given object into the streamvoidwrite(Writer writer, Object o)writes the given object into the writervoidwrite(String file, Object o)writes the given object into the fileElementwriteToXML(Element parent, Object o, String name)adds the given Object to a DOM structure.
-
-
-
Field Detail
-
TAG_OBJECT
public static final String TAG_OBJECT
the tag for an object- See Also:
- Constant Field Values
-
ATT_VERSION
public static final String ATT_VERSION
the version attribute- See Also:
- Constant Field Values
-
ATT_NAME
public static final String ATT_NAME
the tag for the name- See Also:
- Constant Field Values
-
ATT_CLASS
public static final String ATT_CLASS
the tag for the class- See Also:
- Constant Field Values
-
ATT_PRIMITIVE
public static final String ATT_PRIMITIVE
the tag whether primitive or not (yes/no)- See Also:
- Constant Field Values
-
ATT_ARRAY
public static final String ATT_ARRAY
the tag whether array or not (yes/no)- See Also:
- Constant Field Values
-
ATT_NULL
public static final String ATT_NULL
the tag whether null or not (yes/no)- See Also:
- Constant Field Values
-
VAL_YES
public static final String VAL_YES
the value "yes" for the primitive and array attribute- See Also:
- Constant Field Values
-
VAL_NO
public static final String VAL_NO
the value "no" for the primitive and array attribute- See Also:
- Constant Field Values
-
VAL_ROOT
public static final String VAL_ROOT
the value of the name for the root node- See Also:
- Constant Field Values
-
ROOT_NODE
public static final String ROOT_NODE
the root node of the XML document- See Also:
- Constant Field Values
-
ATT_PRIMITIVE_DEFAULT
public static final String ATT_PRIMITIVE_DEFAULT
default value for attribute ATT_PRIMITIVE- See Also:
ATT_PRIMITIVE, Constant Field Values
-
ATT_ARRAY_DEFAULT
public static final String ATT_ARRAY_DEFAULT
default value for attribute ATT_ARRAY- See Also:
ATT_ARRAY, Constant Field Values
-
ATT_NULL_DEFAULT
public static final String ATT_NULL_DEFAULT
default value for attribute ATT_NULL- See Also:
ATT_NULL, Constant Field Values
-
DOCTYPE
public static final String DOCTYPE
the DOCTYPE for the serialization
-
-
Method Detail
-
setSuppressWarnings
public void setSuppressWarnings(boolean suppress)
Set whether to suppress warning messages or not- Parameters:
suppress- true to suppress warnings
-
clear
public void clear() throws Exceptiongenerates internally a new XML document and clears also the IgnoreList and the mappings for the Read/Write-Methods- Throws:
Exception- if something goes wrong
-
getVersion
public String getVersion()
returns the WEKA version with which the serialized object was created- Returns:
- the current version
- See Also:
Version
-
writeToXML
public Element writeToXML(Element parent, Object o, String name) throws Exception
adds the given Object to a DOM structure. (only public due to reflection).
Note: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 methodoverrideClassname(String)is invoked, which searches for an exact match of the classname in the override hashtable.- Parameters:
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 object- Returns:
- the node that was created
- Throws:
Exception- if the DOM creation fails- See Also:
overrideClassname(Object),overrideClassname(String),m_ClassnameOverride
-
toXML
public XMLDocument toXML(Object o) throws Exception
extracts all accesible properties from the given object- Parameters:
o- the object to turn into an XML representation- Returns:
- the generated DOM document
- Throws:
Exception- if XML generation fails
-
readBooleanFromXML
public boolean readBooleanFromXML(Element node) throws Exception
builds the primitive from the given DOM node.- Parameters:
node- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception- if instantiation fails
-
readByteFromXML
public byte readByteFromXML(Element node) throws Exception
builds the primitive from the given DOM node.- Parameters:
node- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception- if instantiation fails
-
readCharFromXML
public char readCharFromXML(Element node) throws Exception
builds the primitive from the given DOM node.- Parameters:
node- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception- if instantiation fails
-
readDoubleFromXML
public double readDoubleFromXML(Element node) throws Exception
builds the primitive from the given DOM node.- Parameters:
node- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception- if instantiation fails
-
readFloatFromXML
public float readFloatFromXML(Element node) throws Exception
builds the primitive from the given DOM node.- Parameters:
node- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception- if instantiation fails
-
readIntFromXML
public int readIntFromXML(Element node) throws Exception
builds the primitive from the given DOM node.- Parameters:
node- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception- if instantiation fails
-
readLongFromXML
public long readLongFromXML(Element node) throws Exception
builds the primitive from the given DOM node.- Parameters:
node- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception- if instantiation fails
-
readShortFromXML
public short readShortFromXML(Element node) throws Exception
builds the primitive from the given DOM node.- Parameters:
node- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception- if instantiation fails
-
readFromXML
public Object readFromXML(Object o, String name, Element child) throws Exception
adds the specific node to the object via a set method- Parameters:
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 add- Returns:
- the provided object, but augmented by the child
- Throws:
Exception- if something goes wrong
-
readFromXML
public Object readFromXML(Element node) throws Exception
builds the object from the given DOM node. (only public due to reflection)- Parameters:
node- the associated XML node- Returns:
- the instance created from the XML description
- Throws:
Exception- if instantiation fails
-
fromXML
public Object fromXML(Document document) throws Exception
returns the given DOM document as an instance of the specified class- Parameters:
document- the parsed DOM document representing the object- Returns:
- the XML as object
- Throws:
Exception- if object instantiation fails
-
read
public Object read(String xml) throws Exception
parses the given XML string (can be XML or a filename) and returns an Object generated from the representation- Parameters:
xml- the xml to parse (if "- Returns:
- the generated instance
- Throws:
Exception- if something goes wrong with the parsing
-
read
public Object read(File file) throws Exception
parses the given file and returns a DOM document- Parameters:
file- the XML file to parse- Returns:
- the parsed DOM document
- Throws:
Exception- if something goes wrong with the parsing
-
read
public Object read(InputStream stream) throws Exception
parses the given stream and returns a DOM document- Parameters:
stream- the XML stream to parse- Returns:
- the parsed DOM document
- Throws:
Exception- if something goes wrong with the parsing
-
read
public Object read(Reader reader) throws Exception
parses the given reader and returns a DOM document- Parameters:
reader- the XML reader to parse- Returns:
- the parsed DOM document
- Throws:
Exception- if something goes wrong with the parsing
-
write
public void write(String file, Object o) throws Exception
writes the given object into the file- Parameters:
file- the filename to write too- the object to serialize as XML- Throws:
Exception- if something goes wrong with the parsing
-
write
public void write(File file, Object o) throws Exception
writes the given object into the file- Parameters:
file- the filename to write too- the object to serialize as XML- Throws:
Exception- if something goes wrong with the parsing
-
write
public void write(OutputStream stream, Object o) throws Exception
writes the given object into the stream- Parameters:
stream- the filename to write too- the object to serialize as XML- Throws:
Exception- if something goes wrong with the parsing
-
write
public void write(Writer writer, Object o) throws Exception
writes the given object into the writer- Parameters:
writer- the filename to write too- the object to serialize as XML- Throws:
Exception- if something goes wrong with the parsing
-
main
public static void main(String[] args) throws Exception
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 atoString()of the generated object.- Throws:
Exception
-
getRevision
public String getRevision()
Returns the revision string.- Specified by:
getRevisionin interfaceRevisionHandler- Returns:
- the revision
-
-