forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxObjectCodec.php
More file actions
827 lines (746 loc) · 20.5 KB
/
mxObjectCodec.php
File metadata and controls
827 lines (746 loc) · 20.5 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
<?php
/**
* $Id: mxObjectCodec.php,v 1.1 2012/11/15 13:26:46 gaudenz Exp $
* Copyright (c) 2006-2010, Gaudenz Alder
*/
class mxObjectCodec
{
/**
* Class: mxObjectCodec
*
* XML codec for PHP object graphs.
*
* Implementation note:
*
* The passing of the argument by reference in <decode>, <decodeNode>,
* <decodeAttributes>, <decodeAttribute>, <decodeChildren>, <decodeChild>,
* <beforeDecode> and <afterDecode> is required since the object may be an
* array instance, which needs an explicit reference operator even in PHP 5
* to be changed in-place.
*
* Variable: template
*
* Holds the template object associated with this codec.
*/
var $template;
/**
* Variable: exclude
*
* Array containing the variable names that should be
* ignored by the codec.
*/
var $exclude;
/**
* Variable: idrefs
*
* Array containing the variable names that should be
* turned into or converted from references. See
* <mxCodec.getId> and <mxCodec.getObject>.
*/
var $idrefs;
/**
* Variable: mapping
*
* Maps from from fieldnames to XML attribute names.
*/
var $mapping;
/**
* Variable: reverse
*
* Maps from from XML attribute names to fieldnames.
*/
var $reverse;
/**
* 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 mxObjectCodec($template, $exclude = array(),
$idrefs = array(), $mapping = array())
{
$this->template = $template;
$this->exclude = $exclude;
$this->idrefs = $idrefs;
$this->mapping = $mapping;
$this->reverse = array();
foreach($mapping as $key => $value)
{
$this->reverse[$value] = $key;
}
}
/**
* Function: getName
*
* Creates a new instance of the template for this codec.
*/
function getName()
{
return mxCodecRegistry::getName($this->template);
}
/**
* Function: cloneTemplate
*
* Creates a new instance of the template for this codec.
*/
function cloneTemplate()
{
if (is_array($this->template))
{
return array();
}
else
{
$tmp = get_class($this->template);
return new $tmp();
}
}
/**
* Function: getFieldName
*
* Returns the fieldname for the given attributename.
* Looks up the value in the <reverse> mapping or returns
* the input if there is no reverse mapping for the
* given name.
*/
function getFieldName($attributename)
{
if ($attributename != null)
{
$mapped = ((in_array($attributename, $this->reverse))) ?
$this->reverse[$attributename] : null;
if ($mapped != null)
{
$attributename = $mapped;
}
}
return $attributename;
}
/**
* Function: getAttributeName
*
* Returns the attributename for the given fieldname.
* Looks up the value in the <mapping> or returns
* the input if there is no mapping for the
* given name.
*/
function getAttributeName($fieldname)
{
if (isset($fieldname) && isset($this->mapping[$fieldname]))
{
$fieldname = $this->mapping[$fieldname];
}
return $fieldname;
}
/**
* Function: isExcluded
*
* Returns true if the given attribute is to be ignored by the codec. This
* implementation returns true if the given fieldname is in <exclude> or
* if the fieldname equals <mxObjectIdentity.FIELD_NAME>.
*
* Parameters:
*
* obj - Object instance that contains the field.
* attr - Fieldname of the field.
* value - Value of the field.
* write - Boolean indicating if the field is being encoded or decoded.
* Write is true if the field is being encoded, else it is being decoded.
*/
function isExcluded($obj, $attr, $value, $write)
{
return mxUtils::indexOf($this->exclude, $attr) >= 0;
}
/**
* Function: isReference
*
* Returns true if the given fieldname is to be treated
* as a textual reference (ID). This implementation returns
* true if the given fieldname is in <idrefs>.
*
* Parameters:
*
* obj - Object instance that contains the field.
* attr - Fieldname of the field.
* value - Value of the field.
* write - Boolean indicating if the field is being encoded or decoded.
* Write is true if the field is being encoded, else it is being decoded.
*/
function isReference($obj, $attr, $value, $write)
{
return mxUtils::indexOf($this->idrefs, $attr) >= 0;
}
/**
* Function: encode
*
* Encodes the specified object and returns a node
* representing then given object. Calls <beforeEncode>
* after creating the node and <afterEncode> with the
* resulting node after processing.
*
* Enc is a reference to the calling encoder. It is used
* to encode complex objects and create references.
*
* This implementation encodes all variables of an
* object according to the following rules:
*
* - If the variable name is in <exclude> then it is ignored.
* - If the variable name is in <idrefs> then <mxCodec.getId>
* is used to replace the object with its ID.
* - The variable name is mapped using <mapping>.
* - If obj is an array and the variable name is numeric
* (ie. an index) then it is not encoded.
* - If the value is an object, then the codec is used to
* create a child node with the variable name encoded into
* the "as" attribute.
* - Else, if <encodeDefaults> is true or the value differs
* from the template value, then ...
* - ... if obj is not an array, then the value is mapped to
* an attribute.
* - ... else if obj is an array, the value is mapped to an
* add child with a value attribute or a text child node,
* if the value is a function.
*
* If no ID exists for a variable in <idrefs> or if an object
* cannot be encoded, a warning is issued using <mxLog.warn>.
*
* Returns the resulting XML node that represents the given
* object.
*
* Parameters:
*
* enc - <mxCodec> that controls the encoding process.
* obj - Object to be encoded.
*/
function encode($enc, $obj)
{
$node = $enc->document->createElement($this->getName());
$obj = $this->beforeEncode($enc, $obj, $node);
$this->encodeObject($enc, $obj, $node);
return $this->afterEncode($enc, $obj, $node);
}
/**
* Function: encodeObject
*
* Encodes the value of each member in then given obj
* into the given node using <encodeValue>.
*
* Parameters:
*
* enc - <mxCodec> that controls the encoding process.
* obj - Object to be encoded.
* node - XML node that contains the encoded object.
*/
function encodeObject($enc, $obj, $node)
{
$enc->setAttribute($node, "id", $enc->getId($obj));
if (is_array($obj))
{
$count = sizeof($obj);
for ($i = 0; $i < $count; $i++)
{
$this->encodeValue($enc, $obj, null, $obj[$i], $node);
}
}
else
{
$vars = get_object_vars($obj);
foreach ($vars as $name => $value)
{
if ($value != null &&
!$this->isExcluded($obj, $name, $value, true))
{
if (is_numeric($name))
{
unset($name);
}
$this->encodeValue($enc, $obj, $name, $value, $node);
}
}
}
}
/**
* Function: encodeValue
*
* Converts the given value according to the mappings
* and id-refs in this codec and uses <writeAttribute>
* to write the attribute into the given node.
*
* Parameters:
*
* enc - <mxCodec> that controls the encoding process.
* obj - Object whose property is going to be encoded.
* name - XML node that contains the encoded object.
* value - Value of the property to be encoded.
* node - XML node that contains the encoded object.
*/
function encodeValue($enc, $obj, $name, $value, $node)
{
if ($value != null)
{
if ($this->isReference($obj, $name, $value, true))
{
$tmp = $enc->getId($value);
if (!isset($tmp))
{
mxLog::warn("mxObjectCodec.encode: No ID for value of ".
$this->getName().".$name of type ".get_class($value));
return; // exit
}
$value = $tmp;
}
$defaults = (is_object($this->template)) ? get_object_vars($this->template) : null;
$defaultValue = (isset($defaults[$name])) ? $defaults[$name] : null;
// Checks if the value is a named default value
if (!isset($name) || $enc->encodeDefaults || $defaultValue !== $value)
{
$name = $this->getAttributeName($name);
$this->writeAttribute($enc, $obj, $name, $value, $node);
}
}
}
/**
* Function: writeAttribute
*
* Writes the given value into node using <writePrimitiveAttribute>
* or <writeComplexAttribute> depending on the type of the value.
*/
function writeAttribute($enc, $obj, $attr, $value, $node)
{
if (!is_object($value) && !is_array($value) /* primitive type */)
{
$this->writePrimitiveAttribute($enc, $obj, $attr, $value, $node);
}
else /* complex type */
{
$this->writeComplexAttribute($enc, $obj, $attr, $value, $node);
}
}
/**
* Function: writePrimitiveAttribute
*
* Writes the given value as an attribute of the given node.
*/
function writePrimitiveAttribute($enc, $obj, $attr, $value, $node)
{
$value = $this->convertValueToXml($value);
if (!isset($attr))
{
$child = $enc->document->createElement("add");
// TODO: Handle "as" attribute for maps here
$enc->setAttribute($child, "value", $value);
$node->appendChild($child);
}
else
{
$enc->setAttribute($node, $attr, $value);
}
}
/**
* Function: writeComplexAttribute
*
* Writes the given value as a child node of the given node.
*/
function writeComplexAttribute($enc, $obj, $attr, $value, $node)
{
$child = $enc->encode($value);
if (isset($child))
{
if (isset($attr))
{
$child->setAttribute("as", $attr);
}
$node->appendChild($child);
}
else
{
mxLog::warn("mxObjectCodec.encode: No node for value of ".
$this->getName().".$attr");
}
}
/**
* Function: convertValueToXml
*
* Returns the given value without applying a conversion.
*/
function convertValueToXml($value)
{
return $value;
}
/**
* Function: convertValueFromXml
*
* Returns the given value. In PHP there is no need to convert the
* boolean strings "0" and "1" to their numeric / boolean values.
*/
function convertValueFromXml($value)
{
return $value;
}
/**
* Function: beforeEncode
*
* Hook for subclassers to pre-process the object before
* encoding. This returns the input object. The return
* value of this function is used in <encode> to perform
* the default encoding into the given node.
*
* Parameters:
*
* enc - <mxCodec> that controls the encoding process.
* obj - Object to be encoded.
* node - XML node to encode the object into.
*/
function beforeEncode($enc, $obj, $node)
{
return $obj;
}
/**
* Function: afterEncode
*
* Hook for subclassers to post-process the node
* for the given object after encoding and return the
* post-processed node. This implementation returns
* the input node. The return value of this method
* is returned to the encoder from <encode>.
*
* Parameters:
*
* enc - <mxCodec> that controls the encoding process.
* obj - Object to be encoded.
* node - XML node that represents the default encoding.
*/
function afterEncode($enc, $obj, $node)
{
return $node;
}
/**
* Function: decode
*
* Parses the given node into the object or returns a new object
* representing the given node.
*
* Dec is a reference to the calling decoder. It is used to decode
* complex objects and resolve references.
*
* If a node has an id attribute then the object cache is checked for the
* object. If the object is not yet in the cache then it is constructed
* using the constructor of <template> and cached in <mxCodec.objects>.
*
* This implementation decodes all attributes and childs of a node
* according to the following rules:
*
* - If the variable name is in <exclude> or if the attribute name is "id"
* or "as" then it is ignored.
* - If the variable name is in <idrefs> then <mxCodec.getObject> is used
* to replace the reference with an object.
* - The variable name is mapped using a reverse <mapping>.
* - If the value has a child node, then the codec is used to create a
* child object with the variable name taken from the "as" attribute.
* - If the object is an array and the variable name is empty then the
* value or child object is appended to the array.
* - If an add child has no value or the object is not an array then
* the child text content is evaluated using <mxUtils.eval>.
*
* For add nodes where the object is not an array and the variable name
* is defined, the default mechanism is used, allowing to override/add
* methods as follows:
*
* (code)
* <Object>
* <add as="hello"><![CDATA[
* function(arg1) {
* alert('Hello '+arg1);
* }
* ]]></add>
* </Object>
* (end)
*
* If no object exists for an ID in <idrefs> a warning is issued
* using <mxLog.warn>.
*
* Returns the resulting object that represents the given XML node
* or the object given to the method as the into parameter.
*
* Parameters:
*
* dec - <mxCodec> that controls the decoding process.
* node - XML node to be decoded.
* into - Optional objec to encode the node into.
*/
function decode($dec, $node, &$into = null)
{
$id = $node->getAttribute("id");
$obj = null;
if (array_key_exists($id, $dec->objects))
{
$obj = $dec->objects[$id];
}
if (!isset($obj))
{
if (isset($into))
{
$obj = $into;
}
else
{
$obj = $this->cloneTemplate();
}
if (strlen($id) > 0)
{
$dec->putObject($id, $obj);
}
}
$node = $this->beforeDecode($dec, $node, $obj);
$this->decodeNode($dec, $node, $obj);
return $this->afterDecode($dec, $node, $obj);
}
/**
* Function: decodeNode
*
* Calls <decodeAttributes> and <decodeChildren> for the given node.
*/
function decodeNode($dec, $node, &$obj)
{
if (isset($node))
{
$this->decodeAttributes($dec, $node, $obj);
$this->decodeChildren($dec, $node, $obj);
}
}
/**
* Function: decodeAttributes
*
* Decodes all attributes of the given node using <decodeAttribute>.
*/
function decodeAttributes($dec, $node, &$obj)
{
$attrs = $node->attributes;
if ($attrs != null)
{
for ($i = 0; $i < $attrs->length; $i++)
{
$this->decodeAttribute($dec, $attrs->item($i), $obj);
}
}
}
/**
* Function: decodeAttribute
*
* Reads the given attribute into the specified object.
*/
function decodeAttribute($dec, $attr, &$obj)
{
$name = $attr->nodeName;
if ($name != "as" && $name != "id")
{
// Converts the string true and false to their boolean values.
// This may require an additional check on the obj to see if
// the existing field is a boolean value or uninitialized, in
// which case we may want to convert true and false to a string.
$value = $this->convertValueFromXml($attr->nodeValue);
$fieldname = $this->getFieldName($name);
if ($this->isReference($obj, $fieldname, $value, false))
{
$tmp = $dec->getObject($value);
if (!isset($tmp))
{
mxLog::warn("mxObjectCodec.decode: No object for ".
$this->getName().".$fieldname=$value");
return; // exit
}
$value = $tmp;
}
if (!$this->isExcluded($obj, $fieldname, $value, false))
{
//mxLog.debug(mxCodecRegistry::getName($obj)."$name=$value");
$obj->$fieldname = $value;
}
}
}
/**
* Function: decodeChildren
*
* Decodec all children of the given node using <decodeChild>.
*/
function decodeChildren($dec, $node, &$obj)
{
$child = $node->firstChild;
while ($child != null)
{
$tmp = $child->nextSibling;
if ($child->nodeType == XML_ELEMENT_NODE &&
!$this->processInclude($dec, $child, $obj))
{
$this->decodeChild($dec, $child, $obj);
}
$child = $tmp;
}
}
/**
* Function: decodeChild
*
* Reads the specified child into the given object.
*/
function decodeChild($dec, $child, &$obj)
{
$fieldname = $this->getFieldName($child->getAttribute("as"));
if (!isset($fieldname) ||
!$this->isExcluded($obj, $fieldname, $child, false))
{
$template = $this->getFieldTemplate($obj, $fieldname, $child);
$value = null;
if ($child->nodeName == "add")
{
$value = $child->getAttribute("value");
if (!isset($value))
{
// TODO: Evaluate text content
//$value = eval($child->get_content());
//mxLog.debug('Decoded '+fieldname+' '+mxUtils.getTextContent(child));
}
}
else
{
$value = $dec->decode($child, $template);
}
$this->addObjectValue($obj, $fieldname, $value, $template);
}
}
/**
* Function: getFieldTemplate
*
* Returns the template instance for the given field. This returns the
* value of the field, null if the value is an array or an empty collection
* if the value is a collection. The value is then used to populate the
* field for a new instance. For strongly typed languages it may be
* required to override this to return the correct collection instance
* based on the encoded child.
*/
function getFieldTemplate(&$obj, $fieldname, $child)
{
$template = (is_object($obj)) ? $obj->$fieldname : null;
// Non-empty arrays are replaced completely
if (is_array($template) && count($template) > 0)
{
$template = null;
}
return $template;
}
/**
* Function: addObjectValue
*
* Sets the decoded child node as a value of the given object. If the
* object is a map, then the value is added with the given fieldname as a
* key. If the fieldname is not empty, then setFieldValue is called or
* else, if the object is a collection, the value is added to the
* collection. For strongly typed languages it may be required to
* override this with the correct code to add an entry to an object.
*/
function addObjectValue(&$obj, $fieldname, $value, $template)
{
if ($value !== null && ($template == null || $value != $template))
{
if (isset($fieldname) && strlen($fieldname) > 0)
{
$obj->$fieldname = $value;
}
else
{
array_push($obj, $value);
}
//mxLog.debug('Decoded '+mxUtils.getFunctionName(obj.constructor)+'.'+fieldname+': '+value);
}
}
/**
* Function: processInclude
*
* Returns true if the given node is an include directive and
* executes the include by decoding the XML document. Returns
* false if the given node is not an include directive.
*
* Parameters:
*
* dec - <mxCodec> that controls the encoding/decoding process.
* node - XML node to be checked.
* into - Optional object to pass-thru to the codec.
*/
function processInclude($dec, $node, $into)
{
if ($node->nodeName == "include")
{
$name = $node->getAttribute("name");
if (isset($name))
{
try
{
$xml = mxUtils::loadXmlDocument($name)->documentElement;
if (isset($xml))
{
$dec->decode($xml, $into);
}
}
catch (Exception $e)
{
// ignore
}
}
return true;
}
return false;
}
/**
* Function: beforeDecode
*
* Hook for subclassers to pre-process the node for
* the specified object and return the node to be
* used for further processing by <decode>.
* The object is created based on the template in the
* calling method and is never null. This implementation
* returns the input node. The return value of this
* function is used in <decode> to perform
* the default decoding into the given object.
*
* Parameters:
*
* dec - <mxCodec> that controls the decoding process.
* node - XML node to be decoded.
* obj - Object to encode the node into.
*/
function beforeDecode($dec, $node, &$obj)
{
return $node;
}
/**
* Function: afterDecode
*
* Hook for subclassers to post-process the object after
* decoding. This implementation returns the given object
* without any changes. The return value of this method
* is returned to the decoder from <decode>.
*
* Parameters:
*
* enc - <mxCodec> that controls the encoding process.
* node - XML node to be decoded.
* obj - Object that represents the default decoding.
*/
function afterDecode($dec, $node, &$obj)
{
return $obj;
}
}
mxCodecRegistry::register(new mxObjectCodec(array()));
?>