2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* XML Helper class file.
|
|
|
|
*
|
|
|
|
* Simplifies the output of XML documents.
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc.
|
|
|
|
* 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
|
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
|
|
|
* @since CakePHP(tm) v 1.2
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
|
|
|
App::import('Core', array('Xml', 'Set'));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* XML Helper class for easy output of XML structures.
|
|
|
|
*
|
|
|
|
* XmlHelper encloses all methods needed while working with XML documents.
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
|
|
|
*/
|
|
|
|
class XmlHelper extends AppHelper {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default document encoding
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $encoding = 'UTF-8';
|
2008-06-21 15:23:58 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
$this->Xml =& new Xml();
|
|
|
|
$this->Xml->options(array('verifyNs' => false));
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns an XML document header
|
|
|
|
*
|
|
|
|
* @param array $attrib Header tag attributes
|
|
|
|
* @return string XML header
|
|
|
|
*/
|
|
|
|
function header($attrib = array()) {
|
|
|
|
if (Configure::read('App.encoding') !== null) {
|
|
|
|
$this->encoding = Configure::read('App.encoding');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($attrib)) {
|
2008-06-03 22:35:44 +00:00
|
|
|
$attrib = array_merge(array('encoding' => $this->encoding), $attrib);
|
|
|
|
}
|
|
|
|
if (is_string($attrib) && strpos($attrib, 'xml') !== 0) {
|
|
|
|
$attrib = 'xml ' . $attrib;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-06-03 22:35:44 +00:00
|
|
|
return $this->output($this->Xml->header($attrib));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Adds a namespace to any documents generated
|
|
|
|
*
|
|
|
|
* @param string $name The namespace name
|
|
|
|
* @param string $url The namespace URI; can be empty if in the default namespace map
|
|
|
|
* @return boolean False if no URL is specified, and the namespace does not exist
|
|
|
|
* default namespace map, otherwise true
|
|
|
|
* @deprecated
|
|
|
|
* @see Xml::addNs()
|
|
|
|
*/
|
|
|
|
function addNs($name, $url = null) {
|
2008-06-03 22:35:44 +00:00
|
|
|
return $this->Xml->addNamespace($name, $url);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Removes a namespace added in addNs()
|
|
|
|
*
|
|
|
|
* @param string $name The namespace name or URI
|
|
|
|
* @deprecated
|
|
|
|
* @see Xml::removeNs()
|
|
|
|
*/
|
|
|
|
function removeNs($name) {
|
2008-06-03 22:35:44 +00:00
|
|
|
return $this->Xml->removeGlobalNamespace($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Generates an XML element
|
|
|
|
*
|
|
|
|
* @param string $name The name of the XML element
|
|
|
|
* @param array $attrib The attributes of the XML element
|
|
|
|
* @param mixed $content XML element content
|
|
|
|
* @param boolean $endTag Whether the end tag of the element should be printed
|
|
|
|
* @return string XML
|
|
|
|
*/
|
|
|
|
function elem($name, $attrib = array(), $content = null, $endTag = true) {
|
2008-06-03 22:35:44 +00:00
|
|
|
$namespace = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($attrib['namespace'])) {
|
2008-06-03 22:35:44 +00:00
|
|
|
$namespace = $attrib['namespace'];
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($attrib['namespace']);
|
|
|
|
}
|
2008-06-03 22:35:44 +00:00
|
|
|
$cdata = false;
|
|
|
|
if (is_array($content) && isset($content['cdata'])) {
|
|
|
|
$cdata = true;
|
|
|
|
unset($content['cdata']);
|
|
|
|
}
|
|
|
|
if (is_array($content) && isset($content['value'])) {
|
|
|
|
$content = $content['value'];
|
|
|
|
}
|
|
|
|
$children = array();
|
|
|
|
if (is_array($content)) {
|
|
|
|
$children = $content;
|
|
|
|
$content = null;
|
|
|
|
}
|
2008-06-21 15:23:58 +00:00
|
|
|
|
2008-06-03 22:35:44 +00:00
|
|
|
$elem =& $this->Xml->createElement($name, $content, $attrib, $namespace);
|
|
|
|
foreach ($children as $child) {
|
|
|
|
$elem->createElement($child);
|
|
|
|
}
|
|
|
|
$out = $elem->toString(array('cdata' => $cdata, 'leaveOpen' => !$endTag));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-06-03 22:35:44 +00:00
|
|
|
if (!$endTag) {
|
|
|
|
$this->Xml =& $elem;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $this->output($out);
|
|
|
|
}
|
2008-06-21 15:23:58 +00:00
|
|
|
/**
|
|
|
|
* Create closing tag for current element
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-06-03 22:35:44 +00:00
|
|
|
function closeElem() {
|
|
|
|
$name = $this->Xml->name();
|
|
|
|
if ($parent =& $this->Xml->parent()) {
|
|
|
|
$this->Xml =& $parent;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-06-03 22:35:44 +00:00
|
|
|
return $this->output('</' . $name . '>');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Serializes a model resultset into XML
|
|
|
|
*
|
|
|
|
* @param mixed $data The content to be converted to XML
|
|
|
|
* @param array $options The data formatting options
|
|
|
|
* @return string A copy of $data in XML format
|
|
|
|
*/
|
|
|
|
function serialize($data, $options = array()) {
|
2008-06-21 15:23:58 +00:00
|
|
|
$data =& new Xml($data, array_merge(array('attributes' => false, 'format' => 'attributes'), $options));
|
2008-05-30 11:40:08 +00:00
|
|
|
return $data->toString(array_merge(array('header' => false), $options));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|