From 14523dc8c4e74608289479b7986012d83f2a888e Mon Sep 17 00:00:00 2001 From: nate Date: Mon, 11 Feb 2008 02:24:10 +0000 Subject: [PATCH] Fixing XML class references git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6450 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/set.php | 2 +- cake/libs/view/helpers/xml.php | 11 ++-- cake/libs/xml.php | 51 ++++++++++--------- .../cases/libs/view/helpers/xml.test.php | 25 +++++++-- 4 files changed, 53 insertions(+), 36 deletions(-) diff --git a/cake/libs/set.php b/cake/libs/set.php index 5a824f9d0..0d3d5d379 100644 --- a/cake/libs/set.php +++ b/cake/libs/set.php @@ -795,7 +795,7 @@ class Set extends Object { */ function reverse($object) { $out = array(); - if (is_a($object, 'XMLNode')) { + if (is_a($object, 'XmlNode')) { if (isset($object->name) && isset($object->children)) { if ($object->name === '#document' && !empty($object->children)) { $out = Set::reverse($object->children[0]); diff --git a/cake/libs/view/helpers/xml.php b/cake/libs/view/helpers/xml.php index d400404d5..fa1917448 100644 --- a/cake/libs/view/helpers/xml.php +++ b/cake/libs/view/helpers/xml.php @@ -24,7 +24,7 @@ * @lastmodified $Date$ * @license http://www.opensource.org/licenses/mit-license.php The MIT License */ -uses('set'); +App::import('Core', array('Xml', 'Set')); /** * XML Helper class for easy output of XML structures. @@ -88,7 +88,7 @@ class XmlHelper extends AppHelper { * @see XML::addNs() */ function addNs($name, $url = null) { - return XML::addNs($name, $url); + return Xml::addNamespace($name, $url); } /** * Removes a namespace added in addNs() @@ -98,7 +98,7 @@ class XmlHelper extends AppHelper { * @see XML::removeNs() */ function removeNs($name) { - XML::removeNs($name); + Xml::removeGlobalNamespace($name); } /** * Prepares the current set of namespaces for output in elem() / __composeAttributes() @@ -211,11 +211,8 @@ class XmlHelper extends AppHelper { * @return string A copy of $data in XML format */ function serialize($data, $options = array()) { - if (!class_exists('XML') && !class_exists('xml')) { - App::import('Core', 'Xml'); - } $data = new Xml($data, array_merge(array('attributes' => false, 'format' => 'attributes'), $options)); - return $data->toString(false); + return $data->toString(array_merge(array('header' => false), $options)); } } diff --git a/cake/libs/xml.php b/cake/libs/xml.php index 43508e1de..bd57fb179 100644 --- a/cake/libs/xml.php +++ b/cake/libs/xml.php @@ -514,7 +514,7 @@ class XmlNode extends Object { function &document() { $document =& $this; while (true) { - if (get_class($document) == 'XML' || $document == null) { + if (get_class($document) == 'Xml' || $document == null) { break; } $document =& $document->parent(); @@ -628,7 +628,7 @@ class XmlNode extends Object { * @subpackage cake.cake.libs * @since CakePHP v .0.10.3.1400 */ -class XML extends XmlNode { +class Xml extends XmlNode { /** * Resource handle to XML parser. @@ -755,7 +755,7 @@ class XML extends XmlNode { * * @return boolean Success * @access public - * @see XML::load() + * @see Xml::load() * @todo figure out how to link attributes and namespaces */ function parse() { @@ -821,7 +821,7 @@ class XML extends XmlNode { * @return string XML data * @access public * @deprecated - * @see XML::toString() + * @see Xml::toString() */ function compose($options = array()) { return $this->toString($options); @@ -891,8 +891,10 @@ class XML extends XmlNode { * @return void */ function addNamespace($prefix, $url) { - if ($ns = $this->__resolveNamespace($name, $url)) { - $this->namespaces = array_merge($this->namespaces, $ns); + $_this =& XmlManager::getInstance(); + + if ($ns = XML::__resolveNamespace($prefix, $url)) { + $_this->namespaces = array_merge($_this->namespaces, $ns); return true; } return false; @@ -908,7 +910,7 @@ class XML extends XmlNode { $options = array('header' => $options); } $defaults = array('header' => false, 'encoding' => $this->encoding); - $options = array_merge($defaults, XML::options(), $options); + $options = array_merge($defaults, Xml::options(), $options); $data = parent::toString($options, 0); if ($options['header']) { @@ -943,8 +945,8 @@ class XML extends XmlNode { * @static */ function addGlobalNs($name, $url = null) { - $_this =& XMLManager::getInstance(); - if ($ns = XML::__resolveNamespace($name, $url)) { + $_this =& XmlManager::getInstance(); + if ($ns = Xml::__resolveNamespace($name, $url)) { $_this->__namespaces = array_merge($_this->__namespaces, $ns); return true; } @@ -959,6 +961,7 @@ class XML extends XmlNode { * @access private */ function __resolveNamespace($name, $url) { + $_this =& XmlManager::getInstance(); if ($url == null && in_array($name, array_keys($_this->__defaultNamespaceMap))) { $url = $_this->__defaultNamespaceMap[$name]; } elseif ($url == null) { @@ -973,13 +976,13 @@ class XML extends XmlNode { return array($name => $url); } /** - * Alias to XML::addNs + * Alias to Xml::addNs * * @access public * @static */ function addGlobalNamespace($name, $url = null) { - return XML::addGlobalNs($name, $url); + return Xml::addGlobalNs($name, $url); } /** * Removes a namespace added in addNs() @@ -989,29 +992,29 @@ class XML extends XmlNode { * @static */ function removeGlobalNs($name) { - $_this =& XMLManager::getInstance(); + $_this =& XmlManager::getInstance(); - if (in_array($name, array_keys($_this->__namespaces))) { - unset($_this->__namespaces[$name]); - } elseif (in_array($name, $_this->__namespaces)) { - $keys = array_keys($_this->__namespaces); + if (in_array($name, array_keys($_this->namespaces))) { + unset($_this->namespaces[$name]); + } elseif (in_array($name, $_this->namespaces)) { + $keys = array_keys($_this->namespaces); $count = count($keys); for ($i = 0; $i < $count; $i++) { - if ($_this->__namespaces[$keys[$i]] == $name) { - unset($_this->__namespaces[$keys[$i]]); + if ($_this->namespaces[$keys[$i]] == $name) { + unset($_this->namespaces[$keys[$i]]); return; } } } } /** - * Alias to XML::removeNs + * Alias to Xml::removeNs * * @access public * @static */ function removeGlobalNamespace($name, $url = null) { - XML::removeGlobalNs($name, $url); + Xml::removeGlobalNs($name, $url); } /** * Sets/gets global XML options @@ -1022,7 +1025,7 @@ class XML extends XmlNode { * @static */ function options($options = array()) { - $_this =& XMLManager::getInstance(); + $_this =& XmlManager::getInstance(); $_this->options = array_merge($_this->options, $options); return $_this->options; } @@ -1129,7 +1132,7 @@ class XmlTextNode extends XmlNode { } $defaults = array('cdata' => true, 'whitespace' => false, 'convertEntities' => false); - $options = array_merge($defaults, XML::options(), $options); + $options = array_merge($defaults, Xml::options(), $options); $val = $this->value; if ($options['convertEntities'] && function_exists('mb_convert_encoding')) { @@ -1154,7 +1157,7 @@ class XmlTextNode extends XmlNode { * * @access private */ -class XMLManager { +class XmlManager { /** * Global XML namespaces. Used in all XML documents processed by this application @@ -1198,7 +1201,7 @@ class XMLManager { static $instance = array(); if (!isset($instance[0]) || !$instance[0]) { - $instance[0] =& new XMLManager(); + $instance[0] =& new XmlManager(); } return $instance[0]; } diff --git a/cake/tests/cases/libs/view/helpers/xml.test.php b/cake/tests/cases/libs/view/helpers/xml.test.php index 9772d3a32..ffbd81d01 100644 --- a/cake/tests/cases/libs/view/helpers/xml.test.php +++ b/cake/tests/cases/libs/view/helpers/xml.test.php @@ -40,14 +40,31 @@ uses('view'.DS.'helpers'.DS.'app_helper', 'controller'.DS.'controller', 'model'. */ class XmlHelperTest extends UnitTestCase { - function skip() { - $this->skipif (true, 'XmlHelper test not implemented'); - } - function setUp() { $this->Xml = new XmlHelper(); } + function testAddNamespace() { + $this->Xml->addNs('custom', 'http://example.com/dtd.xml'); + $manager =& XmlManager::getInstance(); + + $expected = array('custom' => 'http://example.com/dtd.xml'); + $this->assertEqual($manager->namespaces, $expected); + } + + function testRemoveNamespace() { + $this->Xml->addNs('custom', 'http://example.com/dtd.xml'); + $this->Xml->addNs('custom2', 'http://example.com/dtd2.xml'); + $manager =& XmlManager::getInstance(); + + $expected = array('custom' => 'http://example.com/dtd.xml', 'custom2' => 'http://example.com/dtd2.xml'); + $this->assertEqual($manager->namespaces, $expected); + + $this->Xml->removeNs('custom'); + $expected = array('custom2' => 'http://example.com/dtd2.xml'); + $this->assertEqual($manager->namespaces, $expected); + } + function tearDown() { unset($this->Xml); }