source: Dev/branches/cakephp/cake/libs/xml.php @ 126

Last change on this file since 126 was 126, checked in by fpvanagthoven, 14 years ago

Cakephp branch.

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