Generic codec for JavaScript objects that implements a mapping between JavaScript objects and XML nodes that maps each field or element to an attribute or child node, and vice versa.
Consider the following example.
var obj = new Object(); obj.foo = "Foo"; obj.bar = "Bar";
This object is encoded into an XML node using the following.
var enc = new mxCodec(); var node = enc.encode(obj);
The output of the encoding may be viewed using mxLog as follows.
mxLog.show(); mxLog.debug(mxUtils.getPrettyXml(node));
Finally, the result of the encoding looks as follows.
<Object foo="Foo" bar="Bar"/>
In the above output, the foo and bar fields have been mapped to attributes with the same names, and the name of the constructor was used for the nodename.
Since booleans are numbers in JavaScript, all boolean values are encoded into 1 for true and 0 for false. The decoder also accepts the string true and false for boolean values.
The above scheme is applied to all atomic fields, that is, to all non-object fields of an object. For object fields, a child node is created with a special attribute that contains the fieldname. This special attribute is called “as” and hence, as is a reserved word that should not be used for a fieldname.
Consider the following example where foo is an object and bar is an atomic property of foo.
var obj = {foo: {bar: "Bar"}};
This will be mapped to the following XML structure by mxObjectCodec.
<Object> <Object bar="Bar" as="foo"/> </Object>
In the above output, the inner Object node contains the as-attribute that specifies the fieldname in the enclosing object. That is, the field foo was mapped to a child node with an as-attribute that has the value foo.
Arrays are special objects that are either associative, in which case each key, value pair is treated like a field where the key is the fieldname, or they are a sequence of atomic values and objects, which is mapped to a sequence of child nodes. For object elements, the above scheme is applied without the use of the special as-attribute for creating each child. For atomic elements, a special add-node is created with the value stored in the value-attribute.
For example, the following array contains one atomic value and one object with a field called bar. Furthermore it contains two associative entries called bar with an atomic value, and foo with an object value.
var obj = ["Bar", {bar: "Bar"}]; obj["bar"] = "Bar"; obj["foo"] = {bar: "Bar"};
This array is represented by the following XML nodes.
<Array bar="Bar"> <add value="Bar"/> <Object bar="Bar"/> <Object bar="Bar" as="foo"/> </Array>
The Array node name is the name of the constructor. The additional as-attribute in the last child contains the key of the associative entry, whereas the second last child is part of the array sequence and does not have an as-attribute.
Objects may be represented as child nodes or attributes with ID values, which are used to lookup the object in a table within mxCodec. The isReference function is in charge of deciding if a specific field should be encoded as a reference or not. Its default implementation returns true if the fieldname is in idrefs, an array of strings that is used to configure the mxObjectCodec.
Using this approach, the mapping does not guarantee that the referenced object itself exists in the document. The fields that are encoded as references must be carefully chosen to make sure all referenced objects exist in the document, or may be resolved by some other means if necessary.
For example, in the case of the graph model all cells are stored in a tree whose root is referenced by the model’s root field. A tree is a structure that is well suited for an XML representation, however, the additional edges in the graph model have a reference to a source and target cell, which are also contained in the tree. To handle this case, the source and target cell of an edge are treated as references, whereas the children are treated as objects. Since all cells are contained in the tree and no edge references a source or target outside the tree, this setup makes sure all referenced objects are contained in the document.
In the case of a tree structure we must further avoid infinite recursion by ignoring the parent reference of each child. This is done by returning true in isExcluded, whose default implementation uses the array of excluded fieldnames passed to the mxObjectCodec constructor.
References are only used for cells in mxGraph. For defining other referencable object types, the codec must be able to work out the ID of an object. This is done by implementing mxCodec.reference. For decoding a reference, the XML node with the respective id-attribute is fetched from the document, decoded, and stored in a lookup table for later reference. For looking up external objects, mxCodec.lookup may be implemented.
For decoding JavaScript expressions, the add-node may be used with a text content that contains the JavaScript expression. For example, the following creates a field called foo in the enclosing object and assigns it the value of mxConstants.ALIGN_LEFT.
<Object> <add as="foo">mxConstants.ALIGN_LEFT</add> </Object>
The resulting object has a field called foo with the value “left”. Its XML representation looks as follows.
<Object foo="left"/>
This means the expression is evaluated at decoding time and the result of the evaluation is stored in the respective field. Valid expressions are all JavaScript expressions, including function definitions, which are mapped to functions on the resulting object.
Expressions are only evaluated if allowEval is true.
mxObjectCodec | Generic codec for JavaScript objects that implements a mapping between JavaScript objects and XML nodes that maps each field or element to an attribute or child node, and vice versa. |
Functions | |
mxObjectCodec | Constructs a new codec for the specified template object. |
Variables | |
allowEval | Static global switch that specifies if expressions in arrays are allowed. |
template | Holds the template object associated with this codec. |
exclude | Array containing the variable names that should be ignored by the codec. |
idrefs | Array containing the variable names that should be turned into or converted from references. |
mapping | Maps from from fieldnames to XML attribute names. |
reverse | Maps from from XML attribute names to fieldnames. |
Functions | |
getName | Returns the name used for the nodenames and lookup of the codec when classes are encoded and nodes are decoded. |
cloneTemplate | Returns a new instance of the template for this codec. |
getFieldName | Returns the fieldname for the given attributename. |
getAttributeName | Returns the attributename for the given fieldname. |
isExcluded | Returns true if the given attribute is to be ignored by the codec. |
isReference | Returns true if the given fieldname is to be treated as a textual reference (ID). |
encode | Encodes the specified object and returns a node representing then given object. |
encodeObject | Encodes the value of each member in then given obj into the given node using encodeValue. |
encodeValue | Converts the given value according to the mappings and id-refs in this codec and uses writeAttribute to write the attribute into the given node. |
writeAttribute | Writes the given value into node using writePrimitiveAttribute or writeComplexAttribute depending on the type of the value. |
writePrimitiveAttribute | Writes the given value as an attribute of the given node. |
writeComplexAttribute | Writes the given value as a child node of the given node. |
convertAttributeToXml | Converts true to “1” and false to “0” is isBooleanAttribute returns true. |
isBooleanAttribute | Returns true if the given object attribute is a boolean value. |
convertAttributeFromXml | Converts booleans and numeric values to the respective types. |
isNumericAttribute | Returns true if the given XML attribute is a numeric value. |
beforeEncode | Hook for subclassers to pre-process the object before encoding. |
afterEncode | Hook for subclassers to post-process the node for the given object after encoding and return the post-processed node. |
decode | Parses the given node into the object or returns a new object representing the given node. |
decodeNode | Calls decodeAttributes and decodeChildren for the given node. |
decodeAttributes | Decodes all attributes of the given node using decodeAttribute. |
isIgnoredAttribute | Returns true if the given attribute should be ignored. |
decodeAttribute | Reads the given attribute into the specified object. |
decodeChildren | Decodes all children of the given node using decodeChild. |
decodeChild | Reads the specified child into the given object. |
getFieldTemplate | Returns the template instance for the given field. |
addObjectValue | Sets the decoded child node as a value of the given object. |
processInclude | Returns true if the given node is an include directive and executes the include by decoding the XML document. |
beforeDecode | Hook for subclassers to pre-process the node for the specified object and return the node to be used for further processing by decode. |
afterDecode | Hook for subclassers to post-process the object after decoding. |
function mxObjectCodec( template, exclude, idrefs, 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:
var mapping = new Object(); mapping['variableName'] = 'attribute-name';
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. |
mxObjectCodec.prototype.idrefs
Array containing the variable names that should be turned into or converted from references. See mxCodec.getId and mxCodec.getObject.
mxObjectCodec.prototype.getName = function()
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.
mxObjectCodec.prototype.getFieldName = function( attributename )
Returns the fieldname for the given attributename. Looks up the value in the reverse mapping or returns the input if there is no reverse mapping for the given name.
mxObjectCodec.prototype.getAttributeName = function( fieldname )
Returns the attributename for the given fieldname. Looks up the value in the mapping or returns the input if there is no mapping for the given name.
mxObjectCodec.prototype.isExcluded = function( obj, attr, value, write )
Returns true if the given attribute is to be ignored by the codec. This implementation returns true if the given fieldname is in exclude or if the fieldname equals mxObjectIdentity.FIELD_NAME.
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. |
mxObjectCodec.prototype.isReference = function( obj, attr, value, write )
Returns true if the given fieldname is to be treated as a textual reference (ID). This implementation returns true if the given fieldname is in idrefs.
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. |
mxObjectCodec.prototype.encode = function( enc, 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 no ID exists for a variable in idrefs or if an object cannot be encoded, a warning is issued using mxLog.warn.
Returns the resulting XML node that represents the given object.
enc | mxCodec that controls the encoding process. |
obj | Object to be encoded. |
mxObjectCodec.prototype.encodeObject = function( enc, obj, node )
Encodes the value of each member in then given obj into the given node using encodeValue.
enc | mxCodec that controls the encoding process. |
obj | Object to be encoded. |
node | XML node that contains the encoded object. |
mxObjectCodec.prototype.encodeValue = function( enc, obj, name, value, node )
Converts the given value according to the mappings and id-refs in this codec and uses writeAttribute to write the attribute into the given node.
enc | mxCodec that controls the encoding process. |
obj | Object whose property is going to be encoded. |
name | XML node that contains the encoded object. |
value | Value of the property to be encoded. |
node | XML node that contains the encoded object. |
mxObjectCodec.prototype.writeAttribute = function( enc, obj, name, value, node )
Writes the given value into node using writePrimitiveAttribute or writeComplexAttribute depending on the type of the value.
mxObjectCodec.prototype.convertAttributeToXml = function( enc, obj, name, value )
Converts true to “1” and false to “0” is isBooleanAttribute returns true. All other values are not converted.
enc | mxCodec that controls the encoding process. |
obj | Objec to convert the attribute for. |
name | Name of the attribute to be converted. |
value | Value to be converted. |
mxObjectCodec.prototype.isBooleanAttribute = function( enc, obj, name, value )
Returns true if the given object attribute is a boolean value.
enc | mxCodec that controls the encoding process. |
obj | Objec to convert the attribute for. |
name | Name of the attribute to be converted. |
value | Value of the attribute to be converted. |
mxObjectCodec.prototype.convertAttributeFromXml = function( dec, attr, obj )
Converts booleans and numeric values to the respective types. Values are numeric if isNumericAttribute returns true.
dec | mxCodec that controls the decoding process. |
attr | XML attribute to be converted. |
obj | Objec to convert the attribute for. |
mxObjectCodec.prototype.isNumericAttribute = function( dec, attr, obj )
Returns true if the given XML attribute is a numeric value.
dec | mxCodec that controls the decoding process. |
attr | XML attribute to be converted. |
obj | Objec to convert the attribute for. |
mxObjectCodec.prototype.beforeEncode = function( enc, obj, 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.
enc | mxCodec that controls the encoding process. |
obj | Object to be encoded. |
node | XML node to encode the object into. |
mxObjectCodec.prototype.afterEncode = function( enc, obj, 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 encode.
enc | mxCodec that controls the encoding process. |
obj | Object to be encoded. |
node | XML node that represents the default encoding. |
mxObjectCodec.prototype.decode = function( dec, node, 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 template and cached in mxCodec.objects.
This implementation decodes all attributes and childs of a node according to the following rules:
For add nodes where the object is not an array and the variable name is defined, the default mechanism is used, allowing to override/add methods as follows:
<Object> <add as="hello"><![CDATA[ function(arg1) { mxUtils.alert('Hello '+arg1); } ]]></add> </Object>
If no object exists for an ID in idrefs a warning is issued using mxLog.warn.
Returns the resulting object that represents the given XML node or the object given to the method as the into parameter.
dec | mxCodec that controls the decoding process. |
node | XML node to be decoded. |
into | Optional objec to encode the node into. |
mxObjectCodec.prototype.decodeNode = function( dec, node, obj )
Calls decodeAttributes and decodeChildren for the given node.
dec | mxCodec that controls the decoding process. |
node | XML node to be decoded. |
obj | Objec to encode the node into. |
mxObjectCodec.prototype.decodeAttributes = function( dec, node, obj )
Decodes all attributes of the given node using decodeAttribute.
dec | mxCodec that controls the decoding process. |
node | XML node to be decoded. |
obj | Objec to encode the node into. |
mxObjectCodec.prototype.isIgnoredAttribute = function( dec, attr, obj )
Returns true if the given attribute should be ignored. This implementation returns true if the attribute name is “as” or “id”.
dec | mxCodec that controls the decoding process. |
attr | XML attribute to be decoded. |
obj | Objec to encode the attribute into. |
mxObjectCodec.prototype.decodeAttribute = function( dec, attr, obj )
Reads the given attribute into the specified object.
dec | mxCodec that controls the decoding process. |
attr | XML attribute to be decoded. |
obj | Objec to encode the attribute into. |
mxObjectCodec.prototype.decodeChildren = function( dec, node, obj )
Decodes all children of the given node using decodeChild.
dec | mxCodec that controls the decoding process. |
node | XML node to be decoded. |
obj | Objec to encode the node into. |
mxObjectCodec.prototype.decodeChild = function( dec, child, obj )
Reads the specified child into the given object.
dec | mxCodec that controls the decoding process. |
child | XML child element to be decoded. |
obj | Objec to encode the node into. |
mxObjectCodec.prototype.getFieldTemplate = function( obj, fieldname, child )
Returns the template instance for the given field. This returns the value of the field, null if the value is an array or an empty collection if the value is a collection. The value is then used to populate the field for a new instance. For strongly typed languages it may be required to override this to return the correct collection instance based on the encoded child.
mxObjectCodec.prototype.addObjectValue = function( obj, fieldname, value, template )
Sets the decoded child node as a value of the given object. If the object is a map, then the value is added with the given fieldname as a key. If the fieldname is not empty, then setFieldValue is called or else, if the object is a collection, the value is added to the collection. For strongly typed languages it may be required to override this with the correct code to add an entry to an object.
mxObjectCodec.prototype.processInclude = function( dec, node, 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.
dec | mxCodec that controls the encoding/decoding process. |
node | XML node to be checked. |
into | Optional object to pass-thru to the codec. |
mxObjectCodec.prototype.beforeDecode = function( dec, node, 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. 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 to perform the default decoding into the given object.
dec | mxCodec that controls the decoding process. |
node | XML node to be decoded. |
obj | Object to encode the node into. |
mxObjectCodec.prototype.afterDecode = function( dec, node, 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 from decode.
enc | mxCodec that controls the encoding process. |
node | XML node to be decoded. |
obj | Object that represents the default decoding. |
Constructs a new codec for the specified template object.
function mxObjectCodec( template, exclude, idrefs, mapping )
Static global switch that specifies if expressions in arrays are allowed.
mxObjectCodec.allowEval
Holds the template object associated with this codec.
mxObjectCodec.prototype.template
Array containing the variable names that should be ignored by the codec.
mxObjectCodec.prototype.exclude
Array containing the variable names that should be turned into or converted from references.
mxObjectCodec.prototype.idrefs
Maps from from fieldnames to XML attribute names.
mxObjectCodec.prototype.mapping
Maps from from XML attribute names to fieldnames.
mxObjectCodec.prototype.reverse
Returns the name used for the nodenames and lookup of the codec when classes are encoded and nodes are decoded.
mxObjectCodec.prototype.getName = function()
Returns a new instance of the template for this codec.
mxObjectCodec.prototype.cloneTemplate = function()
Returns the fieldname for the given attributename.
mxObjectCodec.prototype.getFieldName = function( attributename )
Returns the attributename for the given fieldname.
mxObjectCodec.prototype.getAttributeName = function( fieldname )
Returns true if the given attribute is to be ignored by the codec.
mxObjectCodec.prototype.isExcluded = function( obj, attr, value, write )
Returns true if the given fieldname is to be treated as a textual reference (ID).
mxObjectCodec.prototype.isReference = function( obj, attr, value, write )
Encodes the specified object and returns a node representing then given object.
mxObjectCodec.prototype.encode = function( enc, obj )
Encodes the value of each member in then given obj into the given node using encodeValue.
mxObjectCodec.prototype.encodeObject = function( enc, obj, node )
Converts the given value according to the mappings and id-refs in this codec and uses writeAttribute to write the attribute into the given node.
mxObjectCodec.prototype.encodeValue = function( enc, obj, name, value, node )
Writes the given value into node using writePrimitiveAttribute or writeComplexAttribute depending on the type of the value.
mxObjectCodec.prototype.writeAttribute = function( enc, obj, name, value, node )
Writes the given value as an attribute of the given node.
mxObjectCodec.prototype.writePrimitiveAttribute = function( enc, obj, name, value, node )
Writes the given value as a child node of the given node.
mxObjectCodec.prototype.writeComplexAttribute = function( enc, obj, name, value, node )
Converts true to “1” and false to “0” is isBooleanAttribute returns true.
mxObjectCodec.prototype.convertAttributeToXml = function( enc, obj, name, value )
Returns true if the given object attribute is a boolean value.
mxObjectCodec.prototype.isBooleanAttribute = function( enc, obj, name, value )
Converts booleans and numeric values to the respective types.
mxObjectCodec.prototype.convertAttributeFromXml = function( dec, attr, obj )
Returns true if the given XML attribute is a numeric value.
mxObjectCodec.prototype.isNumericAttribute = function( dec, attr, obj )
Hook for subclassers to pre-process the object before encoding.
mxObjectCodec.prototype.beforeEncode = function( enc, obj, node )
Hook for subclassers to post-process the node for the given object after encoding and return the post-processed node.
mxObjectCodec.prototype.afterEncode = function( enc, obj, node )
Parses the given node into the object or returns a new object representing the given node.
mxObjectCodec.prototype.decode = function( dec, node, into )
Calls decodeAttributes and decodeChildren for the given node.
mxObjectCodec.prototype.decodeNode = function( dec, node, obj )
Decodes all attributes of the given node using decodeAttribute.
mxObjectCodec.prototype.decodeAttributes = function( dec, node, obj )
Decodes all children of the given node using decodeChild.
mxObjectCodec.prototype.decodeChildren = function( dec, node, obj )
Reads the given attribute into the specified object.
mxObjectCodec.prototype.decodeAttribute = function( dec, attr, obj )
Returns true if the given attribute should be ignored.
mxObjectCodec.prototype.isIgnoredAttribute = function( dec, attr, obj )
Reads the specified child into the given object.
mxObjectCodec.prototype.decodeChild = function( dec, child, obj )
Returns the template instance for the given field.
mxObjectCodec.prototype.getFieldTemplate = function( obj, fieldname, child )
Sets the decoded child node as a value of the given object.
mxObjectCodec.prototype.addObjectValue = function( obj, fieldname, value, template )
Returns true if the given node is an include directive and executes the include by decoding the XML document.
mxObjectCodec.prototype.processInclude = function( dec, node, into )
Hook for subclassers to pre-process the node for the specified object and return the node to be used for further processing by decode.
mxObjectCodec.prototype.beforeDecode = function( dec, node, obj )
Hook for subclassers to post-process the object after decoding.
mxObjectCodec.prototype.afterDecode = function( dec, node, obj )
Hook for subclassers to implement a custom method for retrieving IDs from objects.
mxCodec.prototype.reference = function( obj )
Hook for subclassers to implement a custom lookup mechanism for cell IDs.
mxCodec.prototype.lookup = function( id )
Returns the ID of the specified object.
mxCodec.prototype.getId = function( obj )
Returns the decoded object for the element with the specified ID in document.
mxCodec.prototype.getObject = function( id )
Adds all arguments to the console if WARN is enabled.
warn: function()
Maps from IDs to objects.
mxCodec.prototype.objects
Evaluates the given expression using eval and returns the JavaScript object that represents the expression result.
eval: function( expr )