mxGraph 2.4.0.0


com.mxgraph.io
Class mxCodec

java.lang.Object
  extended by com.mxgraph.io.mxCodec

public class mxCodec
extends Object

XML codec for Java object graphs. In order to resolve forward references when reading files the XML document that contains the data must be passed to the constructor.


Field Summary
protected  Document document
          Holds the owner document of the codec.
protected  boolean encodeDefaults
          Specifies if default values should be encoded.
protected  Map<String,Object> objects
          Maps from IDs to objects.
 
Constructor Summary
mxCodec()
          Constructs an XML encoder/decoder with a new owner document.
mxCodec(Document document)
          Constructs an XML encoder/decoder for the specified owner document.
 
Method Summary
 Object decode(Node node)
          Decodes the given XML node using decode(Node, Object).
 Object decode(Node node, Object into)
          Decodes the given XML node.
 mxICell decodeCell(Node node, boolean restoreStructures)
          Decodes cells that have been encoded using inversion, ie.
 Node encode(Object obj)
          Encodes the specified object and returns the resulting XML node.
 void encodeCell(mxICell cell, Node node, boolean includeChildren)
          Encoding of cell hierarchies is built-into the core, but is a higher-level function that needs to be explicitely used by the respective object encoders (eg.
 Document getDocument()
          Returns the owner document of the codec.
 Node getElementById(String id)
          Returns the element with the given ID from the document.
 Node getElementById(String id, String attr)
          Returns the element with the given ID from document.
 String getId(Object obj)
          Returns the ID of the specified object.
 Object getObject(String id)
          Returns the decoded object for the element with the specified ID in document.
 Map<String,Object> getObjects()
          Returns the object lookup table.
 void insertIntoGraph(mxICell cell)
          Inserts the given cell into its parent and terminal cells.
 boolean isEncodeDefaults()
          Returns if default values of member variables should be encoded.
 Object lookup(String id)
          Hook for subclassers to implement a custom lookup mechanism for cell IDs.
 Object putObject(String id, Object object)
          Assoiates the given object with the given ID.
 String reference(Object obj)
          Hook for subclassers to implement a custom method for retrieving IDs from objects.
static void setAttribute(Node node, String attribute, Object value)
          Sets the attribute on the specified node to value.
 void setDocument(Document value)
          Sets the owner document of the codec.
 void setEncodeDefaults(boolean encodeDefaults)
          Sets if default values of member variables should be encoded.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

document

protected Document document
Holds the owner document of the codec.


objects

protected Map<String,Object> objects
Maps from IDs to objects.


encodeDefaults

protected boolean encodeDefaults
Specifies if default values should be encoded. Default is false.

Constructor Detail

mxCodec

public mxCodec()
Constructs an XML encoder/decoder with a new owner document.


mxCodec

public mxCodec(Document document)
Constructs an XML encoder/decoder for the specified owner document.

Parameters:
document - Optional XML document that contains the data. If no document is specified then a new document is created using mxUtils.createDocument
Method Detail

getDocument

public Document getDocument()
Returns the owner document of the codec.

Returns:
Returns the owner document.

setDocument

public void setDocument(Document value)
Sets the owner document of the codec.


isEncodeDefaults

public boolean isEncodeDefaults()
Returns if default values of member variables should be encoded.


setEncodeDefaults

public void setEncodeDefaults(boolean encodeDefaults)
Sets if default values of member variables should be encoded.


getObjects

public Map<String,Object> getObjects()
Returns the object lookup table.


putObject

public Object putObject(String id,
                        Object object)
Assoiates the given object with the given ID.

Parameters:
id - ID for the object to be associated with.
object - Object to be associated with the ID.
Returns:
Returns the given object.

getObject

public Object getObject(String id)
Returns the decoded object for the element with the specified ID in document. If the object is not known then lookup(String) is used to find an object. If no object is found, then the element with the respective ID from the document is parsed using decode(Node).

Parameters:
id - ID of the object to be returned.
Returns:
Returns the object for the given ID.

lookup

public Object lookup(String id)
Hook for subclassers to implement a custom lookup mechanism for cell IDs. This implementation always returns null.

Parameters:
id - ID of the object to be returned.
Returns:
Returns the object for the given ID.

getElementById

public Node getElementById(String id)
Returns the element with the given ID from the document.

Parameters:
id - ID of the element to be returned.
Returns:
Returns the element for the given ID.

getElementById

public Node getElementById(String id,
                           String attr)
Returns the element with the given ID from document. The optional attr argument specifies the name of the ID attribute. Default is "id". The XPath expression used to find the element is //*[@attr='arg'] where attr is the name of the ID attribute and arg is the given id. Parameters: id - String that contains the ID. attr - Optional string for the attributename. Default is id.


getId

public String getId(Object obj)
Returns the ID of the specified object. This implementation calls reference first and if that returns null handles the object as an mxCell by returning their IDs using mxCell.getId. If no ID exists for the given cell, then an on-the-fly ID is generated using mxCellPath.create.

Parameters:
obj - Object to return the ID for.
Returns:
Returns the ID for the given object.

reference

public String reference(Object obj)
Hook for subclassers to implement a custom method for retrieving IDs from objects. This implementation always returns null.

Parameters:
obj - Object whose ID should be returned.
Returns:
Returns the ID for the given object.

encode

public Node encode(Object obj)
Encodes the specified object and returns the resulting XML node.

Parameters:
obj - Object to be encoded.
Returns:
Returns an XML node that represents the given object.

decode

public Object decode(Node node)
Decodes the given XML node using decode(Node, Object).

Parameters:
node - XML node to be decoded.
Returns:
Returns an object that represents the given node.

decode

public Object decode(Node node,
                     Object into)
Decodes the given XML node. The optional "into" argument specifies an existing object to be used. If no object is given, then a new instance is created using the constructor from the codec. The function returns the passed in object or the new instance if no object was given.

Parameters:
node - XML node to be decoded.
into - Optional object to be decodec into.
Returns:
Returns an object that represents the given node.

encodeCell

public void encodeCell(mxICell cell,
                       Node node,
                       boolean includeChildren)
Encoding of cell hierarchies is built-into the core, but is a higher-level function that needs to be explicitely used by the respective object encoders (eg. mxModelCodec, mxChildChangeCodec and mxRootChangeCodec). This implementation writes the given cell and its children as a (flat) sequence into the given node. The children are not encoded if the optional includeChildren is false. The function is in charge of adding the result into the given node and has no return value.

Parameters:
cell - mxCell to be encoded.
node - Parent XML node to add the encoded cell into.
includeChildren - Boolean indicating if the method should include all descendents.

decodeCell

public mxICell decodeCell(Node node,
                          boolean restoreStructures)
Decodes cells that have been encoded using inversion, ie. where the user object is the enclosing node in the XML, and restores the group and graph structure in the cells. Returns a new instance that represents the given node.

Parameters:
node - XML node that contains the cell data.
restoreStructures - Boolean indicating whether the graph structure should be restored by calling insert and insertEdge on the parent and terminals, respectively.
Returns:
Graph cell that represents the given node.

insertIntoGraph

public void insertIntoGraph(mxICell cell)
Inserts the given cell into its parent and terminal cells.


setAttribute

public static void setAttribute(Node node,
                                String attribute,
                                Object value)
Sets the attribute on the specified node to value. This is a helper method that makes sure the attribute and value arguments are not null.

Parameters:
node - XML node to set the attribute for.
attribute - Name of the attribute whose value should be set.
value - New value of the attribute.

mxGraph 2.4.0.0


Copyright (c) 2010 Gaudenz Alder, David Benson. All rights reserved.