Mercurial > hg > Members > shoshi > webvirt
comparison cake/libs/xml.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
1 <?php | |
2 /** | |
3 * XML handling for Cake. | |
4 * | |
5 * The methods in these classes enable the datasources that use XML to work. | |
6 * | |
7 * PHP versions 4 and 5 | |
8 * | |
9 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | |
10 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
11 * | |
12 * Licensed under The MIT License | |
13 * Redistributions of files must retain the above copyright notice. | |
14 * | |
15 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
16 * @link http://cakephp.org CakePHP(tm) Project | |
17 * @package cake | |
18 * @subpackage cake.cake.libs | |
19 * @since CakePHP v .0.10.3.1400 | |
20 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) | |
21 */ | |
22 App::import('Core', 'Set'); | |
23 | |
24 /** | |
25 * XML node. | |
26 * | |
27 * Single XML node in an XML tree. | |
28 * | |
29 * @package cake | |
30 * @subpackage cake.cake.libs | |
31 * @since CakePHP v .0.10.3.1400 | |
32 */ | |
33 class XmlNode extends Object { | |
34 | |
35 /** | |
36 * Name of node | |
37 * | |
38 * @var string | |
39 * @access public | |
40 */ | |
41 var $name = null; | |
42 | |
43 /** | |
44 * Node namespace | |
45 * | |
46 * @var string | |
47 * @access public | |
48 */ | |
49 var $namespace = null; | |
50 | |
51 /** | |
52 * Namespaces defined for this node and all child nodes | |
53 * | |
54 * @var array | |
55 * @access public | |
56 */ | |
57 var $namespaces = array(); | |
58 | |
59 /** | |
60 * Value of node | |
61 * | |
62 * @var string | |
63 * @access public | |
64 */ | |
65 var $value; | |
66 | |
67 /** | |
68 * Attributes on this node | |
69 * | |
70 * @var array | |
71 * @access public | |
72 */ | |
73 var $attributes = array(); | |
74 | |
75 /** | |
76 * This node's children | |
77 * | |
78 * @var array | |
79 * @access public | |
80 */ | |
81 var $children = array(); | |
82 | |
83 /** | |
84 * Reference to parent node. | |
85 * | |
86 * @var XmlNode | |
87 * @access private | |
88 */ | |
89 var $__parent = null; | |
90 | |
91 /** | |
92 * Constructor. | |
93 * | |
94 * @param string $name Node name | |
95 * @param array $attributes Node attributes | |
96 * @param mixed $value Node contents (text) | |
97 * @param array $children Node children | |
98 */ | |
99 function __construct($name = null, $value = null, $namespace = null) { | |
100 if (strpos($name, ':') !== false) { | |
101 list($prefix, $name) = explode(':', $name); | |
102 if (!$namespace) { | |
103 $namespace = $prefix; | |
104 } | |
105 } | |
106 $this->name = $name; | |
107 if ($namespace) { | |
108 $this->namespace = $namespace; | |
109 } | |
110 | |
111 if (is_array($value) || is_object($value)) { | |
112 $this->normalize($value); | |
113 } elseif (!empty($value) || $value === 0 || $value === '0') { | |
114 $this->createTextNode($value); | |
115 } | |
116 } | |
117 /** | |
118 * Adds a namespace to the current node | |
119 * | |
120 * @param string $prefix The namespace prefix | |
121 * @param string $url The namespace DTD URL | |
122 * @return void | |
123 */ | |
124 function addNamespace($prefix, $url) { | |
125 if ($ns = Xml::addGlobalNs($prefix, $url)) { | |
126 $this->namespaces = array_merge($this->namespaces, $ns); | |
127 return true; | |
128 } | |
129 return false; | |
130 } | |
131 | |
132 /** | |
133 * Adds a namespace to the current node | |
134 * | |
135 * @param string $prefix The namespace prefix | |
136 * @param string $url The namespace DTD URL | |
137 * @return void | |
138 */ | |
139 function removeNamespace($prefix) { | |
140 if (Xml::removeGlobalNs($prefix)) { | |
141 return true; | |
142 } | |
143 return false; | |
144 } | |
145 | |
146 /** | |
147 * Creates an XmlNode object that can be appended to this document or a node in it | |
148 * | |
149 * @param string $name Node name | |
150 * @param string $value Node value | |
151 * @param string $namespace Node namespace | |
152 * @return object XmlNode | |
153 */ | |
154 function &createNode($name = null, $value = null, $namespace = false) { | |
155 $node =& new XmlNode($name, $value, $namespace); | |
156 $node->setParent($this); | |
157 return $node; | |
158 } | |
159 | |
160 /** | |
161 * Creates an XmlElement object that can be appended to this document or a node in it | |
162 * | |
163 * @param string $name Element name | |
164 * @param string $value Element value | |
165 * @param array $attributes Element attributes | |
166 * @param string $namespace Node namespace | |
167 * @return object XmlElement | |
168 */ | |
169 function &createElement($name = null, $value = null, $attributes = array(), $namespace = false) { | |
170 $element =& new XmlElement($name, $value, $attributes, $namespace); | |
171 $element->setParent($this); | |
172 return $element; | |
173 } | |
174 | |
175 /** | |
176 * Creates an XmlTextNode object that can be appended to this document or a node in it | |
177 * | |
178 * @param string $value Node value | |
179 * @return object XmlTextNode | |
180 */ | |
181 function &createTextNode($value = null) { | |
182 $node = new XmlTextNode($value); | |
183 $node->setParent($this); | |
184 return $node; | |
185 } | |
186 | |
187 /** | |
188 * Gets the XML element properties from an object. | |
189 * | |
190 * @param object $object Object to get properties from | |
191 * @return array Properties from object | |
192 * @access public | |
193 */ | |
194 function normalize($object, $keyName = null, $options = array()) { | |
195 if (is_a($object, 'XmlNode')) { | |
196 return $object; | |
197 } | |
198 $name = null; | |
199 $options += array('format' => 'attributes'); | |
200 | |
201 if ($keyName !== null && !is_numeric($keyName)) { | |
202 $name = $keyName; | |
203 } elseif (!empty($object->_name_)) { | |
204 $name = $object->_name_; | |
205 } elseif (isset($object->name)) { | |
206 $name = $object->name; | |
207 } elseif ($options['format'] == 'attributes') { | |
208 $name = get_class($object); | |
209 } | |
210 | |
211 $tagOpts = $this->__tagOptions($name); | |
212 | |
213 if ($tagOpts === false) { | |
214 return; | |
215 } | |
216 | |
217 if (isset($tagOpts['name'])) { | |
218 $name = $tagOpts['name']; | |
219 } elseif ($name != strtolower($name) && $options['slug'] !== false) { | |
220 $name = Inflector::slug(Inflector::underscore($name)); | |
221 } | |
222 | |
223 if (!empty($name)) { | |
224 $node =& $this->createElement($name); | |
225 } else { | |
226 $node =& $this; | |
227 } | |
228 | |
229 $namespace = array(); | |
230 $attributes = array(); | |
231 $children = array(); | |
232 $chldObjs = array(); | |
233 | |
234 if (is_object($object)) { | |
235 $chldObjs = get_object_vars($object); | |
236 } elseif (is_array($object)) { | |
237 $chldObjs = $object; | |
238 } elseif (!empty($object) || $object === 0 || $object === '0') { | |
239 $node->createTextNode($object); | |
240 } | |
241 $attr = array(); | |
242 | |
243 if (isset($tagOpts['attributes'])) { | |
244 $attr = $tagOpts['attributes']; | |
245 } | |
246 if (isset($tagOpts['value']) && isset($chldObjs[$tagOpts['value']])) { | |
247 $node->createTextNode($chldObjs[$tagOpts['value']]); | |
248 unset($chldObjs[$tagOpts['value']]); | |
249 } | |
250 | |
251 $n = $name; | |
252 if (isset($chldObjs['_name_'])) { | |
253 $n = null; | |
254 unset($chldObjs['_name_']); | |
255 } | |
256 $c = 0; | |
257 | |
258 foreach ($chldObjs as $key => $val) { | |
259 if (in_array($key, $attr) && !is_object($val) && !is_array($val)) { | |
260 $attributes[$key] = $val; | |
261 } else { | |
262 if (!isset($tagOpts['children']) || $tagOpts['children'] === array() || (is_array($tagOpts['children']) && in_array($key, $tagOpts['children']))) { | |
263 if (!is_numeric($key)) { | |
264 $n = $key; | |
265 } | |
266 if (is_array($val)) { | |
267 foreach ($val as $n2 => $obj2) { | |
268 if (is_numeric($n2)) { | |
269 $n2 = $n; | |
270 } | |
271 $node->normalize($obj2, $n2, $options); | |
272 } | |
273 } else { | |
274 if (is_object($val)) { | |
275 | |
276 $node->normalize($val, $n, $options); | |
277 } elseif ($options['format'] == 'tags' && $this->__tagOptions($key) !== false) { | |
278 $tmp =& $node->createElement($key); | |
279 if (!empty($val) || $val === 0 || $val === '0') { | |
280 $tmp->createTextNode($val); | |
281 } | |
282 } elseif ($options['format'] == 'attributes') { | |
283 $node->addAttribute($key, $val); | |
284 } | |
285 } | |
286 } | |
287 } | |
288 $c++; | |
289 } | |
290 if (!empty($name)) { | |
291 return $node; | |
292 } | |
293 return $children; | |
294 } | |
295 | |
296 /** | |
297 * Gets the tag-specific options for the given node name | |
298 * | |
299 * @param string $name XML tag name | |
300 * @param string $option The specific option to query. Omit for all options | |
301 * @return mixed A specific option value if $option is specified, otherwise an array of all options | |
302 * @access private | |
303 */ | |
304 function __tagOptions($name, $option = null) { | |
305 if (isset($this->__tags[$name])) { | |
306 $tagOpts = $this->__tags[$name]; | |
307 } elseif (isset($this->__tags[strtolower($name)])) { | |
308 $tagOpts = $this->__tags[strtolower($name)]; | |
309 } else { | |
310 return null; | |
311 } | |
312 if ($tagOpts === false) { | |
313 return false; | |
314 } | |
315 if (empty($option)) { | |
316 return $tagOpts; | |
317 } | |
318 if (isset($tagOpts[$option])) { | |
319 return $tagOpts[$option]; | |
320 } | |
321 return null; | |
322 } | |
323 | |
324 /** | |
325 * Returns the fully-qualified XML node name, with namespace | |
326 * | |
327 * @access public | |
328 */ | |
329 function name() { | |
330 if (!empty($this->namespace)) { | |
331 $_this =& XmlManager::getInstance(); | |
332 if (!isset($_this->options['verifyNs']) || !$_this->options['verifyNs'] || in_array($this->namespace, array_keys($_this->namespaces))) { | |
333 return $this->namespace . ':' . $this->name; | |
334 } | |
335 } | |
336 return $this->name; | |
337 } | |
338 | |
339 /** | |
340 * Sets the parent node of this XmlNode. | |
341 * | |
342 * @access public | |
343 */ | |
344 function setParent(&$parent) { | |
345 if (strtolower(get_class($this)) == 'xml') { | |
346 return; | |
347 } | |
348 if (isset($this->__parent) && is_object($this->__parent)) { | |
349 if ($this->__parent->compare($parent)) { | |
350 return; | |
351 } | |
352 foreach ($this->__parent->children as $i => $child) { | |
353 if ($this->compare($child)) { | |
354 array_splice($this->__parent->children, $i, 1); | |
355 break; | |
356 } | |
357 } | |
358 } | |
359 if ($parent == null) { | |
360 unset($this->__parent); | |
361 } else { | |
362 $parent->children[] =& $this; | |
363 $this->__parent =& $parent; | |
364 } | |
365 } | |
366 | |
367 /** | |
368 * Returns a copy of self. | |
369 * | |
370 * @return object Cloned instance | |
371 * @access public | |
372 */ | |
373 function cloneNode() { | |
374 return clone($this); | |
375 } | |
376 | |
377 /** | |
378 * Compares $node to this XmlNode object | |
379 * | |
380 * @param object An XmlNode or subclass instance | |
381 * @return boolean True if the nodes match, false otherwise | |
382 * @access public | |
383 */ | |
384 function compare($node) { | |
385 $keys = array(get_object_vars($this), get_object_vars($node)); | |
386 return ($keys[0] === $keys[1]); | |
387 } | |
388 | |
389 /** | |
390 * Append given node as a child. | |
391 * | |
392 * @param object $child XmlNode with appended child | |
393 * @param array $options XML generator options for objects and arrays | |
394 * @return object A reference to the appended child node | |
395 * @access public | |
396 */ | |
397 function &append(&$child, $options = array()) { | |
398 if (empty($child)) { | |
399 $return = false; | |
400 return $return; | |
401 } | |
402 | |
403 if (is_object($child)) { | |
404 if ($this->compare($child)) { | |
405 trigger_error(__('Cannot append a node to itself.', true)); | |
406 $return = false; | |
407 return $return; | |
408 } | |
409 } else if (is_array($child)) { | |
410 $child = Set::map($child); | |
411 if (is_array($child)) { | |
412 if (!is_a(current($child), 'XmlNode')) { | |
413 foreach ($child as $i => $childNode) { | |
414 $child[$i] = $this->normalize($childNode, null, $options); | |
415 } | |
416 } else { | |
417 foreach ($child as $childNode) { | |
418 $this->append($childNode, $options); | |
419 } | |
420 } | |
421 return $child; | |
422 } | |
423 } else { | |
424 $attributes = array(); | |
425 if (func_num_args() >= 2) { | |
426 $attributes = func_get_arg(1); | |
427 } | |
428 $child =& $this->createNode($child, null, $attributes); | |
429 } | |
430 | |
431 $child = $this->normalize($child, null, $options); | |
432 | |
433 if (empty($child->namespace) && !empty($this->namespace)) { | |
434 $child->namespace = $this->namespace; | |
435 } | |
436 | |
437 if (is_a($child, 'XmlNode')) { | |
438 $child->setParent($this); | |
439 } | |
440 | |
441 return $child; | |
442 } | |
443 | |
444 /** | |
445 * Returns first child node, or null if empty. | |
446 * | |
447 * @return object First XmlNode | |
448 * @access public | |
449 */ | |
450 function &first() { | |
451 if (isset($this->children[0])) { | |
452 return $this->children[0]; | |
453 } else { | |
454 $return = null; | |
455 return $return; | |
456 } | |
457 } | |
458 | |
459 /** | |
460 * Returns last child node, or null if empty. | |
461 * | |
462 * @return object Last XmlNode | |
463 * @access public | |
464 */ | |
465 function &last() { | |
466 if (count($this->children) > 0) { | |
467 return $this->children[count($this->children) - 1]; | |
468 } else { | |
469 $return = null; | |
470 return $return; | |
471 } | |
472 } | |
473 | |
474 /** | |
475 * Returns child node with given ID. | |
476 * | |
477 * @param string $id Name of child node | |
478 * @return object Child XmlNode | |
479 * @access public | |
480 */ | |
481 function &child($id) { | |
482 $null = null; | |
483 | |
484 if (is_int($id)) { | |
485 if (isset($this->children[$id])) { | |
486 return $this->children[$id]; | |
487 } else { | |
488 return null; | |
489 } | |
490 } elseif (is_string($id)) { | |
491 for ($i = 0; $i < count($this->children); $i++) { | |
492 if ($this->children[$i]->name == $id) { | |
493 return $this->children[$i]; | |
494 } | |
495 } | |
496 } | |
497 return $null; | |
498 } | |
499 | |
500 /** | |
501 * Gets a list of childnodes with the given tag name. | |
502 * | |
503 * @param string $name Tag name of child nodes | |
504 * @return array An array of XmlNodes with the given tag name | |
505 * @access public | |
506 */ | |
507 function children($name) { | |
508 $nodes = array(); | |
509 $count = count($this->children); | |
510 for ($i = 0; $i < $count; $i++) { | |
511 if ($this->children[$i]->name == $name) { | |
512 $nodes[] =& $this->children[$i]; | |
513 } | |
514 } | |
515 return $nodes; | |
516 } | |
517 | |
518 /** | |
519 * Gets a reference to the next child node in the list of this node's parent. | |
520 * | |
521 * @return object A reference to the XmlNode object | |
522 * @access public | |
523 */ | |
524 function &nextSibling() { | |
525 $null = null; | |
526 $count = count($this->__parent->children); | |
527 for ($i = 0; $i < $count; $i++) { | |
528 if ($this->__parent->children[$i] == $this) { | |
529 if ($i >= $count - 1 || !isset($this->__parent->children[$i + 1])) { | |
530 return $null; | |
531 } | |
532 return $this->__parent->children[$i + 1]; | |
533 } | |
534 } | |
535 return $null; | |
536 } | |
537 | |
538 /** | |
539 * Gets a reference to the previous child node in the list of this node's parent. | |
540 * | |
541 * @return object A reference to the XmlNode object | |
542 * @access public | |
543 */ | |
544 function &previousSibling() { | |
545 $null = null; | |
546 $count = count($this->__parent->children); | |
547 for ($i = 0; $i < $count; $i++) { | |
548 if ($this->__parent->children[$i] == $this) { | |
549 if ($i == 0 || !isset($this->__parent->children[$i - 1])) { | |
550 return $null; | |
551 } | |
552 return $this->__parent->children[$i - 1]; | |
553 } | |
554 } | |
555 return $null; | |
556 } | |
557 | |
558 /** | |
559 * Returns parent node. | |
560 * | |
561 * @return object Parent XmlNode | |
562 * @access public | |
563 */ | |
564 function &parent() { | |
565 return $this->__parent; | |
566 } | |
567 | |
568 /** | |
569 * Returns the XML document to which this node belongs | |
570 * | |
571 * @return object Parent XML object | |
572 * @access public | |
573 */ | |
574 function &document() { | |
575 $document =& $this; | |
576 while (true) { | |
577 if (get_class($document) == 'Xml' || $document == null) { | |
578 break; | |
579 } | |
580 $document =& $document->parent(); | |
581 } | |
582 return $document; | |
583 } | |
584 | |
585 /** | |
586 * Returns true if this structure has child nodes. | |
587 * | |
588 * @return bool | |
589 * @access public | |
590 */ | |
591 function hasChildren() { | |
592 if (is_array($this->children) && !empty($this->children)) { | |
593 return true; | |
594 } | |
595 return false; | |
596 } | |
597 | |
598 /** | |
599 * Returns this XML structure as a string. | |
600 * | |
601 * @return string String representation of the XML structure. | |
602 * @access public | |
603 */ | |
604 function toString($options = array(), $depth = 0) { | |
605 if (is_int($options)) { | |
606 $depth = $options; | |
607 $options = array(); | |
608 } | |
609 $defaults = array('cdata' => true, 'whitespace' => false, 'convertEntities' => false, 'showEmpty' => true, 'leaveOpen' => false); | |
610 $options = array_merge($defaults, Xml::options(), $options); | |
611 $tag = !(strpos($this->name, '#') === 0); | |
612 $d = ''; | |
613 | |
614 if ($tag) { | |
615 if ($options['whitespace']) { | |
616 $d .= str_repeat("\t", $depth); | |
617 } | |
618 | |
619 $d .= '<' . $this->name(); | |
620 if (!empty($this->namespaces) > 0) { | |
621 foreach ($this->namespaces as $key => $val) { | |
622 $val = str_replace('"', '\"', $val); | |
623 $d .= ' xmlns:' . $key . '="' . $val . '"'; | |
624 } | |
625 } | |
626 | |
627 $parent =& $this->parent(); | |
628 if ($parent->name === '#document' && !empty($parent->namespaces)) { | |
629 foreach ($parent->namespaces as $key => $val) { | |
630 $val = str_replace('"', '\"', $val); | |
631 $d .= ' xmlns:' . $key . '="' . $val . '"'; | |
632 } | |
633 } | |
634 | |
635 if (is_array($this->attributes) && !empty($this->attributes)) { | |
636 foreach ($this->attributes as $key => $val) { | |
637 if (is_bool($val) && $val === false) { | |
638 $val = 0; | |
639 } | |
640 $d .= ' ' . $key . '="' . htmlspecialchars($val, ENT_QUOTES, Configure::read('App.encoding')) . '"'; | |
641 } | |
642 } | |
643 } | |
644 | |
645 if (!$this->hasChildren() && empty($this->value) && $this->value !== 0 && $tag) { | |
646 if (!$options['leaveOpen']) { | |
647 $d .= ' />'; | |
648 } | |
649 if ($options['whitespace']) { | |
650 $d .= "\n"; | |
651 } | |
652 } elseif ($tag || $this->hasChildren()) { | |
653 if ($tag) { | |
654 $d .= '>'; | |
655 } | |
656 if ($this->hasChildren()) { | |
657 if ($options['whitespace']) { | |
658 $d .= "\n"; | |
659 } | |
660 $count = count($this->children); | |
661 $cDepth = $depth + 1; | |
662 for ($i = 0; $i < $count; $i++) { | |
663 $d .= $this->children[$i]->toString($options, $cDepth); | |
664 } | |
665 if ($tag) { | |
666 if ($options['whitespace'] && $tag) { | |
667 $d .= str_repeat("\t", $depth); | |
668 } | |
669 if (!$options['leaveOpen']) { | |
670 $d .= '</' . $this->name() . '>'; | |
671 } | |
672 if ($options['whitespace']) { | |
673 $d .= "\n"; | |
674 } | |
675 } | |
676 } | |
677 } | |
678 return $d; | |
679 } | |
680 | |
681 /** | |
682 * Return array representation of current object. | |
683 * | |
684 * @param boolean $camelize true will camelize child nodes, false will not alter node names | |
685 * @return array Array representation | |
686 * @access public | |
687 */ | |
688 function toArray($camelize = true) { | |
689 $out = $this->attributes; | |
690 | |
691 foreach ($this->children as $child) { | |
692 $key = $camelize ? Inflector::camelize($child->name) : $child->name; | |
693 | |
694 $leaf = false; | |
695 if (is_a($child, 'XmlTextNode')) { | |
696 $out['value'] = $child->value; | |
697 continue; | |
698 } elseif (isset($child->children[0]) && is_a($child->children[0], 'XmlTextNode')) { | |
699 $value = $child->children[0]->value; | |
700 if ($child->attributes) { | |
701 $value = array_merge(array('value' => $value), $child->attributes); | |
702 } | |
703 if (count($child->children) == 1) { | |
704 $leaf = true; | |
705 } | |
706 } elseif (count($child->children) === 0 && $child->value == '') { | |
707 $value = $child->attributes; | |
708 if (empty($value)) { | |
709 $leaf = true; | |
710 } | |
711 } else { | |
712 $value = $child->toArray($camelize); | |
713 } | |
714 | |
715 if (isset($out[$key])) { | |
716 if(!isset($out[$key][0]) || !is_array($out[$key]) || !is_int(key($out[$key]))) { | |
717 $out[$key] = array($out[$key]); | |
718 } | |
719 $out[$key][] = $value; | |
720 } elseif (isset($out[$child->name])) { | |
721 $t = $out[$child->name]; | |
722 unset($out[$child->name]); | |
723 $out[$key] = array($t); | |
724 $out[$key][] = $value; | |
725 } elseif ($leaf) { | |
726 $out[$child->name] = $value; | |
727 } else { | |
728 $out[$key] = $value; | |
729 } | |
730 } | |
731 return $out; | |
732 } | |
733 | |
734 /** | |
735 * Returns data from toString when this object is converted to a string. | |
736 * | |
737 * @return string String representation of this structure. | |
738 * @access private | |
739 */ | |
740 function __toString() { | |
741 return $this->toString(); | |
742 } | |
743 | |
744 /** | |
745 * Debug method. Deletes the parent. Also deletes this node's children, | |
746 * if given the $recursive parameter. | |
747 * | |
748 * @param boolean $recursive Recursively delete elements. | |
749 * @access protected | |
750 */ | |
751 function _killParent($recursive = true) { | |
752 unset($this->__parent, $this->_log); | |
753 if ($recursive && $this->hasChildren()) { | |
754 for ($i = 0; $i < count($this->children); $i++) { | |
755 $this->children[$i]->_killParent(true); | |
756 } | |
757 } | |
758 } | |
759 } | |
760 | |
761 /** | |
762 * Main XML class. | |
763 * | |
764 * Parses and stores XML data, representing the root of an XML document | |
765 * | |
766 * @package cake | |
767 * @subpackage cake.cake.libs | |
768 * @since CakePHP v .0.10.3.1400 | |
769 */ | |
770 class Xml extends XmlNode { | |
771 | |
772 /** | |
773 * Resource handle to XML parser. | |
774 * | |
775 * @var resource | |
776 * @access private | |
777 */ | |
778 var $__parser; | |
779 | |
780 /** | |
781 * File handle to XML indata file. | |
782 * | |
783 * @var resource | |
784 * @access private | |
785 */ | |
786 var $__file; | |
787 | |
788 /** | |
789 * Raw XML string data (for loading purposes) | |
790 * | |
791 * @var string | |
792 * @access private | |
793 */ | |
794 var $__rawData = null; | |
795 | |
796 /** | |
797 * XML document header | |
798 * | |
799 * @var string | |
800 * @access private | |
801 */ | |
802 var $__header = null; | |
803 | |
804 /** | |
805 * Default array keys/object properties to use as tag names when converting objects or array | |
806 * structures to XML. Set by passing $options['tags'] to this object's constructor. | |
807 * | |
808 * @var array | |
809 * @access private | |
810 */ | |
811 var $__tags = array(); | |
812 | |
813 /** | |
814 * XML document version | |
815 * | |
816 * @var string | |
817 * @access private | |
818 */ | |
819 var $version = '1.0'; | |
820 | |
821 /** | |
822 * XML document encoding | |
823 * | |
824 * @var string | |
825 * @access private | |
826 */ | |
827 var $encoding = 'UTF-8'; | |
828 | |
829 /** | |
830 * Constructor. Sets up the XML parser with options, gives it this object as | |
831 * its XML object, and sets some variables. | |
832 * | |
833 * ### Options | |
834 * - 'root': The name of the root element, defaults to '#document' | |
835 * - 'version': The XML version, defaults to '1.0' | |
836 * - 'encoding': Document encoding, defaults to 'UTF-8' | |
837 * - 'namespaces': An array of namespaces (as strings) used in this document | |
838 * - 'format': Specifies the format this document converts to when parsed or | |
839 * rendered out as text, either 'attributes' or 'tags', defaults to 'attributes' | |
840 * - 'tags': An array specifying any tag-specific formatting options, indexed | |
841 * by tag name. See XmlNode::normalize(). | |
842 * - 'slug': A boolean to indicate whether or not you want the string version of the XML document | |
843 * to have its tags run through Inflector::slug(). Defaults to true | |
844 * | |
845 * @param mixed $input The content with which this XML document should be initialized. Can be a | |
846 * string, array or object. If a string is specified, it may be a literal XML | |
847 * document, or a URL or file path to read from. | |
848 * @param array $options Options to set up with, for valid options see above: | |
849 * @see XmlNode::normalize() | |
850 */ | |
851 function __construct($input = null, $options = array()) { | |
852 $defaults = array( | |
853 'root' => '#document', 'tags' => array(), 'namespaces' => array(), | |
854 'version' => '1.0', 'encoding' => 'UTF-8', 'format' => 'attributes', | |
855 'slug' => true | |
856 ); | |
857 $options = array_merge($defaults, Xml::options(), $options); | |
858 | |
859 foreach (array('version', 'encoding', 'namespaces') as $key) { | |
860 $this->{$key} = $options[$key]; | |
861 } | |
862 $this->__tags = $options['tags']; | |
863 parent::__construct('#document'); | |
864 | |
865 if ($options['root'] !== '#document') { | |
866 $Root =& $this->createNode($options['root']); | |
867 } else { | |
868 $Root =& $this; | |
869 } | |
870 | |
871 if (!empty($input)) { | |
872 if (is_string($input)) { | |
873 $Root->load($input); | |
874 } elseif (is_array($input) || is_object($input)) { | |
875 $Root->append($input, $options); | |
876 } | |
877 } | |
878 } | |
879 | |
880 /** | |
881 * Initialize XML object from a given XML string. Returns false on error. | |
882 * | |
883 * @param string $input XML string, a path to a file, or an HTTP resource to load | |
884 * @return boolean Success | |
885 * @access public | |
886 */ | |
887 function load($input) { | |
888 if (!is_string($input)) { | |
889 return false; | |
890 } | |
891 $this->__rawData = null; | |
892 $this->__header = null; | |
893 | |
894 if (strstr($input, "<")) { | |
895 $this->__rawData = $input; | |
896 } elseif (strpos($input, 'http://') === 0 || strpos($input, 'https://') === 0) { | |
897 App::import('Core', 'HttpSocket'); | |
898 $socket = new HttpSocket(); | |
899 $this->__rawData = $socket->get($input); | |
900 } elseif (file_exists($input)) { | |
901 $this->__rawData = file_get_contents($input); | |
902 } else { | |
903 trigger_error(__('XML cannot be read', true)); | |
904 return false; | |
905 } | |
906 return $this->parse(); | |
907 } | |
908 | |
909 /** | |
910 * Parses and creates XML nodes from the __rawData property. | |
911 * | |
912 * @return boolean Success | |
913 * @access public | |
914 * @see Xml::load() | |
915 * @todo figure out how to link attributes and namespaces | |
916 */ | |
917 function parse() { | |
918 $this->__initParser(); | |
919 $this->__rawData = trim($this->__rawData); | |
920 $this->__header = trim(str_replace( | |
921 array('<' . '?', '?' . '>'), | |
922 array('', ''), | |
923 substr($this->__rawData, 0, strpos($this->__rawData, '?' . '>')) | |
924 )); | |
925 | |
926 xml_parse_into_struct($this->__parser, $this->__rawData, $vals); | |
927 $xml =& $this; | |
928 $count = count($vals); | |
929 | |
930 for ($i = 0; $i < $count; $i++) { | |
931 $data = $vals[$i]; | |
932 $data += array('tag' => null, 'value' => null, 'attributes' => array()); | |
933 switch ($data['type']) { | |
934 case "open" : | |
935 $xml =& $xml->createElement($data['tag'], $data['value'], $data['attributes']); | |
936 break; | |
937 case "close" : | |
938 $xml =& $xml->parent(); | |
939 break; | |
940 case "complete" : | |
941 $xml->createElement($data['tag'], $data['value'], $data['attributes']); | |
942 break; | |
943 case 'cdata': | |
944 $xml->createTextNode($data['value']); | |
945 break; | |
946 } | |
947 } | |
948 xml_parser_free($this->__parser); | |
949 $this->__parser = null; | |
950 return true; | |
951 } | |
952 | |
953 /** | |
954 * Initializes the XML parser resource | |
955 * | |
956 * @return void | |
957 * @access private | |
958 */ | |
959 function __initParser() { | |
960 if (empty($this->__parser)) { | |
961 $this->__parser = xml_parser_create(); | |
962 xml_set_object($this->__parser, $this); | |
963 xml_parser_set_option($this->__parser, XML_OPTION_CASE_FOLDING, 0); | |
964 xml_parser_set_option($this->__parser, XML_OPTION_SKIP_WHITE, 1); | |
965 } | |
966 } | |
967 | |
968 /** | |
969 * Returns a string representation of the XML object | |
970 * | |
971 * @param mixed $options If boolean: whether to include the XML header with the document | |
972 * (defaults to true); if an array, overrides the default XML generation options | |
973 * @return string XML data | |
974 * @access public | |
975 * @deprecated | |
976 * @see Xml::toString() | |
977 */ | |
978 function compose($options = array()) { | |
979 return $this->toString($options); | |
980 } | |
981 | |
982 /** | |
983 * If debug mode is on, this method echoes an error message. | |
984 * | |
985 * @param string $msg Error message | |
986 * @param integer $code Error code | |
987 * @param integer $line Line in file | |
988 * @access public | |
989 */ | |
990 function error($msg, $code = 0, $line = 0) { | |
991 if (Configure::read('debug')) { | |
992 echo $msg . " " . $code . " " . $line; | |
993 } | |
994 } | |
995 | |
996 /** | |
997 * Returns a string with a textual description of the error code, or FALSE if no description was found. | |
998 * | |
999 * @param integer $code Error code | |
1000 * @return string Error message | |
1001 * @access public | |
1002 */ | |
1003 function getError($code) { | |
1004 $r = @xml_error_string($code); | |
1005 return $r; | |
1006 } | |
1007 | |
1008 // Overridden functions from superclass | |
1009 | |
1010 /** | |
1011 * Get next element. NOT implemented. | |
1012 * | |
1013 * @return object | |
1014 * @access public | |
1015 */ | |
1016 function &next() { | |
1017 $return = null; | |
1018 return $return; | |
1019 } | |
1020 | |
1021 /** | |
1022 * Get previous element. NOT implemented. | |
1023 * | |
1024 * @return object | |
1025 * @access public | |
1026 */ | |
1027 function &previous() { | |
1028 $return = null; | |
1029 return $return; | |
1030 } | |
1031 | |
1032 /** | |
1033 * Get parent element. NOT implemented. | |
1034 * | |
1035 * @return object | |
1036 * @access public | |
1037 */ | |
1038 function &parent() { | |
1039 $return = null; | |
1040 return $return; | |
1041 } | |
1042 | |
1043 /** | |
1044 * Adds a namespace to the current document | |
1045 * | |
1046 * @param string $prefix The namespace prefix | |
1047 * @param string $url The namespace DTD URL | |
1048 * @return void | |
1049 */ | |
1050 function addNamespace($prefix, $url) { | |
1051 if ($count = count($this->children)) { | |
1052 for ($i = 0; $i < $count; $i++) { | |
1053 $this->children[$i]->addNamespace($prefix, $url); | |
1054 } | |
1055 return true; | |
1056 } | |
1057 return parent::addNamespace($prefix, $url); | |
1058 } | |
1059 | |
1060 /** | |
1061 * Removes a namespace to the current document | |
1062 * | |
1063 * @param string $prefix The namespace prefix | |
1064 * @return void | |
1065 */ | |
1066 function removeNamespace($prefix) { | |
1067 if ($count = count($this->children)) { | |
1068 for ($i = 0; $i < $count; $i++) { | |
1069 $this->children[$i]->removeNamespace($prefix); | |
1070 } | |
1071 return true; | |
1072 } | |
1073 return parent::removeNamespace($prefix); | |
1074 } | |
1075 | |
1076 /** | |
1077 * Return string representation of current object. | |
1078 * | |
1079 * @return string String representation | |
1080 * @access public | |
1081 */ | |
1082 function toString($options = array()) { | |
1083 if (is_bool($options)) { | |
1084 $options = array('header' => $options); | |
1085 } | |
1086 | |
1087 $defaults = array('header' => false, 'encoding' => $this->encoding); | |
1088 $options = array_merge($defaults, Xml::options(), $options); | |
1089 $data = parent::toString($options, 0); | |
1090 | |
1091 if ($options['header']) { | |
1092 if (!empty($this->__header)) { | |
1093 return $this->header($this->__header) . "\n" . $data; | |
1094 } | |
1095 return $this->header() . "\n" . $data; | |
1096 } | |
1097 | |
1098 return $data; | |
1099 } | |
1100 | |
1101 /** | |
1102 * Return a header used on the first line of the xml file | |
1103 * | |
1104 * @param mixed $attrib attributes of the header element | |
1105 * @return string formated header | |
1106 */ | |
1107 function header($attrib = array()) { | |
1108 $header = 'xml'; | |
1109 if (is_string($attrib)) { | |
1110 $header = $attrib; | |
1111 } else { | |
1112 | |
1113 $attrib = array_merge(array('version' => $this->version, 'encoding' => $this->encoding), $attrib); | |
1114 foreach ($attrib as $key=>$val) { | |
1115 $header .= ' ' . $key . '="' . $val . '"'; | |
1116 } | |
1117 } | |
1118 return '<' . '?' . $header . ' ?' . '>'; | |
1119 } | |
1120 | |
1121 /** | |
1122 * Destructor, used to free resources. | |
1123 * | |
1124 * @access private | |
1125 */ | |
1126 function __destruct() { | |
1127 $this->_killParent(true); | |
1128 } | |
1129 | |
1130 /** | |
1131 * Adds a namespace to any XML documents generated or parsed | |
1132 * | |
1133 * @param string $name The namespace name | |
1134 * @param string $url The namespace URI; can be empty if in the default namespace map | |
1135 * @return boolean False if no URL is specified, and the namespace does not exist | |
1136 * default namespace map, otherwise true | |
1137 * @access public | |
1138 * @static | |
1139 */ | |
1140 function addGlobalNs($name, $url = null) { | |
1141 $_this =& XmlManager::getInstance(); | |
1142 if ($ns = Xml::resolveNamespace($name, $url)) { | |
1143 $_this->namespaces = array_merge($_this->namespaces, $ns); | |
1144 return $ns; | |
1145 } | |
1146 return false; | |
1147 } | |
1148 | |
1149 /** | |
1150 * Resolves current namespace | |
1151 * | |
1152 * @param string $name | |
1153 * @param string $url | |
1154 * @return array | |
1155 */ | |
1156 function resolveNamespace($name, $url) { | |
1157 $_this =& XmlManager::getInstance(); | |
1158 if ($url == null && isset($_this->defaultNamespaceMap[$name])) { | |
1159 $url = $_this->defaultNamespaceMap[$name]; | |
1160 } elseif ($url == null) { | |
1161 return false; | |
1162 } | |
1163 | |
1164 if (!strpos($url, '://') && isset($_this->defaultNamespaceMap[$name])) { | |
1165 $_url = $_this->defaultNamespaceMap[$name]; | |
1166 $name = $url; | |
1167 $url = $_url; | |
1168 } | |
1169 return array($name => $url); | |
1170 } | |
1171 | |
1172 /** | |
1173 * Alias to Xml::addNs | |
1174 * | |
1175 * @access public | |
1176 * @static | |
1177 */ | |
1178 function addGlobalNamespace($name, $url = null) { | |
1179 return Xml::addGlobalNs($name, $url); | |
1180 } | |
1181 | |
1182 /** | |
1183 * Removes a namespace added in addNs() | |
1184 * | |
1185 * @param string $name The namespace name or URI | |
1186 * @access public | |
1187 * @static | |
1188 */ | |
1189 function removeGlobalNs($name) { | |
1190 $_this =& XmlManager::getInstance(); | |
1191 if (isset($_this->namespaces[$name])) { | |
1192 unset($_this->namespaces[$name]); | |
1193 unset($this->namespaces[$name]); | |
1194 return true; | |
1195 } elseif (in_array($name, $_this->namespaces)) { | |
1196 $keys = array_keys($_this->namespaces); | |
1197 $count = count($keys); | |
1198 for ($i = 0; $i < $count; $i++) { | |
1199 if ($_this->namespaces[$keys[$i]] == $name) { | |
1200 unset($_this->namespaces[$keys[$i]]); | |
1201 unset($this->namespaces[$keys[$i]]); | |
1202 return true; | |
1203 } | |
1204 } | |
1205 } | |
1206 return false; | |
1207 } | |
1208 | |
1209 /** | |
1210 * Alias to Xml::removeNs | |
1211 * | |
1212 * @access public | |
1213 * @static | |
1214 */ | |
1215 function removeGlobalNamespace($name) { | |
1216 return Xml::removeGlobalNs($name); | |
1217 } | |
1218 | |
1219 /** | |
1220 * Sets/gets global XML options | |
1221 * | |
1222 * @param array $options | |
1223 * @return array | |
1224 * @access public | |
1225 * @static | |
1226 */ | |
1227 function options($options = array()) { | |
1228 $_this =& XmlManager::getInstance(); | |
1229 $_this->options = array_merge($_this->options, $options); | |
1230 return $_this->options; | |
1231 } | |
1232 } | |
1233 | |
1234 /** | |
1235 * The XML Element | |
1236 * | |
1237 */ | |
1238 class XmlElement extends XmlNode { | |
1239 | |
1240 /** | |
1241 * Construct an Xml element | |
1242 * | |
1243 * @param string $name name of the node | |
1244 * @param string $value value of the node | |
1245 * @param array $attributes | |
1246 * @param string $namespace | |
1247 * @return string A copy of $data in XML format | |
1248 */ | |
1249 function __construct($name = null, $value = null, $attributes = array(), $namespace = false) { | |
1250 parent::__construct($name, $value, $namespace); | |
1251 $this->addAttribute($attributes); | |
1252 } | |
1253 | |
1254 /** | |
1255 * Get all the attributes for this element | |
1256 * | |
1257 * @return array | |
1258 */ | |
1259 function attributes() { | |
1260 return $this->attributes; | |
1261 } | |
1262 | |
1263 /** | |
1264 * Add attributes to this element | |
1265 * | |
1266 * @param string $name name of the node | |
1267 * @param string $value value of the node | |
1268 * @return boolean | |
1269 */ | |
1270 function addAttribute($name, $val = null) { | |
1271 if (is_object($name)) { | |
1272 $name = get_object_vars($name); | |
1273 } | |
1274 if (is_array($name)) { | |
1275 foreach ($name as $key => $val) { | |
1276 $this->addAttribute($key, $val); | |
1277 } | |
1278 return true; | |
1279 } | |
1280 if (is_numeric($name)) { | |
1281 $name = $val; | |
1282 $val = null; | |
1283 } | |
1284 if (!empty($name)) { | |
1285 if (strpos($name, 'xmlns') === 0) { | |
1286 if ($name == 'xmlns') { | |
1287 $this->namespace = $val; | |
1288 } else { | |
1289 list($pre, $prefix) = explode(':', $name); | |
1290 $this->addNamespace($prefix, $val); | |
1291 return true; | |
1292 } | |
1293 } | |
1294 $this->attributes[$name] = $val; | |
1295 return true; | |
1296 } | |
1297 return false; | |
1298 } | |
1299 | |
1300 /** | |
1301 * Remove attributes to this element | |
1302 * | |
1303 * @param string $name name of the node | |
1304 * @return boolean | |
1305 */ | |
1306 function removeAttribute($attr) { | |
1307 if (array_key_exists($attr, $this->attributes)) { | |
1308 unset($this->attributes[$attr]); | |
1309 return true; | |
1310 } | |
1311 return false; | |
1312 } | |
1313 } | |
1314 | |
1315 /** | |
1316 * XML text or CDATA node | |
1317 * | |
1318 * Stores XML text data according to the encoding of the parent document | |
1319 * | |
1320 * @package cake | |
1321 * @subpackage cake.cake.libs | |
1322 * @since CakePHP v .1.2.6000 | |
1323 */ | |
1324 class XmlTextNode extends XmlNode { | |
1325 | |
1326 /** | |
1327 * Harcoded XML node name, represents this object as a text node | |
1328 * | |
1329 * @var string | |
1330 */ | |
1331 var $name = '#text'; | |
1332 | |
1333 /** | |
1334 * The text/data value which this node contains | |
1335 * | |
1336 * @var string | |
1337 */ | |
1338 var $value = null; | |
1339 | |
1340 /** | |
1341 * Construct text node with the given parent object and data | |
1342 * | |
1343 * @param object $parent Parent XmlNode/XmlElement object | |
1344 * @param mixed $value Node value | |
1345 */ | |
1346 function __construct($value = null) { | |
1347 $this->value = $value; | |
1348 } | |
1349 | |
1350 /** | |
1351 * Looks for child nodes in this element | |
1352 * | |
1353 * @return boolean False - not supported | |
1354 */ | |
1355 function hasChildren() { | |
1356 return false; | |
1357 } | |
1358 | |
1359 /** | |
1360 * Append an XML node: XmlTextNode does not support this operation | |
1361 * | |
1362 * @return boolean False - not supported | |
1363 * @todo make convertEntities work without mb support, convert entities to number entities | |
1364 */ | |
1365 function append() { | |
1366 return false; | |
1367 } | |
1368 | |
1369 /** | |
1370 * Return string representation of current text node object. | |
1371 * | |
1372 * @return string String representation | |
1373 * @access public | |
1374 */ | |
1375 function toString($options = array(), $depth = 0) { | |
1376 if (is_int($options)) { | |
1377 $depth = $options; | |
1378 $options = array(); | |
1379 } | |
1380 | |
1381 $defaults = array('cdata' => true, 'whitespace' => false, 'convertEntities' => false); | |
1382 $options = array_merge($defaults, Xml::options(), $options); | |
1383 $val = $this->value; | |
1384 | |
1385 if ($options['convertEntities'] && function_exists('mb_convert_encoding')) { | |
1386 $val = mb_convert_encoding($val,'UTF-8', 'HTML-ENTITIES'); | |
1387 } | |
1388 | |
1389 if ($options['cdata'] === true && !is_numeric($val)) { | |
1390 $val = '<![CDATA[' . $val . ']]>'; | |
1391 } | |
1392 | |
1393 if ($options['whitespace']) { | |
1394 return str_repeat("\t", $depth) . $val . "\n"; | |
1395 } | |
1396 return $val; | |
1397 } | |
1398 } | |
1399 | |
1400 /** | |
1401 * Manages application-wide namespaces and XML parsing/generation settings. | |
1402 * Private class, used exclusively within scope of XML class. | |
1403 * | |
1404 * @access private | |
1405 */ | |
1406 class XmlManager { | |
1407 | |
1408 /** | |
1409 * Global XML namespaces. Used in all XML documents processed by this application | |
1410 * | |
1411 * @var array | |
1412 * @access public | |
1413 */ | |
1414 var $namespaces = array(); | |
1415 | |
1416 /** | |
1417 * Global XML document parsing/generation settings. | |
1418 * | |
1419 * @var array | |
1420 * @access public | |
1421 */ | |
1422 var $options = array(); | |
1423 | |
1424 /** | |
1425 * Map of common namespace URIs | |
1426 * | |
1427 * @access private | |
1428 * @var array | |
1429 */ | |
1430 var $defaultNamespaceMap = array( | |
1431 'dc' => 'http://purl.org/dc/elements/1.1/', // Dublin Core | |
1432 'dct' => 'http://purl.org/dc/terms/', // Dublin Core Terms | |
1433 'g' => 'http://base.google.com/ns/1.0', // Google Base | |
1434 'rc' => 'http://purl.org/rss/1.0/modules/content/', // RSS 1.0 Content Module | |
1435 'wf' => 'http://wellformedweb.org/CommentAPI/', // Well-Formed Web Comment API | |
1436 'fb' => 'http://rssnamespace.org/feedburner/ext/1.0', // FeedBurner extensions | |
1437 'lj' => 'http://www.livejournal.org/rss/lj/1.0/', // Live Journal | |
1438 'itunes' => 'http://www.itunes.com/dtds/podcast-1.0.dtd', // iTunes | |
1439 'xhtml' => 'http://www.w3.org/1999/xhtml', // XHTML, | |
1440 'atom' => 'http://www.w3.org/2005/Atom' // Atom | |
1441 ); | |
1442 | |
1443 /** | |
1444 * Returns a reference to the global XML object that manages app-wide XML settings | |
1445 * | |
1446 * @return object | |
1447 * @access public | |
1448 */ | |
1449 function &getInstance() { | |
1450 static $instance = array(); | |
1451 | |
1452 if (!$instance) { | |
1453 $instance[0] =& new XmlManager(); | |
1454 } | |
1455 return $instance[0]; | |
1456 } | |
1457 } |