ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
23function staticxmlerror($errno, $errstr, $errfile, $errline, $errcontext, $ret = false)
24{
25 static $errs = array();
26
27 if ($ret === true) {
28 return $errs;
29 }
30
31 $tag = 'DOMDocument::validate(): ';
32 $errs[] = str_replace($tag, '', $errstr);
33}
34
36{
37 return new php4DOMDocument($filename);
38}
39
40/*
41* ##added
42*/
43function domxml_open_mem($str, $mode = 0, &$error = NULL)
44{
45 if (!is_int($mode))
46 {
47 $mode = 0;
48 }
49 $doc = new php4DOMDocument($str, false, $mode);
50 if (!$doc->success)
51 {
52 $error = $doc->error;
53 }
54
55 return $doc;
56}
57
58function xpath_eval($xpath_context,$eval_str,$contextnode=null)
59{
60 return $xpath_context->query($eval_str,$contextnode);
61}
62
63function xpath_new_context($dom_document)
64{
65 return new php4DOMXPath($dom_document);
66}
67
69{
71
72 function __construct($aDOMAttr)
73 {
74 $this->myDOMAttr=$aDOMAttr;
75 }
76
77 function Name()
78 {
79 return $this->myDOMAttr->name;
80 }
81
82 function Specified()
83 {
84 return $this->myDOMAttr->specified;
85 }
86
87 function Value()
88 {
89 return $this->myDOMAttr->value;
90 }
91}
92
94{
96
97 function __construct($aDOMCDATASection)
98 {
99 parent::php4DOMNode($aDOMCDATASection); // #added
100 $this->myDOMCDATASection=$aDOMCDATASection;
101 }
102}
103
105{
107
108 // ##altered
109 function __construct($source, $file = true, $a_mode = 0)
110 {
111 $this->myDOMDocument=new DOMDocument();
112 // temporary set error handler
113 set_error_handler('staticxmlerror');
114 $old = ini_set('html_errors', false);
115
116 if (is_object($source))
117 {
118 $this->myDOMDocument = $source;
119 $this->success = true;
120 }
121 else
122 {
123 if ($file)
124 {
125 $this->success = @$this->myDOMDocument->load($source,$a_mode);
126 $this->success = @$this->myDOMDocument->load($source,$a_mode);
127 }
128 else
129 {
130 $this->success = $this->myDOMDocument->loadXML($source,$a_mode);
131 }
132 }
133
134 // Restore error handling
135 ini_set('html_errors', $old);
136 restore_error_handler();
137
138 if (!$this->success)
139 {
140 $this->error_arr = staticxmlerror(null, null, null, null, null, true);
141 foreach($this->error_arr as $error)
142 {
143 $error = str_replace("DOMDocument::loadXML():", "", $error);
144 $this->error.= $error."<br />";
145 }
146 }
147 }
148
149 // ##added
150 function xpath_init()
151 {
152 }
153
154 function free()
155 {
156 unset($this->myDOMDocument);
157 }
158
159 // ##added
161 {
162 return xpath_new_context($this);
163 }
164
165 // ##added
166 function dump_node($node)
167 {
168 $str = $this->myDOMDocument->saveXML($node->myDOMNode);
169 return $str;
170 }
171
172 // ##added
173 function validate(&$error)
174 {
175 $ok = $this->myDOMDocument->validate();
176
177 if (!$ok)
178 {
179 $error = array(array("0", "Unknown Error"));
180
181 if (function_exists("libxml_get_last_error"))
182 {
183 $err = libxml_get_last_error();
184
185 if (is_object($err))
186 {
187 $error = array(array($err->code, $err->message));
188 }
189 }
190 }
191 return $error;
192 }
193
194 function create_attribute($name,$value)
195 {
196 $myAttr=$this->myDOMDocument->createAttribute($name);
197 $myAttr->value=$value;
198
199 return new php4DOMAttr($myAttr);
200 }
201
202 function create_cdata_section($content)
203 {
204 return new php4DOMCDATASection($this->myDOMDocument->createCDATASection($content));
205 }
206
208 {
209 return new php4DOMElement($this->myDOMDocument->createComment($data));
210 }
211
212 function create_element($name)
213 {
214 return new php4DOMElement($this->myDOMDocument->createElement($name));
215 }
216
217 function create_text_node($content)
218 {
219 return new php4DOMNode($this->myDOMDocument->createTextNode($content));
220 }
221
223 {
224 return new php4DOMElement($this->myDOMDocument->documentElement);
225 }
226
227 function dump_file($filename,$compressionmode=false,$format=false)
228 {
229 return $this->myDOMDocument->save($filename);
230 }
231
232 function dump_mem($format=false,$encoding=false)
233 {
234 $r = $this->myDOMDocument->saveXML();
235 return $r;
236 }
237
239 {
240 $myDOMNodeList=$this->myDOMDocument->getElementsByTagName($name);
241 $nodeSet=array();
242 $i=0;
243 while ($node=$myDOMNodeList->item($i))
244 {
245 $nodeSet[]=new php4DOMElement($node);
246 $i++;
247 }
248
249 return $nodeSet;
250 }
251
252 function html_dump_mem()
253 {
254 return $this->myDOMDocument->saveHTML();
255 }
256
257}
258
263{
264 function get_attribute($name)
265 {
266 return $this->myDOMNode->getAttribute($name);
267 }
268
269 function owner_document()
270 {
271 return new php4DOMDocument($this->myDOMNode->ownerDocument);
272 }
273
275 {
276 $myDOMNodeList=$this->myDOMNode->getElementsByTagName($name);
277 $nodeSet=array();
278 $i=0;
279 while ($node=$myDOMNodeList->item($i))
280 {
281 $nodeSet[]=new php4DOMElement($node);
282 $i++;
283 }
284
285 return $nodeSet;
286 }
287
288 function has_attribute($name)
289 {
290 return $this->myDOMNode->hasAttribute($name);
291 }
292
293 function remove_attribute($name)
294 {
295 return $this->myDOMNode->removeAttribute($name);
296 }
297
298 function set_attribute($name,$value)
299 {
300 return $this->myDOMNode->setAttribute($name,$value);
301 }
302
303 function tagname()
304 {
305 return $this->myDOMNode->tagName;
306 }
307
308 // ##added
310 {
311 // the following replace has been added to conform with PHP4.
312 // A set_content("&amp;") brought a get_content() = "&" there,
313 // whereas PHP5 gives a get_content() = "&amp;"
314 $text = str_replace("&lt;", "<", $text);
315 $text = str_replace("&gt;", ">", $text);
316 $text = str_replace("&amp;", "&", $text);
317
318 $text_node = new DOMText();
319 $text_node->appendData($text);
320 if (is_object($this->myDOMNode->firstChild))
321 {
322 $this->myDOMNode->replaceChild($text_node, $this->myDOMNode->firstChild);
323 }
324 else
325 {
326 $this->myDOMNode->appendChild($text_node);
327 }
328 }
329
330 // ##added
331 function get_content()
332 {
333 $text_node = $this->myDOMNode->firstChild;
334
335 if (is_object($text_node))
336 {
337 return $text_node->textContent;
338 }
339 else
340 {
341 return "";
342 }
343 }
344
345 // ## added
346 function unlink($aDomNode)
347 {
348 parent::unlink_node($aDomNode);
349 }
350
351}
352
357{
359
360 function __construct($aDomNode)
361 {
362 $this->myDOMNode=$aDomNode;
363 }
364
365 function append_child($newnode)
366 {
367//echo "BH";
368 //if (strtolower(get_class($newnode)) != "php4domcdatasection")
369 //{
370 $doc = $this->myDOMNode->ownerDocument;
371 //echo "<br>BH1:".get_class($newnode).":";
372 $newnode->myDOMNode = $doc->importNode($newnode->myDOMNode, true);
373 //echo "BH2";
374 return new php4DOMElement($this->myDOMNode->appendChild($newnode->myDOMNode));
375 //}
376 //else
377 //{
378 //}
379 }
380
381 function replace_node($newnode)
382 {
383 return $this->set_content($newnode->myDOMNode->textContent);
384 }
385
386 function append_sibling($newnode)
387 {
388 return new php4DOMElement($this->myDOMNode->parentNode->appendChild($newnode->myDOMNode));
389 }
390
391 function attributes()
392 {
393//echo "<br>node:".$this->myDOMNode->nodeName.":";
394 $myDOMNodeList=$this->myDOMNode->attributes;
395 $nodeSet=array();
396 $i=0;
397 if (is_object($myDOMNodeList))
398 {
399 while ($node=$myDOMNodeList->item($i))
400 {
401 $nodeSet[]=new php4DOMAttr($node);
402 $i++;
403 }
404 }
405
406 return $nodeSet;
407 }
408
409 function child_nodes()
410 {
411 $myDOMNodeList=$this->myDOMNode->childNodes;
412 $nodeSet=array();
413 $i=0;
414 while ($node=$myDOMNodeList->item($i))
415 {
416 $nodeSet[]=new php4DOMElement($node);
417 $i++;
418 }
419 return $nodeSet;
420 }
421
422 // ## added
423 function children()
424 {
425//echo "<br>php4DomNode::children"; flush();
426 return $this->child_nodes();
427 }
428
429 // ## added
430 function unlink_node($aDomNode = "")
431 {
432 // sometimes the node to unlink is passed
433 if (!is_object($aDomNode))
434 {
435 $aDomNode = $this;
436 //$aDomNode = $this;
437 }
438
439 $parent = $aDomNode->myDOMNode->parentNode;
440 if (is_object($parent))
441 {
442 $parent->removeChild($aDomNode->myDOMNode);
443 }
444 }
445
446 function clone_node($deep=false)
447 {
448 return new php4DOMElement($this->myDOMNode->cloneNode($deep));
449 }
450
451 function first_child()
452 {
453 return new php4DOMElement($this->myDOMNode->firstChild);
454 }
455
456 function get_content()
457 {
458 return $this->myDOMNode->textContent;
459 }
460
461 function has_attributes()
462 {
463 return $this->myDOMNode->hasAttributes();
464 }
465
467 {
468 return $this->myDOMNode->hasChildNodes();
469 }
470
471 // ## changed
472 function insert_before($newnode,$refnode)
473 {
474 //echo "BH";
475 $doc = $this->myDOMNode->ownerDocument;
476 $newnode->myDOMNode = $doc->importNode($newnode->myDOMNode, true);
477
478 $mydomnode = $this->myDOMNode;
479 $mynewnode = $newnode->myDOMNode;
480 $myrefnode = $refnode->myDOMNode;
481 try
482 {
483 $domel = $mydomnode->insertBefore($mynewnode,$myrefnode);
484 }
485 catch (DOMException $exception)
486 {
487 // php 4 accepted $this == $refnode -> switch to parent of $this
488 $mydomnode = $this->myDOMNode->parentNode;
489 $domel = $mydomnode->insertBefore($mynewnode,$myrefnode);
490 }
491 $el = new php4DOMElement($domel);
492 return $el;
493 }
494
495 // ## changed
496 function last_child()
497 {
498 $last = $this->myDOMNode->lastChild;
499
500 if (is_object($last))
501 {
502 return new php4DOMElement($last);
503 }
504 else
505 {
506 return false;
507 }
508 }
509
510 // ## changed
511 function next_sibling()
512 {
513 $next = $this->myDOMNode->nextSibling;
514
515 if (is_object($next))
516 {
517 return new php4DOMElement($next);
518 }
519 else
520 {
521 return false;
522 }
523 }
524
525 function node_name($a_local = false)
526 {
527 if ($a_local)
528 {
529 return $this->myDOMNode->localName;
530 }
531 else
532 {
533 return $this->myDOMNode->nodeName;
534 }
535 }
536
537 function node_type()
538 {
539 return $this->myDOMNode->nodeType;
540 }
541
542 function node_value()
543 {
544 return $this->myDOMNode->nodeValue;
545 }
546
547 // ## changed
548 function parent_node()
549 {
550 $parent = $this->myDOMNode->parentNode;
551
552 if (is_object($parent))
553 {
554 return new php4DOMElement($parent);
555 }
556 else
557 {
558 return false;
559 }
560 }
561
562 // ## changed
564 {
565 $prev = $this->myDOMNode->previousSibling;
566
567 if (is_object($prev))
568 {
569 return new php4DOMElement($prev);
570 }
571 else
572 {
573 return false;
574 }
575 }
576
577 function remove_child($oldchild)
578 {
579 return new php4DOMElement($this->myDOMNode->removeChild($oldchild->myDOMNode));
580 }
581
582 function replace_child($oldnode,$newnode)
583 {
584 return new php4DOMElement($this->myDOMNode->replaceChild($oldchild->myDOMNode,$newnode->myDOMNode));
585 }
586
588 {
589 $this->myDOMNode->textContent = $text;
590 return $this->myDOMNode->textContent;
591 }
592}
593
595{
598
599 function __construct($aDOMNodelist)
600 {
601 $this->myDOMNodelist=$aDOMNodelist;
602 $this->nodeset=array();
603 $i=0;
604 while ($node=$this->myDOMNodelist->item($i))
605 {
606 $this->nodeset[]=new php4DOMElement($node);
607 $i++;
608 }
609 }
610}
611
613{
615
616 // ## added
617 function xpath_eval($eval_str)
618 {
619 return xpath_eval($this, $eval_str);
620 }
621
622 function __construct($dom_document)
623 {
624 $this->myDOMXPath=new DOMXPath($dom_document->myDOMDocument);
625 }
626
627 function query($eval_str)
628 {
629 return new php4DOMNodelist($this->myDOMXPath->query($eval_str));
630 }
631
632 function xpath_register_ns($prefix,$namespaceURI)
633 {
634 return $this->myDOMXPath->registerNamespace($prefix,$namespaceURI);
635 }
636}
637
638?>
$error
Definition: Error.php:17
An exception for terminatinating execution or to throw for unit testing.
error($a_errmsg)
set error message @access public
__construct($aDOMAttr)
__construct($aDOMCDATASection)
create_cdata_section($content)
dump_mem($format=false, $encoding=false)
create_attribute($name, $value)
dump_file($filename, $compressionmode=false, $format=false)
__construct($source, $file=true, $a_mode=0)
set_attribute($name, $value)
clone_node($deep=false)
remove_child($oldchild)
node_name($a_local=false)
replace_node($newnode)
replace_child($oldnode, $newnode)
insert_before($newnode, $refnode)
__construct($aDomNode)
append_sibling($newnode)
unlink_node($aDomNode="")
append_child($newnode)
__construct($aDOMNodelist)
xpath_eval($eval_str)
__construct($dom_document)
xpath_register_ns($prefix, $namespaceURI)
$text
$r
Definition: example_031.php:79
xpath_eval($xpath_context, $eval_str, $contextnode=null)
staticxmlerror($errno, $errstr, $errfile, $errline, $errcontext, $ret=false)
domxml_open_file($filename)
domxml_open_mem($str, $mode=0, &$error=NULL)
xpath_new_context($dom_document)
$ret
Definition: parser.php:6
$old
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file