A fix for PHP 5.1.2 and bug://report/1 where the XML module would fail because we...
[isso.git] / xml.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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 Iris Studios, Inc.
35 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
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 * Fields array that is used in this module
93 * @var array
94 * @access private
95 */
96 var $fields = array(
97 'taghandler' => array(REQ_NO, null, false)
98 );
99
100 // ###################################################################
101 /**
102 * Constructor
103 */
104 function __construct(&$registry)
105 {
106 $this->registry =& $registry;
107 }
108
109 // ###################################################################
110 /**
111 * (PHP 4) Constructor
112 */
113 function XML(&$registry)
114 {
115 $this->__construct($registry);
116 }
117
118 // ###################################################################
119 /**
120 * Sets an ISSO field
121 *
122 * @access public
123 *
124 * @param string Field name
125 * @param mixed Value of the field
126 */
127 function set($name, $value)
128 {
129 $this->registry->do_set($name, $value, 'xml');
130 }
131
132 // ###################################################################
133 /**
134 * Gets an ISSO field
135 *
136 * @access public
137 *
138 * @param string Field name
139 *
140 * @return mixed Value of the field
141 */
142 function get($fieldname)
143 {
144 return $this->registry->do_get($fieldname, 'xml');
145 }
146
147 // ###################################################################
148 /**
149 * Parse an XML file
150 *
151 * @access public
152 *
153 * @param string XML file data
154 *
155 * @return array Array with all the XML data parsed
156 */
157 function parse($data)
158 {
159 $this->registry->check_isso_fields(get_class($this));
160
161 $this->stack = array();
162 $this->attribs = array();
163 $this->result = array();
164 $this->cdata = '';
165
166 // create a new parser
167 $this->parser = xml_parser_create();
168 xml_set_object($this->parser, $this);
169 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
170 xml_set_element_handler($this->parser, 'handle_tag_start', 'handle_tag_end');
171 xml_set_character_data_handler($this->parser, 'handle_cdata');
172
173 $this->attach_node($this->result);
174
175 // parse the data and check for errors
176 if (!xml_parse($this->parser, $data))
177 {
178 $error['code'] = xml_get_error_code($this->parser);
179 $error['string'] = xml_error_string($error['code']);
180 $error['line'] = xml_get_current_line_number($this->parser);
181 $error['column'] = xml_get_current_column_number($this->parser);
182 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]", E_USER_ERROR);
183 exit;
184 }
185
186 // destroy the parser
187 xml_parser_free($this->parser);
188
189 // done... send the results back
190 return $this->result;
191 }
192
193 // ###################################################################
194 /**
195 * Process the opening location of an XML tag
196 *
197 * @access private
198 *
199 * @param integer XML parser
200 * @param string Tag name
201 * @param array Tag attributes
202 */
203 function handle_tag_start(&$parser, $name, $attrs)
204 {
205 // we need to keep track of indicies to monitor the last key in $this->attribs
206 static $index;
207
208 // trim attributes
209 array_walk($attrs, 'trim');
210
211 // existing node set
212 if (isset($this->attribs["$name"]))
213 {
214 // node set has < 1 child nodes
215 if (!isset($this->attribs["$name"][0]))
216 {
217 $tmp = $this->attribs["$name"];
218 unset($this->attribs["$name"]);
219 $this->attribs["$name"][0] = $tmp;
220 }
221
222 // create a new child node
223 $this->attribs["$name"][ $index["$name"] ] = (array)$attrs;
224 $this->attach_node($this->attribs["$name"][ $index["$name"] ]);
225 $index["$name"]++;
226 }
227 // node set doesn't exist, so create it
228 else
229 {
230 $this->attribs["$name"] = (array)$attrs;
231 $this->attach_node($this->attribs["$name"]);
232 $index["$name"] = 1;
233 }
234 }
235
236 // ###################################################################
237 /**
238 * Process XML CDATA
239 *
240 * @access private
241 *
242 * @param integer XML parser
243 * @param string CDATA from tag
244 */
245 function handle_cdata(&$parser, $data)
246 {
247 $this->cdata .= $data;
248 }
249
250 // ###################################################################
251 /**
252 * Process the closing of an XML tag
253 *
254 * @access private
255 *
256 * @param integer XML parser
257 * @param string Tag name
258 */
259 function handle_tag_end(&$parser, $name)
260 {
261 // attach data to the node
262 if (($this->cdata = trim($this->cdata)) != '')
263 {
264 // if we have a data handler, operate it now
265 if (isset($this->taghandler["$name"]))
266 {
267 $this->registry->debug("handler: " . $this->taghandler["$name"]);
268 if (function_exists($this->taghandler["$name"]))
269 {
270 $this->cdata = $this->taghandler["$name"]($this->cdata, $this);
271 }
272 else
273 {
274 trigger_error('Could not find the function [' . $this->taghandler["$name"] . '()] for the XML tag "' . $name . '"', E_USER_ERROR);
275 }
276 }
277
278 $this->attribs['value'] = $this->cdata;
279 }
280
281 $this->cdata = '';
282
283 // remove the node
284 $this->detach_node();
285 }
286
287 // ###################################################################
288 /**
289 * Shifts the node tree
290 *
291 * @access private
292 *
293 * @param array Node to place into the stack
294 */
295 function attach_node(&$node)
296 {
297 // create a new node
298 $this->stack[ sizeof($this->stack) ] =& $node;
299
300 // new attributes to work with
301 $this->attribs =& $node;
302 }
303
304 // ###################################################################
305 /**
306 * Unshifts the node tree
307 *
308 * @access private
309 */
310 function detach_node()
311 {
312 // drop the newest node
313 unset($this->stack[ sizeof($this->stack) - 1 ]);
314
315 // assign the attributes to the next newest node
316 $this->attribs =& $this->stack[ sizeof($this->stack) - 1 ];
317 }
318
319 // ###################################################################
320 /**
321 * Unless a node has multiple children, there will not be a numerical
322 * index for the child node. So this means that if you have variable
323 * XML structure with some of the same types of nodes having one children
324 * or multiple children, you'd have different things to parse. If
325 * you want the node unified (as in, all single-children would be indexed
326 * numerically, run this function on the node. It works on references.
327 *
328 * @access public
329 *
330 * @param array The node to int-index
331 */
332 function unify_node(&$node)
333 {
334 if (!isset($node[0]))
335 {
336 $node = array($node);
337 }
338 }
339 }
340
341 /*=====================================================================*\
342 || ###################################################################
343 || # $HeadURL$
344 || # $Id$
345 || ###################################################################
346 \*=====================================================================*/
347 ?>