xml.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: xml.php 8089 2009-03-11 04:09:35Z nate $ */
00003 /**
00004  * XML Helper class file.
00005  *
00006  * Simplifies the output of XML documents.
00007  *
00008  * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
00009  * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00010  *
00011  * Licensed under The MIT License
00012  * Redistributions of files must retain the above copyright notice.
00013  *
00014  * @filesource
00015  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00016  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00017  * @package       cake
00018  * @subpackage    cake.cake.libs.view.helpers
00019  * @since         CakePHP(tm) v 1.2
00020  * @version       $Revision: 8089 $
00021  * @modifiedby    $LastChangedBy: nate $
00022  * @lastmodified  $Date: 2009-03-11 00:09:35 -0400 (Wed, 11 Mar 2009) $
00023  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00024  */
00025 App::import('Core', array('Xml', 'Set'));
00026 
00027 /**
00028  * XML Helper class for easy output of XML structures.
00029  *
00030  * XmlHelper encloses all methods needed while working with XML documents.
00031  *
00032  * @package       cake
00033  * @subpackage    cake.cake.libs.view.helpers
00034  */
00035 class XmlHelper extends AppHelper {
00036 /**
00037  * Default document encoding
00038  *
00039  * @access public
00040  * @var string
00041  */
00042     var $encoding = 'UTF-8';
00043 /**
00044  * Constructor
00045  * @return void
00046  */
00047     function __construct() {
00048         parent::__construct();
00049         $this->Xml =& new Xml();
00050         $this->Xml->options(array('verifyNs' => false));
00051     }
00052 /**
00053  * Returns an XML document header
00054  *
00055  * @param  array $attrib Header tag attributes
00056  * @return string XML header
00057  */
00058     function header($attrib = array()) {
00059         if (Configure::read('App.encoding') !== null) {
00060             $this->encoding = Configure::read('App.encoding');
00061         }
00062 
00063         if (is_array($attrib)) {
00064             $attrib = array_merge(array('encoding' => $this->encoding), $attrib);
00065         }
00066         if (is_string($attrib) && strpos($attrib, 'xml') !== 0) {
00067             $attrib = 'xml ' . $attrib;
00068         }
00069 
00070         return $this->output($this->Xml->header($attrib));
00071     }
00072 /**
00073  * Adds a namespace to any documents generated
00074  *
00075  * @param  string  $name The namespace name
00076  * @param  string  $url  The namespace URI; can be empty if in the default namespace map
00077  * @return boolean False if no URL is specified, and the namespace does not exist
00078  *                 default namespace map, otherwise true
00079  * @deprecated
00080  * @see Xml::addNs()
00081  */
00082     function addNs($name, $url = null) {
00083         return $this->Xml->addNamespace($name, $url);
00084     }
00085 /**
00086  * Removes a namespace added in addNs()
00087  *
00088  * @param  string  $name The namespace name or URI
00089  * @deprecated
00090  * @see Xml::removeNs()
00091  */
00092     function removeNs($name) {
00093         return $this->Xml->removeGlobalNamespace($name);
00094     }
00095 /**
00096  * Generates an XML element
00097  *
00098  * @param  string   $name The name of the XML element
00099  * @param  array    $attrib The attributes of the XML element
00100  * @param  mixed    $content XML element content
00101  * @param  boolean  $endTag Whether the end tag of the element should be printed
00102  * @return string XML
00103  */
00104     function elem($name, $attrib = array(), $content = null, $endTag = true) {
00105         $namespace = null;
00106         if (isset($attrib['namespace'])) {
00107             $namespace = $attrib['namespace'];
00108             unset($attrib['namespace']);
00109         }
00110         $cdata = false;
00111         if (is_array($content) && isset($content['cdata'])) {
00112             $cdata = true;
00113             unset($content['cdata']);
00114         }
00115         if (is_array($content) && isset($content['value'])) {
00116             $content = $content['value'];
00117         }
00118         $children = array();
00119         if (is_array($content)) {
00120             $children = $content;
00121             $content = null;
00122         }
00123 
00124         $elem =& $this->Xml->createElement($name, $content, $attrib, $namespace);
00125         foreach ($children as $child) {
00126             $elem->createElement($child);
00127         }
00128         $out = $elem->toString(array('cdata' => $cdata, 'leaveOpen' => !$endTag));
00129 
00130         if (!$endTag) {
00131             $this->Xml =& $elem;
00132         }
00133         return $this->output($out);
00134     }
00135 /**
00136  * Create closing tag for current element
00137  *
00138  * @return string
00139  */
00140     function closeElem() {
00141         $name = $this->Xml->name();
00142         if ($parent =& $this->Xml->parent()) {
00143             $this->Xml =& $parent;
00144         }
00145         return $this->output('</' . $name . '>');
00146     }
00147 /**
00148  * Serializes a model resultset into XML
00149  *
00150  * @param  mixed  $data The content to be converted to XML
00151  * @param  array  $options The data formatting options.  For a list of valid options, see
00152  *                         XmlNode::__construct().
00153  * @return string A copy of $data in XML format
00154  * @see XmlNode
00155  */
00156     function serialize($data, $options = array()) {
00157         $options += array('attributes' => false, 'format' => 'attributes');
00158         $data =& new Xml($data, $options);
00159         return $data->toString($options + array('header' => false));
00160     }
00161 }
00162 
00163 ?>

Generated on Sun Nov 22 00:30:54 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7