public class mxObjectCodec
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
protected java.util.Map<java.lang.String,java.lang.reflect.Method> |
accessors
Caches accessors for the given method names.
|
protected java.util.Set<java.lang.String> |
exclude
Array containing the variable names that should be ignored by the codec.
|
protected java.util.Map<java.lang.Class,java.util.Map<java.lang.String,java.lang.reflect.Field>> |
fields
Caches fields for faster access.
|
protected java.util.Set<java.lang.String> |
idrefs
Array containing the variable names that should be turned into or
converted from references.
|
protected java.util.Map<java.lang.String,java.lang.String> |
mapping
Maps from from fieldnames to XML attribute names.
|
protected java.util.Map<java.lang.String,java.lang.String> |
reverse
Maps from from XML attribute names to fieldnames.
|
protected java.lang.Object |
template
Holds the template object associated with this codec.
|
Constructor and Description |
---|
mxObjectCodec(java.lang.Object template)
Constructs a new codec for the specified template object.
|
mxObjectCodec(java.lang.Object template,
java.lang.String[] exclude,
java.lang.String[] idrefs,
java.util.Map<java.lang.String,java.lang.String> mapping)
Constructs a new codec for the specified template object.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addObjectValue(java.lang.Object obj,
java.lang.String fieldname,
java.lang.Object value,
java.lang.Object template)
Sets the decoded child node as a value of the given object.
|
java.lang.Object |
afterDecode(mxCodec dec,
org.w3c.dom.Node node,
java.lang.Object obj)
Hook for subclassers to post-process the object after decoding.
|
org.w3c.dom.Node |
afterEncode(mxCodec enc,
java.lang.Object obj,
org.w3c.dom.Node node)
Hook for subclassers to post-process the node for the given object after
encoding and return the post-processed node.
|
org.w3c.dom.Node |
beforeDecode(mxCodec dec,
org.w3c.dom.Node node,
java.lang.Object obj)
Hook for subclassers to pre-process the node for the specified object
and return the node to be used for further processing by
decode(mxCodec, Node) . |
java.lang.Object |
beforeEncode(mxCodec enc,
java.lang.Object obj,
org.w3c.dom.Node node)
Hook for subclassers to pre-process the object before encoding.
|
protected java.lang.Object |
cloneTemplate(org.w3c.dom.Node node)
Returns a new instance of the template object for representing the given
node.
|
protected java.lang.Object |
convertValueFromXml(java.lang.Class<?> type,
java.lang.Object value)
Converts XML attribute values to object of the given type.
|
protected java.lang.Object |
convertValueToXml(java.lang.Object value)
Converts true to "1" and false to "0".
|
java.lang.Object |
decode(mxCodec dec,
org.w3c.dom.Node node)
Parses the given node into the object or returns a new object
representing the given node.
|
java.lang.Object |
decode(mxCodec dec,
org.w3c.dom.Node node,
java.lang.Object into)
Parses the given node into the object or returns a new object
representing the given node.
|
protected void |
decodeAttribute(mxCodec dec,
org.w3c.dom.Node attr,
java.lang.Object obj)
Reads the given attribute into the specified object.
|
protected void |
decodeAttributes(mxCodec dec,
org.w3c.dom.Node node,
java.lang.Object obj)
Decodes all attributes of the given node using decodeAttribute.
|
protected void |
decodeChild(mxCodec dec,
org.w3c.dom.Node child,
java.lang.Object obj)
Reads the specified child into the given object.
|
protected void |
decodeChildren(mxCodec dec,
org.w3c.dom.Node node,
java.lang.Object obj)
Decodec all children of the given node using decodeChild.
|
protected void |
decodeNode(mxCodec dec,
org.w3c.dom.Node node,
java.lang.Object obj)
Calls decodeAttributes and decodeChildren for the given node.
|
org.w3c.dom.Node |
encode(mxCodec enc,
java.lang.Object obj)
Encodes the specified object and returns a node representing then given
object.
|
protected void |
encodeElements(mxCodec enc,
java.lang.Object obj,
org.w3c.dom.Node node)
Encodes the child objects of arrays, maps and collections.
|
protected void |
encodeFields(mxCodec enc,
java.lang.Object obj,
org.w3c.dom.Node node)
Encodes the declared fields of the given object into the given node.
|
protected void |
encodeObject(mxCodec enc,
java.lang.Object obj,
org.w3c.dom.Node node)
Encodes the value of each member in then given obj
into the given node using
encodeFields(mxCodec, Object, Node)
and encodeElements(mxCodec, Object, Node) . |
protected void |
encodeValue(mxCodec enc,
java.lang.Object obj,
java.lang.String fieldname,
java.lang.Object value,
org.w3c.dom.Node node)
Converts the given value according to the mappings
and id-refs in this codec and uses
writeAttribute(mxCodec, Object, String, Object, Node)
to write the attribute into the given node. |
protected java.lang.reflect.Method |
getAccessor(java.lang.Object obj,
java.lang.reflect.Field field,
boolean isGetter)
Returns the accessor (getter, setter) for the specified field.
|
protected java.lang.String |
getAttributeName(java.lang.String fieldname)
Returns the XML node attribute name for the given Java field name.
|
protected java.lang.reflect.Field |
getField(java.lang.Object obj,
java.lang.String fieldname)
Returns the field with the specified name.
|
protected java.lang.String |
getFieldName(java.lang.String attributename)
Returns the Java field name for the given XML attribute name.
|
protected java.lang.Object |
getFieldTemplate(java.lang.Object obj,
java.lang.String fieldname,
org.w3c.dom.Node child)
Returns the template instance for the given field.
|
protected java.lang.Object |
getFieldValue(java.lang.Object obj,
java.lang.String fieldname)
Returns the value of the field with the specified name in the specified
object instance.
|
protected java.lang.Object |
getFieldValueWithAccessor(java.lang.Object obj,
java.lang.reflect.Field field)
Returns the value of the field using the accessor for the field if one exists.
|
protected java.lang.reflect.Method |
getMethod(java.lang.Object obj,
java.lang.String methodname,
java.lang.Class[] params)
Returns the method with the specified signature.
|
java.lang.String |
getName()
Returns the name used for the nodenames and lookup of the codec when
classes are encoded and nodes are decoded.
|
java.lang.Object |
getTemplate()
Returns the template object associated with this codec.
|
boolean |
isExcluded(java.lang.Object obj,
java.lang.String attr,
java.lang.Object value,
boolean write)
Returns true if the given attribute is to be ignored by the codec.
|
protected boolean |
isPrimitiveValue(java.lang.Object value)
Returns true if the given object is a primitive value.
|
boolean |
isReference(java.lang.Object obj,
java.lang.String attr,
java.lang.Object value,
boolean isWrite)
Returns true if the given fieldname is to be treated as a textual
reference (ID).
|
boolean |
processInclude(mxCodec dec,
org.w3c.dom.Node node,
java.lang.Object into)
Returns true if the given node is an include directive and executes the
include by decoding the XML document.
|
protected void |
setFieldValue(java.lang.Object obj,
java.lang.String fieldname,
java.lang.Object value)
Sets the value of the field with the specified name
in the specified object instance.
|
protected void |
setFieldValueWithAccessor(java.lang.Object obj,
java.lang.reflect.Field field,
java.lang.Object value)
Sets the value of the given field using the accessor if one exists.
|
protected void |
writeAttribute(mxCodec enc,
java.lang.Object obj,
java.lang.String attr,
java.lang.Object value,
org.w3c.dom.Node node)
Writes the given value into node using writePrimitiveAttribute
or writeComplexAttribute depending on the type of the value.
|
protected void |
writeComplexAttribute(mxCodec enc,
java.lang.Object obj,
java.lang.String attr,
java.lang.Object value,
org.w3c.dom.Node node)
Writes the given value as a child node of the given node.
|
protected void |
writePrimitiveAttribute(mxCodec enc,
java.lang.Object obj,
java.lang.String attr,
java.lang.Object value,
org.w3c.dom.Node node)
Writes the given value as an attribute of the given node.
|
protected java.lang.Object template
protected java.util.Set<java.lang.String> exclude
protected java.util.Set<java.lang.String> idrefs
protected java.util.Map<java.lang.String,java.lang.String> mapping
protected java.util.Map<java.lang.String,java.lang.String> reverse
protected java.util.Map<java.lang.String,java.lang.reflect.Method> accessors
protected java.util.Map<java.lang.Class,java.util.Map<java.lang.String,java.lang.reflect.Field>> fields
public mxObjectCodec(java.lang.Object template)
public mxObjectCodec(java.lang.Object template, java.lang.String[] exclude, java.lang.String[] idrefs, java.util.Map<java.lang.String,java.lang.String> mapping)
template
- Prototypical instance of the object to be encoded/decoded.exclude
- Optional array of fieldnames to be ignored.idrefs
- Optional array of fieldnames to be converted to/from references.mapping
- Optional mapping from field- to attributenames.public java.lang.String getName()
mxCodecRegistry.register(new mxCellCodec()
{
public String getName()
{
return "anotherName";
}
});
public java.lang.Object getTemplate()
protected java.lang.Object cloneTemplate(org.w3c.dom.Node node)
node
- XML node that the object is going to represent.public boolean isExcluded(java.lang.Object obj, java.lang.String attr, java.lang.Object value, boolean write)
exclude
.obj
- Object instance that contains the field.attr
- Fieldname of the field.value
- Value of the field.write
- Boolean indicating if the field is being encoded or
decoded. write is true if the field is being encoded, else it is
being decoded.public boolean isReference(java.lang.Object obj, java.lang.String attr, java.lang.Object value, boolean isWrite)
idrefs
.obj
- Object instance that contains the field.attr
- Fieldname of the field.value
- Value of the field.isWrite
- Boolean indicating if the field is being encoded or
decoded. isWrite is true if the field is being encoded, else it is being
decoded.public org.w3c.dom.Node encode(mxCodec enc, java.lang.Object obj)
exclude
then it is ignored.idrefs
then
mxCodec.getId(Object)
is used to replace the object with its ID.
mapping
.mxCodec.isEncodeDefaults()
is true or
the value differs from the template value, then ...
idrefs
or if an object cannot be
encoded, a warning is printed to System.err.enc
- Codec that controls the encoding process.obj
- Object to be encoded.protected void encodeObject(mxCodec enc, java.lang.Object obj, org.w3c.dom.Node node)
encodeFields(mxCodec, Object, Node)
and encodeElements(mxCodec, Object, Node)
.enc
- Codec that controls the encoding process.obj
- Object to be encoded.node
- XML node that contains the encoded object.protected void encodeFields(mxCodec enc, java.lang.Object obj, org.w3c.dom.Node node)
enc
- Codec that controls the encoding process.obj
- Object whose fields should be encoded.node
- XML node that contains the encoded object.protected void encodeElements(mxCodec enc, java.lang.Object obj, org.w3c.dom.Node node)
enc
- Codec that controls the encoding process.obj
- Object whose child objects should be encoded.node
- XML node that contains the encoded object.protected void encodeValue(mxCodec enc, java.lang.Object obj, java.lang.String fieldname, java.lang.Object value, org.w3c.dom.Node node)
writeAttribute(mxCodec, Object, String, Object, Node)
to write the attribute into the given node.enc
- Codec that controls the encoding process.obj
- Object whose field is going to be encoded.fieldname
- Name if the field to be encoded.value
- Value of the property to be encoded.node
- XML node that contains the encoded object.protected boolean isPrimitiveValue(java.lang.Object value)
value
- Object that should be checked.protected void writeAttribute(mxCodec enc, java.lang.Object obj, java.lang.String attr, java.lang.Object value, org.w3c.dom.Node node)
protected void writePrimitiveAttribute(mxCodec enc, java.lang.Object obj, java.lang.String attr, java.lang.Object value, org.w3c.dom.Node node)
protected void writeComplexAttribute(mxCodec enc, java.lang.Object obj, java.lang.String attr, java.lang.Object value, org.w3c.dom.Node node)
protected java.lang.Object convertValueToXml(java.lang.Object value)
protected java.lang.Object convertValueFromXml(java.lang.Class<?> type, java.lang.Object value)
protected java.lang.String getAttributeName(java.lang.String fieldname)
protected java.lang.String getFieldName(java.lang.String attributename)
attributename
- The attribute name to be mapped.protected java.lang.reflect.Field getField(java.lang.Object obj, java.lang.String fieldname)
protected java.lang.reflect.Method getAccessor(java.lang.Object obj, java.lang.reflect.Field field, boolean isGetter)
protected java.lang.reflect.Method getMethod(java.lang.Object obj, java.lang.String methodname, java.lang.Class[] params)
protected java.lang.Object getFieldValue(java.lang.Object obj, java.lang.String fieldname)
protected java.lang.Object getFieldValueWithAccessor(java.lang.Object obj, java.lang.reflect.Field field)
protected void setFieldValue(java.lang.Object obj, java.lang.String fieldname, java.lang.Object value)
protected void setFieldValueWithAccessor(java.lang.Object obj, java.lang.reflect.Field field, java.lang.Object value)
public java.lang.Object beforeEncode(mxCodec enc, java.lang.Object obj, org.w3c.dom.Node node)
enc
- Codec that controls the encoding process.obj
- Object to be encoded.node
- XML node to encode the object into.public org.w3c.dom.Node afterEncode(mxCodec enc, java.lang.Object obj, org.w3c.dom.Node node)
enc
- Codec that controls the encoding process.obj
- Object to be encoded.node
- XML node that represents the default encoding.public java.lang.Object decode(mxCodec dec, org.w3c.dom.Node node)
dec
- Codec that controls the encoding process.node
- XML node to be decoded.public java.lang.Object decode(mxCodec dec, org.w3c.dom.Node node, java.lang.Object into)
dec
- Codec that controls the encoding process.node
- XML node to be decoded.into
- Optional object to encode the node into.protected void decodeNode(mxCodec dec, org.w3c.dom.Node node, java.lang.Object obj)
protected void decodeAttributes(mxCodec dec, org.w3c.dom.Node node, java.lang.Object obj)
protected void decodeAttribute(mxCodec dec, org.w3c.dom.Node attr, java.lang.Object obj)
protected void decodeChildren(mxCodec dec, org.w3c.dom.Node node, java.lang.Object obj)
protected void decodeChild(mxCodec dec, org.w3c.dom.Node child, java.lang.Object obj)
protected java.lang.Object getFieldTemplate(java.lang.Object obj, java.lang.String fieldname, org.w3c.dom.Node child)
protected void addObjectValue(java.lang.Object obj, java.lang.String fieldname, java.lang.Object value, java.lang.Object template)
public boolean processInclude(mxCodec dec, org.w3c.dom.Node node, java.lang.Object into)
dec
- Codec that controls the encoding/decoding process.node
- XML node to be checked.into
- Optional object to pass-thru to the codec.public org.w3c.dom.Node beforeDecode(mxCodec dec, org.w3c.dom.Node node, java.lang.Object obj)
decode(mxCodec, Node)
. The object is created based on the
template in the calling method and is never null.
This implementation returns the input node. The return value of this
function is used in decode(mxCodec, Node)
to perform the
default decoding into the given object.dec
- Codec that controls the decoding process.node
- XML node to be decoded.obj
- Object to encode the node into.public java.lang.Object afterDecode(mxCodec dec, org.w3c.dom.Node node, java.lang.Object obj)
decode(mxCodec, Node)
.dec
- Codec that controls the decoding process.node
- XML node to be decoded.obj
- Object that represents the default decoding.Copyright (c) 2010 Gaudenz Alder, David Benson. All rights reserved.