-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrXml.php
828 lines (790 loc) · 27 KB
/
crXml.php
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
<?php
/*
Copyright (c) 2011 Sandeep.C.R, <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Version 1.31
// Dec 30 2011
// List of Recent modifications
// Added nodeType,and htmlEncodedNodeContent keyes to the array that
// Search function prints.
// Generate error on non-existant source nodes when copying
// Added line number in error string using debug_bactrace.
// Made output of errors the default behaviour the default mode using trigger_error
// Added a public static variable boolean variable debug, to toggle error reporting.
// Added a search function that accepts a nodename and return the nodes with that
// name together with the actual php code that is required to access those nodes
// Added crxml::factory static method to create crxml objects on the
// fly from a raw xml string. to use with append ,appendTo, replaceWith
// functions.
// Fixed bug in fullNode function when the called from topmost document
// node
// Modified the behaviour of crxml node to crxml node assignment
// Now simply assigining a crxml node to another crxml node will
// replace the node inner content with source nodes inner content.
// all the attributes of destination will be cleared and attributes
// from source is copied to destination node.The node name of
// destination node remains the same.
// If needed to append childeren of one node to another node
// have to call $node->appendChildren($sourceNode) function.
// If you have to compleatly replace one node with another node
// You have to use the $node->replaceWith($sourcenode) function.
// A small bug fix to prevent fatal error when non existant nodes are accessed.
// Added negative indexes
// Added Isset function
// Added function for getting errors(as mentioned below)
// Added error reporting for string access on non existant child nodes.
// Added Version and encoding option to the object Constructor
// Fixed Issue with old libxml versions when executing domxpath queries
// Fixed misc bugs
// changed the below format ..->{'prefix:localName'}($namespaceURI) to ..->{'namespaceURI|localName|prefix'}
// Added format ..->{'prefix:localName'}($namespaceURI) to add nodes of type <prf:item xmlns:prf = "http://yahoo.com".....
// Added 'addNameSpaceDefNode' function to add nodes of type <prf:item xmlns:prf = "http://yahoo.com".....
// Added remove function to remove a node
// Added emptyNode function to delete all child nodes
// Fixed minor bug in fullNode.
// Made attributes to copy while using fullNode function
// Fixed minor bug in offsetunset
// Fixed a minor bug in offsetSet
// Added fullnode function to use in xml editing.
// Added support for namespaced attributes.
// XML Parser/Generator Class
// Author: Sandeep.C.R
class crxml implements arrayAccess, iterator
{
protected $node;
protected $parent;
protected $version;
protected $encoding;
protected $root;
protected $offset;
protected $nodeName;
protected $current;
protected $error;
protected $nodeMode;
public static $debug = true;
public static function factory($xmlStr= '' ,$version='1.0',$encoding='UTF-8')
{
$crxml = new crxml($version,$encoding);
if(strlen($xmlStr)>0) $crxml->loadXML($xmlStr);
return $crxml;
}
public function __construct($version='1.0',$encoding='UTF-8',crxml $parent=null,crxml $root=null,$nodeName='',$offset=0)
{
$this->root=$root;
$this->nodeMode= null;
if(is_null($parent)) {
$this->version = $version;
$this->encoding = $encoding;
$this->node=new DOMDocument($this->version, $this->encoding);
$this->root=$this;
$this->parent=null;
$this->error = array();
} else {
$this->node=null;
$this->parent=$parent;
$this->error=&$parent->error;
}
$this->offset=$offset;
$this->nodeName=$nodeName;
}
public function __call($name,$args)
{
if(strpos($name,":")!==false) {
list($prefix,$localName)=explode(':',$name);
if(isset($args[0])) $nameSpaceURI = $args[0];else throw new Exception("No URI given for namespace prefix $prefix");
return $this->addNameSpaceDefNode($prefix,$localName,$nameSpaceURI);
}
}
public function __unset($name)
{
if($node=$this->_getNode()) {
if($childNodes=$this->_getChildrenForNode($node,$name)) {
if($childNode=$childNodes->item($this->offset)) {
$node->removeChild($childNode);
}
}
}
}
public function __get($name)
{
if($this->offset>0)
{
$crxml=new crxml($this->version,$this->encoding,$this,$this->root,$name);
if($this->parent->_getNode()) {
if($node=$this->_getChildrenForNode($this->parent->_getNode(),$this->nodeName)->item($this->offset)) {
$childNode=$this->_getChildrenForNode($node,$name)->item(0);
$crxml->node=$childNode;
}
}
return $crxml;
}
else {
$crxml=new crxml($this->version,$this->encoding,$this,$this->root,$name);
if($this->_getNode() && $node=$this->_getChildrenForNode($this->_getNode(),$name)->item(0)) {
$crxml->node=$node;
}
return $crxml;
}
}
public function __set($name,$value)
{
if(is_null($this->node)) {
$this->node=$this->connectToParent();
}
if($this->offset==0) {
$childNodes=$this->_getChildrenForNode($this->_getNode(),$name);
if($node=$childNodes->item(0)) {
$this->_assignValue($node,$value);
} else {
$childNode=$this->_getNewElement($name);
$this->node->appendChild($childNode);
$this->_assignValue($childNode,$value);
}
return;
} else {
if($this->_getChildrenForNode($this->parent->_getNode(),$this->nodeName)->length-1 < $this->offset) {
while($this->_getChildrenForNode($this->parent->_getNode(),$this->nodeName)->length < $this->offset) {
$element=$this->_getNewElement($this->nodeName);
$this->parent->_getNode()->appendChild($element);
}
$element=$this->_getNewElement($this->nodeName);
$lastNode=$this->parent->_getNode()->appendChild($element);
$element=$this->_getNewElement($name);
$lastNode->appendChild($element);
$this->_assignValue($element,$value);
} else {
$targetNode=$this->_getChildrenForNode($this->parent->_getNode(),$this->nodeName)->item($this->offset);
if($node=$this->_getChildrenForNode($targetNode,$name)->item(0)) {
$this->_assignValue($node,$value);
} else {
$element=$this->_getNewElement($name);
$targetNode->appendChild($element);
$this->_assignValue($element,$value);
}
}
}
}
public function debug($flag)
{
self::$debug = $flag;
}
public function __isset($name)
{
if($this->offset>0)
{
if($this->parent->_getNode()) {
if($node=$this->_getChildrenForNode($this->parent->_getNode(),$this->nodeName)->item($this->offset)) {
if($this->_getChildrenForNode($node,$name)->length==0) return false;else return true;
}
}
return false;
}
else {
if($this->_getNode()) {
if($this->_getChildrenForNode($this->_getNode(),$name)->length==0) return false; else return true;
}
}
return false;
}
public function setError($error)
{
$this->error[] = $error;
if(self::$debug) {
if(function_exists('debug_backtrace'))
{
$stack=debug_backtrace(false);
foreach($stack as $v)
if(isset($v['file']) && substr($v['file'],-9)!='crXml.php' )
{
$error_line="\n<b>Called from:".$v['file'].",Line:<span style=\"color:red\">".$v['line']."</span></b>\n";
break;
}
}
trigger_error("<pre>$error{$error_line}</pre>");
}
}
public function getError()
{
return "<pre>".join("\n",$this->error)."</pre>";
}
public function connectToParent()
{
if(is_null($this->parent) || !is_null($this->_getNode())) {
if($this->offset>0) {
if($this->parent) {
return $this->_getChildrenForNode($this->parent->_getNode(),$this->nodeName)->item($this->offset);
}
} else {
return $this->node;
}
} else {
$parentNode=$this->parent->connectToParent();
if($this->_getChildrenForNode($parentNode,$this->nodeName)->length-1 < $this->offset) {
while($this->_getChildrenForNode($parentNode,$this->nodeName)->length < $this->offset) {
$element=$this->_getNewElement($this->nodeName);
$parentNode->appendChild($element);
}
}
if($namespaceURI = $parentNode->namespaceURI) {
if(!$parentNode->lookupPrefix($namespaceURI) ) {
$element=$this->_getNewElement("$namespaceURI|{$this->nodeName}");
} else {
$element=$this->_getNewElement($this->nodeName);
}
} else {
$element=$this->_getNewElement($this->nodeName);
}
$this->node=$parentNode->appendChild($element);
return $this->node;
}
}
// arrayaccess interface
public function offsetExists($offset)
{
if(is_string($offset)) {
if($node=$this->_getNode()) {
if($node->attributes->getNamedItem($offset))
return true;
else
return false;
} else {
return false;
}
} else {
if($node=$this->_getChildrenForNode($this->parent->_getNode(),$this->nodeName)->item($offset)) {
return true;
} else {
return false;
}
}
}
public function offsetGet($offset)
{
if(is_string($offset)) {
if(strpos($offset,":")!==false) {
list($prefix,$localName)=explode(':',$offset);
$nameSpaceURI=$this->root->_getNode()->lookupNamespaceURI($prefix);
if($node=$this->_getNode()->attributes->getNamedItemNS ($nameSpaceURI , $localName )) {
return $node->nodeValue;
}
} else {
if($node=$this->_getNode()) {
if($node = $node->attributes->getNamedItem($offset))
return $node->nodeValue;
else
return false;
} else {
return false;
}
}
} else {
$node=clone $this;
$node->offset=$offset;
return $node;
}
}
public function offsetSet($offset,$value)
{
if(!is_object($value)) $value=str_replace('&','&',$value);
if(is_null($this->_getNode())) {
$this->node=$this->connectToParent();
}
if(is_string($offset)) {
$this->_getNode()->setAttribute($offset,$value);
} else {
if($this->_getChildrenForNode($this->parent->_getNode(),$this->nodeName)->length-1 < $offset) {
while($this->_getChildrenForNode($this->parent->_getNode(),$this->nodeName)->length < $offset) {
$element=$this->_getNewElement($this->nodeName);
$this->parent->_getNode()->appendChild($element);
}
$element=$this->_getNewElement($this->nodeName);
$lastNode=$this->parent->_getNode()->appendChild($element);
$this->_assignValue($element,$value);
} else {
$node=$this->_getChildrenForNode($this->parent->_getNode(),$this->nodeName)->item($offset);
$this->_assignValue($node,$value);
}
}
}
public function offsetUnset($offset)
{
$deadNode=new crxml($this->version,$this->encoding);
if(is_string($offset)) {
$node=$this->_getNode();
$node->removeAttribute($offset);
} else {
$deadNode->node=$this->parent->_getNode();
$deadNode->root=$this->root;
if($node=$deadNode->_getNode()) {
if($childNodes=$deadNode->_getChildrenForNode($node,$this->nodeName)) {
if($childNode=$childNodes->item($offset)) {
$node->removeChild($childNode);
}
}
}
}
}
function nodeValue()
{
return (string)$this;
}
function __toString()
{
if($node=$this->_getNode()) {
$count = 0;
$return='';
while($n = $node->childNodes->item($count)) {
if($n->nodeType == XML_TEXT_NODE || $n->nodeType == XML_CDATA_SECTION_NODE) {
if(!ctype_space($n->nodeValue)) $return = $return.$n->nodeValue;
}
$count++;
}
return $return;
} else {
//Error reporting on attempt to echo non existing child elements
$parent = $this->parent;
$child = $this;
while(!$parent->_getNode()) {
$child = $parent;
$parent = $parent->parent;
}
$foundChildNodes = array();
if($parent->_getNode()->childNodes->length) foreach(range(0,$parent->_getNode()->childNodes->length-1) as $v) {
$childNodeName = $parent->_getNode()->childNodes->item($v)->nodeName;
if($childNodeName !== '#text') $foundChildNodes[] = $childNodeName;
}
$this->setError("<pre>crxml error:No {$child->offset}-nth child '{$child->nodeName}' found for node \" {$parent->nodeName}\".The avaliable child nodes of node {$parent->nodeName} are\n".join("\n",$foundChildNodes)."</pre>");
return '';
}
}
function loadXML($xmlString)
{
$this->node->loadXML($xmlString);
$this->parent=null;
}
function attributes()
{
$return = array();
for($i=0;($attributeNode = $this->_getNode()->attributes->item($i));$i++) {
$return[$attributeNode->nodeName] = $attributeNode->nodeValue;
}
return $return;
}
function xml()
{
$node=$this->_getNode();
$this->root->_getNode()->formatOutput = true;
return $this->root->_getNode()->saveXML($node);
}
function _getNode()
{
if($this->offset==0) {
return $this->node;
} else {
if($this->parent) if($this->parent->_getNode()) {
if($this->offset>0)
return $this->_getChildrenForNode($this->parent->_getNode(),$this->nodeName)->item($this->offset);
else {
$childNodes = $this->_getChildrenForNode($this->parent->_getNode(),$this->nodeName);
$offset = $childNodes->length+$this->offset;
return $childNodes->item($offset);
}
}
}
return null;
}
function count($name='')
{
if(!is_null($this->node)) {
if($name) {
return $this->_getChildrenForNode($this->_getNode(),$name)->length;
}
else {
return $this->_getNode()->childNodes->length;
}
} else return 0;
}
function addNameSpaceDefNode($prefix,$nodeName,$nameSpaceURI)
{
if(is_null($this->node)) {
$this->node=$this->connectToParent();
}
$element=$this->root->_getNode()->createElementNS($nameSpaceURI,"$prefix:$nodeName",'');
$this->node->appendChild($element);
$fullName = "$nameSpaceURI|$nodeName";
return $this->$fullName;
}
function addNameSpace($nameSpaces)
{
if(is_null($this->node)) {
$this->node=$this->connectToParent();
}
foreach($nameSpaces as $prefix=>$nameSpaceURI) {
$this->_getNode()->setAttributeNS('http://www.w3.org/2000/xmlns/' ,"xmlns:$prefix" , $nameSpaceURI);
}
return $this;
}
function _getNewElement($name,$value=null)
{
if(strpos($name,"|")!==false) {
$fragments = explode('|',$name);
if(count($fragments)==3) list($nameSpaceURI,$localName,$prefix) = $fragments;
else {
list($nameSpaceURI,$localName) = $fragments;
$prefix = null;
}
if(isset($prefix)) {
$element=$this->root->_getNode()->createElementNS($nameSpaceURI,"$prefix:$localName",'');
} else {
$element=$this->root->_getNode()->createElementNS($nameSpaceURI,"$localName",'');
}
if($value) $element->nodeValue=$value;
return $element;
} else if(strpos($name,":")!==false) {
list($prefix,$localName)=explode(':',$name);
if(!$nameSpaceURI = $this->root->_getNode()->lookupNamespaceURI($prefix))
$nameSpaceURI = $this->parent->_getNode()->lookupNamespaceURI($prefix);
$element=$this->root->_getNode()->createElementNS($nameSpaceURI,"$prefix:$localName",'');
if($value) $element->nodeValue=$value;
return $element;
} else {
if(isset($this->parent) && $this->parentNode!=null && $parentNode = $this->parent->_getNode()) {
if($parentNode->isDefaultNamespace($parentNode->namespaceURI)) {
$element=$this->root->_getNode()->createElementNS($parentNode->namespaceURI,$name,'');
if($value) $element->nodeValue=$value;
return $element;
}
}
return new DOMElement($name,$value);
}
}
function _getChildrenForNode($node,$name)
{
if(strpos($name,"|")!==false) {
list($nameSpaceURI,$localName)=explode('|',$name);
$domXpath=new DOMXpath($this->root->node);
$domXpath->registerNamespace('atom',$nameSpaceURI);
$childNodes=@$domXpath->query("atom:$localName",$node);
} else if(strpos($name,":")!==false) {
list($prefix,$localName)=explode(':',$name);
$domXpath=new DOMXpath($this->root->node);
if($nameSpaceURI = $this->root->node->lookupNamespaceURI($prefix)) {
$domXpath->registerNamespace($prefix,$nameSpaceURI);
} elseif($node->prefix == $prefix){
$domXpath->registerNamespace($prefix,$node->namespaceURI);
} else {
$found = false;
if($node->childNodes->length) foreach(range(0,$node->childNodes->length-1) as $v) {
$childNode = $node->childNodes->item($v);
$childNodeName = $node->childNodes->item($v)->nodeName;
if($childNodeName == $name ) {
$childNodeNamespaceURI = $childNode->namespaceURI;
$childNodePrefix = $childNode->prefix;
$domXpath->registerNamespace($childNodePrefix,$childNodeNamespaceURI);
$found = true;
}
}
if(!$found) {
throw new Exception("undefined namespace prefix $prefix");
}
}
$childNodes=@$domXpath->query($name,$node);
} elseif($node->isDefaultNamespace($node->namespaceURI)) {
$domXpath=new DOMXpath($this->root->node);
$parentNamespaceURI = $node->namespaceURI;
$domXpath->registerNamespace('atom',$parentNamespaceURI);
$childNodes=@$domXpath->query("atom:$name",$node);
} else{
$domXpath=new DOMXpath($this->root->node);
$childNodes=@$domXpath->query($name,$node);
}
if($childNodes===false) $childNodes = new DOMNodeList();
return $childNodes;
/* deactivated because this generates unneassary notice for the following code
$crxml = new crxml;
$crxml->Item->{'http://imaginarynamespaceforauthors.com|Authors'}->Author->FirstName = 'Collins';
$crxml->Item->Authors = 'Larry';
echo $crxml->xml();
*/
if($childNodes->length == 0 && 0) {
$foundChildNodes = array();
if($node->childNodes->length) foreach(range(0,$node->childNodes->length-1) as $v) {
$childNodeName = $node->childNodes->item($v)->nodeName;
if($childNodeName !== '#text') $foundChildNodes[] = $childNodeName;
if(strpos($childNodeName,":")!==false) list($prefix,$localname)=explode(':',$childNodeName);else {
$prefix = '';
$localname = $childNodeName;
}
if(strpos($name,":")!==false) list($tprefix,$tlocalname)=explode(':',$name);else {
$tlocalname = $name;
$tprefix = '';
}
if($localname==$tlocalname) {
$foundprefix = $this->root->node->lookupPrefix($node->childNodes->item($v)->namespaceURI);
$this->setError("<pre>crxml error:\nNo child '".$name."' with default or specified ($tprefix) name space found for node {$node->nodeName}.\nBut <b>found a node</b>, '".$localname."' within namespace '".$node->childNodes->item($v)->namespaceURI."(prefix:$foundprefix)' for same node.\nPlease use format \n ..->{$node->nodeName}->".((strlen($foundprefix)>0)?"{'{$foundprefix}:{$tlocalname}'} \nto access the above node\n\n":"$tlocalname \nto access the above node\\\n"));
}
}
}
return $childNodes;
}
public function build()
{
if($this->_getNode()) $this->connectToParent();
return $this;
}
public function appendTo($crxmlNode)
{
if($this->_getNode()) {
$fullNode = $this->fullNode();
$fullNode->nodeMode = 'append';
if(!$crxmlNode->_getNode()) $crxmlNode->connectToParent();
$crxmlNode->_assignValue($crxmlNode->_getNode(),$fullNode);
} else {
$this->setError('Non-existant node specified as source for assignment');
}
}
public function appendNode($crxmlNode)
{
if(!$this->_getNode()) $this->connectToParent();
if($crxmlNode->_getNode()) {
$crxmlNode = $crxmlNode->fullNode();
$crxmlNode->nodeMode = 'append';
$this->_assignValue($this->_getNode(),$crxmlNode);
} else {
$this->setError('Non-existant node specified as source for appendNode()');
}
}
public function appendChildren($crxmlNode)
{
if(!$this->_getNode()) $this->connectToParent();
$crxmlNode->nodeMode = 'append';
$this->_assignValue($this->_getNode(),$crxmlNode);
}
public function replaceWith($crxmlNode)
{
if(!$this->_getNode()) $this->connectToParent();
if($crxmlNode->_getNode()) {
$fullNode = $crxmlNode->fullNode();
$fullNode->nodeMode = 'replace';
$this->_assignValue($this->_getNode(),$fullNode);
}
}
public function _assignValue($node,$value)
{
if(!is_object($value)) $value = str_replace('&','&',$value);
if(is_object($value)) {
if(get_class($value) == 'stdClass') {
$node->nodeValue='';
$cDataSection=$this->root->node->createCDATASection($value->scalar);
$node->appendChild($cDataSection);
}
elseif (get_class($value) == 'crxml') {
$currentChildNode = 0;
if($value->nodeMode === 'append') {
//$node->appendChild($this->root->node->importNode($value->_getNode()->cloneNode(true),true));
while($currentChildNodeElement = $value->_getNode()->childNodes->item($currentChildNode)) {
$node->appendChild($this->root->node->importNode($value->_getNode()->childNodes->item($currentChildNode)->cloneNode(true),true));
$currentChildNode++;
}
} elseif($value->nodeMode === 'replace') {
if($value->_getNode()) {
$node->parentNode->replaceChild($this->root->node->importNode($value->_getNode()->childNodes->item(0)->cloneNode(true),true),$node);
} else {
$this->setError('Non-existant node specified as source for assignment');
}
} else {
//clear all attributes
if($this->_getNode()->attributes->length>0) for($i=0;($attributeNode = $this->_getNode()->attributes->item($i));$i++) {
$node->removeAttribute($attributeNode->nodeName);
}
//clear all child nodes
while ($node->childNodes->length) $node->removeChild($node->firstChild);
//copy child nodes
while($currentChildNodeElement = $value->_getNode()->childNodes->item($currentChildNode)) {
$node->appendChild($this->root->node->importNode($value->_getNode()->childNodes->item($currentChildNode)->cloneNode(true),true));
$currentChildNode++;
}
//copy attributes
for($i=0;($attributeNode = $value->_getNode()->attributes->item($i));$i++) {
$node->setAttribute($attributeNode->nodeName,$attributeNode->nodeValue);
}
}
}
} else {
$node->nodeValue=$value;
}
}
function remove()
{
if($this->parent) {
$this->parent->_getNode()->removeChild($this->_getNode());
}
}
function emptyNode()
{
$node = $this->_getNode();
while ($node->childNodes->length) $node->removeChild($node->firstChild);
}
function fullNode()
{
$temp = new crxml($this->version,$this->encoding);
if($node = $this->_getNode()) {
if($node->nodeType==XML_DOCUMENT_NODE) return $this;
$nodeName = $node->nodeName;
if(strpos($nodeName,"|")!==false) {
list($namespaceURI,$localName)=explode('|',$nodeName);
$nodeName = $nameSpaceURI. "|" . $localName;
$temp->$nodeName = $this;
} else if(strpos($nodeName,":")!==false) {
list($prefix,$localName)=explode(':',$nodeName);
$nameSpaceURI=$this->_getNode()->namespaceURI;
$nodeName = $prefix. ":" . $localName;
$temp->addNameSpaceDefNode($prefix,$localName,$nameSpaceURI);
$temp->$nodeName = $this;
} else{
$temp->$nodeName = $this;
}
for($i=0;($attributeNode = $this->_getNode()->attributes->item($i));$i++) {
$temp->$nodeName->offsetSet($attributeNode->nodeName, $attributeNode->nodeValue);
}
return $temp;
} else {
if($this->offset > 0) $offsetPart = "[{$this->offset}]";else $offsetPart = '';
$this->setError("Non-existant node `{$this->nodeName}{$offsetPart}` node given in parameter.");
}
}
//Iterator Interface
function current()
{
$childNodes=$this->_getChildrenForNode($this->_getNode(),'*');
if($node=$childNodes->item($this->current)) {
$return=clone $this;
$return->node=$node;
$return->offset=0;
return $return;
}
}
function key()
{
$childNodes=$this->_getChildrenForNode($this->_getNode(),'*');
if($node=$childNodes->item($this->current)) {
return $node->nodeName;
} else {
return false;
}
}
function next()
{
$this->current++;
}
function rewind()
{
$this->current=0;
}
function valid()
{
$childNodes=$this->_getChildrenForNode($this->_getNode(),'*');
if($node=$childNodes->item($this->current)) {
return true;
} else {
return false;
}
}
private function findWayToTop($node,$topNode=null)
{
while($node->parentNode) {
if(!is_null($topNode)) {
if($topNode->isSameNode($node)) break;
}
$offset = 0;
$temp = $node;
while($sibling = $temp->previousSibling) {
if($sibling->nodeName == $node->nodeName) {
if($sibling->namespaceURI === $node->namespaceURI) {
$offset++;
}
}
$temp = $sibling;
}
if($offset > 0 ) $offsetString = "[$offset]";else $offsetString = '';
if(strlen($node->prefix)) {
$path[] = "{'".$node->prefix.':'.$node->localName."'}$offsetString";
} elseif(strlen($node->namespaceURI)) {
if($prefix= $this->root->node->lookupPrefix($node->namespaceURI)) {
$path[] = "{'$prefix:".$node->localName."'}$offsetString";
}
else {
$path[] = "{'".$node->namespaceURI.'|'.$node->localName."'}$offsetString";
}
} else {
$temp = $node->nodeName;
if(preg_match("#[^a-zA-Z0-9_]#",$temp)) $temp= "{'$temp'}";
$path[] = $temp.$offsetString;
}
$node->nodeName;
$node = $node->parentNode;
}
return array_reverse($path);
}
function search($nodeName,$relative = false)
{
$return = array();
if($thisNode = $node = $this->_getNode()) {
if(strpos($nodeName,":")!==false) {
list($prefix,$localName)=explode(':',$nodeName);
if($nameSpaceURI = $this->root->node->lookupNamespaceURI($prefix)) {
$nodeList = $node->getElementsByTagNameNS($nameSpaceURI,$localName);
$nlList[] = $nodeList;
}
}
else {
$nodeList = $node->getElementsByTagName($nodeName);
$nlList[] = $nodeList;
}
foreach($nlList as $v) {
foreach($v as $node) {
if($relative) $path = $this->findWayToTop($node,$thisNode); else $path = $this->findWayToTop($node);
$nodeContent = $this->root->_getNode()->saveXML($node);
$htmlEncoded = htmlentities($nodeContent);
$return[] = array('nodeName'=>$node->nodeName,'accessStatement'=>'...->'.join('->',$path),'nodeChildrenCount'=>$node->childNodes->length,'nodeType'=>$node->nodeType,'namespaceURI'=>$node->namespaceURI,'nodeContent'=>$nodeContent,'htmlEncodedNodeContent'=>$htmlEncoded);
}
}
}
return $return;
}
function dump($nodeName='*',$relative=false)
{
echo "<pre>".print_r($this->search($nodeName,$relative),true)."</pre>";
}
/*
function dump($node=null,$level = 0)
{
if(!$node) $node = $this->root->node;
foreach($node->childNodes as $k=>$v) {
echo str_repeat("  ",$level);
echo $v->nodeName;
if($v->nodeType == XML_TEXT_NODE || $v->nodeType == XML_CDATA_SECTION_NODE ) echo "=>".$v->nodeValue;
echo "\n<br/>\n";
if(isset($v->childNodes)) if($v->childNodes->length > 0) {
$this->dump($v,$level+1);
}
}
}*/
}