Subversion Repositories Applications.projet

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
431 mathias 1
<?php
2
//
3
// +----------------------------------------------------------------------+
4
// | PHP Version 4                                                        |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2002 The PHP Group                                |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.02 of the PHP license,      |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
16
// | Authors: Bernd Römer <berndr@bonn.edu>                               |
17
// |          Sebastian Bergmann <sb@sebastian-bergmann.de>               |
18
// |          Christian Kühn <ck@chkuehn.de> (escape xml entities)        |
19
// +----------------------------------------------------------------------+
20
//
21
// $Id: Node.php,v 1.1 2005-04-18 16:13:31 jpm Exp $
22
//
23
 
24
/**
25
* PEAR::XML_Tree_Node
26
*
27
* @author  Bernd Römer <berndr@bonn.edu>
28
* @package XML_Tree
29
* @version 1.0  16-Aug-2001
30
*/
31
class XML_Tree_Node {
32
    /**
33
    * Attributes of this node
34
    *
35
    * @var  array
36
    */
37
    var $attributes;
38
 
39
    /**
40
    * Children of this node
41
    *
42
    * @var  array
43
    */
44
    var $children;
45
 
46
    /**
47
    * Content
48
    *
49
    * @var  string
50
    */
51
    var $content;
52
 
53
    /**
54
    * Name
55
    *
56
    * @var  string
57
    */
58
    var $name;
59
 
60
    /**
61
    * Constructor
62
    *
63
    * @param  string  name
64
    * @param  string  content
65
    * @param  array   attributes
66
    */
67
    function XML_Tree_Node($name, $content = '', $attributes = array()) {
68
        $this->attributes = $attributes;
69
        $this->children   = array();
70
        $this->set_content($content);
71
        $this->name       = $name;
72
    }
73
 
74
    /**
75
    * Adds a child node to this node.
76
    *
77
    * @param  mixed   child
78
    * @param  string  content
79
    * @param  array   attributes
80
    * @return object  reference to new child node
81
    */
82
    function &addChild($child, $content = '', $attributes = array()) {
83
        $index = sizeof($this->children);
84
 
85
        if (is_object($child)) {
86
            if (strtolower(get_class($child)) == 'xml_tree_node') {
87
                $this->children[$index] = $child;
88
            }
89
 
90
            if (strtolower(get_class($child)) == 'xml_tree' && isset($child->root)) {
91
                $this->children[$index] = $child->root->get_element();
92
            }
93
        } else {
94
            $this->children[$index] = new XML_Tree_Node($child, $content, $attributes);
95
        }
96
 
97
        return $this->children[$index];
98
    }
99
 
100
    /**
101
    * @deprecated
102
    */
103
    function &add_child($child, $content = '', $attributes = array()) {
104
        return $this->addChild($child, $content, $attributes);
105
    }
106
 
107
    /**
108
    * clone node and all its children (recursive)
109
    *
110
    * @return object reference to the clone-node
111
    */
112
    function &clone() {
113
        $clone=new XML_Tree_Node($this->name,$this->content,$this->attributes);
114
 
115
        $max_child=count($this->children);
116
        for($i=0;$i<$max_child;$i++) {
117
            $clone->children[]=$this->children[$i]->clone();
118
        }
119
 
120
        /* for future use....
121
            // clone all other vars
122
            $temp=get_object_vars($this);
123
            foreach($temp as $varname => $value)
124
                if (!in_array($varname,array('name','content','attributes','children')))
125
                    $clone->$varname=$value;
126
        */
127
 
128
        return($clone);
129
    }
130
 
131
    /**
132
    * inserts child ($child) to a specified child-position ($pos)
133
    *
134
    * @return  inserted node
135
    */
136
    function &insertChild($path,$pos,&$child, $content = '', $attributes = array()) {
137
        // direct insert of objects useing array_splice() faild :(
138
        array_splice($this->children,$pos,0,'dummy');
139
        if (is_object($child)) { // child offered is not instanziated
140
            // insert a single node
141
            if (strtolower(get_class($child)) == 'xml_tree_node') {
142
                $this->children[$pos]=&$child;
143
            }
144
            // insert a tree i.e insert root-element
145
            if (strtolower(get_class($child)) == 'xml_tree' && isset($child->root)) {
146
                $this->children[$pos]=$child->root->get_element();
147
            }
148
        } else { // child offered is not instanziated
149
            $this->children[$pos]=new XML_Tree_Node($child, $content, $attributes);
150
        }
151
        return($this);
152
    }
153
 
154
    /**
155
    * @deprecated
156
    */
157
    function &insert_child($path,$pos,&$child, $content = '', $attributes = array()) {
158
        return $this->insertChild($path,$pos,$child, $content, $attributes);
159
    }
160
 
161
    /**
162
    * removes child ($pos)
163
    *
164
    * @param integer pos position of child in children-list
165
    *
166
    * @return  removed node
167
    */
168
    function &removeChild($pos) {
169
        // array_splice() instead of a simple unset() to maintain index-integrity
170
        return(array_splice($this->children,$pos,1));
171
    }
172
 
173
    /**
174
    * @deprecated
175
    */
176
    function &remove_child($pos) {
177
        return $this->removeChild($pos);
178
    }
179
 
180
    /**
181
    * Returns text representation of this node.
182
    *
183
    * @return  string  xml
184
    */
185
    function &get()
186
    {
187
        static $deep = -1;
188
        static $do_ident = true;
189
        $deep++;
190
        if ($this->name !== null) {
191
            $ident = str_repeat('  ', $deep);
192
            if ($do_ident) {
193
                $out = $ident . '<' . $this->name;
194
            } else {
195
                $out = '<' . $this->name;
196
            }
197
            foreach ($this->attributes as $name => $value) {
198
                $out .= ' ' . $name . '="' . $value . '"';
199
            }
200
 
201
            $out .= '>' . $this->content;
202
 
203
            if (sizeof($this->children) > 0) {
204
                $out .= "\n";
205
                foreach ($this->children as $child) {
206
                    $out .= $child->get();
207
                }
208
            } else {
209
                $ident = '';
210
            }
211
            if ($do_ident) {
212
                $out .= $ident . '</' . $this->name . ">\n";
213
            } else {
214
                $out .= '</' . $this->name . '>';
215
            }
216
            $do_ident = true;
217
        } else {
218
            $out = $this->content;
219
            $do_ident = false;
220
        }
221
        $deep--;
222
        return $out;
223
    }
224
 
225
    /**
226
    * Gets an attribute by its name.
227
    *
228
    * @param  string  name
229
    * @return string  attribute
230
    */
231
    function getAttribute($name) {
232
        return $this->attributes[strtolower($name)];
233
    }
234
 
235
    /**
236
    * @deprecated
237
    */
238
    function get_attribute($name) {
239
        return $this->getAttribute($name);
240
    }
241
 
242
    /**
243
    * Gets an element by its 'path'.
244
    *
245
    * @param  string  path
246
    * @return object  element
247
    */
248
    function &getElement($path) {
249
        if (sizeof($path) == 0) {
250
            return $this;
251
        }
252
 
253
        $next = array_shift($path);
254
 
255
        return $this->children[$next]->get_element($path);
256
    }
257
 
258
    /**
259
    * @deprecated
260
    */
261
    function &get_element($path) {
262
        return $this->getElement($path);
263
    }
264
 
265
    /**
266
    * Sets an attribute.
267
    *
268
    * @param  string  name
269
    * @param  string  value
270
    */
271
    function setAttribute($name, $value = '') {
272
        $this->attributes[strtolower($name)] = $value;
273
    }
274
 
275
    /**
276
    * @deprecated
277
    */
278
    function set_attribute($name, $value = '') {
279
        return $this->setAttribute($name, $value);
280
    }
281
 
282
    /**
283
    * Unsets an attribute.
284
    *
285
    * @param  string  name
286
    */
287
    function unsetAttribute($name) {
288
        unset($this->attributes[strtolower($name)]);
289
    }
290
 
291
    /**
292
    * @deprecated
293
    */
294
    function unset_attribute($name) {
295
        return $this->unsetAttribute($name);
296
    }
297
 
298
    /**
299
    *
300
    *
301
    */
302
    function setContent(&$content)
303
    {
304
        $this->content = $this->_xml_entities($content);
305
    }
306
 
307
    function set_content(&$content)
308
    {
309
        return $this->setContent($content);
310
    }
311
 
312
    /**
313
    * Escape XML entities.
314
    *
315
    * @param   string  xml
316
    * @return  string  xml
317
    * @access  private
318
    */
319
    function _xml_entities($xml) {
320
        $xml = str_replace(array('ü', 'Ü', 'ö',
321
                                 'Ö', 'ä', 'Ä',
322
                                 'ß'
323
                                ),
324
                           array('&#252;', '&#220;', '&#246;',
325
                                 '&#214;', '&#228;', '&#196;',
326
                                 '&#223;'
327
                                ),
328
                           $xml
329
                          );
330
 
331
        $xml = preg_replace(array("/\&([a-z\d\#]+)\;/i",
332
                                  "/\&/",
333
                                  "/\#\|\|([a-z\d\#]+)\|\|\#/i",
334
                                  "/([^a-zA-Z\d\s\<\>\&\;\.\:\=\"\-\/\%\?\!\'\(\)\[\]\{\}\$\#\+\,\@_])/e"
335
                                 ),
336
                            array("#||\\1||#",
337
                                  "&amp;",
338
                                  "&\\1;",
339
                                  "'&#'.ord('\\1').';'"
340
                                 ),
341
                            $xml
342
                           );
343
 
344
        return $xml;
345
    }
346
 
347
    /**
348
    * Print text representation of XML tree.
349
    */
350
    function dump() {
351
        echo $this->get();
352
    }
353
}
354
?>