com.mxgraph.io
Class mxObjectCodec
- java.lang.Object
-
- com.mxgraph.io.mxObjectCodec
-
- Direct Known Subclasses:
- mxCellCodec, mxChildChangeCodec, mxGenericChangeCodec, mxModelCodec, mxRootChangeCodec, mxStylesheetCodec, mxTerminalChangeCodec
public class mxObjectCodec extends java.lang.ObjectGeneric codec for Java objects. See below for a detailed description of the encoding/decoding scheme. Note: Since booleans are numbers in JavaScript, all boolean values are encoded into 1 for true and 0 for false.
-
-
Constructor Summary
Constructors 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description java.lang.ObjectafterDecode(mxCodec dec, org.w3c.dom.Node node, java.lang.Object obj)Hook for subclassers to post-process the object after decoding.org.w3c.dom.NodeafterEncode(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.NodebeforeDecode(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 bydecode(mxCodec, Node).java.lang.ObjectbeforeEncode(mxCodec enc, java.lang.Object obj, org.w3c.dom.Node node)Hook for subclassers to pre-process the object before encoding.java.lang.Objectdecode(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.Objectdecode(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.org.w3c.dom.Nodeencode(mxCodec enc, java.lang.Object obj)Encodes the specified object and returns a node representing then given object.java.lang.StringgetName()Returns the name used for the nodenames and lookup of the codec when classes are encoded and nodes are decoded.java.lang.ObjectgetTemplate()Returns the template object associated with this codec.booleanisExcluded(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.booleanisReference(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).booleanprocessInclude(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.
-
-
-
Constructor Detail
-
mxObjectCodec
public mxObjectCodec(java.lang.Object template)
Constructs a new codec for the specified template object.
-
mxObjectCodec
public 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. The variables in the optional exclude array are ignored by the codec. Variables in the optional idrefs array are turned into references in the XML. The optional mapping may be used to map from variable names to XML attributes. The argument is created as follows:- Parameters:
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.
-
-
Method Detail
-
getName
public java.lang.String getName()
Returns the name used for the nodenames and lookup of the codec when classes are encoded and nodes are decoded. For classes to work with this the codec registry automatically adds an alias for the classname if that is different than what this returns. The default implementation returns the classname of the template class. Here is an example on how to use this for renaming mxCell nodes:mxCodecRegistry.register(new mxCellCodec() { public String getName() { return "anotherName"; } });
-
getTemplate
public java.lang.Object getTemplate()
Returns the template object associated with this codec.- Returns:
- Returns the template object.
-
isExcluded
public 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. This implementation returns true if the given fieldname is inexclude.- Parameters:
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.- Returns:
- Returns true if the given attribute should be ignored.
-
isReference
public 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). This implementation returns true if the given fieldname is inidrefs.- Parameters:
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.- Returns:
- Returns true if the given attribute should be handled as a reference.
-
encode
public org.w3c.dom.Node encode(mxCodec enc, java.lang.Object obj)
Encodes the specified object and returns a node representing then given object. Calls beforeEncode after creating the node and afterEncode with the resulting node after processing. Enc is a reference to the calling encoder. It is used to encode complex objects and create references. This implementation encodes all variables of an object according to the following rules:- If the variable name is in
excludethen it is ignored. - If the variable name is in
idrefsthenmxCodec.getId(Object)is used to replace the object with its ID. - The variable name is mapped using
mapping. - If obj is an array and the variable name is numeric (ie. an index) then it is not encoded.
- If the value is an object, then the codec is used to create a child node with the variable name encoded into the "as" attribute.
- Else, if
mxCodec.isEncodeDefaults()is true or the value differs from the template value, then ...- ... if obj is not an array, then the value is mapped to an attribute.
- ... else if obj is an array, the value is mapped to an add child with a value attribute or a text child node, if the value is a function.
idrefsor if an object cannot be encoded, a warning is printed to System.err.- Parameters:
enc- Codec that controls the encoding process.obj- Object to be encoded.- Returns:
- Returns the resulting XML node that represents the given object.
- If the variable name is in
-
beforeEncode
public 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. This returns the input object. The return value of this function is used in encode to perform the default encoding into the given node.- Parameters:
enc- Codec that controls the encoding process.obj- Object to be encoded.node- XML node to encode the object into.- Returns:
- Returns the object to be encoded by the default encoding.
-
afterEncode
public 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. This implementation returns the input node. The return value of this method is returned to the encoder from. Parameters: - Parameters:
enc- Codec that controls the encoding process.obj- Object to be encoded.node- XML node that represents the default encoding.- Returns:
- Returns the resulting node of the encoding.
-
decode
public 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.- Parameters:
dec- Codec that controls the encoding process.node- XML node to be decoded.- Returns:
- Returns the resulting object that represents the given XML node.
-
decode
public 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. Dec is a reference to the calling decoder. It is used to decode complex objects and resolve references. If a node has an id attribute then the object cache is checked for the object. If the object is not yet in the cache then it is constructed using the constructor of and cached in. This implementation decodes all attributes and childs of a node according to the following rules: - If the variable name is in or if the attribute name is "id" or "as" then it is ignored. - If the variable name is in then is used to replace the reference with an object. - The variable name is mapped using a reverse . - If the value has a child node, then the codec is used to create a child object with the variable name taken from the "as" attribute. - If the object is an array and the variable name is empty then the value or child object is appended to the array. - If an add child has no value or the object is not an array then the child text content is evaluated using . If no object exists for an ID in a warning is issued in System.err. - Parameters:
dec- Codec that controls the encoding process.node- XML node to be decoded.into- Optional object to encode the node into.- Returns:
- Returns the resulting object that represents the given XML node or the object given to the method as the into parameter.
-
processInclude
public 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. Returns false if the given node is not an include directive.- Parameters:
dec- Codec that controls the encoding/decoding process.node- XML node to be checked.into- Optional object to pass-thru to the codec.- Returns:
- Returns true if the given node was processed as an include.
-
beforeDecode
public 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 bydecode(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 indecode(mxCodec, Node)to perform the default decoding into the given object.- Parameters:
dec- Codec that controls the decoding process.node- XML node to be decoded.obj- Object to encode the node into.- Returns:
- Returns the node used for the default decoding.
-
afterDecode
public 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. This implementation returns the given object without any changes. The return value of this method is returned to the decoder fromdecode(mxCodec, Node).- Parameters:
dec- Codec that controls the decoding process.node- XML node to be decoded.obj- Object that represents the default decoding.- Returns:
- Returns the result of the decoding process.
-
-
DataMelt 3.0 © DataMelt by jWork.ORG