forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxCellCodec.cs
More file actions
207 lines (173 loc) · 7.35 KB
/
mxCellCodec.cs
File metadata and controls
207 lines (173 loc) · 7.35 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) 2007-2008, Gaudenz Alder
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace com.mxgraph
{
/// <summary>
/// Codec for mxCells. This class is created and registered
/// dynamically at load time and used implicitely via mxCodec
/// and the mxCodecRegistry.
/// </summary>
public class mxCellCodec : mxObjectCodec
{
/// <summary>
/// Constructs a new cell codec.
/// </summary>
public mxCellCodec()
: this(new mxCell(), new string[] { "children", "edges" }, new string[] { "parent", "source", "target" },
null) { }
/// <summary>
/// Constructs a new cell codec for the given template.
/// </summary>
public mxCellCodec(Object template) : this(template, null, null, null) { }
/// <summary>
/// Constructs a new cell codec for the given arguments.
/// </summary>
public mxCellCodec(Object template, string[] exclude, string[] idrefs,
Dictionary<string, string> mapping)
: base(template, exclude, idrefs, mapping) { }
/// <summary>
/// Excludes user objects that are XML nodes.
/// </summary>
public override bool IsExcluded(Object obj, string attr, Object value,
bool isWrite)
{
return exclude.Contains(attr)
|| (isWrite && attr.Equals("value") && value is XmlNode && ((XmlNode)value)
.NodeType == XmlNodeType.Element);
}
/// <summary>
/// Encodes an mxCell and wraps the XML up inside the
/// XML of the user object (inversion).
/// </summary>
public override XmlNode AfterEncode(mxCodec enc, Object obj, XmlNode node)
{
if (obj is mxCell && node is XmlElement)
{
mxCell cell = (mxCell)obj;
if (cell.Value != null)
{
if (cell.Value is XmlNode)
{
// Wraps the graphical annotation up in the
// user object (inversion) by putting the
// result of the default encoding into
// a clone of the user object (node type 1)
// and returning this cloned user object.
XmlElement tmp = (XmlElement)node;
node = enc.Document.ImportNode((XmlNode)cell.Value, true);
node.AppendChild(tmp);
// Moves the id attribute to the outermost
// XML node, namely the node which denotes
// the object boundaries in the file.
String id = tmp.GetAttribute("id");
((XmlElement)node).SetAttribute("id", id);
tmp.RemoveAttribute("id");
}
}
}
return node;
}
/// <summary>
/// Decodes an mxCell and uses the enclosing XML node as
/// the user object for the cell (inversion).
/// </summary>
public override XmlNode BeforeDecode(mxCodec dec, XmlNode node, Object obj)
{
XmlElement inner = (XmlElement)node;
if (obj is mxCell)
{
mxCell cell = (mxCell)obj;
String classname = GetName();
if (!node.Name.Equals(classname))
{
// Passes the inner graphical annotation node to the
// object codec for further processing of the cell.
XmlNode tmp = inner.GetElementsByTagName(classname)[0];
if (tmp != null && tmp.ParentNode == node)
{
inner = (XmlElement)tmp;
// Removes annotation and whitespace from node
XmlNode tmp2 = tmp.PreviousSibling;
while (tmp2 != null && tmp2.NodeType == XmlNodeType.Text)
{
XmlNode tmp3 = tmp2.PreviousSibling;
if (tmp2.Value.Trim().Length == 0)
{
tmp2.ParentNode.RemoveChild(tmp2);
}
tmp2 = tmp3;
}
// Removes more whitespace
tmp2 = tmp.NextSibling;
while (tmp2 != null && tmp2.NodeType == XmlNodeType.Text)
{
XmlNode tmp3 = tmp2.PreviousSibling;
if (tmp2.Value.Trim().Length == 0)
{
tmp2.ParentNode.RemoveChild(tmp2);
}
tmp2 = tmp3;
}
tmp.ParentNode.RemoveChild(tmp);
}
else
{
inner = null;
}
// Creates the user object out of the XML node
XmlElement value = (XmlElement)node.CloneNode(true);
cell.Value = value;
String id = value.GetAttribute("id");
if (id != null)
{
cell.Id = id;
value.RemoveAttribute("id");
}
}
else
{
cell.Id = ((XmlElement)node).GetAttribute("id");
}
// Preprocesses and removes all Id-references
// in order to use the correct encoder (this)
// for the known references to cells (all).
if (inner != null && idrefs != null)
{
foreach (string attr in idrefs)
{
string rf = inner.GetAttribute(attr);
if (rf != null && rf.Length > 0)
{
inner.RemoveAttribute(attr);
Object tmp = (dec.Objects.ContainsKey(rf)) ? dec.Objects[rf] : null;
if (tmp == null)
{
tmp = dec.Lookup(rf);
}
if (tmp == null)
{
// Needs to decode forward reference
XmlNode element = dec.GetElementById(rf);
if (element != null)
{
mxObjectCodec decoder = mxCodecRegistry
.GetCodec(element.Name);
if (decoder == null)
{
decoder = this;
}
tmp = decoder.Decode(dec, element);
}
}
SetFieldValue(obj, attr, tmp);
}
}
}
}
return inner;
}
}
}