]>
src.bluestatic.org Git - isso.git/blob - xml.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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.
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
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 \*=====================================================================*/
32 * This framework is a wrapper for a robust XML parser.
34 * @author Iris Studios, Inc.
35 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
43 * Framework registry object
57 * An array of function names that are to be executed for each tag name (name => function)
61 var $taghandler = array();
71 * Tag stack of all open nodes
78 * Node list for all open tag attributes
82 var $attribs = array();
85 * Resulting parsed array
89 var $result = array();
92 * Fields array that is used in this module
97 'taghandler' => array(REQ_NO
, null, false)
100 // ###################################################################
104 function __construct(&$registry)
106 $this->registry
=& $registry;
109 // ###################################################################
111 * (PHP 4) Constructor
113 function XML(&$registry)
115 $this->__construct($registry);
118 // ###################################################################
124 * @param string Field name
125 * @param mixed Value of the field
127 function set($name, $value)
129 $this->registry
->do_set($name, $value, 'xml');
132 // ###################################################################
138 * @param string Field name
140 * @return mixed Value of the field
142 function get($fieldname)
144 return $this->registry
->do_get($fieldname, 'xml');
147 // ###################################################################
153 * @param string XML file data
154 * @param string Parse file as UTF-8 instead of ISSO-8859-1
156 * @return array Array with all the XML data parsed
158 function parse($data, $utf8 = false)
160 $this->registry
->check_isso_fields(get_class($this));
162 $this->stack
= array();
163 $this->attribs
= array();
164 $this->result
= array();
169 $data = utf8_encode($data);
170 $this->parser
= xml_parser_create('UTF-8');
174 $this->parser
= xml_parser_create('ISO-8859-1');
177 // create a new parser
178 xml_set_object($this->parser
, $this);
179 xml_parser_set_option($this->parser
, XML_OPTION_CASE_FOLDING
, 0);
180 xml_set_element_handler($this->parser
, 'handle_tag_start', 'handle_tag_end');
181 xml_set_character_data_handler($this->parser
, 'handle_cdata');
183 $this->attach_node($this->result
);
185 // parse the data and check for errors
186 if (!xml_parse($this->parser
, $data))
188 $error['code'] = xml_get_error_code($this->parser
);
189 $error['string'] = xml_error_string($error['code']);
190 $error['line'] = xml_get_current_line_number($this->parser
);
191 $error['column'] = xml_get_current_column_number($this->parser
);
192 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]", E_USER_ERROR
);
196 // destroy the parser
197 xml_parser_free($this->parser
);
199 // done... send the results back
200 return $this->result
;
203 // ###################################################################
205 * Process the opening location of an XML tag
209 * @param integer XML parser
210 * @param string Tag name
211 * @param array Tag attributes
213 function handle_tag_start(&$parser, $name, $attrs)
215 // we need to keep track of indicies to monitor the last key in $this->attribs
219 array_walk($attrs, 'trim');
222 if (isset($this->attribs
["$name"]))
224 // node set has < 1 child nodes
225 if (!isset($this->attribs["$name"][0]))
227 $tmp = $this->attribs
["$name"];
228 unset($this->attribs["$name"]);
229 $this->attribs
["$name"][0] = $tmp;
232 // create a new child node
233 $this->attribs["$name"][ $index["$name"] ] = (array)$attrs;
234 $this->attach_node($this->attribs["$name"][ $index["$name"] ]);
237 // node set doesn't exist, so create it
240 $this->attribs
["$name"] = (array)$attrs;
241 $this->attach_node($this->attribs["$name"]);
246 // ###################################################################
252 * @param integer XML parser
253 * @param string CDATA from tag
255 function handle_cdata(&$parser, $data)
257 $this->cdata .= $data;
260 // ###################################################################
262 * Process the closing of an XML tag
266 * @param integer XML parser
267 * @param string Tag name
269 function handle_tag_end(&$parser, $name)
271 // attach data to the node
272 if (($this->cdata = trim($this->cdata)) != '')
274 // if we have a data handler, operate it now
275 if (isset($this->taghandler["$name"]))
277 $this->registry
->debug("handler: " . $this->taghandler
["$name"]);
278 if (function_exists($this->taghandler["$name"]))
280 $this->cdata
= $this->taghandler
["$name"]($this->cdata, $this);
284 trigger_error('Could not find the function [' . $this->taghandler["$name"] . '()] for the XML tag "' . $name . '"', E_USER_ERROR
);
288 $this->attribs
['value'] = $this->cdata
;
294 $this->detach_node();
297 // ###################################################################
299 * Shifts the node tree
303 * @param array Node to place into the stack
305 function attach_node(&$node)
308 $this->stack
[ sizeof($this->stack
) ] =& $node;
310 // new attributes to work with
311 $this->attribs
=& $node;
314 // ###################################################################
316 * Unshifts the node tree
320 function detach_node()
322 // drop the newest node
323 unset($this->stack
[ sizeof($this->stack
) - 1 ]);
325 // assign the attributes to the next newest node
326 $this->attribs
=& $this->stack
[ sizeof($this->stack
) - 1 ];
329 // ###################################################################
331 * Unless a node has multiple children, there will not be a numerical
332 * index for the child node. So this means that if you have variable
333 * XML structure with some of the same types of nodes having one children
334 * or multiple children, you'd have different things to parse. If
335 * you want the node unified (as in, all single-children would be indexed
336 * numerically, run this function on the node. It works on references.
340 * @param array The node to int-index
342 function unify_node(&$node)
344 if (!isset($node[0]))
346 $node = array($node);
351 /*=====================================================================*\
352 || ###################################################################
355 || ###################################################################
356 \*=====================================================================*/