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