org.xeril.xml
Class XmlToObject

java.lang.Object
  |
  +--org.xeril.xml.XmlToObject

public class XmlToObject
extends java.lang.Object

This class is used to generate an object from XML


Constructor Summary
XmlToObject()
          Constructor.
XmlToObject(boolean validate)
          Constructor.
 
Method Summary
protected  java.lang.Object getArray(org.w3c.dom.Node node, java.lang.Class cl, XmlMapDescriptor map_desc)
          Gets the array from the node.
protected  java.lang.Object getArray(org.w3c.dom.NodeList nodes, java.lang.Class cl, XmlMapper mapper)
          Gets the array from the node.
protected  java.lang.Object getMappedValue(org.w3c.dom.Node node, XmlMapper mapper)
          Gets the value from the node.
protected  java.lang.Object getNodeValue(org.w3c.dom.Node node, java.lang.Class cl, XmlMapper mapper)
          Gets the value from the node.
 java.lang.Object getObject(java.io.InputStream input, java.lang.Class cl)
          From an xml input stream, tries to create the corresponding object.
 java.lang.Object getObject(org.w3c.dom.Node node, java.lang.Class cl)
          Creates the object.
protected  java.lang.Object getValue(org.w3c.dom.Node node, java.lang.Class cl, XmlMapper mapper)
          Gets the value from the node.
protected  java.lang.Object getValue(java.lang.String stringValue, java.lang.Class cl, XmlMapper mapper)
          Gets the value from the string value.
protected  XmlMapDescriptor getXmlMapDescriptor(java.lang.String xml_name, XmlInfo info)
          Tries to extract the map descriptor from the xml name and info object.
static void main(java.lang.String[] args)
           
protected  void setField(java.lang.Object o, org.w3c.dom.Node node, XmlInfo info)
          Sets the field from the node.
static java.lang.Object xmlToObject(java.io.InputStream input, java.lang.Class cl)
          From an xml stream, tries to create the corresponding object.
static java.lang.Object xmlToObject(java.lang.String filename, java.lang.Class cl)
          From an xml file, tries to create the corresponding object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlToObject

public XmlToObject()
Constructor. By default, validate the xml


XmlToObject

public XmlToObject(boolean validate)
Constructor.

Parameters:
validate - to know whether to validate the xml or no
Method Detail

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception

xmlToObject

public static java.lang.Object xmlToObject(java.io.InputStream input,
                                           java.lang.Class cl)
                                    throws XmlToObjectException
From an xml stream, tries to create the corresponding object. You have to specify the class of your object because it cannot be guessed.

Parameters:
input - the input stream from which to read the xml
cl - the class represented by this file
Returns:
a newly created object
Throws:
XmlToObjectException - when there is a problem building the object

xmlToObject

public static java.lang.Object xmlToObject(java.lang.String filename,
                                           java.lang.Class cl)
                                    throws XmlToObjectException,
                                           java.io.IOException
From an xml file, tries to create the corresponding object. You have to specify the class of your object because it cannot be guessed.

Parameters:
filename - the name of the xml file to read
cl - the class represented by this file
Returns:
a newly created object
Throws:
XmlToObjectException - when there is a problem building the object
java.io.IOException - when there is an IO problem

getObject

public java.lang.Object getObject(java.io.InputStream input,
                                  java.lang.Class cl)
                           throws XmlToObjectException
From an xml input stream, tries to create the corresponding object. You have to specify the class of your object because it cannot be guessed.

Parameters:
input - the input stream from which to read the xml
cl - the class represented by this file
Returns:
a newly created object
Throws:
XmlToObjectException - when there is a problem building the object

getObject

public java.lang.Object getObject(org.w3c.dom.Node node,
                                  java.lang.Class cl)
                           throws XmlToObjectException,
                                  java.lang.InstantiationException,
                                  java.lang.IllegalAccessException,
                                  java.lang.NoSuchFieldException,
                                  XmlMapperException
Creates the object. You provide the starting node as well as the class of the object (it is impossible to know from the node what is the class of the object!), especially if there is an XmlInfo class for that object.

