/
Node.php
524 lines (476 loc) · 15.7 KB
/
Node.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
<?php
// +----------------------------------------------------------------------+
// | PEAR :: XML_Tree |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Bernd Römer <berndr@bonn.edu> |
// | Sebastian Bergmann <sb@sebastian-bergmann.de> |
// | Christian Kühn <ck@chkuehn.de> (escape xml entities) |
// | Michele Manzato <michele.manzato@verona.miz.it> |
// +----------------------------------------------------------------------+
//
// $Id$
//
/**
* PEAR::XML_Tree_Node
*
* @author Bernd Römer <berndr@bonn.edu>
* @package XML_Tree
* @version 1.0 16-Aug-2001
*/
class XML_Tree_Node {
/**
* Attributes of this node
*
* @var array
*/
var $attributes;
/**
* Children of this node
*
* @var array
*/
var $children;
/**
* Content (text) of this node
*
* @var string
*/
var $content;
/**
* Name of the node
*
* @var string
*/
var $name;
/**
* Constructor
*
* @param string name Node name
* @param string content Node content (text)
* @param array attributes Attribute-hash for the node
*/
function XML_Tree_Node($name, $content = '', $attributes = array())
{
$this->name = $name;
$this->setContent($content);
$this->attributes = $attributes;
$this->children = array();
}
/**
* Append a child node to this node, after all other nodes
*
* @param mixed child Child to insert (XML_Tree or XML_Tree_Node),
* or name of child node
* @param string content Content (text) for the new node (only if
* $child is the node name)
* @param array attributes Attribute-hash for new node
*
* @return object reference to new child node
* @access public
*/
function &addChild($child, $content = '', $attributes = array())
{
$index = sizeof($this->children);
if (is_object($child)) {
if (strtolower(get_class($child)) == 'xml_tree_node') {
$this->children[$index] = $child;
}
if (strtolower(get_class($child)) == 'xml_tree' && isset($child->root)) {
$this->children[$index] = $child->root->getElement();
}
} else {
$this->children[$index] = new XML_Tree_Node($child, $content, $attributes);
}
return $this->children[$index];
}
/**
* @deprecated
*/
function &add_child($child, $content = '', $attributes = array()) {
return $this->addChild($child, $content, $attributes);
}
/**
* Get a copy of this node by clone this node and all of its children,
* recursively.
*
* @return object Reference to the cloned copy.
* @access public
*/
function &clone()
{
$clone = new XML_Tree_Node($this->name,$this->content,$this->attributes);
$max_child=count($this->children);
for($i=0;$i<$max_child;$i++) {
$clone->children[]=$this->children[$i]->clone();
}
/* for future use....
// clone all other vars
$temp=get_object_vars($this);
foreach($temp as $varname => $value)
if (!in_array($varname,array('name','content','attributes','children')))
$clone->$varname=$value;
*/
return $clone;
}
/**
* Inserts child ($child) to a specified child-position ($pos)
*
* @param mixed path Path to parent node to add child (see getNodeAt()
* for format). If null child is inserted in the
* current node.
* @param integer pos Position where to insert the new child.
* 0 < means |$pos| elements before the end,
* e.g. -1 appends as last child.
* @param mixed child Child to insert (XML_Tree or XML_Tree_Node),
* or name of child node
* @param string content Content (text) for the new node (only if
* $child is the node name)
* @param array attributes Attribute-hash for new node
*
* @return Reference to the newly inserted node, or PEAR_Error upon insertion error.
* @access public
*/
function &insertChild($path,$pos,&$child, $content = '', $attributes = array())
{
$parent =& $this->getNodeAt($path);
if (PEAR::isError($parent)) {
// $path was not found
return $parent;
} elseif ($parent != $this) {
// Insert at the node found
return $parent->insertChild(null, $pos, $child, $content, $attributes);
}
if (($pos < -count($this->children)) || ($pos > count($this->children))) {
return new PEAR_Error("Invalid insert position.");
}
if (is_object($child)) { // child is an object
// insert a single node
if (strtolower(get_class($child)) == 'xml_tree_node') {
array_splice($this->children, $pos, 0, 'dummy');
if ($pos < 0) {
$pos = count($this->children) + $pos - 1;
}
$this->children[$pos] = &$child;
// insert a tree i.e insert root-element of tree
} elseif (strtolower(get_class($child)) == 'xml_tree' && isset($child->root)) {
array_splice($this->children, $pos, 0, 'dummy');
if ($pos < 0) {
$pos = count($this->children) + $pos - 1;
}
$this->children[$pos] = $child->root;
} else {
return new PEAR_Error("Bad node (must be a XML_Tree or an XML_Tree_Node)");
}
} else { // child offered is a string
array_splice($this->children, $pos, 0, 'dummy');
if ($pos < 0) {
$pos = count($this->children) + $pos - 1;
}
$this->children[$pos] = new XML_Tree_Node($child, $content, $attributes);
}
return $this;
}
/**
* @deprecated
*/
function &insert_child($path,$pos,&$child, $content = '', $attributes = array()) {
return $this->insertChild($path,$pos,$child, $content, $attributes);
}
/**
* Removes child at a given position
*
* @param integer pos position of child to remove in children-list.
* 0 < means |$pos| elements before the end,
* e.g. -1 removes the last child.
*
* @return mixed The removed node, or PEAR_Error upon removal error.
* @access public
*/
function &removeChild($pos)
{
if (($pos < -count($this->children)) || ($pos >= count($this->children))) {
return new PEAR_Error("Invalid remove position.");
}
// Using array_splice() instead of a simple unset() to maintain index-integrity
return array_splice($this->children, $pos, 1);
}
/**
* @deprecated
*/
function &remove_child($pos) {
return $this->removeChild($pos);
}
/**
* Returns text representation of this node.
*
* @return string text (xml) representation of this node. Each tag is
* indented according to its level.
* @access public
*/
function &get()
{
static $deep = -1;
static $do_ident = true;
$deep++;
if ($this->name !== null) {
$ident = str_repeat(' ', $deep);
if ($do_ident) {
$out = $ident . '<' . $this->name;
} else {
$out = '<' . $this->name;
}
foreach ($this->attributes as $name => $value) {
$out .= ' ' . $name . '="' . $value . '"';
}
$out .= '>' . $this->content;
if (sizeof($this->children) > 0) {
$out .= "\n";
foreach ($this->children as $child) {
$out .= $child->get();
}
} else {
$ident = '';
}
if ($do_ident) {
$out .= $ident . '</' . $this->name . ">\n";
} else {
$out .= '</' . $this->name . '>';
}
$do_ident = true;
} else {
$out = $this->content;
$do_ident = false;
}
$deep--;
return $out;
}
/**
* Get an attribute by its name.
*
* @param string $name Name of attribute to retrieve
*
* @return string attribute, or null if attribute is unset.
* @access public
*/
function getAttribute($name)
{
if (isset($this->attributes[strtolower($name)])) {
return $this->attributes[strtolower($name)];
}
return null;
}
/**
* @deprecated
*/
function get_attribute($name) {
return $this->getAttribute($name);
}
/**
* Sets an attribute for this node.
*
* @param string name Name of attribute to set
* @param string value Value of attribute
*
* @access public
*/
function setAttribute($name, $value = '')
{
$this->attributes[strtolower($name)] = $value;
}
/**
* @deprecated
*/
function set_attribute($name, $value = '')
{
return $this->setAttribute($name, $value);
}
/**
* Unsets an attribute of this node.
*
* @param string $name Name of attribute to unset
*
* @access public
*/
function unsetAttribute($name)
{
if (isset($this->attributes[strtolower($name)])) {
unset($this->attributes[strtolower($name)]);
}
}
/**
* @deprecated
*/
function unset_attribute($name)
{
return $this->unsetAttribute($name);
}
/**
* Sets the content for this node.
*
* @param string content Node content to assign
*
* @access public
*/
function setContent(&$content)
{
$this->content = $this->encodeXmlEntities($content);
}
/**
* @deprecated
*/
function set_content(&$content)
{
return $this->setContent($content);
}
/**
* Gets an element by its 'path'.
*
* @param array path path to element: sequence of indexes to the
* children. E.g. array(1, 2, 3) means "third
* child of second child of first child" of the node.
*
* @return object reference to element found, or PEAR_Error if node can't
* be found.
* @access public
*/
function &getElement($path)
{
if (!is_array($path)) {
$path = array($path);
}
if (sizeof($path) == 0) {
return $this;
}
$path1 = $path;
$next = array_shift($path1);
if (isset($this->children[$next])) {
$x =& $this->children[$next]->getElement($path1);
if (!PEAR::isError($x)) {
return $x;
}
}
return new PEAR_Error("Bad path to node: [".implode('-', $path)."]");
}
/**
* Get a reference to a node by its 'path'.
*
* @param mixed path Path to node. Can be either a string (slash-separated
* children names) or an array (sequence of children names) both
* starting from this node. The first name in sequence
* is a child name, not the name of this node.
*
* @return object Reference to the XML_Tree_Node found, or PEAR_Error if
* the path does not match any node. Note that if more than
* one element matches then only the first matching node is
* returned.
* @access public
*/
function &getNodeAt($path)
{
if (is_string($path))
$path = explode("/", $path);
if (sizeof($path) == 0) {
return $this;
}
$path1 = $path;
$next = array_shift($path1);
// Get the first children of this node whose name is '$next'
$child = null;
for ($i = 0; $i < count($this->children); $i++) {
if ($this->children[$i]->name == $next) {
$child =& $this->children[$i];
break;
}
}
if (!is_null($child)) {
$x =& $child->getNodeAt($path1);
if (!PEAR::isError($x)) {
return $x;
}
}
// No node with that name found
return new PEAR_Error("Bad path to node: [".implode('/', $path)."]");
}
/**
* @deprecated
*/
function &get_element($path)
{
return $this->getElement($path);
}
/**
* Escape XML entities.
*
* @param string xml Text string to escape.
*
* @return string xml
* @access public
*/
function encodeXmlEntities($xml)
{
$xml = str_replace(array('ü', 'Ü', 'ö',
'Ö', 'ä', 'Ä',
'ß'
),
array('ü', 'Ü', 'ö',
'Ö', 'ä', 'Ä',
'ß'
),
$xml
);
$xml = preg_replace(array("/\&([a-z\d\#]+)\;/i",
"/\&/",
"/\#\|\|([a-z\d\#]+)\|\|\#/i",
"/([^a-zA-Z\d\s\<\>\&\;\.\:\=\"\-\/\%\?\!\'\(\)\[\]\{\}\$\#\+\,\@_])/e"
),
array("#||\\1||#",
"&",
"&\\1;",
"'&#'.ord('\\1').';'"
),
$xml
);
return $xml;
}
/**
* Decode XML entities in a text string.
*
* @param string xml Text to decode
*
* @return string Decoded text
* @access public
*/
function decodeXmlEntities($xml)
{
static $trans_tbl = null;
if (!$trans_tbl) {
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
$trans_tbl = array_flip($trans_tbl);
}
for ($i = 1; $i <= 255; $i++) {
$ent = sprintf("&#%03d;", $i);
$ch = chr($i);
$xml = str_replace($ent, $ch, $xml);
}
return strtr($xml, $trans_tbl);
}
/**
* Print text representation of XML_Tree_Node.
*
* @access public
*/
function dump() {
echo $this->get();
}
}
?>