/
Tree.php
484 lines (444 loc) · 13.7 KB
/
Tree.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
<?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> |
// | Tomas V.V.Cox <cox@idecnet.com> (tree mapping from xml file)|
// | Michele Manzato <michele.manzato@verona.miz.it> |
// +----------------------------------------------------------------------+
//
// $Id$
//
require_once 'XML/Parser.php';
require_once 'XML/Tree/Node.php';
/**
* PEAR::XML_Tree
*
* Purpose
*
* Allows for the building of XML data structures
* using a tree representation, without the need
* for an extension like DOMXML.
*
* Example
*
* $tree = new XML_Tree;
* $root =& $tree->addRoot('root');
* $foo =& $root->addChild('foo');
*
* $tree->dump(true);
*
* @author Bernd Römer <berndr@bonn.edu>
* @package XML
* @version $Version$ - 1.0
*/
class XML_Tree extends XML_Parser
{
/**
* File Handle
*
* @var resource
*/
var $file = NULL;
/**
* Filename from which the XML_Tree was read
*
* @var string
*/
var $filename = '';
/**
* Namespace
*
* @var array
*/
var $namespace = array();
/**
* Root node of the XML tree
*
* @var object XML_Tree_Node
*/
var $root = NULL;
/**
* XML Version
*
* @var string
*/
var $version = '1.0';
/**
* Constructor
*
* @param string filename Filename where to read the XML
* @param string version XML Version to apply
*/
function XML_Tree($filename = '', $version = '1.0')
{
$this->filename = $filename;
$this->version = $version;
}
/**
* Gets the root node
*
* @return object Root XML_Tree_Node, or PEAR_Error if there isn't any root node.
*
* @access public
*/
function &getRoot()
{
if (!is_null($this->root)) {
return $this->root;
}
return $this->raiseError("No root");
}
/**
* Sets the root node of the XML tree.
*
* @param string name Name of root element
*
* @return object XML_Tree_Node Reference to the newly created root node
* @access public
*/
function &addRoot($name, $content = '', $attributes = array())
{
$this->root = new XML_Tree_Node($name, $content, $attributes);
return $this->root;
}
/**
* @deprecated
*/
function &add_root($name, $content = '', $attributes = array()) {
return $this->addRoot($name, $content, $attributes);
}
/**
* Inserts a child/tree (child) into tree ($path,$pos) and maintains
* namespace integrity
*
* @param mixed path Path to parent node to add child (see
* getNodeAt() for format)
* @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 object Reference to the inserted child (node), or PEAR_Error upon error
* @access public
* @see getNodeAt()
*/
function &insertChild($path, $pos, $child, $content = '', $attributes = array())
{
$parent =& $this->getNodeAt($path);
if (PEAR::isError($parent)) {
return $parent;
}
$x =& $parent->insertChild(null, $pos, $child, $content, $attributes);
if (!PEAR::isError($x)) {
// update namespace to maintain namespace integrity
$count = count($path);
foreach ($this->namespace as $key => $val) {
if ((array_slice($val,0,$count)==$path) && ($val[$count]>=$pos)) {
$this->namespace[$key][$count]++;
}
}
}
return $x;
}
/**
* @deprecated
*/
function &insert_child($path,$pos,$child, $content = '', $attributes = array()) {
return $this->insertChild($path, $child, $content, $attributes);
}
/*
* Removes a child node from tree and maintains namespace integrity
*
* @param array path Path to the parent of child to remove (see
* getNodeAt() for format)
* @param integer pos Position of child in parent children-list
* 0 < means |$pos| elements before the end,
* e.g. -1 removes the last child.
*
* @return object Parent XML_Tree_Node whose child was removed, or PEAR_Error upon error
* @access public
* @see getNodeAt()
*/
function &removeChild($path, $pos)
{
$parent =& $this->getNodeAt($path);
if (PEAR::isError($parent)) {
return $parent;
}
$x =& $parent->removeChild($pos);
if (!PEAR::isError($x)) {
// Update namespace to maintain namespace integrity
$count=count($path);
foreach($this->namespace as $key => $val) {
if (array_slice($val,0,$count)==$path) {
if ($val[$count]==$pos) {
unset($this->namespace[$key]); break;
}
if ($val[$count]>$pos) {
$this->namespace[$key][$count]--;
}
}
}
}
return $x;
}
/**
* @deprecated
*/
function &remove_child($path, $pos) {
return $this->removeChild($path, $pos);
}
/*
* Maps a XML file to a XML_Tree
*
* @return mixed The XML tree root (an XML_Tree_Node), or PEAR_Error upon error.
* @access public
*/
function &getTreeFromFile ()
{
$this->folding = false;
$this->XML_Parser(null, 'event');
$err = $this->setInputFile($this->filename);
if (PEAR::isError($err)) {
return $err;
}
$this->cdata = null;
$err = $this->parse();
if (PEAR::isError($err)) {
return $err;
}
return $this->root;
}
/*
* Maps an XML string to an XML_Tree.
*
* @return mixed The XML tree root (an XML_Tree_Node), or PEAR_Error upon error.
* @access public
*/
function getTreeFromString($str)
{
$this->folding = false;
$this->XML_Parser(null, 'event');
$this->cdata = null;
$err = $this->parseString($str);
if (PEAR::isError($err)) {
return $err;
}
return $this->root;
}
/**
* Handler for the xml-data
* Used by XML_Parser::XML_Parser() when parsing an XML stream.
*
* @param mixed xp ignored
* @param string elem name of the element
* @param array attribs attributes for the generated node
*
* @access private
*/
function startHandler($xp, $elem, &$attribs)
{
// root elem
if (!isset($this->i)) {
$this->obj1 =& $this->add_root($elem, null, $attribs);
$this->i = 2;
} else {
// mixed contents
if (!empty($this->cdata)) {
$parent_id = 'obj' . ($this->i - 1);
$parent =& $this->$parent_id;
$parent->children[] = &new XML_Tree_Node(null, $this->cdata);
}
$obj_id = 'obj' . $this->i++;
$this->$obj_id = &new XML_Tree_Node($elem, null, $attribs);
}
$this->cdata = null;
return null;
}
/**
* Handler for the xml-data
* Used by XML_Parser::XML_Parser() when parsing an XML stream.
*
* @param mixed xp ignored
* @param string elem name of the element
*
* @access private
*/
function endHandler($xp, $elem)
{
$this->i--;
if ($this->i > 1) {
$obj_id = 'obj' . $this->i;
// recover the node created in StartHandler
$node =& $this->$obj_id;
// mixed contents
if (count($node->children) > 0) {
if (trim($this->cdata)) {
$node->children[] = &new XML_Tree_Node(null, $this->cdata);
}
} else {
$node->set_content($this->cdata);
}
$parent_id = 'obj' . ($this->i - 1);
$parent =& $this->$parent_id;
// attach the node to its parent node children array
$parent->children[] = $node;
}
$this->cdata = null;
return null;
}
/*
* The xml character data handler
* Used by XML_Parser::XML_Parser() when parsing an XML stream.
*
* @param mixed xp ignored
* @param string data PCDATA between tags
*
* @access private
*/
function cdataHandler($xp, $data)
{
if (trim($data)) {
$this->cdata .= $data;
}
}
/**
* Get a copy of this tree by cloning and all of its nodes, recursively.
*
* @return object XML_Tree copy of this node.
* @access public
*/
function clone()
{
$clone = new XML_Tree($this->filename, $this->version);
if (!is_null($this->root)) {
$clone->root = $this->root->clone();
}
// clone all other vars
$temp = get_object_vars($this);
foreach($temp as $varname => $value) {
if (!in_array($varname,array('filename','version','root'))) {
$clone->$varname=$value;
}
}
return $clone;
}
/**
* Print text representation of XML tree.
*
* @param bool xmlHeader if true then generate also the leading XML
* 'Content-type' header directive, e.g. for
* direct output to a web page.
*
* @access public
*/
function dump($xmlHeader = false)
{
if ($xmlHeader) {
header('Content-type: text/xml');
}
echo $this->get();
}
/**
* Get text representation of XML tree.
*
* @return string Text (XML) representation of the tree
* @access public
*/
function &get()
{
$out = '<?xml version="' . $this->version . "\"?>\n";
if (!is_null($this->root))
{
if(!is_object($this->root) || (get_class($this->root) != 'xml_tree_node'))
return $this->raiseError("Bad XML root node");
$out .= $this->root->get();
}
return $out;
}
/**
* Get current namespace.
*
* @param string name namespace
* @return string
*
* @access public
*/
function &getName($name) {
return $this->root->getElement($this->namespace[$name]);
}
/**
* @deprecated
*/
function &get_name($name) {
return $this->getName($name);
}
/**
* Register a namespace.
*
* @param string $name namespace
* @param string $path path
*
* @access public
*/
function registerName($name, $path) {
$this->namespace[$name] = $path;
}
/**
* @deprecated
*/
function register_name($name, $path) {
return $this->registerName($name, $path);
}
/**
* Get a reference to a node. Node is searched 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
* of them starting from node. Note that the first name in sequence
* must be the name of the document root.
* @return object Reference to the XML_Tree_Node found, or PEAR_Error if
* the path does not exist. If more than one element matches
* then only the first match is returned.
* @access public
*/
function &getNodeAt($path)
{
if (is_null($this->root)){
return $this->raiseError("XML_Tree hasn't a root node");
}
if (is_string($path))
$path = explode("/", $path);
if (sizeof($path) == 0) {
return $this->raiseError("Path to node is empty");
}
$path1 = $path;
$rootName = array_shift($path1);
if ($this->root->name != $rootName) {
return $this->raiseError("Path does not match the document root");
}
$x =& $this->root->getNodeAt($path1);
if (!PEAR::isError($x))
return $x;
// No node with that name found
return $this->raiseError("Bad path to node: [".implode('/', $path)."]");
}
}
?>