| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: xml.php 7945 2008-12-19 02:16:01Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* XML Helper class file.
|
|
|
5 |
*
|
|
|
6 |
* Simplifies the output of XML documents.
|
|
|
7 |
*
|
|
|
8 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
9 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
10 |
*
|
|
|
11 |
* Licensed under The MIT License
|
|
|
12 |
* Redistributions of files must retain the above copyright notice.
|
|
|
13 |
*
|
|
|
14 |
* @filesource
|
|
|
15 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
16 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
17 |
* @package cake
|
|
|
18 |
* @subpackage cake.cake.libs.view.helpers
|
|
|
19 |
* @since CakePHP(tm) v 1.2
|
|
|
20 |
* @version $Revision: 7945 $
|
|
|
21 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
22 |
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
|
|
|
23 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
24 |
*/
|
|
|
25 |
App::import('Core', array('Xml', 'Set'));
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* XML Helper class for easy output of XML structures.
|
|
|
29 |
*
|
|
|
30 |
* XmlHelper encloses all methods needed while working with XML documents.
|
|
|
31 |
*
|
|
|
32 |
* @package cake
|
|
|
33 |
* @subpackage cake.cake.libs.view.helpers
|
|
|
34 |
*/
|
|
|
35 |
class XmlHelper extends AppHelper {
|
|
|
36 |
/**
|
|
|
37 |
* Default document encoding
|
|
|
38 |
*
|
|
|
39 |
* @access public
|
|
|
40 |
* @var string
|
|
|
41 |
*/
|
|
|
42 |
var $encoding = 'UTF-8';
|
|
|
43 |
/**
|
|
|
44 |
* Constructor
|
|
|
45 |
* @return void
|
|
|
46 |
*/
|
|
|
47 |
function __construct() {
|
|
|
48 |
parent::__construct();
|
|
|
49 |
$this->Xml =& new Xml();
|
|
|
50 |
$this->Xml->options(array('verifyNs' => false));
|
|
|
51 |
}
|
|
|
52 |
/**
|
|
|
53 |
* Returns an XML document header
|
|
|
54 |
*
|
|
|
55 |
* @param array $attrib Header tag attributes
|
|
|
56 |
* @return string XML header
|
|
|
57 |
*/
|
|
|
58 |
function header($attrib = array()) {
|
|
|
59 |
if (Configure::read('App.encoding') !== null) {
|
|
|
60 |
$this->encoding = Configure::read('App.encoding');
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
if (is_array($attrib)) {
|
|
|
64 |
$attrib = array_merge(array('encoding' => $this->encoding), $attrib);
|
|
|
65 |
}
|
|
|
66 |
if (is_string($attrib) && strpos($attrib, 'xml') !== 0) {
|
|
|
67 |
$attrib = 'xml ' . $attrib;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
return $this->output($this->Xml->header($attrib));
|
|
|
71 |
}
|
|
|
72 |
/**
|
|
|
73 |
* Adds a namespace to any documents generated
|
|
|
74 |
*
|
|
|
75 |
* @param string $name The namespace name
|
|
|
76 |
* @param string $url The namespace URI; can be empty if in the default namespace map
|
|
|
77 |
* @return boolean False if no URL is specified, and the namespace does not exist
|
|
|
78 |
* default namespace map, otherwise true
|
|
|
79 |
* @deprecated
|
|
|
80 |
* @see Xml::addNs()
|
|
|
81 |
*/
|
|
|
82 |
function addNs($name, $url = null) {
|
|
|
83 |
return $this->Xml->addNamespace($name, $url);
|
|
|
84 |
}
|
|
|
85 |
/**
|
|
|
86 |
* Removes a namespace added in addNs()
|
|
|
87 |
*
|
|
|
88 |
* @param string $name The namespace name or URI
|
|
|
89 |
* @deprecated
|
|
|
90 |
* @see Xml::removeNs()
|
|
|
91 |
*/
|
|
|
92 |
function removeNs($name) {
|
|
|
93 |
return $this->Xml->removeGlobalNamespace($name);
|
|
|
94 |
}
|
|
|
95 |
/**
|
|
|
96 |
* Generates an XML element
|
|
|
97 |
*
|
|
|
98 |
* @param string $name The name of the XML element
|
|
|
99 |
* @param array $attrib The attributes of the XML element
|
|
|
100 |
* @param mixed $content XML element content
|
|
|
101 |
* @param boolean $endTag Whether the end tag of the element should be printed
|
|
|
102 |
* @return string XML
|
|
|
103 |
*/
|
|
|
104 |
function elem($name, $attrib = array(), $content = null, $endTag = true) {
|
|
|
105 |
$namespace = null;
|
|
|
106 |
if (isset($attrib['namespace'])) {
|
|
|
107 |
$namespace = $attrib['namespace'];
|
|
|
108 |
unset($attrib['namespace']);
|
|
|
109 |
}
|
|
|
110 |
$cdata = false;
|
|
|
111 |
if (is_array($content) && isset($content['cdata'])) {
|
|
|
112 |
$cdata = true;
|
|
|
113 |
unset($content['cdata']);
|
|
|
114 |
}
|
|
|
115 |
if (is_array($content) && isset($content['value'])) {
|
|
|
116 |
$content = $content['value'];
|
|
|
117 |
}
|
|
|
118 |
$children = array();
|
|
|
119 |
if (is_array($content)) {
|
|
|
120 |
$children = $content;
|
|
|
121 |
$content = null;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
$elem =& $this->Xml->createElement($name, $content, $attrib, $namespace);
|
|
|
125 |
foreach ($children as $child) {
|
|
|
126 |
$elem->createElement($child);
|
|
|
127 |
}
|
|
|
128 |
$out = $elem->toString(array('cdata' => $cdata, 'leaveOpen' => !$endTag));
|
|
|
129 |
|
|
|
130 |
if (!$endTag) {
|
|
|
131 |
$this->Xml =& $elem;
|
|
|
132 |
}
|
|
|
133 |
return $this->output($out);
|
|
|
134 |
}
|
|
|
135 |
/**
|
|
|
136 |
* Create closing tag for current element
|
|
|
137 |
*
|
|
|
138 |
* @return string
|
|
|
139 |
*/
|
|
|
140 |
function closeElem() {
|
|
|
141 |
$name = $this->Xml->name();
|
|
|
142 |
if ($parent =& $this->Xml->parent()) {
|
|
|
143 |
$this->Xml =& $parent;
|
|
|
144 |
}
|
|
|
145 |
return $this->output('</' . $name . '>');
|
|
|
146 |
}
|
|
|
147 |
/**
|
|
|
148 |
* Serializes a model resultset into XML
|
|
|
149 |
*
|
|
|
150 |
* @param mixed $data The content to be converted to XML
|
|
|
151 |
* @param array $options The data formatting options
|
|
|
152 |
* @return string A copy of $data in XML format
|
|
|
153 |
*/
|
|
|
154 |
function serialize($data, $options = array()) {
|
|
|
155 |
$data =& new Xml($data, array_merge(array('attributes' => false, 'format' => 'attributes'), $options));
|
|
|
156 |
return $data->toString(array_merge(array('header' => false), $options));
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
?>
|