Parameters:
node - the starting node (root)
cl - the class of the object to generate

setField

protected void setField(java.lang.Object o,
                        org.w3c.dom.Node node,
                        XmlInfo info)
                 throws java.lang.NoSuchFieldException,
                        XmlToObjectException,
                        java.lang.IllegalAccessException,
                        java.lang.InstantiationException,
                        XmlMapperException
Sets the field from the node.

Parameters:
o - the object to set the fields on
node - the node from which to extract the field
info - the info object (can be null)

getNodeValue

protected java.lang.Object getNodeValue(org.w3c.dom.Node node,
                                        java.lang.Class cl,
                                        XmlMapper mapper)
                                 throws java.lang.IllegalAccessException,
                                        java.lang.InstantiationException,
                                        XmlToObjectException,
                                        java.lang.NoSuchFieldException,
                                        XmlMapperException
Gets the value from the node. In this method, the node can contain 'garbage' (unwanted \n or spaces).

Parameters:
node - the node from which to extract the value
cl - the class of the value we are supposed to create
mapper - the mapper to extract the value
Returns:
the value

getValue

protected java.lang.Object getValue(org.w3c.dom.Node node,
                                    java.lang.Class cl,
                                    XmlMapper mapper)
                             throws java.lang.IllegalAccessException,
                                    java.lang.InstantiationException,
                                    XmlToObjectException,
                                    java.lang.NoSuchFieldException,
                                    XmlMapperException
Gets the value from the node. Here the node is clean, so it's real data.

Parameters:
node - the node from which to extract the value
cl - the class of the value we are supposed to create
mapper - the mapper to extract the value
Returns:
the value

getValue

protected java.lang.Object getValue(java.lang.String stringValue,
                                    java.lang.Class cl,
                                    XmlMapper mapper)
                             throws java.lang.IllegalAccessException,
                                    java.lang.InstantiationException,
                                    XmlToObjectException,
                                    java.lang.NoSuchFieldException,
                                    XmlMapperException
Gets the value from the string value.

Parameters:
stringValue - the value as a String
cl - the class of the value we are supposed to create
mapper - the mapper to extract the value
Returns:
the value

getArray

protected java.lang.Object getArray(org.w3c.dom.Node node,
                                    java.lang.Class cl,
                                    XmlMapDescriptor map_desc)
                             throws java.lang.IllegalAccessException,
                                    java.lang.InstantiationException,
                                    XmlToObjectException,
                                    java.lang.NoSuchFieldException,
                                    XmlMapperException
Gets the array from the node.

Parameters:
node - the node from which we extract the array
cl - the class of the value we are supposed to create
map_desc - the map descriptor of this node/field
Returns:
the array

getMappedValue

protected java.lang.Object getMappedValue(org.w3c.dom.Node node,
                                          XmlMapper mapper)
                                   throws java.lang.IllegalAccessException,
                                          java.lang.InstantiationException,
                                          XmlToObjectException,
                                          java.lang.NoSuchFieldException,
                                          XmlMapperException
Gets the value from the node. It is supposed to be a mapped value.

Parameters:
node - the node from which we extract the mapped value
mapper - the mapper to unmap the value
Returns:
the value

getArray

protected java.lang.Object getArray(org.w3c.dom.NodeList nodes,
                                    java.lang.Class cl,
                                    XmlMapper mapper)
                             throws java.lang.IllegalAccessException,
                                    java.lang.InstantiationException,
                                    XmlToObjectException,
                                    java.lang.NoSuchFieldException,
                                    XmlMapperException
Gets the array from the node.

Parameters:
nodes - the list of nodes from which to extract the array values
cl - the class of the value we are supposed to create
mapper - the mapper (indexed)
Returns:
the array

getXmlMapDescriptor

protected XmlMapDescriptor getXmlMapDescriptor(java.lang.String xml_name,
                                               XmlInfo info)
Tries to extract the map descriptor from the xml name and info object. If no information is found in the info object then null is returned.

Parameters:
xml_name - the name of the xml tag we are looking for its map desc
info - the info object that may hold the tag name
Returns:
the map descriptor