Package com.mxgraph.io
Class mxCodec
- java.lang.Object
-
- com.mxgraph.io.mxCodec
-
public class mxCodec extends java.lang.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
Fields Modifier and Type Field Description protected org.w3c.dom.Document
document
Holds the owner document of the codec.protected java.util.Map<java.lang.String,org.w3c.dom.Node>
elements
Maps from IDs to elements.protected boolean
encodeDefaults
Specifies if default values should be encoded.protected java.util.Map<java.lang.String,java.lang.Object>
objects
Maps from IDs to objects.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addElement(org.w3c.dom.Node node)
Adds the given element toif it has an ID. java.lang.Object
decode(org.w3c.dom.Node node)
Decodes the given XML node usingdecode(Node, Object)
.java.lang.Object
decode(org.w3c.dom.Node node, java.lang.Object into)
Decodes the given XML node.mxICell
decodeCell(org.w3c.dom.Node node, boolean restoreStructures)
Decodes cells that have been encoded using inversion, ie.org.w3c.dom.Node
encode(java.lang.Object obj)
Encodes the specified object and returns the resulting XML node.void
encodeCell(mxICell cell, org.w3c.dom.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.org.w3c.dom.Document
getDocument()
Returns the owner document of the codec.org.w3c.dom.Node
getElementById(java.lang.String id)
Returns the element with the given ID from the document.java.lang.String
getId(java.lang.Object obj)
Returns the ID of the specified object.java.lang.Object
getObject(java.lang.String id)
Returns the decoded object for the element with the specified ID indocument
.java.util.Map<java.lang.String,java.lang.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.java.lang.Object
lookup(java.lang.String id)
Hook for subclassers to implement a custom lookup mechanism for cell IDs.java.lang.Object
putObject(java.lang.String id, java.lang.Object object)
Assoiates the given object with the given ID.java.lang.String
reference(java.lang.Object obj)
Hook for subclassers to implement a custom method for retrieving IDs from objects.static void
setAttribute(org.w3c.dom.Node node, java.lang.String attribute, java.lang.Object value)
Sets the attribute on the specified node to value.void
setDocument(org.w3c.dom.Document value)
Sets the owner document of the codec.void
setEncodeDefaults(boolean encodeDefaults)
Sets if default values of member variables should be encoded.
-
-
-
Field Detail
-
document
protected org.w3c.dom.Document document
Holds the owner document of the codec.
-
objects
protected java.util.Map<java.lang.String,java.lang.Object> objects
Maps from IDs to objects.
-
elements
protected java.util.Map<java.lang.String,org.w3c.dom.Node> elements
Maps from IDs to elements.
-
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(org.w3c.dom.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 org.w3c.dom.Document getDocument()
Returns the owner document of the codec.- Returns:
- Returns the owner document.
-
setDocument
public void setDocument(org.w3c.dom.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 java.util.Map<java.lang.String,java.lang.Object> getObjects()
Returns the object lookup table.
-
putObject
public java.lang.Object putObject(java.lang.String id, java.lang.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 java.lang.Object getObject(java.lang.String id)
Returns the decoded object for the element with the specified ID indocument
. If the object is not known thenlookup(String)
is used to find an object. If no object is found, then the element with the respective ID from the document is parsed usingdecode(Node)
.- Parameters:
id
- ID of the object to be returned.- Returns:
- Returns the object for the given ID.
-
lookup
public java.lang.Object lookup(java.lang.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 org.w3c.dom.Node getElementById(java.lang.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.
-
addElement
protected void addElement(org.w3c.dom.Node node)
Adds the given element toif it has an ID.
-
getId
public java.lang.String getId(java.lang.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 java.lang.String reference(java.lang.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 org.w3c.dom.Node encode(java.lang.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 java.lang.Object decode(org.w3c.dom.Node node)
Decodes the given XML node usingdecode(Node, Object)
.- Parameters:
node
- XML node to be decoded.- Returns:
- Returns an object that represents the given node.
-
decode
public java.lang.Object decode(org.w3c.dom.Node node, java.lang.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, org.w3c.dom.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(org.w3c.dom.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 newinstance 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(org.w3c.dom.Node node, java.lang.String attribute, java.lang.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.
-
-