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