Package com.mxgraph.io
Class mxCodec
java.lang.Object
com.mxgraph.io.mxCodec
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
FieldsModifier and TypeFieldDescriptionprotected Document
Holds the owner document of the codec.Maps from IDs to elements.protected boolean
Specifies if default values should be encoded.Maps from IDs to objects. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addElement
(Node node) Adds the given element toif it has an ID. Decodes the given XML node usingdecode(Node, Object)
.Decodes the given XML node.decodeCell
(Node node, boolean restoreStructures) Decodes cells that have been encoded using inversion, ie.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.Returns the owner document of the codec.getElementById
(String id) Returns the element with the given ID from the document.Returns the ID of the specified object.Returns the decoded object for the element with the specified ID indocument
.Returns the object lookup table.void
insertIntoGraph
(mxICell cell) Inserts the given cell into its parent and terminal cells.boolean
Returns if default values of member variables should be encoded.Hook for subclassers to implement a custom lookup mechanism for cell IDs.Assoiates the given object with the given ID.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.
-
Field Details
-
document
Holds the owner document of the codec. -
objects
Maps from IDs to objects. -
elements
Maps from IDs to elements. -
encodeDefaults
protected boolean encodeDefaultsSpecifies if default values should be encoded. Default is false.
-
-
Constructor Details
-
mxCodec
public mxCodec()Constructs an XML encoder/decoder with a new owner document. -
mxCodec
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 Details
-
getDocument
Returns the owner document of the codec.- Returns:
- Returns the owner document.
-
setDocument
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
Returns the object lookup table. -
putObject
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
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
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
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
Adds the given element toif it has an ID. -
getId
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
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
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
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
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
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
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
Inserts the given cell into its parent and terminal cells. -
setAttribute
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.
-