• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

include/inc.xml5compliance.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | Copyright (c) by Alexandre Alapetite,                                       |
00005         | http://alexandre.alapetite.net/cv/alexandre-alapetite.en.html               |
00006         | http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/                   |
00007         | Modifications by Alex Killing, alex.killing@gmx.de  (search for ##)         |
00008         |-----------------------------------------------------------------------------|
00009         | Allows PHP4/DOMXML scripts to run on PHP5/DOM                               |
00010         |                                                                             |
00011         | Typical use:                                                                |
00012         | {                                                                           |
00013         |       if (version_compare(PHP_VERSION,'5','>='))                                |
00014         |               require_once('domxml-php4-to-php5.php');                              |
00015         | }                                                                           |
00016         |-----------------------------------------------------------------------------|
00017         | This code is published under Creative Commons                               |
00018         | Attribution-ShareAlike 2.0 "BY-SA" licence.                                 |
00019         | See http://creativecommons.org/licenses/by-sa/2.0/ for details.             |
00020         +-----------------------------------------------------------------------------+
00021 */
00022 
00023 function staticxmlerror($errno, $errstr, $errfile, $errline, $errcontext, $ret = false)
00024 {
00025    static $errs = array();
00026 
00027    if ($ret === true) {
00028        return $errs;
00029    }
00030 
00031    $tag = 'DOMDocument::validate(): ';
00032    $errs[] = str_replace($tag, '', $errstr);
00033 }
00034 
00035 function domxml_open_file($filename)
00036 {
00037         return new php4DOMDocument($filename);
00038 }
00039 
00040 /*
00041 * ##added
00042 */
00043 function domxml_open_mem($str, $mode = DOMXML_LOAD_PARSING, &$error = NULL)
00044 {
00045         $doc = new php4DOMDocument($str, false);
00046         if (!$doc->success)
00047         {
00048                 $error = $doc->error;
00049         }
00050         
00051         return $doc;
00052 }
00053 
00054 function xpath_eval($xpath_context,$eval_str,$contextnode=null)
00055 {
00056         return $xpath_context->query($eval_str,$contextnode);
00057 }
00058 
00059 function xpath_new_context($dom_document)
00060 {
00061         return new php4DOMXPath($dom_document);
00062 }
00063 
00064 class php4DOMAttr extends php4DOMNode
00065 {
00066         var $myDOMAttr;
00067 
00068         function php4DOMAttr($aDOMAttr)
00069         {
00070                 $this->myDOMAttr=$aDOMAttr;
00071         }
00072 
00073         function Name()
00074         {
00075                 return $this->myDOMAttr->name;
00076         }
00077 
00078         function Specified()
00079         {
00080                 return $this->myDOMAttr->specified;
00081         }
00082 
00083         function Value()
00084         {
00085                 return $this->myDOMAttr->value;
00086         }
00087 }
00088 
00089 class php4DOMCDATASection extends php4DOMNode
00090 {
00091         var $myDOMCDATASection;
00092 
00093         function php4DOMCDATASection($aDOMCDATASection)
00094         {
00095                 parent::php4DOMNode($aDOMCDATASection);                                         // #added
00096                 $this->myDOMCDATASection=$aDOMCDATASection;
00097         }
00098 }
00099 
00100 class php4DOMDocument
00101 {
00102         var $myDOMDocument;
00103 
00104         // ##altered
00105         function php4DOMDocument($source, $file = true)
00106         {
00107                 $this->myDOMDocument=new DOMDocument();
00108                 
00109                 // temporary set error handler
00110                 set_error_handler('staticxmlerror');
00111                 $old = ini_set('html_errors', false);
00112 
00113                 if (is_object($source))
00114                 {
00115                         $this->myDOMDocument = $source;
00116                         $this->success = true;
00117                 }
00118                 else
00119                 {
00120                         if ($file)
00121                         {
00122                                 $this->success = @$this->myDOMDocument->load($source);
00123                         }
00124                         else
00125                         {
00126                                 $this->success = @$this->myDOMDocument->loadXML($source);
00127                         }
00128                 }
00129                                 
00130                 // Restore error handling
00131                 ini_set('html_errors', $old);
00132                 restore_error_handler();
00133                 
00134                 if (!$this->success)
00135                 {
00136                         $this->error_arr = staticxmlerror(null, null, null, null, null, true);
00137                         foreach($this->error_arr as $error)
00138                         {
00139                                 $error = str_replace("DOMDocument::loadXML():", "", $error);
00140                                 $this->error.= $error."<br />";
00141                         }
00142                 }
00143         }
00144 
00145         // ##added
00146         function xpath_init()
00147         {
00148         }
00149 
00150         function free()
00151         {
00152                 unset($this->myDOMDocument);
00153         }
00154 
00155         // ##added
00156         function xpath_new_context()
00157         {
00158                 return xpath_new_context($this);
00159         }
00160 
00161         // ##added
00162         function dump_node($node)
00163         {
00164                 $str = $this->myDOMDocument->saveXML($node->myDOMNode);
00165                 return $str;
00166         }
00167 
00168         // ##added
00169         function validate(&$error)
00170         {
00171                 $ok = $this->myDOMDocument->validate();
00172                 if (!$ok)
00173                 {
00174                         $error = array(array("0", "Unknown Error"));
00175                 }
00176                 return $error;
00177         }
00178 
00179         function create_attribute($name,$value)
00180         {
00181                 $myAttr=$this->myDOMDocument->createAttribute($name);
00182                 $myAttr->value=$value;
00183 
00184                 return new php4DOMAttr($myAttr);
00185         }
00186 
00187         function create_cdata_section($content)
00188         {
00189                 return new php4DOMCDATASection($this->myDOMDocument->createCDATASection($content));
00190         }
00191 
00192         function create_comment($data)
00193         {
00194                 return new php4DOMElement($this->myDOMDocument->createComment($data));
00195         }
00196 
00197         function create_element($name)
00198         {
00199                 return new php4DOMElement($this->myDOMDocument->createElement($name));
00200         }
00201 
00202         function create_text_node($content)
00203         {
00204                 return new php4DOMNode($this->myDOMDocument->createTextNode($content));
00205         }
00206 
00207         function document_element()
00208         {
00209                 return new php4DOMElement($this->myDOMDocument->documentElement);
00210         }
00211 
00212         function dump_file($filename,$compressionmode=false,$format=false)
00213         {
00214                 return $this->myDOMDocument->save($filename);
00215         }
00216 
00217         function dump_mem($format=false,$encoding=false)
00218         {
00219                 return $this->myDOMDocument->saveXML();
00220         }
00221 
00222         function get_elements_by_tagname($name)
00223         {
00224                 $myDOMNodeList=$this->myDOMDocument->getElementsByTagName($name);
00225                 $nodeSet=array();
00226                 $i=0;
00227                 while ($node=$myDOMNodeList->item($i))
00228                 {
00229                         $nodeSet[]=new php4DOMElement($node);
00230                         $i++;
00231                 }
00232 
00233                 return $nodeSet;
00234         }
00235 
00236         function html_dump_mem()
00237         {
00238                 return $this->myDOMDocument->saveHTML();
00239         }
00240         
00241 }
00242 
00246 class php4DOMElement extends php4DOMNode
00247 {
00248         function get_attribute($name)
00249         {
00250                 return $this->myDOMNode->getAttribute($name);
00251         }
00252         
00253         function owner_document()
00254         {
00255                 return new php4DOMDocument($this->myDOMNode->ownerDocument);
00256         }
00257 
00258         function get_elements_by_tagname($name)
00259         {
00260                 $myDOMNodeList=$this->myDOMNode->getElementsByTagName($name);
00261                 $nodeSet=array();
00262                 $i=0;
00263                 while ($node=$myDOMNodeList->item($i))
00264                 {
00265                         $nodeSet[]=new php4DOMElement($node);
00266                         $i++;
00267                 }
00268 
00269                 return $nodeSet;
00270         }
00271 
00272         function has_attribute($name)
00273         {
00274                 return $this->myDOMNode->hasAttribute($name);
00275         }
00276 
00277         function remove_attribute($name)
00278         {
00279                 return $this->myDOMNode->removeAttribute($name);
00280         }
00281 
00282         function set_attribute($name,$value)
00283         {
00284                 return $this->myDOMNode->setAttribute($name,$value);
00285         }
00286 
00287         function tagname()
00288         {
00289                 return $this->myDOMNode->tagName;
00290         }
00291 
00292         // ##added
00293         function set_content($text)
00294         {
00295                 // the following replace has been added to conform with PHP4.
00296                 // A set_content("&amp;") brought a get_content() = "&" there,
00297                 // whereas PHP5 gives a get_content() = "&amp;"
00298                 $text = str_replace("&lt;", "<", $text);
00299                 $text = str_replace("&gt;", ">", $text);
00300                 $text = str_replace("&amp;", "&", $text);
00301                 
00302                 $text_node =& new DOMText();
00303                 $text_node->appendData($text);
00304                 if (is_object($this->myDOMNode->firstChild))
00305                 {
00306                         $this->myDOMNode->replaceChild($text_node, $this->myDOMNode->firstChild);
00307                 }
00308                 else
00309                 {
00310                         $this->myDOMNode->appendChild($text_node);
00311                 }
00312         }
00313 
00314         // ##added
00315         function get_content()
00316         {
00317                 $text_node =& $this->myDOMNode->firstChild;
00318 
00319                 if (is_object($text_node))
00320                 {
00321                         return $text_node->textContent;
00322                 }
00323                 else
00324                 {
00325                         return "";
00326                 }
00327         }
00328         
00329         // ## added
00330         function unlink($aDomNode)
00331         {
00332                 parent::unlink_node($aDomNode);
00333         }
00334 
00335 }
00336 
00340 class php4DOMNode
00341 {
00342         var $myDOMNode;
00343 
00344         function php4DOMNode($aDomNode)
00345         {
00346                 $this->myDOMNode=$aDomNode;
00347         }
00348 
00349         function append_child($newnode)
00350         {
00351 //echo "BH";
00352                 //if (strtolower(get_class($newnode)) != "php4domcdatasection")
00353                 //{
00354                         $doc =& $this->myDOMNode->ownerDocument;
00355         //echo "<br>BH1:".get_class($newnode).":";
00356                         $newnode->myDOMNode =& $doc->importNode($newnode->myDOMNode, true);
00357         //echo "BH2";
00358                         return new php4DOMElement($this->myDOMNode->appendChild($newnode->myDOMNode));
00359                 //}
00360                 //else
00361                 //{
00362                 //}
00363         }
00364 
00365         function replace_node($newnode)
00366         {
00367                 return $this->set_content($newnode->myDOMNode->textContent);
00368         }
00369         
00370         function append_sibling($newnode)
00371         {
00372                 return new php4DOMElement($this->myDOMNode->parentNode->appendChild($newnode->myDOMNode));
00373         }
00374 
00375         function attributes()
00376         {
00377 //echo "<br>node:".$this->myDOMNode->nodeName.":";
00378                 $myDOMNodeList=$this->myDOMNode->attributes;
00379                 $nodeSet=array();
00380                 $i=0;
00381                 if (is_object($myDOMNodeList))
00382                 {
00383                         while ($node=$myDOMNodeList->item($i))
00384                         {
00385                                 $nodeSet[]=new php4DOMAttr($node);
00386                                 $i++;
00387                         }
00388                 }
00389 
00390                 return $nodeSet;
00391         }
00392 
00393         function child_nodes()
00394         {
00395                 $myDOMNodeList=$this->myDOMNode->childNodes;
00396                 $nodeSet=array();
00397                 $i=0;
00398                 while ($node=$myDOMNodeList->item($i))
00399                 {
00400                         $nodeSet[]=new php4DOMElement($node);
00401                         $i++;
00402                 }
00403                 return $nodeSet;
00404         }
00405 
00406         // ## added
00407         function children()
00408         {
00409 //echo "<br>php4DomNode::children"; flush();
00410                 return $this->child_nodes();
00411         }
00412 
00413         // ## added
00414         function unlink_node($aDomNode = "")
00415         {
00416                 // sometimes the node to unlink is passed
00417                 if (!is_object($aDomNode))
00418                 {
00419                         $aDomNode =& $this;
00420                         //$aDomNode = $this;
00421                 }
00422 
00423                 $parent =& $aDomNode->myDOMNode->parentNode;
00424                 if (is_object($parent))
00425                 {
00426                         $parent->removeChild($aDomNode->myDOMNode);
00427                 }
00428         }
00429 
00430         function clone_node($deep=false)
00431         {
00432                 return new php4DOMElement($this->myDOMNode->cloneNode($deep));
00433         }
00434 
00435         function first_child()
00436         {
00437                 return new php4DOMElement($this->myDOMNode->firstChild);
00438         }
00439 
00440         function get_content()
00441         {
00442                 return $this->myDOMNode->textContent;
00443         }
00444 
00445         function has_attributes()
00446         {
00447                 return $this->myDOMNode->hasAttributes();
00448         }
00449 
00450         function has_child_nodes()
00451         {
00452                 return $this->myDOMNode->hasChildNodes();
00453         }
00454 
00455         // ## changed
00456         function insert_before($newnode,$refnode)
00457         {
00458                 //echo "BH";
00459                 $doc =& $this->myDOMNode->ownerDocument;
00460                 $newnode->myDOMNode =& $doc->importNode($newnode->myDOMNode, true);
00461                 
00462                 $mydomnode =& $this->myDOMNode;
00463                 $mynewnode =& $newnode->myDOMNode;
00464                 $myrefnode =& $refnode->myDOMNode;
00465                 try
00466                 {
00467                         $domel =& $mydomnode->insertBefore($mynewnode,$myrefnode);
00468                 }
00469                 catch (DOMException $exception)
00470                 {
00471                         // php 4 accepted $this == $refnode -> switch to parent of $this
00472                         $mydomnode =& $this->myDOMNode->parentNode;
00473                         $domel =& $mydomnode->insertBefore($mynewnode,$myrefnode);
00474                 }
00475                 $el =& new php4DOMElement($domel);
00476                 return $el;
00477         }
00478 
00479         // ## changed
00480         function last_child()
00481         {
00482                 $last =& $this->myDOMNode->lastChild;
00483 
00484                 if (is_object($last))
00485                 {
00486                         return new php4DOMElement($last);
00487                 }
00488                 else
00489                 {
00490                         return false;
00491                 }
00492         }
00493 
00494         // ## changed
00495         function next_sibling()
00496         {
00497                 $next =& $this->myDOMNode->nextSibling;
00498 
00499                 if (is_object($next))
00500                 {
00501                         return new php4DOMElement($next);
00502                 }
00503                 else
00504                 {
00505                         return false;
00506                 }
00507         }
00508 
00509         function node_name($a_local = false)
00510         {
00511                 if ($a_local)
00512                 {
00513                         return $this->myDOMNode->localName;
00514                 }
00515                 else
00516                 {
00517                         return $this->myDOMNode->nodeName;
00518                 }
00519         }
00520 
00521         function node_type()
00522         {
00523                 return $this->myDOMNode->nodeType;
00524         }
00525 
00526         function node_value()
00527         {
00528                 return $this->myDOMNode->nodeValue;
00529         }
00530 
00531         // ## changed
00532         function parent_node()
00533         {
00534                 $parent =& $this->myDOMNode->parentNode;
00535 
00536                 if (is_object($parent))
00537                 {
00538                         return new php4DOMElement($parent);
00539                 }
00540                 else
00541                 {
00542                         return false;
00543                 }
00544         }
00545 
00546         // ## changed
00547         function previous_sibling()
00548         {
00549                 $prev =& $this->myDOMNode->previousSibling;
00550 
00551                 if (is_object($prev))
00552                 {
00553                         return new php4DOMElement($prev);
00554                 }
00555                 else
00556                 {
00557                         return false;
00558                 }
00559         }
00560 
00561         function remove_child($oldchild)
00562         {
00563                 return new php4DOMElement($this->myDOMNode->removeChild($oldchild->myDOMNode));
00564         }
00565 
00566         function replace_child($oldnode,$newnode)
00567         {
00568                 return new php4DOMElement($this->myDOMNode->replaceChild($oldchild->myDOMNode,$newnode->myDOMNode));
00569         }
00570 
00571         function set_content($text)
00572         {
00573                 $this->myDOMNode->textContent = $text;
00574                 return $this->myDOMNode->textContent;
00575         }
00576 }
00577 
00578 class php4DOMNodelist
00579 {
00580         var $myDOMNodelist;
00581         var $nodeset;
00582 
00583         function php4DOMNodelist($aDOMNodelist)
00584         {
00585                 $this->myDOMNodelist=$aDOMNodelist;
00586                 $this->nodeset=array();
00587                 $i=0;
00588                 while ($node=$this->myDOMNodelist->item($i))
00589                 {
00590                         $this->nodeset[]=new php4DOMElement($node);
00591                         $i++;
00592                 }
00593         }
00594 }
00595 
00596 class php4DOMXPath
00597 {
00598         var $myDOMXPath;
00599 
00600         // ## added
00601         function xpath_eval($eval_str)
00602         {
00603                 return xpath_eval($this, $eval_str);
00604         }
00605 
00606         function php4DOMXPath($dom_document)
00607         {
00608                 $this->myDOMXPath=new DOMXPath($dom_document->myDOMDocument);
00609         }
00610 
00611         function query($eval_str)
00612         {
00613                 return new php4DOMNodelist($this->myDOMXPath->query($eval_str));
00614         }
00615 
00616         function xpath_register_ns($prefix,$namespaceURI)
00617         {
00618                 return $this->myDOMXPath->registerNamespace($prefix,$namespaceURI);
00619         }
00620 }
00621 
00622 ?>

Generated on Fri Dec 13 2013 13:52:11 for ILIAS Release_3_7_x_branch .rev 46817 by  doxygen 1.7.1