-
-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathxml_domit_parser.php
More file actions
3668 lines (3141 loc) · 108 KB
/
Copy pathxml_domit_parser.php
File metadata and controls
3668 lines (3141 loc) · 108 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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* DOMIT! is a non-validating, but lightweight and fast DOM parser for PHP
* @package domit-xmlparser
* @subpackage domit-xmlparser-main
* @version 1.01
* @copyright (C) 2004 John Heinstein. All rights reserved
* @license http://www.gnu.org/copyleft/lesser.html LGPL License
* @author John Heinstein <johnkarl@nbnet.nb.ca>
* @link http://www.engageinteractive.com/domit/ DOMIT! Home Page
* DOMIT! is Free Software
**/
if (!defined('DOMIT_INCLUDE_PATH')) {
define('DOMIT_INCLUDE_PATH', (dirname(__FILE__) . "/"));
}
/** current version of DOMIT! */
define ('DOMIT_VERSION', '1.01');
/** XML namespace URI */
define ('DOMIT_XML_NAMESPACE', 'http://www.w3.org/xml/1998/namespace');
/** XMLNS namespace URI */
define ('DOMIT_XMLNS_NAMESPACE', 'http://www.w3.org/2000/xmlns/');
/**
* @global array Flipped version of $definedEntities array, to allow two-way conversion of entities
*
* Made global so that Attr nodes, which have no ownerDocument property, can access the array
*/
$GLOBALS['DOMIT_defined_entities_flip'] = array();
require_once(DOMIT_INCLUDE_PATH . 'xml_domit_shared.php');
/**
* The base class of all DOMIT node types
*
* @package domit-xmlparser
* @subpackage domit-xmlparser-main
* @author John Heinstein <johnkarl@nbnet.nb.ca>
*/
class DOMIT_Node {
/** @var string The name of the node, varies according to node type */
var $nodeName = null;
/** @var string The value of the node, varies according to node type */
var $nodeValue = null;
/** @var int The type of node, e.g. CDataSection */
var $nodeType = null;
/** @var Object A reference to the parent of the current node */
var $parentNode = null;
/** @var Array An array of child node references */
var $childNodes = null;
/** @var Object A reference to the first node in the childNodes list */
var $firstChild = null;
/** @var Object A reference to the last node in the childNodes list */
var $lastChild = null;
/** @var Object A reference to the node prior to the current node in its parents childNodes list */
var $previousSibling = null;
/** @var Object A reference to the node after the current node in its parents childNodes list */
var $nextSibling = null;
/** @var Object A NodeList of attribute nodes */
var $attributes = null;
/** @var Object A reference to the Document node */
var $ownerDocument = null;
/** @var String A URI that identifies the XML namespace to which the node belongs */
var $namespaceURI = null;
/** @var String The namespace prefix for the node */
var $prefix = null;
/** @var String The local name of the node */
var $localName = null;
/** @var string The unique node id */
var $uid;
/** @var int The number of children of the current node */
var $childCount = 0;
/**
* Raises error if abstract class is directly instantiated
*/
function DOMIT_Node() {
DOMIT_DOMException::raiseException(DOMIT_ABSTRACT_CLASS_INSTANTIATION_ERR,
'Cannot instantiate abstract class DOMIT_Node');
} //DOMIT_Node
/**
* DOMIT_Node constructor, assigns a uid
*/
function _constructor() {
global $uidFactory;
$this->uid = $uidFactory->generateUID();
} //_constructor
/**
* Appends a node to the childNodes list of the current node
* @abstract
* @param Object The node to be appended
* @return Object The appended node
*/
function &appendChild(&$child) {
DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR,
('Method appendChild cannot be called by class ' . get_class($this)));
} //appendChild
/**
* Inserts a node to the childNodes list of the current node
* @abstract
* @param Object The node to be inserted
* @param Object The node before which the insertion is to occur
* @return Object The inserted node
*/
function &insertBefore(&$newChild, &$refChild) {
DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR,
('Method insertBefore cannot be called by class ' . get_class($this)));
} //insertBefore
/**
* Replaces a node with another
* @abstract
* @param Object The new node
* @param Object The old node
* @return Object The new node
*/
function &replaceChild(&$newChild, &$oldChild) {
DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR,
('Method replaceChild cannot be called by class ' . get_class($this)));
} //replaceChild
/**
* Removes a node from the childNodes list of the current node
* @abstract
* @param Object The node to be removed
* @return Object The removed node
*/
function &removeChild(&$oldChild) {
DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR,
('Method removeChild cannot be called by class ' . get_class($this)));
} //removeChild
/**
* Returns the index of the specified node in a childNodes list
* @param Array The childNodes array to be searched
* @param Object The node targeted by the search
* @return int The index of the target node, or -1 if not found
*/
function getChildNodeIndex(&$arr, &$child) {
$index = -1;
$total = count($arr);
for ($i = 0; $i < $total; $i++) {
if ($child->uid == $arr[$i]->uid) {
$index = $i;
break;
}
}
return $index;
} //getChildNodeIndex
/**
* Determines whether a node has any children
* @return boolean True if any child nodes are present
*/
function hasChildNodes() {
return ($this->childCount > 0);
} //hasChildNodes
/**
* Determines whether a node has any attributes
* @return boolean True if the node has attributes
*/
function hasAttributes() {
//overridden in DOMIT_Element
return false;
} //hasChildNodes
/**
* Collapses adjacent text nodes in entire node subtree
*/
function normalize() {
if (($this->nodeType == DOMIT_DOCUMENT_NODE) && ($this->documentElement != null)) {
$this->documentElement->normalize();
}
} //normalize
/**
* Copies a node and/or its children
* @abstract
* @param boolean True if all child nodes are also to be cloned
* @return Object A copy of the node and/or its children
*/
function &cloneNode($deep = false) {
DOMIT_DOMException::raiseException(DOMIT_ABSTRACT_METHOD_INVOCATION_ERR,
'Cannot invoke abstract method DOMIT_Node->cloneNode($deep). Must provide an overridden method in your subclass.');
} //cloneNode
/**
* Adds elements with the specified tag name to a NodeList collection
* @param Object The NodeList collection
* @param string The tag name of matching elements
*/
function getNamedElements(&$nodeList, $tagName) {
//Implemented in DOMIT_Element.
//Needs to be here though! This is called against all nodes in the document.
} //getNamedElements
/**
* Sets the ownerDocument property of a node to the containing DOMIT_Document
* @param Object A reference to the document element of the DOMIT_Document
*/
function setOwnerDocument(&$rootNode) {
if ($rootNode->ownerDocument == null) {
unset($this->ownerDocument);
$this->ownerDocument = null;
}
else {
$this->ownerDocument =& $rootNode->ownerDocument;
}
$total = $this->childCount;
for ($i = 0; $i < $total; $i++) {
$this->childNodes[$i]->setOwnerDocument($rootNode);
}
} //setOwnerDocument
/**
* Tests whether a value is null, and if so, returns a default value
* @param mixed The value to be tested
* @param mixed The default value
* @return mixed The specified value, or the default value if null
*/
function &nvl(&$value,$default) {
if (is_null($value)) return $default;
return $value;
} //nvl
/**
* Retrieves an element or DOMIT_NodeList of elements corresponding to an Xpath-like expression.
* @abstract
* @param string The query pattern
* @param int If a single node is to be returned (rather than the entire NodeList) the index of that node
* @return mixed A NodeList or single node that matches the pattern
*/
function &getElementsByPath($pattern, $nodeIndex = 0) {
DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR,
('Method getElementsByPath cannot be called by class ' . get_class($this)));
} //getElementsByPath
/**
* Retrieves an element or DOMIT_NodeList of elements corresponding to an Xpath-like attribute expression (NOT YET IMPLEMENTED!)
* @abstract
* @param string The query pattern
* @param int If a single node is to be returned (rather than the entire NodeList) the index of that node
* @return mixed A NodeList or single node that matches the pattern
*/
function &getElementsByAttributePath($pattern, $nodeIndex = 0) {
DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR,
('Method getElementsByAttributePath cannot be called by class ' . get_class($this)));
} //getElementsByAttributePath
/**
* Adds all child nodes of the specified nodeType to the NodeList
* @abstract
* @param Object The NodeList collection
* @param string The nodeType of matching nodes
*/
function getTypedNodes(&$nodeList, $type) {
DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR,
('Method getTypedNodes cannot be called by class ' . get_class($this)));
} //getTypedNodes
/**
* Adds all child nodes of the specified nodeValue to the NodeList
* @abstract
* @param Object The NodeList collection
* @param string The nodeValue of matching nodes
*/
function getValuedNodes(&$nodeList, $value) {
DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR,
('Method getValuedNodes cannot be called by class ' . get_class($this)));
} //getValuedNodes
/**
* Returns the concatented text of the current node and its children
* @return string The concatented text of the current node and its children
*/
function getText() {
return $this->nodeValue;
} //getText
/**
* Indicates whether the specified feature is supported by the DOM implementation and this node
* @param string The feature
* @param string The version of the DOM implementation
* @return boolean True if the specified feature is supported
*/
function isSupported($feature, $version = null) {
//don't worry about parsing based on version at this point in time;
//the only feature that is supported is 'XML'...
if (($version == '1.0') || ($version == '2.0') || ($version == null)) {
if (strtoupper($feature) == 'XML') {
return true;
}
}
return false;
} //isSupported
/**
* Formats a string for presentation as HTML
* @param string The string to be formatted
* @param boolean True if the string is to be sent directly to output
* @return string The HTML formatted string
*/
function forHTML($str, $doPrint = false) {
require_once(DOMIT_INCLUDE_PATH . 'xml_domit_utilities.php');
return DOMIT_Utilities::forHTML($str, $doPrint);
} //forHTML
/**
* Generates an array representation of the node and its children
* @abstract
* @return Array A representation of the node and its children
*/
function toArray() {
DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR,
('Method toArray cannot be called by class ' . get_class($this)));
} //toArray
/**
* A node event that can be set to fire upon document loading, used for node initialization
* @abstract
*/
function onLoad() {
//you can override this method if you subclass any of the
//DOMIT_Nodes. It's a way of performing
//initialization of your subclass as soon as the document
//has been loaded (as opposed to as soon as the current node
//has been instantiated).
} //onLoad
/**
* Clears previousSibling, nextSibling, and parentNode references from a node that has been removed
*/
function clearReferences() {
if ($this->previousSibling != null) {
unset($this->previousSibling);
$this->previousSibling = null;
}
if ($this->nextSibling != null) {
unset($this->nextSibling);
$this->nextSibling = null;
}
if ($this->parentNode != null) {
unset($this->parentNode);
$this->parentNode = null;
}
} //clearReferences
/**
* Removes the node from the document
*/
function delete () {
if ($this->parentNode != null) {
$this->parentNode->removeChild($node);
}
} //delete
/**
* Generates a normalized (formatted for readability) representation of the node and its children
* @param boolean True if HTML readable output is desired
* @param boolean True if illegal xml characters in text nodes and attributes should be converted to entities
* @return string The formatted string representation
*/
function toNormalizedString($htmlSafe = false, $subEntities = false) {
//require this file for generating a normalized (readable) xml string representation
require_once(DOMIT_INCLUDE_PATH . 'xml_domit_utilities.php');
global $DOMIT_defined_entities_flip;
$result = DOMIT_Utilities::toNormalizedString($this, $subEntities, $DOMIT_defined_entities_flip);
if ($htmlSafe) $result = $this->forHTML($result);
return $result;
} //toNormalizedString
} //DOMIT_Node
/**
* A parent class for nodes which possess child nodes
*
* @package domit-xmlparser
* @subpackage domit-xmlparser-main
* @author John Heinstein <johnkarl@nbnet.nb.ca>
*/
class DOMIT_ChildNodes_Interface extends DOMIT_Node {
/**
* Raises error if abstract class is directly instantiated
*/
function DOMIT_ChildNodes_Interface() {
DOMIT_DOMException::raiseException(DOMIT_ABSTRACT_CLASS_INSTANTIATION_ERR,
'Cannot instantiate abstract class DOMIT_ChildNodes_Interface');
} //DOMIT_ChildNodes_Interface
/**
* Appends a node to the childNodes list of the current node
* @param Object The node to be appended
* @return Object The appended node
*/
function &appendChild(&$child) {
if ($child->nodeType == DOMIT_ATTRIBUTE_NODE) {
DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR,
('Cannot add a node of type ' . get_class($child) . ' using appendChild'));
}
else if ($child->nodeType == DOMIT_DOCUMENT_FRAGMENT_NODE) {
$total = $child->childCount;
for ($i = 0; $i < $total; $i++) {
$currChild =& $child->childNodes[$i];
$this->appendChild($currChild);
}
}
else {
if (!($this->hasChildNodes())) {
$this->childNodes[0] =& $child;
$this->firstChild =& $child;
}
else {
//remove $child if it already exists
$index = $this->getChildNodeIndex($this->childNodes, $child);
if ($index != -1) {
$this->removeChild($child);
}
//append child
$numNodes = $this->childCount;
$prevSibling =& $this->childNodes[($numNodes - 1)];
$this->childNodes[$numNodes] =& $child;
//set next and previous relationships
$child->previousSibling =& $prevSibling;
$prevSibling->nextSibling =& $child;
}
$this->lastChild =& $child;
$child->parentNode =& $this;
unset($child->nextSibling);
$child->nextSibling = null;
$child->setOwnerDocument($this);
$this->childCount++;
}
return $child;
} //appendChild
/**
* Inserts a node to the childNodes list of the current node
* @param Object The node to be inserted
* @param Object The node before which the insertion is to occur
* @return Object The inserted node
*/
function &insertBefore(&$newChild, &$refChild) {
if ($newChild->nodeType == DOMIT_ATTRIBUTE_NODE) {
DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR,
('Cannot add a node of type ' . get_class($newChild) . ' using insertBefore'));
}
if (($refChild->nodeType == DOMIT_DOCUMENT_NODE) ||
//($refChild->parentNode->nodeType == DOMIT_DOCUMENT_NODE) ||
($refChild->parentNode == null)) {
DOMIT_DOMException::raiseException(DOMIT_NOT_FOUND_ERR,
'Reference child not present in the child nodes list.');
}
//if reference child is also the node to be inserted
//leave the document as is and don't raise an exception
if ($refChild->uid == $newChild->uid) {
return $newChild;
}
//if $newChild is a DocumentFragment,
//loop through and insert each node separately
if ($newChild->nodeType == DOMIT_DOCUMENT_FRAGMENT_NODE) {
$total = $newChild->childCount;
for ($i = 0; $i < $total; $i++) {
$currChild =& $newChild->childNodes[$i];
$this->insertBefore($currChild, $refChild);
}
return $newChild;
}
//remove $newChild if it already exists
$index = $this->getChildNodeIndex($this->childNodes, $newChild);
if ($index != -1) {
$this->removeChild($newChild);
}
//find index of $refChild in childNodes
$index = $this->getChildNodeIndex($this->childNodes, $refChild);
if ($index != -1) {
//reset sibling chain
if ($refChild->previousSibling != null) {
$refChild->previousSibling->nextSibling =& $newChild;
$newChild->previousSibling =& $refChild->previousSibling;
}
else {
$this->firstChild =& $newChild;
if ($newChild->previousSibling != null) {
unset($newChild->previousSibling);
$newChild->previousSibling = null;
}
}
$newChild->parentNode =& $refChild->parentNode;
$newChild->nextSibling =& $refChild;
$refChild->previousSibling =& $newChild;
//add node to childNodes
$i = $this->childCount;
while ($i >= 0) {
if ($i > $index) {
$this->childNodes[$i] =& $this->childNodes[($i - 1)];
}
else if ($i == $index) {
$this->childNodes[$i] =& $newChild;
}
$i--;
}
$this->childCount++;
}
else {
$this->appendChild($newChild);
}
$newChild->setOwnerDocument($this);
return $newChild;
} //insertBefore
/**
* Replaces a node with another
* @param Object The new node
* @param Object The old node
* @return Object The new node
*/
function &replaceChild(&$newChild, &$oldChild) {
if ($newChild->nodeType == DOMIT_ATTRIBUTE_NODE) {
DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR,
('Cannot add a node of type ' . get_class($newChild) . ' using replaceChild'));
}
else if ($newChild->nodeType == DOMIT_DOCUMENT_FRAGMENT_NODE) { //if $newChild is a DocumentFragment
//replace the first node then loop through and insert each node separately
$total = $newChild->childCount;
if ($total > 0) {
$newRef =& $newChild->lastChild;
$this->replaceChild($newRef, $oldChild);
for ($i = 0; $i < ($total - 1); $i++) {
$currChild =& $newChild->childNodes[$i];
$this->insertBefore($currChild, $newRef);
}
}
return $newChild;
}
else {
if ($this->hasChildNodes()) {
//remove $newChild if it already exists
$index = $this->getChildNodeIndex($this->childNodes, $newChild);
if ($index != -1) {
$this->removeChild($newChild);
}
//find index of $oldChild in childNodes
$index = $this->getChildNodeIndex($this->childNodes, $oldChild);
if ($index != -1) {
$newChild->ownerDocument =& $oldChild->ownerDocument;
$newChild->parentNode =& $oldChild->parentNode;
//reset sibling chain
if ($oldChild->previousSibling == null) {
unset($newChild->previousSibling);
$newChild->previousSibling = null;
}
else {
$oldChild->previousSibling->nextSibling =& $newChild;
$newChild->previousSibling =& $oldChild->previousSibling;
}
if ($oldChild->nextSibling == null) {
unset($newChild->nextSibling);
$newChild->nextSibling = null;
}
else {
$oldChild->nextSibling->previousSibling =& $newChild;
$newChild->nextSibling =& $oldChild->nextSibling;
}
$this->childNodes[$index] =& $newChild;
if ($index == 0) $this->firstChild =& $newChild;
if ($index == ($this->childCount - 1)) $this->lastChild =& $newChild;
$newChild->setOwnerDocument($this);
return $newChild;
}
}
DOMIT_DOMException::raiseException(DOMIT_NOT_FOUND_ERR,
('Reference node for replaceChild not found.'));
}
} //replaceChild
/**
* Removes a node from the childNodes list of the current node
* @param Object The node to be removed
* @return Object The removed node
*/
function &removeChild(&$oldChild) {
if ($this->hasChildNodes()) {
//find index of $oldChild in childNodes
$index = $this->getChildNodeIndex($this->childNodes, $oldChild);
if ($index != -1) {
//reset sibling chain
if (($oldChild->previousSibling != null) && ($oldChild->nextSibling != null)) {
$oldChild->previousSibling->nextSibling =& $oldChild->nextSibling;
$oldChild->nextSibling->previousSibling =& $oldChild->previousSibling;
}
else if (($oldChild->previousSibling != null) && ($oldChild->nextSibling == null)) {
$this->lastChild =& $oldChild->previousSibling;
unset($oldChild->previousSibling->nextSibling);
$oldChild->previousSibling->nextSibling = null;
}
else if (($oldChild->previousSibling == null) && ($oldChild->nextSibling != null)) {
unset($oldChild->nextSibling->previousSibling);
$oldChild->nextSibling->previousSibling = null;
$this->firstChild =& $oldChild->nextSibling;
}
else if (($oldChild->previousSibling == null) && ($oldChild->nextSibling == null)) {
unset($this->firstChild);
$this->firstChild = null;
unset($this->lastChild);
$this->lastChild = null;
}
$total = $this->childCount;
//remove node from childNodes
for ($i = 0; $i < $total; $i++) {
if ($i == ($total - 1)) {
array_splice($this->childNodes, $i, 1);
}
else if ($i >= $index) {
$this->childNodes[$i] =& $this->childNodes[($i + 1)];
}
}
$this->childCount--;
$oldChild->clearReferences();
return $oldChild;
}
}
DOMIT_DOMException::raiseException(DOMIT_NOT_FOUND_ERR,
('Target node for removeChild not found.'));
} //removeChild
/**
* Searches the element tree for an element with the specified attribute name and value.
* @param string The value of the attribute
* @param string The name of the attribute
* @param boolean True if the first found node is to be returned as a node instead of a nodelist
* @param boolean True if uid is to be considered an attribute
* @return object A NodeList of found elements, or null
*/
function &getElementsByAttribute($attrName = 'id', $attrValue = '',
$returnFirstFoundNode = false, $treatUIDAsAttribute = false) {
require_once(DOMIT_INCLUDE_PATH . 'xml_domit_nodemaps.php');
$nodelist = new DOMIT_NodeList();
switch ($this->nodeType) {
case DOMIT_ELEMENT_NODE:
$this->_getElementsByAttribute($nodelist, $attrName, $attrValue,
$returnFirstFoundNode, $treatUIDAsAttribute);
break;
case DOMIT_DOCUMENT_NODE:
if ($this->documentElement != null) {
$this->documentElement->_getElementsByAttribute($nodelist,
$attrName, $attrValue, $returnFirstFoundNode, $treatUIDAsAttribute);
}
break;
}
if ($returnFirstFoundNode) {
if ($nodelist->getLength() > 0) {
return $nodelist->item(0);
}
else {
$null = null;
return $null;
}
}
else {
return $nodelist;
}
} //getElementsByAttribute
/**
* Searches the element tree for an element with the specified attribute name and value.
* @param object The node list of found elements
* @param string The value of the attribute
* @param string The name of the attribute
* @param boolean True if the first found node is to be returned as a node instead of a nodelist
* @param boolean True if uid is to be considered an attribute
* @param boolean True the node has been found
*/
function _getElementsByAttribute(&$nodelist, $attrName, $attrValue,
$returnFirstFoundNode, $treatUIDAsAttribute, $foundNode = false) {
if (!($foundNode && $returnFirstFoundNode)) {
if (($this->getAttribute($attrName) == $attrValue) ||
($treatUIDAsAttribute && ($attrName == 'uid') && ($this->uid == $attrValue))) {
$nodelist->appendNode($this);
$foundNode = true;
if ($returnFirstFoundNode) return;
}
$total = $this->childCount;
for ($i = 0; $i < $total; $i++) {
$currNode =& $this->childNodes[$i];
if ($currNode->nodeType == DOMIT_ELEMENT_NODE) {
$currNode->_getElementsByAttribute($nodelist,
$attrName, $attrValue, $returnFirstFoundNode,
$treatUIDAsAttribute, $foundNode);
}
}
}
} //_getElementsByAttribute
/**
* Performs an XPath query
* @param string The query pattern
* @return Object A NodeList containing the found nodes
*/
function &selectNodes($pattern, $nodeIndex = 0) {
require_once(DOMIT_INCLUDE_PATH . 'xml_domit_xpath.php');
$xpParser = new DOMIT_XPath();
return $xpParser->parsePattern($this, $pattern, $nodeIndex);
} //selectNodes
/**
* Converts the childNodes array into a NodeList object
* @return Object A NodeList containing elements of the childNodes array
*/
function &childNodesAsNodeList() {
require_once('xml_domit_nodemaps.php');
$myNodeList = new DOMIT_NodeList();
$total = $this->childCount;
for ($i = 0; $i < $total; $i++) {
$myNodeList->appendNode($this->childNodes[$i]);
}
return $myNodeList;
} //childNodesAsNodeList
} //DOMIT_ChildNodes_Interface
/**
* A class representing the DOM Document
*
* @package domit-xmlparser
* @subpackage domit-xmlparser-main
* @author John Heinstein <johnkarl@nbnet.nb.ca>
*/
class DOMIT_Document extends DOMIT_ChildNodes_Interface {
/** @var Object The xml declaration processing instruction */
var $xmlDeclaration;
/** @var Object A reference to a DOMIT_DocType object */
var $doctype;
/** @var Object A reference to the root node of the DOM document */
var $documentElement;
/** @var string The parser used to process the DOM document, either "EXPAT" or "SAXY" */
var $parser;
/** @var Object A reference to the DOMIT_DOMImplementation object */
var $implementation;
/** @var boolean True if the DOM document has been modifed since being parsed (NOT YET IMPLEMENTED!) */
var $isModified;
/** @var boolean True if whitespace is to be preserved during parsing */
var $preserveWhitespace = false;
/** @var Array User defined translation table for XML entities; passed to SAXY */
var $definedEntities = array();
/** @var boolean If true, loadXML or parseXML will attempt to detect and repair invalid xml */
var $doResolveErrors = false;
/** @var boolean If true, elements tags will be rendered to string as <element></element> rather than <element/> */
var $doExpandEmptyElementTags = false;
/** @var array A list of exceptions to the empty element expansion rule */
var $expandEmptyElementExceptions = array();
/** @var boolean If true, namespaces will be processed */
var $isNamespaceAware = false;
/** @var int The error code returned by the SAX parser */
var $errorCode = 0;
/** @var string The error string returned by the SAX parser */
var $errorString = '';
/** @var object A reference to a http connection or proxy server, if one is required */
var $httpConnection = null;
/** @var boolean True if php_http_client_generic is to be used instead of PHP get_file_contents to retrieve xml data */
var $doUseHTTPClient = false;
/** @var array An array of namespacesURIs mapped to prefixes */
var $namespaceURIMap = array();
/**
* DOM Document constructor
*/
function DOMIT_Document() {
$this->_constructor();
$this->xmlDeclaration = null;
$this->doctype = null;
$this->documentElement = null;
$this->nodeType = DOMIT_DOCUMENT_NODE;
$this->nodeName = '#document';
$this->ownerDocument =& $this;
$this->parser = '';
$this->implementation = new DOMIT_DOMImplementation();
} //DOMIT_Document
/**
* Specifies whether DOMIT! will try to fix invalid XML before parsing begins
* @param boolean True if errors are to be resolved
*/
function resolveErrors($truthVal) {
$this->doResolveErrors = $truthVal;
} //resolveErrors
/**
* Specifies whether DOMIT! processes namespace information
* @param boolean True if namespaces are to be processed
*/
function setNamespaceAwareness($truthVal) {
$this->isNamespaceAware = $truthVal;
} //setNamespaceAwareness
/**
* Specifies whether DOMIT! preserves whitespace when parsing
* @param boolean True if whitespace is to be preserved
*/
function preserveWhitespace($truthVal) {
$this->preserveWhitespace = $truthVal;
} //preserveWhitespace
/**
* Specifies the parameters of the http conection used to obtain the xml data
* @param string The ip address or domain name of the connection
* @param string The path of the connection
* @param int The port that the connection is listening on
* @param int The timeout value for the connection
* @param string The user name, if authentication is required
* @param string The password, if authentication is required
*/
function setConnection($host, $path = '/', $port = 80, $timeout = 0, $user = null, $password = null) {
require_once(DOMIT_INCLUDE_PATH . 'php_http_client_generic.php');
$this->httpConnection = new php_http_client_generic($host, $path, $port, $timeout, $user, $password);
} //setConnection
/**
* Specifies basic authentication for an http connection
* @param string The user name
* @param string The password
*/
function setAuthorization($user, $password) {
$this->httpConnection->setAuthorization($user, $password);
} //setAuthorization
/**
* Specifies that a proxy is to be used to obtain the xml data
* @param string The ip address or domain name of the proxy
* @param string The path to the proxy
* @param int The port that the proxy is listening on
* @param int The timeout value for the connection
* @param string The user name, if authentication is required
* @param string The password, if authentication is required
*/
function setProxyConnection($host, $path = '/', $port = 80, $timeout = 0, $user = null, $password = null) {
require_once(DOMIT_INCLUDE_PATH . 'php_http_proxy.php');
$this->httpConnection = new php_http_proxy($host, $path, $port, $timeout, $user, $password);
} //setProxyConnection
/**
* Specifies basic authentication for the proxy
* @param string The user name
* @param string The password
*/
function setProxyAuthorization($user, $password) {
$this->httpConnection->setProxyAuthorization($user, $password);
} //setProxyAuthorization
/**
* Specifies whether an HTTP client should be used to establish a connection
* @param boolean True if an HTTP client is to be used to establish the connection
*/
function useHTTPClient($truthVal) {
$this->doUseHTTPClient = $truthVal;
} //useHTTPClient
/**
* Returns the error code from the underlying SAX parser
* @return int The error code
*/
function getErrorCode() {
return $this->errorCode;
} //getErrorCode
/**
* Returns the error string from the underlying SAX parser
* @return string The error string
*/
function getErrorString() {
return $this->errorString;
} //getErrorString
/**
* Specifies whether elements tags will be rendered to string as <element></element> rather than <element/>
* @param boolean True if the expanded form is to be used
* @param mixed An array of tag names that should be excepted from expandEmptyElements rule (optional)
*/
function expandEmptyElementTags($truthVal, $expandEmptyElementExceptions = false) {
$this->doExpandEmptyElementTags = $truthVal;
if (is_array($expandEmptyElementExceptions)) {
$this->expandEmptyElementExceptions = $expandEmptyElementExceptions;
}
} //expandEmptyElementTags
/**
* Set the specified node as document element
* @param Object The node that is to become document element
* @return Object The new document element
*/
function &setDocumentElement(&$node) {
if ($node->nodeType == DOMIT_ELEMENT_NODE) {
if ($this->documentElement == null) {
parent::appendChild($node);
}
else {
parent::replaceChild($node, $this->documentElement);
}
$this->documentElement =& $node;
}
else {
DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR,
('Cannot add a node of type ' . get_class($node) . ' as a Document Element.'));
}
return $node;
} //setDocumentElement
/**
* Imports a node (and optionally its children) from another DOM Document
* @param object A reference to the node to be imported
* @param boolean True if the children of the imported node are also to be imported
* @return object The imported node (and, if specified, its children)
*/
function &importNode(&$importedNode, $deep = true) {
$parentNode = null;
return $this->_importNode($parentNode, $importedNode, $deep);
} //importNode
/**
* Imports a node (and optionally its children) from another DOM Document
* @param object A reference to the parent of the node to be imported
* @param object A reference to the node to be imported
* @param boolean True if the children of the imported node are also to be imported
* @return object The imported node if it is the top level node, otherwise null
*/
function &_importNode(&$parentNode, &$sourceNode, $deep) {
$hasChildren = false;