fixes #5209, appending node in Xml. Adds more test coverage form Xml class and adds removeNamespace for convenience.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7505 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2008-08-26 01:18:15 +00:00
parent 47d36456f8
commit 59efe5c445
2 changed files with 88 additions and 25 deletions

View file

@ -129,7 +129,19 @@ class XmlNode extends Object {
} }
return false; return false;
} }
/**
* Adds a namespace to the current node
*
* @param string $prefix The namespace prefix
* @param string $url The namespace DTD URL
* @return void
*/
function removeNamespace($prefix) {
if (Xml::removeGlobalNs($prefix)) {
return true;
}
return false;
}
/** /**
* Creates an XmlNode object that can be appended to this document or a node in it * Creates an XmlNode object that can be appended to this document or a node in it
* *
@ -184,9 +196,9 @@ class XmlNode extends Object {
if ($keyName !== null && !is_numeric($keyName)) { if ($keyName !== null && !is_numeric($keyName)) {
$name = $keyName; $name = $keyName;
} elseif (isset($object->_name_) && !empty($object->_name_)) { } elseif (!empty($object->_name_)) {
$name = $object->_name_; $name = $object->_name_;
} elseif (isset($object->name) && $object->name != null) { } elseif (isset($object->name)) {
$name = $object->name; $name = $object->name;
} elseif ($options['format'] == 'attributes') { } elseif ($options['format'] == 'attributes') {
$name = get_class($object); $name = get_class($object);
@ -375,15 +387,14 @@ class XmlNode extends Object {
return $return; return $return;
} }
if (is_array($child) || is_object($child)) { if (is_object($child)) {
if (is_object($child) && is_a($child, 'XmlNode') && $this->compare($child)) { if ($this->compare($child)) {
trigger_error('Cannot append a node to itself.'); trigger_error('Cannot append a node to itself.');
$return = false; $return = false;
return $return; return $return;
} }
if (is_array($child)) { } else if (is_array($child)) {
$child = Set::map($child); $child = Set::map($child);
}
if (is_array($child)) { if (is_array($child)) {
if (!is_a(current($child), 'XmlNode')) { if (!is_a(current($child), 'XmlNode')) {
foreach ($child as $i => $childNode) { foreach ($child as $i => $childNode) {
@ -396,21 +407,24 @@ class XmlNode extends Object {
} }
return $child; return $child;
} }
if (!is_a($child, 'XmlNode')) { } else {
$child = $this->normalize($child, null, $options); $attributes = array();
if (func_num_args() >= 2) {
$attributes = func_get_arg(1);
} }
$child =& $this->createNode($child, null, $attributes);
}
$child = $this->normalize($child, null, $options);
if (empty($child->namespace) && !empty($this->namespace)) { if (empty($child->namespace) && !empty($this->namespace)) {
$child->namespace = $this->namespace; $child->namespace = $this->namespace;
} }
} elseif (is_string($child)) {
$attr = array(); if (is_a($child, 'XmlNode')) {
if (func_num_args() >= 2 && is_array(func_get_arg(1))) { $child->setParent($this);
$attributes = func_get_arg(1);
}
$document = $this->document();
$child =& $document->createElement($child, null, $attributes);
} }
return $child; return $child;
} }
/** /**
@ -971,6 +985,21 @@ class Xml extends XmlNode {
} }
return parent::addNamespace($prefix, $url); return parent::addNamespace($prefix, $url);
} }
/**
* Removes a namespace to the current document
*
* @param string $prefix The namespace prefix
* @return void
*/
function removeNamespace($prefix) {
if ($count = count($this->children)) {
for ($i = 0; $i < $count; $i++) {
$this->children[$i]->removeNamespace($prefix);
}
return true;
}
return parent::removeNamespace($prefix);
}
/** /**
* Return string representation of current object. * Return string representation of current object.
* *
@ -1086,6 +1115,7 @@ class Xml extends XmlNode {
if (in_array($name, array_keys($_this->namespaces))) { if (in_array($name, array_keys($_this->namespaces))) {
unset($_this->namespaces[$name]); unset($_this->namespaces[$name]);
unset($this->namespaces[$name]); unset($this->namespaces[$name]);
return true;
} elseif (in_array($name, $_this->namespaces)) { } elseif (in_array($name, $_this->namespaces)) {
$keys = array_keys($_this->namespaces); $keys = array_keys($_this->namespaces);
$count = count($keys); $count = count($keys);
@ -1093,10 +1123,11 @@ class Xml extends XmlNode {
if ($_this->namespaces[$keys[$i]] == $name) { if ($_this->namespaces[$keys[$i]] == $name) {
unset($_this->namespaces[$keys[$i]]); unset($_this->namespaces[$keys[$i]]);
unset($this->namespaces[$keys[$i]]); unset($this->namespaces[$keys[$i]]);
return; return true;
} }
} }
} }
return false;
} }
/** /**
* Alias to Xml::removeNs * Alias to Xml::removeNs
@ -1104,8 +1135,8 @@ class Xml extends XmlNode {
* @access public * @access public
* @static * @static
*/ */
function removeGlobalNamespace($name, $url = null) { function removeGlobalNamespace($name) {
Xml::removeGlobalNs($name, $url); return Xml::removeGlobalNs($name);
} }
/** /**
* Sets/gets global XML options * Sets/gets global XML options

View file

@ -745,5 +745,37 @@ class XmlTest extends CakeTestCase {
)); ));
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }
function testAppend() {
$parentNode = new XmlNode('ourParentNode');
$parentNode->append( new XmlNode('ourChildNode'));
$first =& $parentNode->first();
$this->assertEqual($first->name, 'ourChildNode');
$string = 'ourChildNode';
$parentNode = new XmlNode('ourParentNode');
$parentNode->append($string);
$last =& $parentNode->last();
$this->assertEqual($last->name, 'ourChildNode');
}
function testNamespacing() {
$node = new Xml('<xml></xml>');
$node->addNamespace('cake', 'http://cakephp.org');
$this->assertEqual($node->toString(), '<xml xmlns:cake="http://cakephp.org" />');
$this->assertTrue($node->removeNamespace('cake'));
$this->assertEqual($node->toString(), '<xml />');
$node = new Xml('<xml xmlns:cake="http://cakephp.org" />');
$this->assertTrue($node->removeNamespace('cake'));
$this->assertEqual($node->toString(), '<xml />');
$node->addNamespace('cake', 'http://cakephp.org');
$this->assertEqual($node->toString(), '<xml xmlns:cake="http://cakephp.org" />');
}
} }
?> ?>