Merging r693 from trunk back to the 2.1.x branch
[isso.git] / xml.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * XML Parser
24 * xml.php
25 *
26 * @package ISSO
27 */
28
29 /**
30 * XML Parser
31 *
32 * This framework is a wrapper for a robust XML parser.
33 *
34 * @author Blue Static
35 * @copyright Copyright ©2002 - [#]year[#], Blue Static
36 * @version $Revision$
37 * @package ISSO
38 *
39 */
40 class XML
41 {
42 /**
43 * Framework registry object
44 * @var object
45 * @access private
46 */
47 var $registry = null;
48
49 /**
50 * Parser resource
51 * @var integer
52 * @access private
53 */
54 var $parser = null;
55
56 /**
57 * An array of function names that are to be executed for each tag name (name => function)
58 * @var array()
59 * @access private
60 */
61 var $taghandler = array();
62
63 /**
64 * Current CDATA value
65 * @var string
66 * @access private
67 */
68 var $cdata = '';
69
70 /**
71 * Tag stack of all open nodes
72 * @var array
73 * @access private
74 */
75 var $stack = array();
76
77 /**
78 * Node list for all open tag attributes
79 * @var array
80 * @access private
81 */
82 var $attribs = array();
83
84 /**
85 * Resulting parsed array
86 * @var array
87 * @access private
88 */
89 var $result = array();
90
91 // ###################################################################
92 /**
93 * Constructor
94 */
95 function __construct(&$registry)
96 {
97 $this->registry =& $registry;
98 }
99
100 // ###################################################################
101 /**
102 * (PHP 4) Constructor
103 */
104 function XML(&$registry)
105 {
106 $this->__construct($registry);
107 }
108
109 // ###################################################################
110 /**
111 * Parse an XML file
112 *
113 * @access public
114 *
115 * @param string XML file data
116 * @param string Parse file as UTF-8 instead of ISSO-8859-1
117 *
118 * @return array Array with all the XML data parsed
119 */
120 function parse($data, $utf8 = false)
121 {
122 $this->stack = array();
123 $this->attribs = array();
124 $this->result = array();
125 $this->cdata = '';
126
127 if ($utf8)
128 {
129 $data = utf8_encode($data);
130 $this->parser = xml_parser_create('UTF-8');
131 }
132 else
133 {
134 $this->parser = xml_parser_create('ISO-8859-1');
135 }
136
137 // create a new parser
138 xml_set_object($this->parser, $this);
139 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
140 xml_set_element_handler($this->parser, 'handle_tag_start', 'handle_tag_end');
141 xml_set_character_data_handler($this->parser, 'handle_cdata');
142
143 $this->attach_node($this->result);
144
145 // parse the data and check for errors
146 if (!xml_parse($this->parser, $data))
147 {
148 $error['code'] = xml_get_error_code($this->parser);
149 $error['string'] = xml_error_string($error['code']);
150 $error['line'] = xml_get_current_line_number($this->parser);
151 $error['column'] = xml_get_current_column_number($this->parser);
152 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]", E_USER_ERROR);
153 exit;
154 }
155
156 // destroy the parser
157 xml_parser_free($this->parser);
158
159 // done... send the results back
160 return $this->result;
161 }
162
163 // ###################################################################
164 /**
165 * Process the opening location of an XML tag
166 *
167 * @access private
168 *
169 * @param integer XML parser
170 * @param string Tag name
171 * @param array Tag attributes
172 */
173 function handle_tag_start(&$parser, $name, $attrs)
174 {
175 // we need to keep track of indicies to monitor the last key in $this->attribs
176 static $index;
177
178 // trim attributes
179 array_walk($attrs, 'trim');
180
181 // existing node set
182 if (isset($this->attribs["$name"]))
183 {
184 // node set has < 1 child nodes
185 if (!isset($this->attribs["$name"][0]))
186 {
187 $tmp = $this->attribs["$name"];
188 unset($this->attribs["$name"]);
189 $this->attribs["$name"][0] = $tmp;
190 }
191
192 // create a new child node
193 $this->attribs["$name"][ $index["$name"] ] = (array)$attrs;
194 $this->attach_node($this->attribs["$name"][ $index["$name"] ]);
195 $index["$name"]++;
196 }
197 // node set doesn't exist, so create it
198 else
199 {
200 $this->attribs["$name"] = (array)$attrs;
201 $this->attach_node($this->attribs["$name"]);
202 $index["$name"] = 1;
203 }
204 }
205
206 // ###################################################################
207 /**
208 * Process XML CDATA
209 *
210 * @access private
211 *
212 * @param integer XML parser
213 * @param string CDATA from tag
214 */
215 function handle_cdata(&$parser, $data)
216 {
217 $this->cdata .= $data;
218 }
219
220 // ###################################################################
221 /**
222 * Process the closing of an XML tag
223 *
224 * @access private
225 *
226 * @param integer XML parser
227 * @param string Tag name
228 */
229 function handle_tag_end(&$parser, $name)
230 {
231 // attach data to the node
232 if (($this->cdata = trim($this->cdata)) != '')
233 {
234 $this->attribs['value'] = $this->cdata;
235 }
236
237 $this->cdata = '';
238
239 // remove the node
240 $this->detach_node();
241 }
242
243 // ###################################################################
244 /**
245 * Shifts the node tree
246 *
247 * @access private
248 *
249 * @param array Node to place into the stack
250 */
251 function attach_node(&$node)
252 {
253 // create a new node
254 $this->stack[ sizeof($this->stack) ] =& $node;
255
256 // new attributes to work with
257 $this->attribs =& $node;
258 }
259
260 // ###################################################################
261 /**
262 * Unshifts the node tree
263 *
264 * @access private
265 */
266 function detach_node()
267 {
268 // drop the newest node
269 unset($this->stack[ sizeof($this->stack) - 1 ]);
270
271 // assign the attributes to the next newest node
272 $this->attribs =& $this->stack[ sizeof($this->stack) - 1 ];
273 }
274
275 // ###################################################################
276 /**
277 * Unless a node has multiple children, there will not be a numerical
278 * index for the child node. So this means that if you have variable
279 * XML structure with some of the same types of nodes having one children
280 * or multiple children, you'd have different things to parse. If
281 * you want the node unified (as in, all single-children would be indexed
282 * numerically, run this function on the node. It works on references.
283 *
284 * @access public
285 *
286 * @param array The node to int-index
287 */
288 function unify_node(&$node)
289 {
290 if (!isset($node[0]))
291 {
292 $node = array($node);
293 }
294 }
295 }
296
297 /*=====================================================================*\
298 || ###################################################################
299 || # $HeadURL$
300 || # $Id$
301 || ###################################################################
302 \*=====================================================================*/
303 ?>