forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxStylesheetCodec.php
More file actions
181 lines (153 loc) · 3.75 KB
/
mxStylesheetCodec.php
File metadata and controls
181 lines (153 loc) · 3.75 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
<?php
/**
* Copyright (c) 2006-2013, Gaudenz Alder
*/
class mxStylesheetCodec extends mxObjectCodec
{
/**
* Class: mxStylesheetCodec
*
* Codec for <mxStylesheets>. This class is created and registered
* dynamically at load time and used implicitely via <mxCodec>
* and the <mxCodecRegistry>.
*
* Constructor: mxObjectCodec
*
* Constructs a new codec for the specified template object.
* The variables in the optional exclude array are ignored by
* the codec. Variables in the optional idrefs array are
* turned into references in the XML. The optional mapping
* may be used to map from variable names to XML attributes.
*
* Parameters:
*
* template - Prototypical instance of the object to be
* encoded/decoded.
* exclude - Optional array of fieldnames to be ignored.
* idrefs - Optional array of fieldnames to be converted to/from
* references.
* mapping - Optional mapping from field- to attributenames.
*/
function mxStylesheetCodec($template)
{
parent::mxObjectCodec($template);
}
/**
* Override <mxObjectCodec.encode>.
*/
function encode($enc, $obj)
{
$node = $enc->document->createElement($this->getName());
foreach ($obj->styles as $i => $value)
{
$styleNode = $enc->document->createElement("add");
if (isset($i))
{
$styleNode->setAttribute("as", $i);
foreach ($style as $j => $value)
{
$value = $this->getStringValue($j, $value);
if (isset($value))
{
$entry = $enc->document->createElement("add");
$entry->setAttribute("value", $value);
$entry->setAttribute("as", $j);
$styleNode->appendChild($entry);
}
}
if ($styleNode->getChildCount() > 0)
{
$node->appendChild($styleNode);
}
}
}
return node;
}
/**
* Returns the string for encoding the given value.
*/
function getStringValue($key, $value)
{
return (!function_exists($value) && !is_object($value)) ? $value : null;
}
/**
* Override <mxObjectCodec.decode>.
*/
function decode($dec, $node, &$into = null)
{
$id = $node->getAttribute("id");
$obj = (in_array($id, $dec->objects)) ? $dec->objects[$id] : null;
if (!isset($obj))
{
if (isset($into))
{
$obj = $into;
}
else
{
$tmp = get_class($this->template);
$obj = new $tmp();
}
if (isset($id))
{
$dec->putObject($id, $obj);
}
}
$node = $node->firstChild;
while (isset($node))
{
if (!$this->processInclude($dec, $node, $obj) &&
$node->nodeName == "add")
{
$as = $node->getAttribute("as");
if (strlen($as) > 0)
{
$extend = $node->getAttribute("extend");
$style = (strlen($extend) > 0 &&
isset($obj->styles[$extend])) ?
array_slice($obj->styles[$extend], 0) :
null;
if (!isset($style))
{
$style = array();
}
$entry = $node->firstChild;
while (isset($entry))
{
if ($entry->nodeType == XML_ELEMENT_NODE)
{
$key = $entry->getAttribute("as");
if ($entry->nodeName == "add")
{
$text = $entry->textContent;
$value = null;
if (isset($text) && strlen($text) > 0)
{
$value = mxUtils::evaluate($text);
}
else
{
$value = $entry->getAttribute("value");
}
if ($value != null)
{
$style[$key] = $value;
}
}
else if ($entry->nodeName == "remove")
{
unset($style[$key]);
}
}
$entry = $entry->nextSibling;
}
$obj->putCellStyle($as, $style);
}
}
$node = $node->nextSibling;
}
return $obj;
}
}
mxCodecRegistry::register(new mxStylesheetCodec(new mxStylesheet()));
?>