ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
inc.xml5compliance.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | Copyright (c) by Alexandre Alapetite, |
5 | http://alexandre.alapetite.net/cv/alexandre-alapetite.en.html |
6 | http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/ |
7 | Modifications by Alex Killing, alex.killing@gmx.de (search for ##) |
8 |-----------------------------------------------------------------------------|
9 | Allows PHP4/DOMXML scripts to run on PHP5/DOM |
10 | |
11 | Typical use: |
12 | { |
13 | if (version_compare(PHP_VERSION,'5','>=')) |
14 | require_once('domxml-php4-to-php5.php'); |
15 | } |
16 |-----------------------------------------------------------------------------|
17 | This code is published under Creative Commons |
18 | Attribution-ShareAlike 2.0 "BY-SA" licence. |
19 | See http://creativecommons.org/licenses/by-sa/2.0/ for details. |
20 +-----------------------------------------------------------------------------+
21*/
22
23
25{
26 return new php4DOMDocument($filename);
27}
28
29/*
30* ##added
31*/
32function domxml_open_mem($str)
33{
34 return new php4DOMDocument($str, false);
35}
36
37function xpath_eval($xpath_context,$eval_str)
38{
39 return $xpath_context->query($eval_str);
40}
41
42function xpath_new_context($dom_document)
43{
44 return new php4DOMXPath($dom_document);
45}
46
47class php4DOMAttr extends php4DOMNode
48{
49 var $myDOMAttr;
50
51 function php4DOMAttr($aDOMAttr)
52 {
53 $this->myDOMAttr=$aDOMAttr;
54 }
55
56 function Name()
57 {
58 return $this->myDOMAttr->name;
59 }
60
61 function Specified()
62 {
63 return $this->myDOMAttr->specified;
64 }
65
66 function Value()
67 {
68 return $this->myDOMAttr->value;
69 }
70}
71
73{
75
76 function php4DOMCDATASection($aDOMCDATASection)
77 {
78 parent::php4DOMNode($aDOMCDATASection); // #added
79 $this->myDOMCDATASection=$aDOMCDATASection;
80 }
81}
82
84{
86
87 // ##altered
88 function php4DOMDocument($source, $file = true)
89 {
90 $this->myDOMDocument=new DOMDocument();
91 if ($file)
92 {
93 $this->myDOMDocument->load($source);
94 }
95 else
96 {
97 $this->myDOMDocument->loadXML($source);
98 }
99 }
100
101 // ##added
102 function xpath_init()
103 {
104 }
105
106 function free()
107 {
108 unset($this->myDOMDocument);
109 }
110
111 // ##added
113 {
114 return xpath_new_context($this);
115 }
116
117 // ##added
118 function dump_node($node)
119 {
120 $str = $this->myDOMDocument->saveXML($node->myDOMNode);
121 return $str;
122 }
123
124 // ##added
125 function validate(&$error)
126 {
127 $ok = $this->myDOMDocument->validate();
128 if (!$ok)
129 {
130 $error = array(array("0", "Unknown Error"));
131 }
132 return $error;
133 }
134
135 function create_attribute($name,$value)
136 {
137 $myAttr=$this->myDOMDocument->createAttribute($name);
138 $myAttr->value=$value;
139
140 return new php4DOMAttr($myAttr);
141 }
142
143 function create_cdata_section($content)
144 {
145 return new php4DOMCDATASection($this->myDOMDocument->createCDATASection($content));
146 }
147
149 {
150 return new php4DOMElement($this->myDOMDocument->createComment($data));
151 }
152
153 function create_element($name)
154 {
155 return new php4DOMElement($this->myDOMDocument->createElement($name));
156 }
157
158 function create_text_node($content)
159 {
160 return new php4DOMNode($this->myDOMDocument->createTextNode($content));
161 }
162
164 {
165 return new php4DOMElement($this->myDOMDocument->documentElement);
166 }
167
168 function dump_file($filename,$compressionmode=false,$format=false)
169 {
170 return $this->myDOMDocument->save($filename);
171 }
172
173 function dump_mem($format=false,$encoding=false)
174 {
175 return $this->myDOMDocument->saveXML();
176 }
177
179 {
180 $myDOMNodeList=$this->myDOMDocument->getElementsByTagName($name);
181 $nodeSet=array();
182 $i=0;
183 while ($node=$myDOMNodeList->item($i))
184 {
185 $nodeSet[]=new php4DOMElement($node);
186 $i++;
187 }
188
189 return $nodeSet;
190 }
191
192 function html_dump_mem()
193 {
194 return $this->myDOMDocument->saveHTML();
195 }
196
197}
198
202class php4DOMElement extends php4DOMNode
203{
204 function get_attribute($name)
205 {
206 return $this->myDOMNode->getAttribute($name);
207 }
208
210 {
211 $myDOMNodeList=$this->myDOMNode->getElementsByTagName($name);
212 $nodeSet=array();
213 $i=0;
214 while ($node=$myDOMNodeList->item($i))
215 {
216 $nodeSet[]=new php4DOMElement($node);
217 $i++;
218 }
219
220 return $nodeSet;
221 }
222
223 function has_attribute($name)
224 {
225 return $this->myDOMNode->hasAttribute($name);
226 }
227
228 function remove_attribute($name)
229 {
230 return $this->myDOMNode->removeAttribute($name);
231 }
232
233 function set_attribute($name,$value)
234 {
235 return $this->myDOMNode->setAttribute($name,$value);
236 }
237
238 function tagname()
239 {
240 return $this->myDOMNode->tagName;
241 }
242
243 // ##added
245 {
246 // the following replace has been added to conform with PHP4.
247 // A set_content("&amp;") brought a get_content() = "&" there,
248 // whereas PHP5 gives a get_content() = "&amp;"
249 $text = str_replace("&lt;", "<", $text);
250 $text = str_replace("&gt;", ">", $text);
251 $text = str_replace("&amp;", "&", $text);
252
253 $text_node =& new DOMText();
254 $text_node->appendData($text);
255 if (is_object($this->myDOMNode->firstChild))
256 {
257 $this->myDOMNode->replaceChild($text_node, $this->myDOMNode->firstChild);
258 }
259 else
260 {
261 $this->myDOMNode->appendChild($text_node);
262 }
263 }
264
265 // ##added
266 function get_content()
267 {
268 $text_node =& $this->myDOMNode->firstChild;
269
270 if (is_object($text_node))
271 {
272 return $text_node->textContent;
273 }
274 else
275 {
276 return "";
277 }
278 }
279
280 // ## added
281 function unlink($aDomNode)
282 {
283 parent::unlink_node($aDomNode);
284 }
285
286}
287
291class php4DOMNode
292{
293 var $myDOMNode;
294
295 function php4DOMNode($aDomNode)
296 {
297 $this->myDOMNode=$aDomNode;
298 }
299
300 function append_child($newnode)
301 {
302//echo "BH";
303 //if (strtolower(get_class($newnode)) != "php4domcdatasection")
304 //{
305 $doc =& $this->myDOMNode->ownerDocument;
306 //echo "<br>BH1:".get_class($newnode).":";
307 $newnode->myDOMNode =& $doc->importNode($newnode->myDOMNode, true);
308 //echo "BH2";
309 return new php4DOMElement($this->myDOMNode->appendChild($newnode->myDOMNode));
310 //}
311 //else
312 //{
313 //}
314 }
315
316 function replace_node($newnode)
317 {
318 return $this->set_content($newnode->myDOMNode->textContent);
319 }
320
321 function append_sibling($newnode)
322 {
323 return new php4DOMElement($this->myDOMNode->parentNode->appendChild($newnode->myDOMNode));
324 }
325
326 function attributes()
327 {
328//echo "<br>node:".$this->myDOMNode->nodeName.":";
329 $myDOMNodeList=$this->myDOMNode->attributes;
330 $nodeSet=array();
331 $i=0;
332 if (is_object($myDOMNodeList))
333 {
334 while ($node=$myDOMNodeList->item($i))
335 {
336 $nodeSet[]=new php4DOMAttr($node);
337 $i++;
338 }
339 }
340
341 return $nodeSet;
342 }
343
344 function child_nodes()
345 {
346 $myDOMNodeList=$this->myDOMNode->childNodes;
347 $nodeSet=array();
348 $i=0;
349 while ($node=$myDOMNodeList->item($i))
350 {
351 $nodeSet[]=new php4DOMElement($node);
352 $i++;
353 }
354 return $nodeSet;
355 }
356
357 // ## added
358 function children()
359 {
360//echo "<br>php4DomNode::children"; flush();
361 return $this->child_nodes();
362 }
363
364 // ## added
365 function unlink_node($aDomNode = "")
366 {
367 // sometimes the node to unlink is passed
368 if (!is_object($aDomNode))
369 {
370 $aDomNode =& $this;
371 }
372
373 $parent =& $aDomNode->myDOMNode->parentNode;
374 if (is_object($parent))
375 {
376 $parent->removeChild($aDomNode->myDOMNode);
377 }
378 }
379
380 function clone_node($deep=false)
381 {
382 return new php4DOMElement($this->myDOMNode->cloneNode($deep));
383 }
384
385 function first_child()
386 {
387 return new php4DOMElement($this->myDOMNode->firstChild);
388 }
389
390 function get_content()
391 {
392 return $this->myDOMNode->textContent;
393 }
394
395 function has_attributes()
396 {
397 return $this->myDOMNode->hasAttributes();
398 }
399
401 {
402 return $this->myDOMNode->hasChildNodes();
403 }
404
405 // ## changed
406 function insert_before($newnode,$refnode)
407 {
408 //echo "BH";
409 $doc =& $this->myDOMNode->ownerDocument;
410 $newnode->myDOMNode =& $doc->importNode($newnode->myDOMNode, true);
411
412 $mydomnode =& $this->myDOMNode;
413 $mynewnode =& $newnode->myDOMNode;
414 $myrefnode =& $refnode->myDOMNode;
415 try
416 {
417 $domel =& $mydomnode->insertBefore($mynewnode,$myrefnode);
418 }
419 catch (DOMException $exception)
420 {
421 // php 4 accepted $this == $refnode -> switch to parent of $this
422 $mydomnode =& $this->myDOMNode->parentNode;
423 $domel =& $mydomnode->insertBefore($mynewnode,$myrefnode);
424 }
425 $el =& new php4DOMElement($domel);
426 return $el;
427 }
428
429 // ## changed
430 function last_child()
431 {
432 $last =& $this->myDOMNode->lastChild;
433
434 if (is_object($last))
435 {
436 return new php4DOMElement($last);
437 }
438 else
439 {
440 return false;
441 }
442 }
443
444 // ## changed
445 function next_sibling()
446 {
447 $next =& $this->myDOMNode->nextSibling;
448
449 if (is_object($next))
450 {
451 return new php4DOMElement($next);
452 }
453 else
454 {
455 return false;
456 }
457 }
458
459 function node_name()
460 {
461 return $this->myDOMNode->nodeName;
462 }
463
464 function node_type()
465 {
466 return $this->myDOMNode->nodeType;
467 }
468
469 function node_value()
470 {
471 return $this->myDOMNode->nodeValue;
472 }
473
474 // ## changed
475 function parent_node()
476 {
477 $parent =& $this->myDOMNode->parentNode;
478
479 if (is_object($parent))
480 {
481 return new php4DOMElement($parent);
482 }
483 else
484 {
485 return false;
486 }
487 }
488
489 // ## changed
491 {
492 $prev =& $this->myDOMNode->previousSibling;
493
494 if (is_object($prev))
495 {
496 return new php4DOMElement($prev);
497 }
498 else
499 {
500 return false;
501 }
502 }
503
504 function remove_child($oldchild)
505 {
506 return new php4DOMElement($this->myDOMNode->removeChild($oldchild->myDOMNode));
507 }
508
509 function replace_child($oldnode,$newnode)
510 {
511 return new php4DOMElement($this->myDOMNode->replaceChild($oldchild->myDOMNode,$newnode->myDOMNode));
512 }
513
515 {
516 $this->myDOMNode->textContent = $text;
517 return $this->myDOMNode->textContent;
518 }
519}
520
521class php4DOMNodelist
522{
523 var $myDOMNodelist;
524 var $nodeset;
525
526 function php4DOMNodelist($aDOMNodelist)
527 {
528 $this->myDOMNodelist=$aDOMNodelist;
529 $this->nodeset=array();
530 $i=0;
531 while ($node=$this->myDOMNodelist->item($i))
532 {
533 $this->nodeset[]=new php4DOMElement($node);
534 $i++;
535 }
536 }
537}
538
539class php4DOMXPath
540{
541 var $myDOMXPath;
542
543 // ## added
544 function xpath_eval($eval_str)
545 {
546 return xpath_eval($this, $eval_str);
547 }
548
549 function php4DOMXPath($dom_document)
550 {
551 $this->myDOMXPath=new DOMXPath($dom_document->myDOMDocument);
552 }
553
554 function query($eval_str)
555 {
556 return new php4DOMNodelist($this->myDOMXPath->query($eval_str));
557 }
558
559 function xpath_register_ns($prefix,$namespaceURI)
560 {
561 return $this->myDOMXPath->registerNamespace($prefix,$namespaceURI);
562 }
563}
564
565?>
print $file
$filename
Definition: buildRTE.php:89
php4DOMAttr($aDOMAttr)
php4DOMCDATASection($aDOMCDATASection)
create_cdata_section($content)
dump_mem($format=false, $encoding=false)
create_attribute($name, $value)
dump_file($filename, $compressionmode=false, $format=false)
php4DOMDocument($source, $file=true)
set_attribute($name, $value)
clone_node($deep=false)
remove_child($oldchild)
php4DOMNode($aDomNode)
replace_node($newnode)
replace_child($oldnode, $newnode)
insert_before($newnode, $refnode)
append_sibling($newnode)
unlink_node($aDomNode="")
append_child($newnode)
php4DOMNodelist($aDOMNodelist)
xpath_eval($eval_str)
php4DOMXPath($dom_document)
xpath_register_ns($prefix, $namespaceURI)
$data
$text
domxml_open_mem($str, $mode=DOMXML_LOAD_PARSING, &$error=NULL)
xpath_eval($xpath_context, $eval_str, $contextnode=null)
domxml_open_file($filename)
xpath_new_context($dom_document)