Fix checkRpItemsPosition
[ryzomcore.git] / web / public_php / webtt / cake / libs / xml.php
blobb6798ca21988ddfd65d81904a9914a739685c7f8
1 <?php
2 /**
3 * XML handling for Cake.
5 * The methods in these classes enable the datasources that use XML to work.
7 * PHP versions 4 and 5
9 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
10 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
12 * Licensed under The MIT License
13 * Redistributions of files must retain the above copyright notice.
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)
22 App::import('Core', 'Set');
24 /**
25 * XML node.
27 * Single XML node in an XML tree.
29 * @package cake
30 * @subpackage cake.cake.libs
31 * @since CakePHP v .0.10.3.1400
33 class XmlNode extends Object {
35 /**
36 * Name of node
38 * @var string
39 * @access public
41 var $name = null;
43 /**
44 * Node namespace
46 * @var string
47 * @access public
49 var $namespace = null;
51 /**
52 * Namespaces defined for this node and all child nodes
54 * @var array
55 * @access public
57 var $namespaces = array();
59 /**
60 * Value of node
62 * @var string
63 * @access public
65 var $value;
67 /**
68 * Attributes on this node
70 * @var array
71 * @access public
73 var $attributes = array();
75 /**
76 * This node's children
78 * @var array
79 * @access public
81 var $children = array();
83 /**
84 * Reference to parent node.
86 * @var XmlNode
87 * @access private
89 var $__parent = null;
91 /**
92 * Constructor.
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
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;
106 $this->name = $name;
107 if ($namespace) {
108 $this->namespace = $namespace;
111 if (is_array($value) || is_object($value)) {
112 $this->normalize($value);
113 } elseif (!empty($value) || $value === 0 || $value === '0') {
114 $this->createTextNode($value);
118 * Adds a namespace to the current node
120 * @param string $prefix The namespace prefix
121 * @param string $url The namespace DTD URL
122 * @return void
124 function addNamespace($prefix, $url) {
125 if ($ns = Xml::addGlobalNs($prefix, $url)) {
126 $this->namespaces = array_merge($this->namespaces, $ns);
127 return true;
129 return false;
133 * Adds a namespace to the current node
135 * @param string $prefix The namespace prefix
136 * @param string $url The namespace DTD URL
137 * @return void
139 function removeNamespace($prefix) {
140 if (Xml::removeGlobalNs($prefix)) {
141 return true;
143 return false;
147 * Creates an XmlNode object that can be appended to this document or a node in it
149 * @param string $name Node name
150 * @param string $value Node value
151 * @param string $namespace Node namespace
152 * @return object XmlNode
154 function &createNode($name = null, $value = null, $namespace = false) {
155 $node =& new XmlNode($name, $value, $namespace);
156 $node->setParent($this);
157 return $node;
161 * Creates an XmlElement object that can be appended to this document or a node in it
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
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;
176 * Creates an XmlTextNode object that can be appended to this document or a node in it
178 * @param string $value Node value
179 * @return object XmlTextNode
181 function &createTextNode($value = null) {
182 $node = new XmlTextNode($value);
183 $node->setParent($this);
184 return $node;
188 * Gets the XML element properties from an object.
190 * @param object $object Object to get properties from
191 * @return array Properties from object
192 * @access public
194 function normalize($object, $keyName = null, $options = array()) {
195 if (is_a($object, 'XmlNode')) {
196 return $object;
198 $name = null;
199 $options += array('format' => 'attributes');
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);
211 $tagOpts = $this->__tagOptions($name);
213 if ($tagOpts === false) {
214 return;
217 if (isset($tagOpts['name'])) {
218 $name = $tagOpts['name'];
219 } elseif ($name != strtolower($name) && $options['slug'] !== false) {
220 $name = Inflector::slug(Inflector::underscore($name));
223 if (!empty($name)) {
224 $node =& $this->createElement($name);
225 } else {
226 $node =& $this;
229 $namespace = array();
230 $attributes = array();
231 $children = array();
232 $chldObjs = array();
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);
241 $attr = array();
243 if (isset($tagOpts['attributes'])) {
244 $attr = $tagOpts['attributes'];
246 if (isset($tagOpts['value']) && isset($chldObjs[$tagOpts['value']])) {
247 $node->createTextNode($chldObjs[$tagOpts['value']]);
248 unset($chldObjs[$tagOpts['value']]);
251 $n = $name;
252 if (isset($chldObjs['_name_'])) {
253 $n = null;
254 unset($chldObjs['_name_']);
256 $c = 0;
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;
266 if (is_array($val)) {
267 foreach ($val as $n2 => $obj2) {
268 if (is_numeric($n2)) {
269 $n2 = $n;
271 $node->normalize($obj2, $n2, $options);
273 } else {
274 if (is_object($val)) {
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);
282 } elseif ($options['format'] == 'attributes') {
283 $node->addAttribute($key, $val);
288 $c++;
290 if (!empty($name)) {
291 return $node;
293 return $children;
297 * Gets the tag-specific options for the given node name
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
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;
312 if ($tagOpts === false) {
313 return false;
315 if (empty($option)) {
316 return $tagOpts;
318 if (isset($tagOpts[$option])) {
319 return $tagOpts[$option];
321 return null;
325 * Returns the fully-qualified XML node name, with namespace
327 * @access public
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;
336 return $this->name;
340 * Sets the parent node of this XmlNode.
342 * @access public
344 function setParent(&$parent) {
345 if (strtolower(get_class($this)) == 'xml') {
346 return;
348 if (isset($this->__parent) && is_object($this->__parent)) {
349 if ($this->__parent->compare($parent)) {
350 return;
352 foreach ($this->__parent->children as $i => $child) {
353 if ($this->compare($child)) {
354 array_splice($this->__parent->children, $i, 1);
355 break;
359 if ($parent == null) {
360 unset($this->__parent);
361 } else {
362 $parent->children[] =& $this;
363 $this->__parent =& $parent;
368 * Returns a copy of self.
370 * @return object Cloned instance
371 * @access public
373 function cloneNode() {
374 return clone($this);
378 * Compares $node to this XmlNode object
380 * @param object An XmlNode or subclass instance
381 * @return boolean True if the nodes match, false otherwise
382 * @access public
384 function compare($node) {
385 $keys = array(get_object_vars($this), get_object_vars($node));
386 return ($keys[0] === $keys[1]);
390 * Append given node as a child.
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
397 function &append(&$child, $options = array()) {
398 if (empty($child)) {
399 $return = false;
400 return $return;
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;
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);
416 } else {
417 foreach ($child as $childNode) {
418 $this->append($childNode, $options);
421 return $child;
423 } else {
424 $attributes = array();
425 if (func_num_args() >= 2) {
426 $attributes = func_get_arg(1);
428 $child =& $this->createNode($child, null, $attributes);
431 $child = $this->normalize($child, null, $options);
433 if (empty($child->namespace) && !empty($this->namespace)) {
434 $child->namespace = $this->namespace;
437 if (is_a($child, 'XmlNode')) {
438 $child->setParent($this);
441 return $child;
445 * Returns first child node, or null if empty.
447 * @return object First XmlNode
448 * @access public
450 function &first() {
451 if (isset($this->children[0])) {
452 return $this->children[0];
453 } else {
454 $return = null;
455 return $return;
460 * Returns last child node, or null if empty.
462 * @return object Last XmlNode
463 * @access public
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;
475 * Returns child node with given ID.
477 * @param string $id Name of child node
478 * @return object Child XmlNode
479 * @access public
481 function &child($id) {
482 $null = null;
484 if (is_int($id)) {
485 if (isset($this->children[$id])) {
486 return $this->children[$id];
487 } else {
488 return null;
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];
497 return $null;
501 * Gets a list of childnodes with the given tag name.
503 * @param string $name Tag name of child nodes
504 * @return array An array of XmlNodes with the given tag name
505 * @access public
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];
515 return $nodes;
519 * Gets a reference to the next child node in the list of this node's parent.
521 * @return object A reference to the XmlNode object
522 * @access public
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;
532 return $this->__parent->children[$i + 1];
535 return $null;
539 * Gets a reference to the previous child node in the list of this node's parent.
541 * @return object A reference to the XmlNode object
542 * @access public
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;
552 return $this->__parent->children[$i - 1];
555 return $null;
559 * Returns parent node.
561 * @return object Parent XmlNode
562 * @access public
564 function &parent() {
565 return $this->__parent;
569 * Returns the XML document to which this node belongs
571 * @return object Parent XML object
572 * @access public
574 function &document() {
575 $document =& $this;
576 while (true) {
577 if (get_class($document) == 'Xml' || $document == null) {
578 break;
580 $document =& $document->parent();
582 return $document;
586 * Returns true if this structure has child nodes.
588 * @return bool
589 * @access public
591 function hasChildren() {
592 if (is_array($this->children) && !empty($this->children)) {
593 return true;
595 return false;
599 * Returns this XML structure as a string.
601 * @return string String representation of the XML structure.
602 * @access public
604 function toString($options = array(), $depth = 0) {
605 if (is_int($options)) {
606 $depth = $options;
607 $options = array();
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 = '';
614 if ($tag) {
615 if ($options['whitespace']) {
616 $d .= str_repeat("\t", $depth);
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 . '"';
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 . '"';
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;
640 $d .= ' ' . $key . '="' . htmlspecialchars($val, ENT_QUOTES, Configure::read('App.encoding')) . '"';
645 if (!$this->hasChildren() && empty($this->value) && $this->value !== 0 && $tag) {
646 if (!$options['leaveOpen']) {
647 $d .= ' />';
649 if ($options['whitespace']) {
650 $d .= "\n";
652 } elseif ($tag || $this->hasChildren()) {
653 if ($tag) {
654 $d .= '>';
656 if ($this->hasChildren()) {
657 if ($options['whitespace']) {
658 $d .= "\n";
660 $count = count($this->children);
661 $cDepth = $depth + 1;
662 for ($i = 0; $i < $count; $i++) {
663 $d .= $this->children[$i]->toString($options, $cDepth);
665 if ($tag) {
666 if ($options['whitespace'] && $tag) {
667 $d .= str_repeat("\t", $depth);
669 if (!$options['leaveOpen']) {
670 $d .= '</' . $this->name() . '>';
672 if ($options['whitespace']) {
673 $d .= "\n";
678 return $d;
682 * Return array representation of current object.
684 * @param boolean $camelize true will camelize child nodes, false will not alter node names
685 * @return array Array representation
686 * @access public
688 function toArray($camelize = true) {
689 $out = $this->attributes;
691 foreach ($this->children as $child) {
692 $key = $camelize ? Inflector::camelize($child->name) : $child->name;
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);
703 if (count($child->children) == 1) {
704 $leaf = true;
706 } elseif (count($child->children) === 0 && $child->value == '') {
707 $value = $child->attributes;
708 if (empty($value)) {
709 $leaf = true;
711 } else {
712 $value = $child->toArray($camelize);
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]);
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;
731 return $out;
735 * Returns data from toString when this object is converted to a string.
737 * @return string String representation of this structure.
738 * @access private
740 function __toString() {
741 return $this->toString();
745 * Debug method. Deletes the parent. Also deletes this node's children,
746 * if given the $recursive parameter.
748 * @param boolean $recursive Recursively delete elements.
749 * @access protected
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);
762 * Main XML class.
764 * Parses and stores XML data, representing the root of an XML document
766 * @package cake
767 * @subpackage cake.cake.libs
768 * @since CakePHP v .0.10.3.1400
770 class Xml extends XmlNode {
773 * Resource handle to XML parser.
775 * @var resource
776 * @access private
778 var $__parser;
781 * File handle to XML indata file.
783 * @var resource
784 * @access private
786 var $__file;
789 * Raw XML string data (for loading purposes)
791 * @var string
792 * @access private
794 var $__rawData = null;
797 * XML document header
799 * @var string
800 * @access private
802 var $__header = null;
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.
808 * @var array
809 * @access private
811 var $__tags = array();
814 * XML document version
816 * @var string
817 * @access private
819 var $version = '1.0';
822 * XML document encoding
824 * @var string
825 * @access private
827 var $encoding = 'UTF-8';
830 * Constructor. Sets up the XML parser with options, gives it this object as
831 * its XML object, and sets some variables.
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
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()
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
857 $options = array_merge($defaults, Xml::options(), $options);
859 foreach (array('version', 'encoding', 'namespaces') as $key) {
860 $this->{$key} = $options[$key];
862 $this->__tags = $options['tags'];
863 parent::__construct('#document');
865 if ($options['root'] !== '#document') {
866 $Root =& $this->createNode($options['root']);
867 } else {
868 $Root =& $this;
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);
881 * Initialize XML object from a given XML string. Returns false on error.
883 * @param string $input XML string, a path to a file, or an HTTP resource to load
884 * @return boolean Success
885 * @access public
887 function load($input) {
888 if (!is_string($input)) {
889 return false;
891 $this->__rawData = null;
892 $this->__header = null;
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;
906 return $this->parse();
910 * Parses and creates XML nodes from the __rawData property.
912 * @return boolean Success
913 * @access public
914 * @see Xml::load()
915 * @todo figure out how to link attributes and namespaces
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, '?' . '>'))
926 xml_parse_into_struct($this->__parser, $this->__rawData, $vals);
927 $xml =& $this;
928 $count = count($vals);
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;
948 xml_parser_free($this->__parser);
949 $this->__parser = null;
950 return true;
954 * Initializes the XML parser resource
956 * @return void
957 * @access private
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);
969 * Returns a string representation of the XML object
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()
978 function compose($options = array()) {
979 return $this->toString($options);
983 * If debug mode is on, this method echoes an error message.
985 * @param string $msg Error message
986 * @param integer $code Error code
987 * @param integer $line Line in file
988 * @access public
990 function error($msg, $code = 0, $line = 0) {
991 if (Configure::read('debug')) {
992 echo $msg . " " . $code . " " . $line;
997 * Returns a string with a textual description of the error code, or FALSE if no description was found.
999 * @param integer $code Error code
1000 * @return string Error message
1001 * @access public
1003 function getError($code) {
1004 $r = @xml_error_string($code);
1005 return $r;
1008 // Overridden functions from superclass
1011 * Get next element. NOT implemented.
1013 * @return object
1014 * @access public
1016 function &next() {
1017 $return = null;
1018 return $return;
1022 * Get previous element. NOT implemented.
1024 * @return object
1025 * @access public
1027 function &previous() {
1028 $return = null;
1029 return $return;
1033 * Get parent element. NOT implemented.
1035 * @return object
1036 * @access public
1038 function &parent() {
1039 $return = null;
1040 return $return;
1044 * Adds a namespace to the current document
1046 * @param string $prefix The namespace prefix
1047 * @param string $url The namespace DTD URL
1048 * @return void
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);
1055 return true;
1057 return parent::addNamespace($prefix, $url);
1061 * Removes a namespace to the current document
1063 * @param string $prefix The namespace prefix
1064 * @return void
1066 function removeNamespace($prefix) {
1067 if ($count = count($this->children)) {
1068 for ($i = 0; $i < $count; $i++) {
1069 $this->children[$i]->removeNamespace($prefix);
1071 return true;
1073 return parent::removeNamespace($prefix);
1077 * Return string representation of current object.
1079 * @return string String representation
1080 * @access public
1082 function toString($options = array()) {
1083 if (is_bool($options)) {
1084 $options = array('header' => $options);
1087 $defaults = array('header' => false, 'encoding' => $this->encoding);
1088 $options = array_merge($defaults, Xml::options(), $options);
1089 $data = parent::toString($options, 0);
1091 if ($options['header']) {
1092 if (!empty($this->__header)) {
1093 return $this->header($this->__header) . "\n" . $data;
1095 return $this->header() . "\n" . $data;
1098 return $data;
1102 * Return a header used on the first line of the xml file
1104 * @param mixed $attrib attributes of the header element
1105 * @return string formated header
1107 function header($attrib = array()) {
1108 $header = 'xml';
1109 if (is_string($attrib)) {
1110 $header = $attrib;
1111 } else {
1113 $attrib = array_merge(array('version' => $this->version, 'encoding' => $this->encoding), $attrib);
1114 foreach ($attrib as $key=>$val) {
1115 $header .= ' ' . $key . '="' . $val . '"';
1118 return '<' . '?' . $header . ' ?' . '>';
1122 * Destructor, used to free resources.
1124 * @access private
1126 function __destruct() {
1127 $this->_killParent(true);
1131 * Adds a namespace to any XML documents generated or parsed
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
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;
1146 return false;
1150 * Resolves current namespace
1152 * @param string $name
1153 * @param string $url
1154 * @return array
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;
1164 if (!strpos($url, '://') && isset($_this->defaultNamespaceMap[$name])) {
1165 $_url = $_this->defaultNamespaceMap[$name];
1166 $name = $url;
1167 $url = $_url;
1169 return array($name => $url);
1173 * Alias to Xml::addNs
1175 * @access public
1176 * @static
1178 function addGlobalNamespace($name, $url = null) {
1179 return Xml::addGlobalNs($name, $url);
1183 * Removes a namespace added in addNs()
1185 * @param string $name The namespace name or URI
1186 * @access public
1187 * @static
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;
1206 return false;
1210 * Alias to Xml::removeNs
1212 * @access public
1213 * @static
1215 function removeGlobalNamespace($name) {
1216 return Xml::removeGlobalNs($name);
1220 * Sets/gets global XML options
1222 * @param array $options
1223 * @return array
1224 * @access public
1225 * @static
1227 function options($options = array()) {
1228 $_this =& XmlManager::getInstance();
1229 $_this->options = array_merge($_this->options, $options);
1230 return $_this->options;
1235 * The XML Element
1238 class XmlElement extends XmlNode {
1241 * Construct an Xml element
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
1249 function __construct($name = null, $value = null, $attributes = array(), $namespace = false) {
1250 parent::__construct($name, $value, $namespace);
1251 $this->addAttribute($attributes);
1255 * Get all the attributes for this element
1257 * @return array
1259 function attributes() {
1260 return $this->attributes;
1264 * Add attributes to this element
1266 * @param string $name name of the node
1267 * @param string $value value of the node
1268 * @return boolean
1270 function addAttribute($name, $val = null) {
1271 if (is_object($name)) {
1272 $name = get_object_vars($name);
1274 if (is_array($name)) {
1275 foreach ($name as $key => $val) {
1276 $this->addAttribute($key, $val);
1278 return true;
1280 if (is_numeric($name)) {
1281 $name = $val;
1282 $val = null;
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;
1294 $this->attributes[$name] = $val;
1295 return true;
1297 return false;
1301 * Remove attributes to this element
1303 * @param string $name name of the node
1304 * @return boolean
1306 function removeAttribute($attr) {
1307 if (array_key_exists($attr, $this->attributes)) {
1308 unset($this->attributes[$attr]);
1309 return true;
1311 return false;
1316 * XML text or CDATA node
1318 * Stores XML text data according to the encoding of the parent document
1320 * @package cake
1321 * @subpackage cake.cake.libs
1322 * @since CakePHP v .1.2.6000
1324 class XmlTextNode extends XmlNode {
1327 * Harcoded XML node name, represents this object as a text node
1329 * @var string
1331 var $name = '#text';
1334 * The text/data value which this node contains
1336 * @var string
1338 var $value = null;
1341 * Construct text node with the given parent object and data
1343 * @param object $parent Parent XmlNode/XmlElement object
1344 * @param mixed $value Node value
1346 function __construct($value = null) {
1347 $this->value = $value;
1351 * Looks for child nodes in this element
1353 * @return boolean False - not supported
1355 function hasChildren() {
1356 return false;
1360 * Append an XML node: XmlTextNode does not support this operation
1362 * @return boolean False - not supported
1363 * @todo make convertEntities work without mb support, convert entities to number entities
1365 function append() {
1366 return false;
1370 * Return string representation of current text node object.
1372 * @return string String representation
1373 * @access public
1375 function toString($options = array(), $depth = 0) {
1376 if (is_int($options)) {
1377 $depth = $options;
1378 $options = array();
1381 $defaults = array('cdata' => true, 'whitespace' => false, 'convertEntities' => false);
1382 $options = array_merge($defaults, Xml::options(), $options);
1383 $val = $this->value;
1385 if ($options['convertEntities'] && function_exists('mb_convert_encoding')) {
1386 $val = mb_convert_encoding($val,'UTF-8', 'HTML-ENTITIES');
1389 if ($options['cdata'] === true && !is_numeric($val)) {
1390 $val = '<![CDATA[' . $val . ']]>';
1393 if ($options['whitespace']) {
1394 return str_repeat("\t", $depth) . $val . "\n";
1396 return $val;
1401 * Manages application-wide namespaces and XML parsing/generation settings.
1402 * Private class, used exclusively within scope of XML class.
1404 * @access private
1406 class XmlManager {
1409 * Global XML namespaces. Used in all XML documents processed by this application
1411 * @var array
1412 * @access public
1414 var $namespaces = array();
1417 * Global XML document parsing/generation settings.
1419 * @var array
1420 * @access public
1422 var $options = array();
1425 * Map of common namespace URIs
1427 * @access private
1428 * @var array
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
1444 * Returns a reference to the global XML object that manages app-wide XML settings
1446 * @return object
1447 * @access public
1449 function &getInstance() {
1450 static $instance = array();
1452 if (!$instance) {
1453 $instance[0] =& new XmlManager();
1455 return $instance[0];