forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxModelCodec.java
More file actions
117 lines (98 loc) · 2.43 KB
/
mxModelCodec.java
File metadata and controls
117 lines (98 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Copyright (c) 2006-2013, Gaudenz Alder, David Benson
*/
package com.mxgraph.io;
import java.util.Map;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.model.mxICell;
/**
* Codec for mxGraphModels. This class is created and registered
* dynamically at load time and used implicitly via mxCodec
* and the mxCodecRegistry.
*/
public class mxModelCodec extends mxObjectCodec
{
/**
* Constructs a new model codec.
*/
public mxModelCodec()
{
this(new mxGraphModel());
}
/**
* Constructs a new model codec for the given template.
*/
public mxModelCodec(Object template)
{
this(template, null, null, null);
}
/**
* Constructs a new model codec for the given arguments.
*/
public mxModelCodec(Object template, String[] exclude, String[] idrefs,
Map<String, String> mapping)
{
super(template, exclude, idrefs, mapping);
}
/**
* Encodes the given mxGraphModel by writing a (flat) XML sequence
* of cell nodes as produced by the mxCellCodec. The sequence is
* wrapped-up in a node with the name root.
*/
protected void encodeObject(mxCodec enc, Object obj, Node node)
{
if (obj instanceof mxGraphModel)
{
Node rootNode = enc.document.createElement("root");
mxGraphModel model = (mxGraphModel) obj;
enc.encodeCell((mxICell) model.getRoot(), rootNode, true);
node.appendChild(rootNode);
}
}
/**
* Reads the cells into the graph model. All cells are children of the root
* element in the node.
*/
public Node beforeDecode(mxCodec dec, Node node, Object into)
{
if (node instanceof Element)
{
Element elt = (Element) node;
mxGraphModel model = null;
if (into instanceof mxGraphModel)
{
model = (mxGraphModel) into;
}
else
{
model = new mxGraphModel();
}
// Reads the cells into the graph model. All cells
// are children of the root element in the node.
Node root = elt.getElementsByTagName("root").item(0);
mxICell rootCell = null;
if (root != null)
{
Node tmp = root.getFirstChild();
while (tmp != null)
{
mxICell cell = dec.decodeCell(tmp, true);
if (cell != null && cell.getParent() == null)
{
rootCell = cell;
}
tmp = tmp.getNextSibling();
}
root.getParentNode().removeChild(root);
}
// Sets the root on the model if one has been decoded
if (rootCell != null)
{
model.setRoot(rootCell);
}
}
return node;
}
}