|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectit.unimo.dbgroup.momis.XmlTools.BaseXmlTools
it.unimo.dbgroup.momis.XmlTools.BaseObjectDomStreamer
it.unimo.dbgroup.momis.XmlTools.XmlReader
This class reads an Xml document produced by XmlWriter and deserializes any object encoutered.
XmlReader looks for "ObjectArchive" and other attributes in the XML root element to understand the serialization format used in the document.
You can create an XmlReader by passing an external
input stream or by passing an existing DOM tree. If you
pass an input stream, it will be parsed and a DOM tree is constructed
before any of the readXXXX methods can be called.
On the other hand if you pass an existing DOM tree the readXXXX
begins their process from a start element of your choice.
XmlReader mantains an internal cursor that points to the current
position in the DOM tree, XML elements are processed in the same
order they are defined in the original document.
Any readXXXX method will process the current element and all the
child nodes (and sub-Elements). After a reading method is called
the current position is updated, if you read past last element
in the current sub-tree an exception will be thrown.
You can deserialze an object by calling the
readObject()
method, as you would do with an ObjectInputStream.
Customizing the serialization mechanism.
XmlReader is compatible with the standard Java serialization
mechanism. You can implement Externalizable interface or a
private readObject(ObjectInputStream in);
method.
Please note that this compatibility is obtained through an
ObjectInputStream wrapper (see XmlObjectInput), it is more
efficient to implement XmlExternalizable interface or a
private readObject(XmlReader out);
method.
You can also implement a normal readObject(ObjectInputStream in)
and obtain the XmlReader object used by the wrapper as follow:
cast the ObjectInputStream "in" parameter as a XmlObjectInput instance
and call any readXXXX method directly.
When you use this technique pay attention to not collide with
java standard deserialization: the 'in' parameter is a true
XmlObjectInput instance only if you are using XmlReader or XmlObjectInput
as deserializing tool.
These methods and interfaces work as described in Java Object Serialization Specification, please refer to that document for more details.
| Nested Class Summary | |
private class |
XmlReader.CallbackEntry
Stores an object validator and the corresponding priority call. |
| Field Summary | |
private boolean |
_balanced
blanced flag read from the source document. |
private boolean |
_classNameAsTagName
classNameAsTagName property value read from the source document. |
protected org.w3c.dom.Document |
_dox
Current document being processed. |
private boolean |
_fullClassNameAsAttribute
fullClassNameAsAttribute property value read from the source document. |
private boolean |
_fullJavaClassName
fullJavaClassName property value read from the source document. |
private java.io.ObjectInputStream |
_inWrap
Internal reference to an ObjectOutputStream wrapper. |
private int |
_readCount
Internal counter, if greater than 0 then objects reading is in progress. |
private static boolean |
_requireDefConstructor
Flag that indicate if default constructors are needed or not. |
private java.util.Stack |
_stacklists
Stack used to store validaror lists. |
private boolean |
_suidCheck
Internal value for the suidCheck property |
protected java.util.HashMap |
_suidMap
Map with Class objects as keys and corresponding suid as values. |
private java.util.ArrayList |
_validators
Current list of obect validators |
protected javax.xml.parsers.DocumentBuilder |
db
Builder used to obtain DOM documents. |
static javax.xml.parsers.DocumentBuilderFactory |
dbf
Factory used to obtain document builders. |
protected boolean |
def_active
If true, the defaultRead() method is enabled. |
protected java.lang.Class |
def_cls
defaultRead() cloacked argument. |
protected java.lang.Object |
def_obj
defaultRead() cloacked argument. |
protected DomNavigator |
domNav
Represent the current position in the DOM tree. |
protected java.util.HashMap |
objs_elem
Map with elements' objectID as keys and corresponding elements as value. |
protected java.util.HashMap |
objs_read
Cache with objects already processed by XmlReader. |
private static java.lang.Class[] |
parTypes
Parameter type used when searching for a custom readObject method. |
private static java.lang.Class[] |
parTypesWrap
Parameter type used when searching for a custom readObject method. |
boolean |
trimRight
This field rules the way a java string is retreived from the DOM: if true any any character following the last "\n" (and the "\n" itself) will be removed from the returned string. |
| Fields inherited from class it.unimo.dbgroup.momis.XmlTools.BaseXmlTools |
_debug, ATTR_TYPE, attrArrayLength, attrBalanced, attrClassName, attrClassNameAsTagName, attrDescr, attrField, attrFullClassNameAsAttribute, attrFullJavaClassName, attrObjectArchive, attrObjectID, attrObjectREF, attrSUID, attrValue, CDATA_TYPE, COMMENT_TYPE, DOCFRAG_TYPE, DOCTYPE_TYPE, DOCUMENT_TYPE, ELEMENT_TYPE, ENTITY_TYPE, ENTITYREF_TYPE, metaNameSpace, NOTATION_TYPE, PROCINSTR_TYPE, tagArray, tagBoolean, tagByte, tagChar, tagDouble, tagEndDefaultData, tagFloat, tagInt, tagLong, tagMapEntry, tagMeta, tagNull, tagObject, tagShort, tagSUIDEntry, tagSuperclass, TEXT_TYPE |
| Constructor Summary | |
XmlReader(org.w3c.dom.Document dox,
org.w3c.dom.Element startElement)
Creates an XmlReader using an existing DOM tree. |
|
XmlReader(java.io.InputStream in)
Creates a new XmlReader instance parsing an InputStream XML source. |
|
| Method Summary | |
private static java.lang.Object |
AllocateObject(java.lang.Class cl)
Allocates a new java instance for class cl. |
protected java.lang.Object |
allocateObjectInstance(org.w3c.dom.Element el,
java.lang.Class cl)
Given an object Element, this method allocates a new partially initialized instance. |
protected boolean |
CheckCustomization(java.lang.Object obj,
java.lang.Class cl)
Checks if an object declares a custom readObject method and possibly calls it. |
protected void |
createElementsMap()
This methods scan the whole DOM and construct the objs_elem Map. |
protected boolean |
createSUIDTable(org.w3c.dom.Element el)
Reads the 'meta' tag and constructs the suid table. |
void |
defaultRead()
Implements the default serializing mechanism. |
java.lang.String |
denormalizeClassName(java.lang.String className)
Decodes an encoded XML class name and returns a valid Java class name. |
protected java.lang.Class |
getClassFromElement(org.w3c.dom.Element el)
Given an object Element, this method returns the corresponding Class object. |
protected boolean |
hasInterface(java.lang.Class cl,
java.lang.Class Interface)
Checks if the specified class declare the specified interface. |
private boolean |
isElement(org.w3c.dom.Node nd)
Returns true if the DOM node nd is an Element instance. |
protected boolean |
isEndDefaultData(org.w3c.dom.Element el)
Checks if the specified element is an end-default-data tag marker. |
protected boolean |
isMapEntry(org.w3c.dom.Element el)
Returns true if the specified element represent a described map entry tag. |
boolean |
isNull(org.w3c.dom.Element el)
Returns true if element el represent a "null" element, i.e. |
protected boolean |
isSuperclass(org.w3c.dom.Element el)
Returns true if element el represents a superclass tag. |
protected java.lang.reflect.Field |
mapField(java.lang.Class cl,
org.w3c.dom.Element el)
Try to map an XML element to a class field. |
protected void |
rdArray(org.w3c.dom.Element el,
java.lang.Object obj)
Process an array element. |
protected boolean |
rdBoolean(org.w3c.dom.Element el)
This method interpets a given element as a 'boolean' tag and return his value. |
protected byte |
rdByte(org.w3c.dom.Element el)
This method interpets a given element as a 'byte' tag and return his value. |
protected char |
rdChar(org.w3c.dom.Element el)
This method interpets a given element as a 'char' tag and return his value. |
protected double |
rdDouble(org.w3c.dom.Element el)
This method interpets a given element as a 'double' tag and return his value. |
protected void |
rdField(org.w3c.dom.Element el,
java.lang.Object obj,
java.lang.Class cl)
Process the element el, try to find a suitable field in class cl and assigns the retreived value to obj instance. |
protected float |
rdFloat(org.w3c.dom.Element el)
This method interpets a given element as an 'float' tag and return his value. |
protected int |
rdInt(org.w3c.dom.Element el)
This method interpets a given element as an 'int' tag and return his value. |
protected java.lang.Object |
rdKnown(org.w3c.dom.Element el,
java.lang.Class cl)
Checks and possibly process an element as a special known type. |
protected long |
rdLong(org.w3c.dom.Element el)
This method interpets a given element as a 'long' tag and return his value. |
protected java.lang.Object |
rdObjectElement(org.w3c.dom.Element el)
Process an element as an objec description element. |
protected java.lang.Object |
rdPrimitiveType(org.w3c.dom.Element el,
java.lang.Class flc)
This method will read a primitive element and wrap it to the corresponding primitive wrapper. |
protected java.lang.Object |
rdPrimitiveWrapper(org.w3c.dom.Element el,
java.lang.Class cl)
Interprets element el as an primitive wrapper element. |
protected short |
rdShort(org.w3c.dom.Element el)
This method interpets a given element as a 'short' tag and return his value. |
boolean |
readBoolean()
Process the current DOM element as a boolean element. |
byte |
readByte()
Process the current DOM element as a byte element. |
char |
readChar()
Process the current DOM element as a char element. |
double |
readDouble()
Process the current DOM element as a 'double' element. |
protected void |
readFields(org.w3c.dom.Node nd,
java.lang.Object obj)
Deserializes all member fields for all the superclasses of obj. |
float |
readFloat()
Process the current DOM element as a float element. |
int |
readInt()
Process the current DOM element as an int element. |
long |
readLong()
Process the current DOM element as a 'long' element. |
java.lang.Object |
readObject()
Process the current Dom element as an object element and return the new allocated object. |
protected void |
readRootElement()
Process the start element of the DOM tree and set various internal flags. |
short |
readShort()
Process the current DOM element as a 'short' element. |
private void |
registerObject(org.w3c.dom.Element el,
java.lang.Object obj)
Inserts obj in the list of already processed objects (objs_read). |
void |
registerValidation(java.io.ObjectInputValidation obj,
int priority)
Registers an object validator. |
void |
setObjectInputWrapper(java.io.ObjectInputStream inWrap)
Sets the a new ObjectInputStream wrapper that will be used when calling custom reading methods. |
void |
setSuidCheck(boolean value)
Sets the suidCheck property value. |
void |
setTrimRight(boolean value)
Sets the trimRight property value. |
| Methods inherited from class it.unimo.dbgroup.momis.XmlTools.BaseObjectDomStreamer |
getClassDetails, getSerialVersionUID, isArray, isDeserializable, isPrimitiveWrapper, isPrimitiveWrapper, isSerializable |
| Methods inherited from class it.unimo.dbgroup.momis.XmlTools.BaseXmlTools |
debug, getElementString, throwNodeException |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
private static boolean _requireDefConstructor
You can found source code for the library in
XmlTools_XmlReader.c
XmlTools_XmlReader.h
files, please refer to your system documentation on how to
create a dynamic library or look in the Java swing tutorial
for some tips on Java Native interface (recommended if you are
new to the argument).
private java.util.Stack _stacklists
private java.util.ArrayList _validators
public static javax.xml.parsers.DocumentBuilderFactory dbf
You can assign your own factory object if you want to use others DOM compatible implementations.
protected javax.xml.parsers.DocumentBuilder db
protected org.w3c.dom.Document _dox
private boolean _classNameAsTagName
if true class names are used as tag name for object elements.
private boolean _fullClassNameAsAttribute
if true, every object element has the attribute "class" with the full java class name as value.
private boolean _fullJavaClassName
When java class names are used as object tags, it tells if the full name or the compact name should be used.
private boolean _balanced
It tells if the DOM to be processed is a balanced DOM tree.
protected java.lang.Object def_obj
It represents the instance where retreived field values must be assigned to.
protected java.lang.Class def_cls
Class where to look for to map an XML element to a destination field.
protected boolean def_active
private static java.lang.Class[] parTypes
Internal use. DO NOT MODIFY!
private static java.lang.Class[] parTypesWrap
Internal use. DO NOT MODIFY!
protected DomNavigator domNav
protected java.util.HashMap objs_read
protected java.util.HashMap objs_elem
This Map is istantiated only if the source XML is
a balanced document (a balanceDOM() call was issued in XmlWriter
that created it).
When an object reference element is encountered, a query to this
Map occurs.
public boolean trimRight
This variable is provided because when a DOM is constructed from
a Xml file Jaxp add a "\n" and some white spaces that were not included
in the original string.
On other hand when a DOM is constructed
from scratch no "\n" is added so using this variable may be usefull
if dealing with latter DOMs.
The default value is true.
private int _readCount
private java.io.ObjectInputStream _inWrap
private boolean _suidCheck
protected java.util.HashMap _suidMap
If source document doesn't define a suid table this variable is null.
| Constructor Detail |
public XmlReader(org.w3c.dom.Document dox,
org.w3c.dom.Element startElement)
throws XmlToolsException
dox - An object that implemens the w3c Document interface,
it is the DOM tree access object.startElement - One of dox elements that acts as start element, usually
this is the root element.
XmlToolsException - if initialization falis.
public XmlReader(java.io.InputStream in)
throws XmlToolsException
in - The input stream to parse and process.
XmlToolsException - if initialization falis.| Method Detail |
private boolean isElement(org.w3c.dom.Node nd)
nd - DOM node to check.
private static java.lang.Object AllocateObject(java.lang.Class cl)
throws java.lang.InstantiationException,
java.lang.NoSuchMethodError,
java.lang.ExceptionInInitializerError
This is a native method that use JNI. It requires the Xrnm dynamic library.
java.lang.InstantiationException
java.lang.NoSuchMethodError
java.lang.ExceptionInInitializerError
private void registerObject(org.w3c.dom.Element el,
java.lang.Object obj)
throws XmlToolsException
If objs already exists in objs_read then an exception is thrown.
el - Element that describe the object obj.obj - the object instance to be registered.
XmlToolsException - if:
protected void readRootElement()
throws XmlToolsException
XmlToolsExcpetion - if the start element is not valid.
XmlToolsException
protected boolean createSUIDTable(org.w3c.dom.Element el)
throws XmlToolsException
If 'meta' tag is not present then _suidMap must be assigned a null value.
el - The 'meta' tag.
XmlToolsException - if any of the class specified in the
suid.entry tags was not found.
protected void createElementsMap()
throws XmlToolsException
XmlToolsException - if the scan process fails.
protected void rdArray(org.w3c.dom.Element el,
java.lang.Object obj)
throws XmlToolsException,
java.lang.ClassNotFoundException
This method decodes an array element by calling rdObjectElement or rdPrimitiveType for each array element and assign the retreived values to an array object.
el - The array element to process.obj - Must be an array of a compatible type with elements that
are being readed.
XmlToolsException - if an error occurs during the process.
java.lang.ClassNotFoundException - if the some classes could not be
loaded.
protected java.lang.Class getClassFromElement(org.w3c.dom.Element el)
throws XmlToolsException,
java.lang.ClassNotFoundException
el - The object element to process.
XmlToolsException - If the specified tag is not valid.
java.lang.ClassNotFoundException - if the class specified in el
could not be loaded.
protected java.lang.Object allocateObjectInstance(org.w3c.dom.Element el,
java.lang.Class cl)
throws XmlToolsException
Conversely if _requireDefConstructor is true the default constructor for the class is called. If no default constructor exists, an exception is thrown.
Value of _requireDefConstructor flag depends on the presence of Xrnm dynamic library.
el - Element that describe the instancecl - Suggested class for the object. The cl value is based
on a pre-processing of element el.
XmlToolsException - if:
protected java.lang.Object rdPrimitiveWrapper(org.w3c.dom.Element el,
java.lang.Class cl)
throws XmlToolsException
el - An XML element that describe a java primitive wrapper.cl - Class object for the primitive wrapper (obtained by a
previous pre-processing of element el).
XmlToolsException - if cl is not a Class object of a primitive
wrapper (Integer, Boolean, Float ecc..).
protected java.lang.Object rdObjectElement(org.w3c.dom.Element el)
throws XmlToolsException,
java.lang.ClassNotFoundException
This mthod works as follow:
el - XML element to be processed.
XmlToolsException - if:java.lang.ClassNotFoundException - if the class pointed by Element el
could not be found.
protected boolean hasInterface(java.lang.Class cl,
java.lang.Class Interface)
cl - Class object to examine.Interface - Class object of the interface to be looked for.
Interface.protected boolean isMapEntry(org.w3c.dom.Element el)
el - Element to be examined.
protected java.lang.Object rdKnown(org.w3c.dom.Element el,
java.lang.Class cl)
throws XmlToolsException,
java.lang.ClassNotFoundException
This method is dual to the wrKnownField of XmlWriter.
This implementatio introduce support for the following class types:
el - element to be examined.cl - Suggested Class object for element el. Class cl is
the suggested class by XmlReader, it may be null. Subclasses are
free to ignore the Class parameter.
XmlToolsException - if el is a known type but for some reason
deserialization could not be completed.
java.lang.ClassNotFoundException - due to recoursive calls to rdObjectElement.
protected boolean CheckCustomization(java.lang.Object obj,
java.lang.Class cl)
throws XmlToolsException
This method checks in the order, for the following methods signatures (they must be declared exactly as follow):
private void readObject (ObjectInputStream in);private void readObject (XmlReader rdr);
If both are presents only the first is called. You can write customized deserialization code by implementing one of these methods in your classes. While in this methods you can use the XmlReader.registerValidation or ObjectInputStream.registerValidation methods to request a callback after the whol objects graph is constructed.
Please note that readObject compatibility is obtained throughout an XmlReader wrapper that extends the ObjectInputStream class, usually an XmlObjectInput class, you can specify your own wrapper by calling the XmlReader.setObjectInputWrapper method.
obj - object instance whose methods must possibly be called.cl - Class object to be examined.
XmlToolsException - if an exception is raised by an invoked
custom method or if they are not accessibles.protected boolean isEndDefaultData(org.w3c.dom.Element el)
See alse BaseXmlTools.tagEndDefaultData.
el - The element to be examined.
protected boolean isSuperclass(org.w3c.dom.Element el)
el - The Element to be examined.
protected void readFields(org.w3c.dom.Node nd,
java.lang.Object obj)
throws XmlToolsException,
java.lang.ClassNotFoundException
This method call custom readObject methods or default read for the obj's Class object and for all its superclasses.
nd - the DOM element to deserialize.obj - the allocated target instance of the deserialization process.
XmlToolsException
java.lang.ClassNotFoundException
protected java.lang.reflect.Field mapField(java.lang.Class cl,
org.w3c.dom.Element el)
throws XmlToolsException
A field is mapped if and only if it has the same name of the 'field' attribute of XML element, and has the same class type. No type conversion is performed.
el - XML element that should have the 'field' attributecl - Class object where to search for.
XmlToolsException
protected int rdInt(org.w3c.dom.Element el)
throws XmlToolsException
XmlToolsException
protected float rdFloat(org.w3c.dom.Element el)
throws XmlToolsException
XmlToolsException
protected double rdDouble(org.w3c.dom.Element el)
throws XmlToolsException
XmlToolsException
protected long rdLong(org.w3c.dom.Element el)
throws XmlToolsException
XmlToolsException
protected byte rdByte(org.w3c.dom.Element el)
throws XmlToolsException
XmlToolsException
protected short rdShort(org.w3c.dom.Element el)
throws XmlToolsException
XmlToolsException
protected char rdChar(org.w3c.dom.Element el)
throws XmlToolsException
XmlToolsException
protected boolean rdBoolean(org.w3c.dom.Element el)
throws XmlToolsException
XmlToolsException
protected java.lang.Object rdPrimitiveType(org.w3c.dom.Element el,
java.lang.Class flc)
throws XmlToolsException
el - Element representing a primitive type to be processed.flc - Primitive class type to wrap.
XmlToolsException - if flc is not a primitive type Class object and if
element el is an invalid primitive element.
protected void rdField(org.w3c.dom.Element el,
java.lang.Object obj,
java.lang.Class cl)
throws XmlToolsException,
java.lang.ClassNotFoundException
This method first search in class cl a suitable field by calling the mapField, if mapping is successful then process element el and assigns the value to the mapped field. If no field can be mapped then this method returns.
The element can represent any java type, primitive or complex.
rdObjectElement is called to process object elements, rdPrimitiveType
is called to process simple java types.
el - Element to be processed.obj - Object instance where to assign the retreived value.cl - obj's class or one of obj's superclasses, the field is searched
in the declared fields of class cl only.
XmlToolsException
java.lang.ClassNotFoundException
public java.lang.Object readObject()
throws XmlToolsException,
java.lang.ClassNotFoundException
After the object and all its components are deserialized, readObject calls any object validator that was registered during the deserialization. Object validators are callback method and can be supplied by calling the registerValidation() method.
XmlToolsException - if:
java.lang.ClassNotFoundException - if the class specified by
the current DOM Element could not be loaded.
public boolean readBoolean()
throws XmlToolsException
XmlToolsException - if an error occurs during the process.
public byte readByte()
throws XmlToolsException
XmlToolsException - if an error occurs during the process.
public short readShort()
throws XmlToolsException
XmlToolsException - if an error occurs during the process.
public char readChar()
throws XmlToolsException
XmlToolsException - if an error occurs during the process.
public long readLong()
throws XmlToolsException
XmlToolsException - if an error occurs during the process.
public float readFloat()
throws XmlToolsException
XmlToolsException - if an error occurs during the process.
public double readDouble()
throws XmlToolsException
XmlToolsException - if an error occurs during the process.
public int readInt()
throws XmlToolsException
XmlToolsException - if an error occurs during the process.public java.lang.String denormalizeClassName(java.lang.String className)
Given a class name encoded with the XmlWriter.normalizeClassName
method.
This method decodes the input string
and return a full java class name.
Please refer to XmlWriter.normalizeClassName for details on
normalization process.
className - the input string, usually an XML tag name.
public boolean isNull(org.w3c.dom.Element el)
el - The element to be examined.
public void defaultRead()
throws XmlToolsException,
java.lang.ClassNotFoundException
readObject
implementation.
- def_active
- def_cls
- def_Obj
defaultRead must read all def_Obj fields for class
def_cls beginning from the current position mantained by
domNav object instance.
XmlToolsException - If some object fields are not accessible
or if defaultWrite is called outside a custom writeObject method.
java.lang.ClassNotFoundException
public void registerValidation(java.io.ObjectInputValidation obj,
int priority)
throws java.io.NotActiveException,
java.io.InvalidObjectException
Call this method from your custom readXmlExternal or readObject
methods to register a callback method.
When the whole graph is constructed but before
XmlReader.readObject returns, registered callback are
called in ascending priority oredr.
You can implement java.io.ObjectInputValidation interface in your serializable objects and use it to reopen file descriptors, streams, perform validity checks and so on...
obj - Object that implements ObjectInputValidation.priority - validators with higher value are called earlier,
use 0 as default value.
java.io.NotActiveException - if XmlReader is not processing any
object.
java.io.InvalidObjectException - if obj is null.public void setSuidCheck(boolean value)
This property is provided to avoid conflicts beetween different versions of the same class.
When this property is true, XmlReader compares the serial version unique identifier (suid) of any serialized class with the current suid for that class, if they differ, an exception will be thrown and graph reconstruction will be impossible.
If this property is set to false no version check is done and the serialized class is always considered compatible with the current class.
You may refer to the Java Object Serialization Specification
for compatible class definition.
As described in the document above, please consider to declare
a your own suid instead of setting this property to false:
declaring a own suid is more reliable than disabling any suid check.
Default value for suidCheck property is true.
public void setObjectInputWrapper(java.io.ObjectInputStream inWrap)
inWrap - The new input wrapper.public void setTrimRight(boolean value)
This property rules the way a java string is retreived from the DOM: if true any any character following the last "\n" (and the "\n" itself) will be removed from the returned string.
This property is provided because when a DOM is constructed from
a Xml file Jaxp add a "\n" and some white spaces that were not included
in the original string.
On other hand when a DOM is constructed from scratch no "\n"
is added so using this property may be usefull if dealing with latter DOMs.
The default value is true.
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||