ryxeo-glpi-git / htdocs / lib / phpcas / domxml-php4-php5.php @ 1c14bcc4
Historique | Voir | Annoter | Télécharger (22,2 ko)
1 |
<?php
|
---|---|
2 |
/*
|
3 |
Requires PHP5, uses built-in DOM extension.
|
4 |
To be used in PHP4 scripts using DOMXML extension: allows PHP4/DOMXML scripts to run on PHP5/DOM.
|
5 |
(Optional: requires PHP5/XSL extension for domxml_xslt functions, PHP>=5.1 for XPath evaluation functions, and PHP>=5.1/libxml for DOMXML error reports)
|
6 |
|
7 |
Typical use:
|
8 |
{
|
9 |
if (PHP_VERSION>='5')
|
10 |
require_once('domxml-php4-to-php5.php');
|
11 |
}
|
12 |
|
13 |
Version 1.20a, 2008-11-06, http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/
|
14 |
|
15 |
------------------------------------------------------------------
|
16 |
Written by Alexandre Alapetite, http://alexandre.alapetite.net/cv/
|
17 |
|
18 |
Copyright 2004-2008, GNU Lesser General Public License,
|
19 |
http://www.gnu.org/licenses/lgpl.html
|
20 |
|
21 |
This program is free software: you can redistribute it and/or modify
|
22 |
it under the terms of the GNU Lesser General Public License as published by
|
23 |
the Free Software Foundation, either version 3 of the License, or
|
24 |
(at your option) any later version.
|
25 |
This program is distributed in the hope that it will be useful,
|
26 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
27 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
28 |
GNU Lesser General Public License for more details.
|
29 |
You should have received a copy of the GNU Lesser General Public License
|
30 |
along with this program. If not, see <http://www.gnu.org/licenses/lgpl.html>
|
31 |
|
32 |
== Rights and obligations ==
|
33 |
- Attribution: You must give the original author credit.
|
34 |
- Share Alike: If you alter or transform this library,
|
35 |
you may distribute the resulting library only under the same license GNU/LGPL.
|
36 |
- In case of jurisdiction dispute, the French law is authoritative.
|
37 |
- Any of these conditions can be waived if you get permission from Alexandre Alapetite.
|
38 |
- Not required, but please send to Alexandre Alapetite the modifications you make,
|
39 |
in order to improve this file for the benefit of everybody.
|
40 |
|
41 |
If you want to distribute this code, please do it as a link to:
|
42 |
http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/
|
43 |
*/
|
44 |
|
45 |
define('DOMXML_LOAD_PARSING',0); |
46 |
define('DOMXML_LOAD_VALIDATING',1); |
47 |
define('DOMXML_LOAD_RECOVERING',2); |
48 |
define('DOMXML_LOAD_SUBSTITUTE_ENTITIES',4); |
49 |
//define('DOMXML_LOAD_COMPLETE_ATTRS',8);
|
50 |
define('DOMXML_LOAD_DONT_KEEP_BLANKS',16); |
51 |
|
52 |
function domxml_new_doc($version) {return new php4DOMDocument();} |
53 |
function domxml_new_xmldoc($version) {return new php4DOMDocument();} |
54 |
function domxml_open_file($filename,$mode=DOMXML_LOAD_PARSING,&$error=null) |
55 |
{ |
56 |
$dom=new php4DOMDocument($mode); |
57 |
$errorMode=(func_num_args()>2)&&defined('LIBXML_VERSION'); |
58 |
if ($errorMode) libxml_use_internal_errors(true); |
59 |
if (!$dom->myDOMNode->load($filename)) $dom=null; |
60 |
if ($errorMode) |
61 |
{ |
62 |
$error=array_map('_error_report',libxml_get_errors()); |
63 |
libxml_clear_errors(); |
64 |
} |
65 |
return $dom; |
66 |
} |
67 |
function domxml_open_mem($str,$mode=DOMXML_LOAD_PARSING,&$error=null) |
68 |
{ |
69 |
$dom=new php4DOMDocument($mode); |
70 |
$errorMode=(func_num_args()>2)&&defined('LIBXML_VERSION'); |
71 |
if ($errorMode) libxml_use_internal_errors(true); |
72 |
if (!$dom->myDOMNode->loadXML($str)) $dom=null; |
73 |
if ($errorMode) |
74 |
{ |
75 |
$error=array_map('_error_report',libxml_get_errors()); |
76 |
libxml_clear_errors(); |
77 |
} |
78 |
return $dom; |
79 |
} |
80 |
function html_doc($html_doc,$from_file=false) |
81 |
{ |
82 |
$dom=new php4DOMDocument(); |
83 |
if ($from_file) $result=$dom->myDOMNode->loadHTMLFile($html_doc); |
84 |
else $result=$dom->myDOMNode->loadHTML($html_doc); |
85 |
return $result ? $dom : null; |
86 |
} |
87 |
function html_doc_file($filename) {return html_doc($filename,true);} |
88 |
function xmldoc($str) {return domxml_open_mem($str);} |
89 |
function xmldocfile($filename) {return domxml_open_file($filename);} |
90 |
function xpath_eval($xpath_context,$eval_str,$contextnode=null) {return $xpath_context->xpath_eval($eval_str,$contextnode);} |
91 |
function xpath_new_context($dom_document) {return new php4DOMXPath($dom_document);} |
92 |
function xpath_register_ns($xpath_context,$prefix,$namespaceURI) {return $xpath_context->myDOMXPath->registerNamespace($prefix,$namespaceURI);} |
93 |
function _entityDecode($text) {return html_entity_decode(strtr($text,array('''=>'\'')),ENT_QUOTES,'UTF-8');} |
94 |
function _error_report($error) {return array('errormessage'=>$error->message,'nodename'=>'','line'=>$error->line,'col'=>$error->column)+($error->file==''?array():array('directory'=>dirname($error->file),'file'=>basename($error->file)));} |
95 |
|
96 |
class php4DOMAttr extends php4DOMNode |
97 |
{ |
98 |
function __get($name) |
99 |
{ |
100 |
if ($name==='name') return $this->myDOMNode->name; |
101 |
else return parent::__get($name); |
102 |
} |
103 |
function name() {return $this->myDOMNode->name;} |
104 |
function set_content($text) {} |
105 |
//function set_value($content) {return $this->myDOMNode->value=htmlspecialchars($content,ENT_QUOTES);}
|
106 |
function specified() {return $this->myDOMNode->specified;} |
107 |
function value() {return $this->myDOMNode->value;} |
108 |
} |
109 |
|
110 |
class php4DOMDocument extends php4DOMNode |
111 |
{ |
112 |
function php4DOMDocument($mode=DOMXML_LOAD_PARSING) |
113 |
{ |
114 |
$this->myDOMNode=new DOMDocument(); |
115 |
$this->myOwnerDocument=$this; |
116 |
if ($mode & DOMXML_LOAD_VALIDATING) $this->myDOMNode->validateOnParse=true; |
117 |
if ($mode & DOMXML_LOAD_RECOVERING) $this->myDOMNode->recover=true; |
118 |
if ($mode & DOMXML_LOAD_SUBSTITUTE_ENTITIES) $this->myDOMNode->substituteEntities=true; |
119 |
if ($mode & DOMXML_LOAD_DONT_KEEP_BLANKS) $this->myDOMNode->preserveWhiteSpace=false; |
120 |
} |
121 |
function add_root($name) |
122 |
{ |
123 |
if ($this->myDOMNode->hasChildNodes()) $this->myDOMNode->removeChild($this->myDOMNode->firstChild); |
124 |
return new php4DOMElement($this->myDOMNode->appendChild($this->myDOMNode->createElement($name)),$this->myOwnerDocument); |
125 |
} |
126 |
function create_attribute($name,$value) |
127 |
{ |
128 |
$myAttr=$this->myDOMNode->createAttribute($name); |
129 |
$myAttr->value=htmlspecialchars($value,ENT_QUOTES); |
130 |
return new php4DOMAttr($myAttr,$this); |
131 |
} |
132 |
function create_cdata_section($content) {return new php4DOMNode($this->myDOMNode->createCDATASection($content),$this);} |
133 |
function create_comment($data) {return new php4DOMNode($this->myDOMNode->createComment($data),$this);} |
134 |
function create_element($name) {return new php4DOMElement($this->myDOMNode->createElement($name),$this);} |
135 |
function create_element_ns($uri,$name,$prefix=null) |
136 |
{ |
137 |
if ($prefix==null) $prefix=$this->myDOMNode->lookupPrefix($uri); |
138 |
if (($prefix==null)&&(($this->myDOMNode->documentElement==null)||(!$this->myDOMNode->documentElement->isDefaultNamespace($uri)))) $prefix='a'.sprintf('%u',crc32($uri)); |
139 |
return new php4DOMElement($this->myDOMNode->createElementNS($uri,$prefix==null ? $name : $prefix.':'.$name),$this); |
140 |
} |
141 |
function create_entity_reference($content) {return new php4DOMNode($this->myDOMNode->createEntityReference($content),$this);} //By Walter Ebert 2007-01-22 |
142 |
function create_processing_instruction($target,$data=''){return new php4DomProcessingInstruction($this->myDOMNode->createProcessingInstruction($target,$data),$this);} |
143 |
function create_text_node($content) {return new php4DOMText($this->myDOMNode->createTextNode($content),$this);} |
144 |
function document_element() {return parent::_newDOMElement($this->myDOMNode->documentElement,$this);} |
145 |
function dump_file($filename,$compressionmode=false,$format=false) |
146 |
{ |
147 |
$format0=$this->myDOMNode->formatOutput; |
148 |
$this->myDOMNode->formatOutput=$format; |
149 |
$res=$this->myDOMNode->save($filename); |
150 |
$this->myDOMNode->formatOutput=$format0; |
151 |
return $res; |
152 |
} |
153 |
function dump_mem($format=false,$encoding=false) |
154 |
{ |
155 |
$format0=$this->myDOMNode->formatOutput; |
156 |
$this->myDOMNode->formatOutput=$format; |
157 |
$encoding0=$this->myDOMNode->encoding; |
158 |
if ($encoding) $this->myDOMNode->encoding=$encoding; |
159 |
$dump=$this->myDOMNode->saveXML(); |
160 |
$this->myDOMNode->formatOutput=$format0; |
161 |
if ($encoding) $this->myDOMNode->encoding= $encoding0=='' ? 'UTF-8' : $encoding0; //UTF-8 is XML default encoding |
162 |
return $dump; |
163 |
} |
164 |
function free() |
165 |
{ |
166 |
if ($this->myDOMNode->hasChildNodes()) $this->myDOMNode->removeChild($this->myDOMNode->firstChild); |
167 |
$this->myDOMNode=null; |
168 |
$this->myOwnerDocument=null; |
169 |
} |
170 |
function get_element_by_id($id) {return parent::_newDOMElement($this->myDOMNode->getElementById($id),$this);} |
171 |
function get_elements_by_tagname($name) |
172 |
{ |
173 |
$myDOMNodeList=$this->myDOMNode->getElementsByTagName($name); |
174 |
$nodeSet=array(); |
175 |
$i=0; |
176 |
if (isset($myDOMNodeList)) |
177 |
while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=new php4DOMElement($node,$this); |
178 |
return $nodeSet; |
179 |
} |
180 |
function html_dump_mem() {return $this->myDOMNode->saveHTML();} |
181 |
function root() {return parent::_newDOMElement($this->myDOMNode->documentElement,$this);} |
182 |
function xpath_new_context() {return new php4DOMXPath($this);} |
183 |
} |
184 |
|
185 |
class php4DOMElement extends php4DOMNode |
186 |
{ |
187 |
function add_namespace($uri,$prefix) |
188 |
{ |
189 |
if ($this->myDOMNode->hasAttributeNS('http://www.w3.org/2000/xmlns/',$prefix)) return false; |
190 |
else
|
191 |
{ |
192 |
$this->myDOMNode->setAttributeNS('http://www.w3.org/2000/xmlns/','xmlns:'.$prefix,$uri); //By Daniel Walker 2006-09-08 |
193 |
return true; |
194 |
} |
195 |
} |
196 |
function get_attribute($name) {return $this->myDOMNode->getAttribute($name);} |
197 |
function get_attribute_node($name) {return parent::_newDOMElement($this->myDOMNode->getAttributeNode($name),$this->myOwnerDocument);} |
198 |
function get_elements_by_tagname($name) |
199 |
{ |
200 |
$myDOMNodeList=$this->myDOMNode->getElementsByTagName($name); |
201 |
$nodeSet=array(); |
202 |
$i=0; |
203 |
if (isset($myDOMNodeList)) |
204 |
while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=new php4DOMElement($node,$this->myOwnerDocument); |
205 |
return $nodeSet; |
206 |
} |
207 |
function has_attribute($name) {return $this->myDOMNode->hasAttribute($name);} |
208 |
function remove_attribute($name) {return $this->myDOMNode->removeAttribute($name);} |
209 |
function set_attribute($name,$value) |
210 |
{ |
211 |
//return $this->myDOMNode->setAttribute($name,$value); //Does not return a DomAttr
|
212 |
$myAttr=$this->myDOMNode->ownerDocument->createAttribute($name); |
213 |
$myAttr->value=htmlspecialchars($value,ENT_QUOTES); //Entity problem reported by AL-DesignWorks 2007-09-07 |
214 |
$this->myDOMNode->setAttributeNode($myAttr); |
215 |
return new php4DOMAttr($myAttr,$this->myOwnerDocument); |
216 |
} |
217 |
/*function set_attribute_node($attr)
|
218 |
{
|
219 |
$this->myDOMNode->setAttributeNode($this->_importNode($attr));
|
220 |
return $attr;
|
221 |
}*/
|
222 |
function set_name($name) |
223 |
{ |
224 |
if ($this->myDOMNode->prefix=='') $newNode=$this->myDOMNode->ownerDocument->createElement($name); |
225 |
else $newNode=$this->myDOMNode->ownerDocument->createElementNS($this->myDOMNode->namespaceURI,$this->myDOMNode->prefix.':'.$name); |
226 |
$myDOMNodeList=$this->myDOMNode->attributes; |
227 |
$i=0; |
228 |
if (isset($myDOMNodeList)) |
229 |
while ($node=$myDOMNodeList->item($i++)) |
230 |
if ($node->namespaceURI=='') $newNode->setAttribute($node->name,$node->value); |
231 |
else $newNode->setAttributeNS($node->namespaceURI,$node->nodeName,$node->value); |
232 |
$myDOMNodeList=$this->myDOMNode->childNodes; |
233 |
if (isset($myDOMNodeList)) |
234 |
while ($node=$myDOMNodeList->item(0)) $newNode->appendChild($node); |
235 |
$this->myDOMNode->parentNode->replaceChild($newNode,$this->myDOMNode); |
236 |
$this->myDOMNode=$newNode; |
237 |
return true; |
238 |
} |
239 |
function tagname() {return $this->tagname;} |
240 |
} |
241 |
|
242 |
class php4DOMNode |
243 |
{ |
244 |
public $myDOMNode; |
245 |
public $myOwnerDocument; |
246 |
function php4DOMNode($aDomNode,$aOwnerDocument) |
247 |
{ |
248 |
$this->myDOMNode=$aDomNode; |
249 |
$this->myOwnerDocument=$aOwnerDocument; |
250 |
} |
251 |
function __get($name) |
252 |
{ |
253 |
switch ($name) |
254 |
{ |
255 |
case 'type': return $this->myDOMNode->nodeType; |
256 |
case 'tagname': return ($this->myDOMNode->nodeType===XML_ELEMENT_NODE) ? $this->myDOMNode->localName : $this->myDOMNode->tagName; //Avoid namespace prefix for DOMElement |
257 |
case 'content': return $this->myDOMNode->textContent; |
258 |
case 'value': return $this->myDOMNode->value; |
259 |
default:
|
260 |
$myErrors=debug_backtrace(); |
261 |
trigger_error('Undefined property: '.get_class($this).'::$'.$name.' ['.$myErrors[0]['file'].':'.$myErrors[0]['line'].']',E_USER_NOTICE); |
262 |
return false; |
263 |
} |
264 |
} |
265 |
function add_child($newnode) {return append_child($newnode);} |
266 |
function add_namespace($uri,$prefix) {return false;} |
267 |
function append_child($newnode) {return self::_newDOMElement($this->myDOMNode->appendChild($this->_importNode($newnode)),$this->myOwnerDocument);} |
268 |
function append_sibling($newnode) {return self::_newDOMElement($this->myDOMNode->parentNode->appendChild($this->_importNode($newnode)),$this->myOwnerDocument);} |
269 |
function attributes() |
270 |
{ |
271 |
$myDOMNodeList=$this->myDOMNode->attributes; |
272 |
if (!(isset($myDOMNodeList)&&$this->myDOMNode->hasAttributes())) return null; |
273 |
$nodeSet=array(); |
274 |
$i=0; |
275 |
while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=new php4DOMAttr($node,$this->myOwnerDocument); |
276 |
return $nodeSet; |
277 |
} |
278 |
function child_nodes() |
279 |
{ |
280 |
$myDOMNodeList=$this->myDOMNode->childNodes; |
281 |
$nodeSet=array(); |
282 |
$i=0; |
283 |
if (isset($myDOMNodeList)) |
284 |
while ($node=$myDOMNodeList->item($i++)) $nodeSet[]=self::_newDOMElement($node,$this->myOwnerDocument); |
285 |
return $nodeSet; |
286 |
} |
287 |
function children() {return $this->child_nodes();} |
288 |
function clone_node($deep=false) {return self::_newDOMElement($this->myDOMNode->cloneNode($deep),$this->myOwnerDocument);} |
289 |
//dump_node($node) should only be called on php4DOMDocument
|
290 |
function dump_node($node=null) {return $node==null ? $this->myOwnerDocument->myDOMNode->saveXML($this->myDOMNode) : $this->myOwnerDocument->myDOMNode->saveXML($node->myDOMNode);} |
291 |
function first_child() {return self::_newDOMElement($this->myDOMNode->firstChild,$this->myOwnerDocument);} |
292 |
function get_content() {return $this->myDOMNode->textContent;} |
293 |
function has_attributes() {return $this->myDOMNode->hasAttributes();} |
294 |
function has_child_nodes() {return $this->myDOMNode->hasChildNodes();} |
295 |
function insert_before($newnode,$refnode) {return self::_newDOMElement($this->myDOMNode->insertBefore($this->_importNode($newnode),$refnode==null?null:$refnode->myDOMNode),$this->myOwnerDocument);} |
296 |
function is_blank_node() {return ($this->myDOMNode->nodeType===XML_TEXT_NODE)&&preg_match('%^\s*$%',$this->myDOMNode->nodeValue);} |
297 |
function last_child() {return self::_newDOMElement($this->myDOMNode->lastChild,$this->myOwnerDocument);} |
298 |
function new_child($name,$content) |
299 |
{ |
300 |
$mySubNode=$this->myDOMNode->ownerDocument->createElement($name); |
301 |
$mySubNode->appendChild($this->myDOMNode->ownerDocument->createTextNode(_entityDecode($content))); |
302 |
$this->myDOMNode->appendChild($mySubNode); |
303 |
return new php4DOMElement($mySubNode,$this->myOwnerDocument); |
304 |
} |
305 |
function next_sibling() {return self::_newDOMElement($this->myDOMNode->nextSibling,$this->myOwnerDocument);} |
306 |
function node_name() {return ($this->myDOMNode->nodeType===XML_ELEMENT_NODE) ? $this->myDOMNode->localName : $this->myDOMNode->nodeName;} //Avoid namespace prefix for DOMElement |
307 |
function node_type() {return $this->myDOMNode->nodeType;} |
308 |
function node_value() {return $this->myDOMNode->nodeValue;} |
309 |
function owner_document() {return $this->myOwnerDocument;} |
310 |
function parent_node() {return self::_newDOMElement($this->myDOMNode->parentNode,$this->myOwnerDocument);} |
311 |
function prefix() {return $this->myDOMNode->prefix;} |
312 |
function previous_sibling() {return self::_newDOMElement($this->myDOMNode->previousSibling,$this->myOwnerDocument);} |
313 |
function remove_child($oldchild) {return self::_newDOMElement($this->myDOMNode->removeChild($oldchild->myDOMNode),$this->myOwnerDocument);} |
314 |
function replace_child($newnode,$oldnode) {return self::_newDOMElement($this->myDOMNode->replaceChild($this->_importNode($newnode),$oldnode->myDOMNode),$this->myOwnerDocument);} |
315 |
function replace_node($newnode) {return self::_newDOMElement($this->myDOMNode->parentNode->replaceChild($this->_importNode($newnode),$this->myDOMNode),$this->myOwnerDocument);} |
316 |
function set_content($text) {return $this->myDOMNode->appendChild($this->myDOMNode->ownerDocument->createTextNode(_entityDecode($text)));} //Entity problem reported by AL-DesignWorks 2007-09-07 |
317 |
//function set_name($name) {return $this->myOwnerDocument->renameNode($this->myDOMNode,$this->myDOMNode->namespaceURI,$name);}
|
318 |
function set_namespace($uri,$prefix=null) |
319 |
{//Contributions by Daniel Walker 2006-09-08
|
320 |
$nsprefix=$this->myDOMNode->lookupPrefix($uri); |
321 |
if ($nsprefix==null) |
322 |
{ |
323 |
$nsprefix= $prefix==null ? $nsprefix='a'.sprintf('%u',crc32($uri)) : $prefix; |
324 |
if ($this->myDOMNode->nodeType===XML_ATTRIBUTE_NODE) |
325 |
{ |
326 |
if (($prefix!=null)&&$this->myDOMNode->ownerElement->hasAttributeNS('http://www.w3.org/2000/xmlns/',$nsprefix)&& |
327 |
($this->myDOMNode->ownerElement->getAttributeNS('http://www.w3.org/2000/xmlns/',$nsprefix)!=$uri)) |
328 |
{//Remove namespace
|
329 |
$parent=$this->myDOMNode->ownerElement; |
330 |
$parent->removeAttributeNode($this->myDOMNode); |
331 |
$parent->setAttribute($this->myDOMNode->localName,$this->myDOMNode->nodeValue); |
332 |
$this->myDOMNode=$parent->getAttributeNode($this->myDOMNode->localName); |
333 |
return;
|
334 |
} |
335 |
$this->myDOMNode->ownerElement->setAttributeNS('http://www.w3.org/2000/xmlns/','xmlns:'.$nsprefix,$uri); |
336 |
} |
337 |
} |
338 |
if ($this->myDOMNode->nodeType===XML_ATTRIBUTE_NODE) |
339 |
{ |
340 |
$parent=$this->myDOMNode->ownerElement; |
341 |
$parent->removeAttributeNode($this->myDOMNode); |
342 |
$parent->setAttributeNS($uri,$nsprefix.':'.$this->myDOMNode->localName,$this->myDOMNode->nodeValue); |
343 |
$this->myDOMNode=$parent->getAttributeNodeNS($uri,$this->myDOMNode->localName); |
344 |
} |
345 |
elseif ($this->myDOMNode->nodeType===XML_ELEMENT_NODE) |
346 |
{ |
347 |
$NewNode=$this->myDOMNode->ownerDocument->createElementNS($uri,$nsprefix.':'.$this->myDOMNode->localName); |
348 |
foreach ($this->myDOMNode->attributes as $n) $NewNode->appendChild($n->cloneNode(true)); |
349 |
foreach ($this->myDOMNode->childNodes as $n) $NewNode->appendChild($n->cloneNode(true)); |
350 |
$xpath=new DOMXPath($this->myDOMNode->ownerDocument); |
351 |
$myDOMNodeList=$xpath->query('namespace::*[name()!="xml"]',$this->myDOMNode); //Add old namespaces |
352 |
foreach ($myDOMNodeList as $n) $NewNode->setAttributeNS('http://www.w3.org/2000/xmlns/',$n->nodeName,$n->nodeValue); |
353 |
$this->myDOMNode->parentNode->replaceChild($NewNode,$this->myDOMNode); |
354 |
$this->myDOMNode=$NewNode; |
355 |
} |
356 |
} |
357 |
function unlink_node() |
358 |
{ |
359 |
if ($this->myDOMNode->parentNode!=null) |
360 |
{ |
361 |
if ($this->myDOMNode->nodeType===XML_ATTRIBUTE_NODE) $this->myDOMNode->parentNode->removeAttributeNode($this->myDOMNode); |
362 |
else $this->myDOMNode->parentNode->removeChild($this->myDOMNode); |
363 |
} |
364 |
} |
365 |
protected function _importNode($newnode) {return $this->myOwnerDocument===$newnode->myOwnerDocument ? $newnode->myDOMNode : $this->myOwnerDocument->myDOMNode->importNode($newnode->myDOMNode,true);} //To import DOMNode from another DOMDocument |
366 |
static function _newDOMElement($aDOMNode,$aOwnerDocument) |
367 |
{//Check the PHP5 DOMNode before creating a new associated PHP4 DOMNode wrapper
|
368 |
if ($aDOMNode==null) return null; |
369 |
switch ($aDOMNode->nodeType) |
370 |
{ |
371 |
case XML_ELEMENT_NODE: return new php4DOMElement($aDOMNode,$aOwnerDocument); |
372 |
case XML_TEXT_NODE: return new php4DOMText($aDOMNode,$aOwnerDocument); |
373 |
case XML_ATTRIBUTE_NODE: return new php4DOMAttr($aDOMNode,$aOwnerDocument); |
374 |
case XML_PI_NODE: return new php4DomProcessingInstruction($aDOMNode,$aOwnerDocument); |
375 |
default: return new php4DOMNode($aDOMNode,$aOwnerDocument); |
376 |
} |
377 |
} |
378 |
} |
379 |
|
380 |
class php4DomProcessingInstruction extends php4DOMNode |
381 |
{ |
382 |
function data() {return $this->myDOMNode->data;} |
383 |
function target() {return $this->myDOMNode->target;} |
384 |
} |
385 |
|
386 |
class php4DOMText extends php4DOMNode |
387 |
{ |
388 |
function __get($name) |
389 |
{ |
390 |
if ($name==='tagname') return '#text'; |
391 |
else return parent::__get($name); |
392 |
} |
393 |
function tagname() {return '#text';} |
394 |
function set_content($text) {$this->myDOMNode->nodeValue=$text; return true;} |
395 |
} |
396 |
|
397 |
if (!defined('XPATH_NODESET')) |
398 |
{ |
399 |
define('XPATH_UNDEFINED',0); |
400 |
define('XPATH_NODESET',1); |
401 |
define('XPATH_BOOLEAN',2); |
402 |
define('XPATH_NUMBER',3); |
403 |
define('XPATH_STRING',4); |
404 |
/*define('XPATH_POINT',5);
|
405 |
define('XPATH_RANGE',6);
|
406 |
define('XPATH_LOCATIONSET',7);
|
407 |
define('XPATH_USERS',8);
|
408 |
define('XPATH_XSLT_TREE',9);*/
|
409 |
} |
410 |
|
411 |
class php4DOMNodelist |
412 |
{ |
413 |
private $myDOMNodelist; |
414 |
public $nodeset; |
415 |
public $type=XPATH_UNDEFINED; |
416 |
public $value; |
417 |
function php4DOMNodelist($aDOMNodelist,$aOwnerDocument) |
418 |
{ |
419 |
if (!isset($aDOMNodelist)) return; |
420 |
elseif (is_object($aDOMNodelist)||is_array($aDOMNodelist)) |
421 |
{ |
422 |
if ($aDOMNodelist->length>0) |
423 |
{ |
424 |
$this->myDOMNodelist=$aDOMNodelist; |
425 |
$this->nodeset=array(); |
426 |
$this->type=XPATH_NODESET; |
427 |
$i=0; |
428 |
while ($node=$this->myDOMNodelist->item($i++)) $this->nodeset[]=php4DOMNode::_newDOMElement($node,$aOwnerDocument); |
429 |
} |
430 |
} |
431 |
elseif (is_int($aDOMNodelist)||is_float($aDOMNodelist)) |
432 |
{ |
433 |
$this->type=XPATH_NUMBER; |
434 |
$this->value=$aDOMNodelist; |
435 |
} |
436 |
elseif (is_bool($aDOMNodelist)) |
437 |
{ |
438 |
$this->type=XPATH_BOOLEAN; |
439 |
$this->value=$aDOMNodelist; |
440 |
} |
441 |
elseif (is_string($aDOMNodelist)) |
442 |
{ |
443 |
$this->type=XPATH_STRING; |
444 |
$this->value=$aDOMNodelist; |
445 |
} |
446 |
} |
447 |
} |
448 |
|
449 |
class php4DOMXPath |
450 |
{ |
451 |
public $myDOMXPath; |
452 |
private $myOwnerDocument; |
453 |
function php4DOMXPath($dom_document) |
454 |
{ |
455 |
//TODO: If $dom_document is a DomElement, make that default $contextnode and modify XPath. Ex: '/test'
|
456 |
$this->myOwnerDocument=$dom_document->myOwnerDocument; |
457 |
$this->myDOMXPath=new DOMXPath($this->myOwnerDocument->myDOMNode); |
458 |
} |
459 |
function xpath_eval($eval_str,$contextnode=null) |
460 |
{ |
461 |
if (method_exists($this->myDOMXPath,'evaluate')) $xp=isset($contextnode) ? $this->myDOMXPath->evaluate($eval_str,$contextnode->myDOMNode) : $this->myDOMXPath->evaluate($eval_str); |
462 |
else $xp=isset($contextnode) ? $this->myDOMXPath->query($eval_str,$contextnode->myDOMNode) : $this->myDOMXPath->query($eval_str); |
463 |
$xp=new php4DOMNodelist($xp,$this->myOwnerDocument); |
464 |
return ($xp->type===XPATH_UNDEFINED) ? false : $xp; |
465 |
} |
466 |
function xpath_register_ns($prefix,$namespaceURI) {return $this->myDOMXPath->registerNamespace($prefix,$namespaceURI);} |
467 |
} |
468 |
|
469 |
if (extension_loaded('xsl')) |
470 |
{//See also: http://alexandre.alapetite.net/doc-alex/xslt-php4-php5/
|
471 |
function domxml_xslt_stylesheet($xslstring) {return new php4DomXsltStylesheet(DOMDocument::loadXML($xslstring));} |
472 |
function domxml_xslt_stylesheet_doc($dom_document) {return new php4DomXsltStylesheet($dom_document);} |
473 |
function domxml_xslt_stylesheet_file($xslfile) {return new php4DomXsltStylesheet(DOMDocument::load($xslfile));} |
474 |
class php4DomXsltStylesheet |
475 |
{ |
476 |
private $myxsltProcessor; |
477 |
function php4DomXsltStylesheet($dom_document) |
478 |
{ |
479 |
$this->myxsltProcessor=new xsltProcessor(); |
480 |
$this->myxsltProcessor->importStyleSheet($dom_document); |
481 |
} |
482 |
function process($dom_document,$xslt_parameters=array(),$param_is_xpath=false) |
483 |
{ |
484 |
foreach ($xslt_parameters as $param=>$value) $this->myxsltProcessor->setParameter('',$param,$value); |
485 |
$myphp4DOMDocument=new php4DOMDocument(); |
486 |
$myphp4DOMDocument->myDOMNode=$this->myxsltProcessor->transformToDoc($dom_document->myDOMNode); |
487 |
return $myphp4DOMDocument; |
488 |
} |
489 |
function result_dump_file($dom_document,$filename) |
490 |
{ |
491 |
$html=$dom_document->myDOMNode->saveHTML(); |
492 |
file_put_contents($filename,$html); |
493 |
return $html; |
494 |
} |
495 |
function result_dump_mem($dom_document) {return $dom_document->myDOMNode->saveHTML();} |
496 |
} |
497 |
} |
498 |
?>
|