2006-06-14 23:04:23 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* XML handling for Cake.
|
|
|
|
*
|
|
|
|
* The methods in these classes enable the datasources that use XML to work.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2007-02-02 10:39:45 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
2008-01-01 22:18:17 +00:00
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc.
|
2006-06-14 23:04:23 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2007-05-26 19:33:55 +00:00
|
|
|
*
|
2006-06-14 23:04:23 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2008-01-01 22:18:17 +00:00
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-02-02 10:39:45 +00:00
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
2006-06-14 23:04:23 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
* @since CakePHP v .0.10.3.1400
|
2007-02-02 10:39:45 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2006-10-08 14:30:28 +00:00
|
|
|
uses('set');
|
2006-06-14 23:04:23 +00:00
|
|
|
|
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* XML node.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* Single XML node in an XML tree.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
* @since CakePHP v .0.10.3.1400
|
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
class XMLNode extends Object {
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Name of node
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @var string
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
var $name = null;
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Value of node
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @var string
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
var $value;
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Attributes on this node
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @var array
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
var $attributes = array();
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* This node's children
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @var array
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
var $children = array();
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Reference to parent node.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @var XMLNode
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access private
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
var $__parent = null;
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Constructor.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @param string $name Node name
|
2007-05-26 19:33:55 +00:00
|
|
|
* @param array $attributes Node attributes
|
|
|
|
* @param mixed $value Node contents (text)
|
|
|
|
* @param array $children Node children
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function __construct($name = null, $attributes = array(), $value = null, $children = array()) {
|
|
|
|
$this->name = $name;
|
|
|
|
$this->attributes = $attributes;
|
|
|
|
$this->value = $value;
|
2006-06-14 23:04:23 +00:00
|
|
|
|
2007-05-14 14:58:52 +00:00
|
|
|
$c = count($children);
|
|
|
|
for ($i = 0; $i < $c; $i++) {
|
|
|
|
if (is_a($children[$i], 'XMLNode') || is_a($children[$i], 'xmlnode')) {
|
|
|
|
$this->append($children[$i]);
|
|
|
|
} elseif (is_array($children[$i])) {
|
|
|
|
$cName = '';
|
|
|
|
$cAttr = $cChildren = array();
|
|
|
|
list($cName, $cAttr, $cChildren) = $children[$i];
|
|
|
|
$node = new XMLNode($name, $cAttr, $cChildren);
|
|
|
|
$this->append($node);
|
|
|
|
unset($node);
|
|
|
|
} else {
|
|
|
|
$child = $children[$i];
|
|
|
|
$this->append($child);
|
|
|
|
unset($child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-26 19:33:55 +00:00
|
|
|
* Gets the XML element properties from an object.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @param object $object Object to get properties from
|
|
|
|
* @return array Properties from object
|
|
|
|
* @access private
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-12-22 05:09:28 +00:00
|
|
|
function __objectToNode($object, $key = false) {
|
2007-05-14 14:58:52 +00:00
|
|
|
if (is_array($object)) {
|
|
|
|
$objects = array();
|
|
|
|
foreach ($object as $obj) {
|
|
|
|
$objects[] = $this->__objectToNode($obj);
|
|
|
|
}
|
|
|
|
return $objects;
|
|
|
|
}
|
2006-08-20 22:20:44 +00:00
|
|
|
|
2007-12-22 04:46:00 +00:00
|
|
|
if (isset($object->_name_) && !empty($object->_name_)) {
|
|
|
|
$name = $object->_name_;
|
2007-12-22 05:09:28 +00:00
|
|
|
} elseif ($key) {
|
|
|
|
$name = $key;
|
2007-05-14 14:58:52 +00:00
|
|
|
} elseif (isset($object->name) && $object->name != null) {
|
|
|
|
$name = $object->name;
|
|
|
|
} else {
|
|
|
|
$name = get_class($object);
|
|
|
|
}
|
2007-12-22 05:09:28 +00:00
|
|
|
|
2007-12-08 06:08:03 +00:00
|
|
|
if ($name != strtolower($name)) {
|
2007-12-22 05:09:28 +00:00
|
|
|
$name = Inflector::slug(Inflector::underscore($name));
|
2007-05-14 14:58:52 +00:00
|
|
|
}
|
2006-08-20 22:20:44 +00:00
|
|
|
|
2007-05-14 14:58:52 +00:00
|
|
|
if (is_object($object)) {
|
|
|
|
$attributes = get_object_vars($object);
|
|
|
|
} elseif (is_array($object)) {
|
|
|
|
$attributes = $object[$name];
|
|
|
|
if (is_object($attributes)) {
|
|
|
|
$attributes = get_object_vars($attributes);
|
2006-10-08 14:30:28 +00:00
|
|
|
}
|
2007-05-14 14:58:52 +00:00
|
|
|
}
|
2006-10-08 14:30:28 +00:00
|
|
|
|
2007-05-14 14:58:52 +00:00
|
|
|
$children = array();
|
|
|
|
$attr = $attributes;
|
2006-10-08 14:30:28 +00:00
|
|
|
|
2007-05-14 14:58:52 +00:00
|
|
|
foreach ($attr as $key => $val) {
|
|
|
|
if (is_array($val)) {
|
|
|
|
foreach ($val as $i => $obj2) {
|
|
|
|
$children[] = $this->__objectToNode($obj2);
|
|
|
|
unset($attributes[$key]);
|
|
|
|
}
|
|
|
|
} elseif (is_object($val)) {
|
2007-12-22 05:09:28 +00:00
|
|
|
$children[] = $this->__objectToNode($val, $key);
|
2007-05-14 14:58:52 +00:00
|
|
|
unset($attributes[$key]);
|
2006-10-08 14:30:28 +00:00
|
|
|
}
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2007-12-22 04:46:00 +00:00
|
|
|
unset($attributes['_name_']);
|
2006-08-20 22:20:44 +00:00
|
|
|
|
2007-05-14 14:58:52 +00:00
|
|
|
$node = new XMLNode($name, $attributes, null, $children);
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
/**
|
2007-05-26 19:33:55 +00:00
|
|
|
* Sets the parent node of this XMLNode.
|
2007-05-14 14:58:52 +00:00
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-05-14 14:58:52 +00:00
|
|
|
*/
|
|
|
|
function setParent(&$parent) {
|
|
|
|
$this->__parent =& $parent;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns a copy of self.
|
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @return object Cloned instance
|
|
|
|
* @access public
|
2007-05-14 14:58:52 +00:00
|
|
|
*/
|
|
|
|
function cloneNode() {
|
2007-05-26 19:33:55 +00:00
|
|
|
return clone($this);
|
2007-05-14 14:58:52 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Append given node as a child.
|
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @param object $child XMLNode with appended child
|
|
|
|
* @access public
|
2007-05-14 14:58:52 +00:00
|
|
|
*/
|
|
|
|
function &append(&$child) {
|
|
|
|
if (is_object($child)) {
|
|
|
|
$this->children[] =& $child;
|
|
|
|
} elseif (is_string($child)) {
|
|
|
|
$attr = array();
|
|
|
|
if (func_num_args() >= 2 && is_array(func_get_arg(1))) {
|
|
|
|
$attr = func_get_arg(1);
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2007-05-26 19:33:55 +00:00
|
|
|
$tmp =& new XMLNode();
|
2007-05-14 14:58:52 +00:00
|
|
|
$tmp->name = $child;
|
|
|
|
$tmp->attributes = $attr;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2007-05-14 14:58:52 +00:00
|
|
|
return $tmp;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Returns first child node, or null if empty.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @return object First XMLNode
|
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function &first() {
|
2007-06-20 06:15:35 +00:00
|
|
|
if (isset($this->children[0])) {
|
2007-05-14 14:58:52 +00:00
|
|
|
return $this->children[0];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns last child node, or null if empty.
|
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @return object Last XMLNode
|
|
|
|
* @access public
|
2007-05-14 14:58:52 +00:00
|
|
|
*/
|
|
|
|
function &last() {
|
2007-06-20 06:15:35 +00:00
|
|
|
if (count($this->children) > 0) {
|
2007-05-14 14:58:52 +00:00
|
|
|
return $this->children[count($this->children) - 1];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns child node with given ID.
|
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @param string $id Name of child node
|
|
|
|
* @return object Child XMLNode
|
|
|
|
* @access public
|
2007-05-14 14:58:52 +00:00
|
|
|
*/
|
|
|
|
function &child($id) {
|
|
|
|
$null = null;
|
2006-10-08 14:30:28 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (is_int($id)) {
|
|
|
|
if (isset($this->children[$id])) {
|
2007-05-14 14:58:52 +00:00
|
|
|
return $this->children[$id];
|
2006-10-08 14:30:28 +00:00
|
|
|
} else {
|
2007-05-14 14:58:52 +00:00
|
|
|
return null;
|
|
|
|
}
|
2007-06-20 06:15:35 +00:00
|
|
|
} elseif (is_string($id)) {
|
|
|
|
for ($i = 0; $i < count($this->children); $i++) {
|
|
|
|
if ($this->children[$i]->name == $id) {
|
2007-05-14 14:58:52 +00:00
|
|
|
return $this->children[$i];
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-14 14:58:52 +00:00
|
|
|
return $null;
|
|
|
|
} else {
|
|
|
|
return $null;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Gets a list of childnodes with the given tag name.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @param string $name Tag name of child nodes
|
|
|
|
* @return array An array of XMLNodes with the given tag name
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function children($name) {
|
|
|
|
$nodes = array();
|
|
|
|
$count = count($this->children);
|
2007-06-20 06:15:35 +00:00
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
if ($this->children[$i]->name == $name) {
|
2007-05-14 14:58:52 +00:00
|
|
|
$nodes[] =& $this->children[$i];
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-14 14:58:52 +00:00
|
|
|
return $nodes;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-26 19:33:55 +00:00
|
|
|
* Gets a reference to the next child node in the list of this node's parent.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @return object A reference to the XMLNode object
|
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function &nextSibling() {
|
|
|
|
$count = count($this->__parent->children);
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
if ($this->__parent->children === $this) {
|
|
|
|
if ($i >= $count - 1 || !isset($this->__parent->children[$i + 1])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->__parent->children[$i + 1];
|
|
|
|
}
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-26 19:33:55 +00:00
|
|
|
* Gets a reference to the previous child node in the list of this node's parent.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @return object A reference to the XMLNode object
|
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function &previousSibling() {
|
|
|
|
$count = count($this->__parent->children);
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
if ($this->__parent->children === $this) {
|
|
|
|
if ($i == 0 || !isset($this->__parent->children[$i - 1])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->__parent->children[$i - 1];
|
|
|
|
}
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Returns parent node.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @return object Parent XMLNode
|
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function &parent() {
|
|
|
|
return $this->__parent;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Returns true if this structure has child nodes.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-10-22 05:52:20 +00:00
|
|
|
* @return bool
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function hasChildren() {
|
2007-06-20 06:15:35 +00:00
|
|
|
if (is_array($this->children) && count($this->children) > 0) {
|
2007-05-14 14:58:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Returns this XML structure as a string.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @return string String representation of the XML structure.
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function toString() {
|
|
|
|
$d = '';
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($this->name != '') {
|
2007-05-14 14:58:52 +00:00
|
|
|
$d .= '<' . $this->name;
|
2007-06-20 06:15:35 +00:00
|
|
|
if (is_array($this->attributes) && count($this->attributes) > 0) {
|
|
|
|
foreach ($this->attributes as $key => $val) {
|
2007-05-14 14:58:52 +00:00
|
|
|
$d .= " $key=\"$val\"";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!$this->hasChildren() && empty($this->value)) {
|
|
|
|
if ($this->name != '') {
|
2007-05-14 14:58:52 +00:00
|
|
|
$d .= " />\n";
|
|
|
|
}
|
|
|
|
} else {
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($this->name != '') {
|
2007-05-14 14:58:52 +00:00
|
|
|
$d .= ">";
|
|
|
|
}
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($this->hasChildren()) {
|
2007-05-14 14:58:52 +00:00
|
|
|
if (is_string($this->value) || empty($this->value)) {
|
|
|
|
if (!empty($this->value)) {
|
|
|
|
$d .= $this->value;
|
|
|
|
}
|
|
|
|
$count = count($this->children);
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2007-05-14 14:58:52 +00:00
|
|
|
$d .= $this->children[$i]->toString();
|
|
|
|
}
|
|
|
|
} elseif (is_array($this->value)) {
|
|
|
|
$count = count($this->value);
|
2007-06-20 06:15:35 +00:00
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2007-05-14 14:58:52 +00:00
|
|
|
$d .= $this->value[$i];
|
|
|
|
if (isset($this->children[$i])) {
|
|
|
|
$d .= $this->children[$i]->toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$count = count($this->children);
|
|
|
|
if ($i < $count) {
|
|
|
|
for ($i = $i; $i < $count; $i++) {
|
|
|
|
$d .= $this->children[$i]->toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($this->value)) {
|
|
|
|
$d .= $this->value;
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($this->name != '' && ($this->hasChildren() || !empty($this->value))) {
|
2007-05-14 14:58:52 +00:00
|
|
|
$d .= "</" . $this->name . ">\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $d;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Returns data from toString when this object is converted to a string.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @return string String representation of this structure.
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access private
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function __toString() {
|
|
|
|
return $this->toString();
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2007-05-14 14:58:52 +00:00
|
|
|
/**
|
|
|
|
* Debug method. Deletes the parent. Also deletes this node's children,
|
|
|
|
* if given the $recursive parameter.
|
|
|
|
*
|
2007-10-22 16:54:36 +00:00
|
|
|
* @param boolean $recursive Recursively delete elements.
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access private
|
2007-05-14 14:58:52 +00:00
|
|
|
*/
|
|
|
|
function __killParent($recursive = true) {
|
|
|
|
unset($this->__parent);
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($recursive && $this->hasChildren()) {
|
|
|
|
for ($i = 0; $i < count($this->children); $i++) {
|
2007-05-14 14:58:52 +00:00
|
|
|
$this->children[$i]->__killParent(true);
|
|
|
|
}
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* XML handling.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* Operations on XML data.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
* @since CakePHP v .0.10.3.1400
|
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
class XML extends XMLNode {
|
|
|
|
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Resource handle to XML parser.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @var resource
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access private
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
var $__parser;
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* File handle to XML indata file.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @var resource
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access private
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
var $__file;
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Raw XML string data (for loading purposes)
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @var string
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access private
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
var $__rawData = null;
|
|
|
|
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* XML document header
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @var string
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access private
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
var $__header = null;
|
|
|
|
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* XML document version
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @var string
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access private
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
var $version = '1.0';
|
|
|
|
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* XML document encoding
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @var string
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access private
|
2006-08-20 22:20:44 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
var $encoding = 'UTF-8';
|
2006-08-20 22:20:44 +00:00
|
|
|
|
2006-10-08 14:30:28 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Constructor. Sets up the XML parser with options, gives it this object as
|
|
|
|
* its XML object, and sets some variables.
|
2006-10-08 14:30:28 +00:00
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @param string $input What should be used to set up
|
|
|
|
* @param array $options Options to set up with
|
2006-10-08 14:30:28 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function __construct($input = null, $options = array()) {
|
|
|
|
parent::__construct('root');
|
|
|
|
$this->__parser = xml_parser_create_ns();
|
2006-10-08 14:30:28 +00:00
|
|
|
|
2007-05-14 14:58:52 +00:00
|
|
|
xml_set_object($this->__parser, $this);
|
|
|
|
xml_parser_set_option($this->__parser, XML_OPTION_CASE_FOLDING, 0);
|
|
|
|
xml_parser_set_option($this->__parser, XML_OPTION_SKIP_WHITE, 1);
|
2006-10-08 14:30:28 +00:00
|
|
|
|
2007-05-14 14:58:52 +00:00
|
|
|
$this->children = array();
|
2006-10-08 14:30:28 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($input != null) {
|
2007-05-14 14:58:52 +00:00
|
|
|
$vars = null;
|
|
|
|
if (is_string($input)) {
|
|
|
|
$this->load($input);
|
|
|
|
} elseif (is_array($input)) {
|
|
|
|
$vars = $this->__objectToNode(Set::map($input));
|
|
|
|
} elseif (is_object($input)) {
|
|
|
|
$vars = $this->__objectToNode($input);
|
2006-10-08 14:30:28 +00:00
|
|
|
}
|
|
|
|
|
2007-05-14 14:58:52 +00:00
|
|
|
if ($vars != null) {
|
|
|
|
$this->children = $vars;
|
|
|
|
}
|
2006-10-08 14:30:28 +00:00
|
|
|
|
2007-05-14 14:58:52 +00:00
|
|
|
if (!is_array($this->children)) {
|
|
|
|
$this->children = array($this->children);
|
2006-10-08 14:30:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-25 09:08:36 +00:00
|
|
|
if (Configure::read('App.encoding') !== null) {
|
|
|
|
$this->encoding = Configure::read('App.encoding');
|
|
|
|
}
|
|
|
|
|
2007-05-14 14:58:52 +00:00
|
|
|
foreach ($options as $key => $val) {
|
|
|
|
switch ($key) {
|
|
|
|
case 'version':
|
|
|
|
$this->version = $val;
|
|
|
|
break;
|
|
|
|
case 'encoding':
|
|
|
|
$this->encoding = $val;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-10-08 14:30:28 +00:00
|
|
|
}
|
2007-05-14 14:58:52 +00:00
|
|
|
|
2006-08-20 22:20:44 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Initialize XML object from a given XML string. Returns false on error.
|
2006-08-20 22:20:44 +00:00
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @param string $in XML string to initialize with
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean Success
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function load($in) {
|
|
|
|
$this->__rawData = null;
|
|
|
|
$this->header = null;
|
|
|
|
|
|
|
|
if (is_string($in)) {
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (strstr($in, "<")) {
|
2007-05-14 14:58:52 +00:00
|
|
|
// Input is raw xml data
|
|
|
|
$this->__rawData = $in;
|
|
|
|
} else {
|
|
|
|
// Input is an xml file
|
2007-06-20 06:15:35 +00:00
|
|
|
if (strpos($in, '://') || file_exists($in)) {
|
2007-05-14 14:58:52 +00:00
|
|
|
$this->__rawData = @file_get_contents($in);
|
|
|
|
if ($this->__rawData == null) {
|
|
|
|
$this->error("XML file $in is empty or could not be read (possible permissions error).");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->error("XML file $in does not exist");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->parse();
|
|
|
|
|
|
|
|
} elseif (is_object($in)) {
|
2007-05-26 19:33:55 +00:00
|
|
|
|
2007-05-14 14:58:52 +00:00
|
|
|
}
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Parses and creates XML nodes from the __rawData property.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean Success
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-05-14 14:58:52 +00:00
|
|
|
* @see load()
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function parse() {
|
2007-12-08 06:08:03 +00:00
|
|
|
$this->header = trim(str_replace(a('<'.'?', '?'.'>'), a('', ''), substr(trim($this->__rawData), 0, strpos($this->__rawData, "\n"))));
|
2007-05-14 14:58:52 +00:00
|
|
|
|
|
|
|
xml_parse_into_struct($this->__parser, $this->__rawData, $vals);
|
|
|
|
$xml = new XMLNode();
|
|
|
|
|
|
|
|
$count = count($vals);
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$data = $vals[$i];
|
|
|
|
switch($data['type']) {
|
|
|
|
case "open" :
|
|
|
|
$tmpXML = new XMLNode();
|
|
|
|
$tmpXML->name = $data['tag'];
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (isset($data['value'])) {
|
2007-05-14 14:58:52 +00:00
|
|
|
$tmpXML->value = $data['value'];
|
|
|
|
}
|
2007-06-20 06:15:35 +00:00
|
|
|
if (isset($data['attributes'])) {
|
2007-05-14 14:58:52 +00:00
|
|
|
$tmpXML->attributes = $data['attributes'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$tmpXML->setParent($xml);
|
|
|
|
$ct = count($xml->children);
|
|
|
|
$xml->children[$ct] = $tmpXML;
|
|
|
|
$xml =& $xml->children[$ct];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "close" :
|
|
|
|
$xml =& $xml->parent();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "complete" :
|
|
|
|
$tmpXML = new XMLNode();
|
|
|
|
$tmpXML->name = $data['tag'];
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (isset($data['value'])) {
|
2007-05-14 14:58:52 +00:00
|
|
|
$tmpXML->value = $data['value'];
|
|
|
|
}
|
2007-06-20 06:15:35 +00:00
|
|
|
if (isset($data['attributes'])) {
|
2007-05-14 14:58:52 +00:00
|
|
|
$tmpXML->attributes = $data['attributes'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$tmpXML->__parent =& $xml;
|
|
|
|
$xml->children[] = $tmpXML;
|
|
|
|
break;
|
|
|
|
case 'cdata':
|
|
|
|
if (is_string($xml->value)) {
|
|
|
|
$xml->value = a($xml->value, $data['value']);
|
|
|
|
} else {
|
|
|
|
$xml->value[] = $data['value'];
|
|
|
|
}
|
|
|
|
break;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-14 14:58:52 +00:00
|
|
|
$this->children =& $xml->children;
|
|
|
|
return true;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-14 14:58:52 +00:00
|
|
|
* Returns a string representation of the XML object
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-10-22 16:54:36 +00:00
|
|
|
* @param boolean $useHeader Whether to include the XML header with the document (defaults to true)
|
2007-05-14 14:58:52 +00:00
|
|
|
* @return string XML data
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function compose($useHeader = true) {
|
|
|
|
if (!empty($this->__header)) {
|
|
|
|
$header = '<'.'?'.$this->__header.' ?'.'>'."\n";
|
2006-08-20 22:20:44 +00:00
|
|
|
} else {
|
2007-05-14 14:58:52 +00:00
|
|
|
$header = '<'.'?xml version="'.$this->version.'" encoding="'.$this->encoding.'" ?'.'>'."\n";
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2007-05-14 14:58:52 +00:00
|
|
|
if (!$this->hasChildren() && !$useHeader) {
|
2006-08-20 22:20:44 +00:00
|
|
|
return null;
|
2007-05-14 14:58:52 +00:00
|
|
|
} elseif (!$this->hasChildren()) {
|
|
|
|
return $header;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2007-02-06 17:19:11 +00:00
|
|
|
|
2007-05-14 14:58:52 +00:00
|
|
|
$data = '';
|
|
|
|
foreach ($this->children as $i => $node) {
|
|
|
|
$data .= $this->children[$i]->__toString();
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2007-05-14 14:58:52 +00:00
|
|
|
|
|
|
|
if ($useHeader) {
|
|
|
|
return $header.$data;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2007-05-14 14:58:52 +00:00
|
|
|
return $data;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-08-16 05:44:06 +00:00
|
|
|
* If debug mode is on, this method echoes an error message.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @param string $msg Error message
|
2007-10-22 16:11:12 +00:00
|
|
|
* @param integer $code Error code
|
|
|
|
* @param integer $line Line in file
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function error($msg, $code = 0, $line = 0) {
|
2007-08-16 05:44:06 +00:00
|
|
|
if (Configure::read('debug')) {
|
2007-05-14 14:58:52 +00:00
|
|
|
echo $msg . " " . $code . " " . $line;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-26 19:33:55 +00:00
|
|
|
* Returns a string with a textual description of the error code, or FALSE if no description was found.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-10-22 16:11:12 +00:00
|
|
|
* @param integer $code Error code
|
2007-05-14 14:58:52 +00:00
|
|
|
* @return string Error message
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function getError($code) {
|
|
|
|
$r = @xml_error_string($code);
|
|
|
|
return $r;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2007-05-14 14:58:52 +00:00
|
|
|
|
|
|
|
// Overridden functions from superclass
|
|
|
|
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-26 19:33:55 +00:00
|
|
|
* Get next element. NOT implemented.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @return object
|
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function &next() {
|
|
|
|
return null;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-26 19:33:55 +00:00
|
|
|
* Get previous element. NOT implemented.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @return object
|
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function &previous() {
|
|
|
|
return null;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
/**
|
2007-05-26 19:33:55 +00:00
|
|
|
* Get parent element. NOT implemented.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @return object
|
|
|
|
* @access public
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function &parent() {
|
|
|
|
return null;
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2007-05-14 14:58:52 +00:00
|
|
|
|
2007-05-26 19:33:55 +00:00
|
|
|
/**
|
|
|
|
* Return string representation of current object.
|
|
|
|
*
|
|
|
|
* @return string String representation
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function toString() {
|
|
|
|
return $this->compose();
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2007-05-14 14:58:52 +00:00
|
|
|
|
2007-05-26 19:33:55 +00:00
|
|
|
/**
|
|
|
|
* Destructor, used to free resources.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function __destruct() {
|
|
|
|
if (is_resource($this->__parser)) {
|
|
|
|
xml_parser_free($this->__parser);
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-14 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|