3 * XML handling for Cake.
5 * The methods in these classes enable the datasources that use XML to work.
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
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');
27 * Single XML node in an XML tree.
30 * @subpackage cake.cake.libs
31 * @since CakePHP v .0.10.3.1400
33 class XmlNode
extends Object {
49 var $namespace = null;
52 * Namespaces defined for this node and all child nodes
57 var $namespaces = array();
68 * Attributes on this node
73 var $attributes = array();
76 * This node's children
81 var $children = array();
84 * Reference to parent node.
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);
103 $namespace = $prefix;
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
124 function addNamespace($prefix, $url) {
125 if ($ns = Xml
::addGlobalNs($prefix, $url)) {
126 $this->namespaces
= array_merge($this->namespaces
, $ns);
133 * Adds a namespace to the current node
135 * @param string $prefix The namespace prefix
136 * @param string $url The namespace DTD URL
139 function removeNamespace($prefix) {
140 if (Xml
::removeGlobalNs($prefix)) {
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);
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);
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);
188 * Gets the XML element properties from an object.
190 * @param object $object Object to get properties from
191 * @return array Properties from object
194 function normalize($object, $keyName = null, $options = array()) {
195 if (is_a($object, 'XmlNode')) {
199 $options +
= array('format' => 'attributes');
201 if ($keyName !== null && !is_numeric($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) {
217 if (isset($tagOpts['name'])) {
218 $name = $tagOpts['name'];
219 } elseif ($name != strtolower($name) && $options['slug'] !== false) {
220 $name = Inflector
::slug(Inflector
::underscore($name));
224 $node =& $this->createElement($name);
229 $namespace = array();
230 $attributes = array();
234 if (is_object($object)) {
235 $chldObjs = get_object_vars($object);
236 } elseif (is_array($object)) {
238 } elseif (!empty($object) ||
$object === 0 ||
$object === '0') {
239 $node->createTextNode($object);
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']]);
252 if (isset($chldObjs['_name_'])) {
254 unset($chldObjs['_name_']);
258 foreach ($chldObjs as $key => $val) {
259 if (in_array($key, $attr) && !is_object($val) && !is_array($val)) {
260 $attributes[$key] = $val;
262 if (!isset($tagOpts['children']) ||
$tagOpts['children'] === array() ||
(is_array($tagOpts['children']) && in_array($key, $tagOpts['children']))) {
263 if (!is_numeric($key)) {
266 if (is_array($val)) {
267 foreach ($val as $n2 => $obj2) {
268 if (is_numeric($n2)) {
271 $node->normalize($obj2, $n2, $options);
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);
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
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)];
312 if ($tagOpts === false) {
315 if (empty($option)) {
318 if (isset($tagOpts[$option])) {
319 return $tagOpts[$option];
325 * Returns the fully-qualified XML node name, with namespace
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
;
340 * Sets the parent node of this XmlNode.
344 function setParent(&$parent) {
345 if (strtolower(get_class($this)) == 'xml') {
348 if (isset($this->__parent
) && is_object($this->__parent
)) {
349 if ($this->__parent
->compare($parent)) {
352 foreach ($this->__parent
->children
as $i => $child) {
353 if ($this->compare($child)) {
354 array_splice($this->__parent
->children
, $i, 1);
359 if ($parent == null) {
360 unset($this->__parent
);
362 $parent->children
[] =& $this;
363 $this->__parent
=& $parent;
368 * Returns a copy of self.
370 * @return object Cloned instance
373 function cloneNode() {
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
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
397 function &append(&$child, $options = array()) {
403 if (is_object($child)) {
404 if ($this->compare($child)) {
405 trigger_error(__('Cannot append a node to itself.', true));
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);
417 foreach ($child as $childNode) {
418 $this->append($childNode, $options);
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);
445 * Returns first child node, or null if empty.
447 * @return object First XmlNode
451 if (isset($this->children
[0])) {
452 return $this->children
[0];
460 * Returns last child node, or null if empty.
462 * @return object Last XmlNode
466 if (count($this->children
) > 0) {
467 return $this->children
[count($this->children
) - 1];
475 * Returns child node with given ID.
477 * @param string $id Name of child node
478 * @return object Child XmlNode
481 function &child($id) {
485 if (isset($this->children
[$id])) {
486 return $this->children
[$id];
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];
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
507 function children($name) {
509 $count = count($this->children
);
510 for ($i = 0; $i < $count; $i++
) {
511 if ($this->children
[$i]->name
== $name) {
512 $nodes[] =& $this->children
[$i];
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
524 function &nextSibling() {
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])) {
532 return $this->__parent
->children
[$i +
1];
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
544 function &previousSibling() {
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])) {
552 return $this->__parent
->children
[$i - 1];
559 * Returns parent node.
561 * @return object Parent XmlNode
565 return $this->__parent
;
569 * Returns the XML document to which this node belongs
571 * @return object Parent XML object
574 function &document() {
577 if (get_class($document) == 'Xml' ||
$document == null) {
580 $document =& $document->parent();
586 * Returns true if this structure has child nodes.
591 function hasChildren() {
592 if (is_array($this->children
) && !empty($this->children
)) {
599 * Returns this XML structure as a string.
601 * @return string String representation of the XML structure.
604 function toString($options = array(), $depth = 0) {
605 if (is_int($options)) {
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);
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) {
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']) {
649 if ($options['whitespace']) {
652 } elseif ($tag ||
$this->hasChildren()) {
656 if ($this->hasChildren()) {
657 if ($options['whitespace']) {
660 $count = count($this->children
);
661 $cDepth = $depth +
1;
662 for ($i = 0; $i < $count; $i++
) {
663 $d .= $this->children
[$i]->toString($options, $cDepth);
666 if ($options['whitespace'] && $tag) {
667 $d .= str_repeat("\t", $depth);
669 if (!$options['leaveOpen']) {
670 $d .= '</' . $this->name() . '>';
672 if ($options['whitespace']) {
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
688 function toArray($camelize = true) {
689 $out = $this->attributes
;
691 foreach ($this->children
as $child) {
692 $key = $camelize ? Inflector
::camelize($child->name
) : $child->name
;
695 if (is_a($child, 'XmlTextNode')) {
696 $out['value'] = $child->value
;
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) {
706 } elseif (count($child->children
) === 0 && $child->value
== '') {
707 $value = $child->attributes
;
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;
726 $out[$child->name
] = $value;
735 * Returns data from toString when this object is converted to a string.
737 * @return string String representation of this structure.
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.
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);
764 * Parses and stores XML data, representing the root of an XML document
767 * @subpackage cake.cake.libs
768 * @since CakePHP v .0.10.3.1400
770 class Xml
extends XmlNode
{
773 * Resource handle to XML parser.
781 * File handle to XML indata file.
789 * Raw XML string data (for loading purposes)
794 var $__rawData = null;
797 * XML document header
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.
811 var $__tags = array();
814 * XML document version
819 var $version = '1.0';
822 * XML document encoding
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.
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()) {
853 'root' => '#document', 'tags' => array(), 'namespaces' => array(),
854 'version' => '1.0', 'encoding' => 'UTF-8', 'format' => 'attributes',
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']);
871 if (!empty($input)) {
872 if (is_string($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
887 function load($input) {
888 if (!is_string($input)) {
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);
903 trigger_error(__('XML cannot be read', true));
906 return $this->parse();
910 * Parses and creates XML nodes from the __rawData property.
912 * @return boolean Success
915 * @todo figure out how to link attributes and namespaces
918 $this->__initParser();
919 $this->__rawData
= trim($this->__rawData
);
920 $this->__header
= trim(str_replace(
921 array('<' . '?', '?' . '>'),
923 substr($this->__rawData
, 0, strpos($this->__rawData
, '?' . '>'))
926 xml_parse_into_struct($this->__parser
, $this->__rawData
, $vals);
928 $count = count($vals);
930 for ($i = 0; $i < $count; $i++
) {
932 $data +
= array('tag' => null, 'value' => null, 'attributes' => array());
933 switch ($data['type']) {
935 $xml =& $xml->createElement($data['tag'], $data['value'], $data['attributes']);
938 $xml =& $xml->parent();
941 $xml->createElement($data['tag'], $data['value'], $data['attributes']);
944 $xml->createTextNode($data['value']);
948 xml_parser_free($this->__parser
);
949 $this->__parser
= null;
954 * Initializes the XML parser resource
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
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
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
1003 function getError($code) {
1004 $r = @xml_error_string
($code);
1008 // Overridden functions from superclass
1011 * Get next element. NOT implemented.
1022 * Get previous element. NOT implemented.
1027 function &previous() {
1033 * Get parent element. NOT implemented.
1038 function &parent() {
1044 * Adds a namespace to the current document
1046 * @param string $prefix The namespace prefix
1047 * @param string $url The namespace DTD URL
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);
1057 return parent
::addNamespace($prefix, $url);
1061 * Removes a namespace to the current document
1063 * @param string $prefix The namespace prefix
1066 function removeNamespace($prefix) {
1067 if ($count = count($this->children
)) {
1068 for ($i = 0; $i < $count; $i++
) {
1069 $this->children
[$i]->removeNamespace($prefix);
1073 return parent
::removeNamespace($prefix);
1077 * Return string representation of current object.
1079 * @return string String representation
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;
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()) {
1109 if (is_string($attrib)) {
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.
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
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);
1150 * Resolves current namespace
1152 * @param string $name
1153 * @param string $url
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) {
1164 if (!strpos($url, '://') && isset($_this->defaultNamespaceMap
[$name])) {
1165 $_url = $_this->defaultNamespaceMap
[$name];
1169 return array($name => $url);
1173 * Alias to Xml::addNs
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
1189 function removeGlobalNs($name) {
1190 $_this =& XmlManager
::getInstance();
1191 if (isset($_this->namespaces
[$name])) {
1192 unset($_this->namespaces
[$name]);
1193 unset($this->namespaces
[$name]);
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]]);
1210 * Alias to Xml::removeNs
1215 function removeGlobalNamespace($name) {
1216 return Xml
::removeGlobalNs($name);
1220 * Sets/gets global XML options
1222 * @param array $options
1227 function options($options = array()) {
1228 $_this =& XmlManager
::getInstance();
1229 $_this->options
= array_merge($_this->options
, $options);
1230 return $_this->options
;
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
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
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);
1280 if (is_numeric($name)) {
1284 if (!empty($name)) {
1285 if (strpos($name, 'xmlns') === 0) {
1286 if ($name == 'xmlns') {
1287 $this->namespace = $val;
1289 list($pre, $prefix) = explode(':', $name);
1290 $this->addNamespace($prefix, $val);
1294 $this->attributes
[$name] = $val;
1301 * Remove attributes to this element
1303 * @param string $name name of the node
1306 function removeAttribute($attr) {
1307 if (array_key_exists($attr, $this->attributes
)) {
1308 unset($this->attributes
[$attr]);
1316 * XML text or CDATA node
1318 * Stores XML text data according to the encoding of the parent document
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
1331 var $name = '#text';
1334 * The text/data value which this node contains
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() {
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
1370 * Return string representation of current text node object.
1372 * @return string String representation
1375 function toString($options = array(), $depth = 0) {
1376 if (is_int($options)) {
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";
1401 * Manages application-wide namespaces and XML parsing/generation settings.
1402 * Private class, used exclusively within scope of XML class.
1409 * Global XML namespaces. Used in all XML documents processed by this application
1414 var $namespaces = array();
1417 * Global XML document parsing/generation settings.
1422 var $options = array();
1425 * Map of common namespace URIs
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
1449 function &getInstance() {
1450 static $instance = array();
1453 $instance[0] =& new XmlManager();
1455 return $instance[0];