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/>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
2006-06-14 23:04:23 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2007-02-02 10:39:45 +00:00
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* @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
|
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
|
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
|
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
|
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
|
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
|
|
|
|
* @param array $attributes Node attributes
|
|
|
|
* @param mixed $value Node contents (text)
|
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-14 14:58:52 +00:00
|
|
|
* Gets the XML element properties from an object
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @param object $object
|
|
|
|
* @return array
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function __objectToNode($object) {
|
2006-08-20 22:20:44 +00:00
|
|
|
|
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-05-14 14:58:52 +00:00
|
|
|
if (isset($object->__identity__) && !empty($object->__identity__)) {
|
|
|
|
$name = $object->__identity__;
|
|
|
|
} elseif (isset($object->name) && $object->name != null) {
|
|
|
|
$name = $object->name;
|
|
|
|
} else {
|
|
|
|
$name = get_class($object);
|
|
|
|
}
|
|
|
|
if ($name != low($name)) {
|
|
|
|
$name = Inflector::underscore($name);
|
|
|
|
}
|
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)) {
|
|
|
|
$children[] = $this->__objectToNode($val);
|
|
|
|
unset($attributes[$key]);
|
2006-10-08 14:30:28 +00:00
|
|
|
}
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
2007-05-14 14:58:52 +00:00
|
|
|
unset($attributes['__identity__']);
|
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;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Sets the parent node of this XMLNode
|
|
|
|
*
|
|
|
|
* @return XMLNode
|
|
|
|
*/
|
|
|
|
function setParent(&$parent) {
|
|
|
|
$this->__parent =& $parent;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns a copy of self.
|
|
|
|
*
|
|
|
|
* @return XMLNode
|
|
|
|
*/
|
|
|
|
function cloneNode() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Append given node as a child.
|
|
|
|
*
|
|
|
|
* @param XMLNode $child
|
|
|
|
*/
|
|
|
|
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-14 14:58:52 +00:00
|
|
|
$tmp = new XMLNode();
|
|
|
|
$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-14 14:58:52 +00:00
|
|
|
* @return XMLNode
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function &first() {
|
|
|
|
if(isset($this->children[0])) {
|
|
|
|
return $this->children[0];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns last child node, or null if empty.
|
|
|
|
*
|
|
|
|
* @return XMLNode
|
|
|
|
*/
|
|
|
|
function &last() {
|
|
|
|
if(count($this->children) > 0) {
|
|
|
|
return $this->children[count($this->children) - 1];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns child node with given ID.
|
|
|
|
*
|
|
|
|
* @param string $id Name of childnode
|
|
|
|
* @return XMLNode
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function &child($id) {
|
|
|
|
$null = null;
|
2006-10-08 14:30:28 +00:00
|
|
|
|
2007-05-14 14:58:52 +00:00
|
|
|
if(is_int($id)) {
|
|
|
|
if(isset($this->children[$id])) {
|
|
|
|
return $this->children[$id];
|
2006-10-08 14:30:28 +00:00
|
|
|
} else {
|
2007-05-14 14:58:52 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} elseif(is_string($id)) {
|
|
|
|
for($i = 0; $i < count($this->children); $i++) {
|
|
|
|
if($this->children[$i]->name == $id) {
|
|
|
|
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
|
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);
|
|
|
|
for($i = 0; $i < $count; $i++) {
|
|
|
|
if($this->children[$i]->name == $name) {
|
|
|
|
$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-14 14:58:52 +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-14 14:58:52 +00:00
|
|
|
* @return XMLNode A reference to the XMLNode object
|
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-14 14:58:52 +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-14 14:58:52 +00:00
|
|
|
* @return XMLNode A reference to the XMLNode object
|
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-14 14:58:52 +00:00
|
|
|
* @return XMLNode
|
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-05-14 14:58:52 +00:00
|
|
|
* @return boolean
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function hasChildren() {
|
|
|
|
if(is_array($this->children) && count($this->children) > 0) {
|
|
|
|
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.
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function toString() {
|
|
|
|
$d = '';
|
|
|
|
if($this->name != '') {
|
|
|
|
$d .= '<' . $this->name;
|
|
|
|
if(is_array($this->attributes) && count($this->attributes) > 0) {
|
|
|
|
foreach($this->attributes as $key => $val) {
|
|
|
|
$d .= " $key=\"$val\"";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$this->hasChildren() && empty($this->value)) {
|
|
|
|
if($this->name != '') {
|
|
|
|
$d .= " />\n";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if($this->name != '') {
|
|
|
|
$d .= ">";
|
|
|
|
}
|
|
|
|
if($this->hasChildren()) {
|
|
|
|
if (is_string($this->value) || empty($this->value)) {
|
|
|
|
if (!empty($this->value)) {
|
|
|
|
$d .= $this->value;
|
|
|
|
}
|
|
|
|
$count = count($this->children);
|
|
|
|
|
|
|
|
for($i = 0; $i < $count; $i++) {
|
|
|
|
$d .= $this->children[$i]->toString();
|
|
|
|
}
|
|
|
|
} elseif (is_array($this->value)) {
|
|
|
|
$count = count($this->value);
|
|
|
|
for($i = 0; $i < $count; $i++) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->name != '' && ($this->hasChildren() || !empty($this->value))) {
|
|
|
|
$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.
|
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.
|
|
|
|
*
|
|
|
|
* @param boolean $recursive
|
|
|
|
*/
|
|
|
|
function __killParent($recursive = true) {
|
|
|
|
unset($this->__parent);
|
|
|
|
if($recursive && $this->hasChildren()) {
|
|
|
|
for($i = 0; $i < count($this->children); $i++) {
|
|
|
|
$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
|
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
|
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
|
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
|
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
|
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
|
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-14 14:58:52 +00:00
|
|
|
* @param string $input
|
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-05-14 14:58:52 +00:00
|
|
|
if($input != null) {
|
|
|
|
$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-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-14 14:58:52 +00:00
|
|
|
* @param string $in
|
|
|
|
* @return boolean Success
|
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)) {
|
|
|
|
|
|
|
|
if(strstr($in, "<")) {
|
|
|
|
// Input is raw xml data
|
|
|
|
$this->__rawData = $in;
|
|
|
|
} else {
|
|
|
|
// Input is an xml file
|
|
|
|
if(strpos($in, '://') || file_exists($in)) {
|
|
|
|
$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)) {
|
|
|
|
|
|
|
|
}
|
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-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() {
|
|
|
|
$this->header = trim(r(a('<'.'?', '?'.'>'), a('', ''), substr(trim($this->__rawData), 0, strpos($this->__rawData, "\n"))));
|
|
|
|
|
|
|
|
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'];
|
|
|
|
|
|
|
|
if(isset($data['value'])) {
|
|
|
|
$tmpXML->value = $data['value'];
|
|
|
|
}
|
|
|
|
if(isset($data['attributes'])) {
|
|
|
|
$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'];
|
|
|
|
|
|
|
|
if(isset($data['value'])) {
|
|
|
|
$tmpXML->value = $data['value'];
|
|
|
|
}
|
|
|
|
if(isset($data['attributes'])) {
|
|
|
|
$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-05-14 14:58:52 +00:00
|
|
|
* @param boolean $useHeader Whether to include the XML header with the document (defaults to true)
|
|
|
|
* @return string XML data
|
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-05-14 14:58:52 +00:00
|
|
|
* If DEBUG 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
|
|
|
|
* @param integer $code Error code
|
|
|
|
* @param integer $line Line in file
|
2006-06-14 23:04:23 +00:00
|
|
|
*/
|
2007-05-14 14:58:52 +00:00
|
|
|
function error($msg, $code = 0, $line = 0) {
|
|
|
|
if(DEBUG) {
|
|
|
|
echo $msg . " " . $code . " " . $line;
|
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 with a textual description of the error code, or FALSE if no description was found.
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @param integer $code
|
|
|
|
* @return string Error message
|
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-14 14:58:52 +00:00
|
|
|
* Enter description here...
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @return unknown
|
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-14 14:58:52 +00:00
|
|
|
* Enter description here...
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @return null
|
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-14 14:58:52 +00:00
|
|
|
* Enter description here...
|
2006-06-14 23:04:23 +00:00
|
|
|
*
|
2007-05-14 14:58:52 +00:00
|
|
|
* @return null
|
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
|
|
|
|
|
|
|
function toString() {
|
|
|
|
return $this->compose();
|
2006-08-20 22:20:44 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
?>
|