2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
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.
35 * @copyright Copyright ©2002 - [#]year[#], Blue Static
43 * Framework registry object
46 private $registry = null
;
52 private $parser = null
;
55 * An array of function names that are to be executed for each tag name (name => function)
58 private $taghandler = array();
67 * Tag stack of all open nodes
70 private $stack = array();
73 * Node list for all open tag attributes
76 private $attribs = array();
79 * Resulting parsed array
82 private $result = array();
85 * Fields array that is used in this module
88 private $fields = array(
89 'taghandler' => array(REQ_NO
, null
, false
)
92 // ###################################################################
96 function __construct(&$registry)
98 $this->registry
=& $registry;
101 // ###################################################################
107 * @param string Field name
108 * @param mixed Value of the field
110 function set($name, $value)
112 $this->registry
->do_set($name, $value, 'xml');
115 // ###################################################################
121 * @param string Field name
123 * @return mixed Value of the field
125 function get($fieldname)
127 return $this->registry
->do_get($fieldname, 'xml');
130 // ###################################################################
136 * @param string XML file data
137 * @param string Parse file as UTF-8 instead of ISSO-8859-1
139 * @return array Array with all the XML data parsed
141 function parse($data, $utf8 = false
)
143 $this->registry
->check_isso_fields(get_class($this));
145 $this->stack
= array();
146 $this->attribs
= array();
147 $this->result
= array();
152 $data = utf8_encode($data);
153 $this->parser
= xml_parser_create('UTF-8');
157 $this->parser
= xml_parser_create('ISO-8859-1');
160 // create a new parser
161 xml_set_object($this->parser
, $this);
162 xml_parser_set_option($this->parser
, XML_OPTION_CASE_FOLDING
, 0);
163 xml_set_element_handler($this->parser
, 'handle_tag_start', 'handle_tag_end');
164 xml_set_character_data_handler($this->parser
, 'handle_cdata');
166 $this->attach_node($this->result
);
168 // parse the data and check for errors
169 if (!xml_parse($this->parser
, $data))
171 $error['code'] = xml_get_error_code($this->parser
);
172 $error['string'] = xml_error_string($error['code']);
173 $error['line'] = xml_get_current_line_number($this->parser
);
174 $error['column'] = xml_get_current_column_number($this->parser
);
175 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]", E_USER_ERROR
);
179 // destroy the parser
180 xml_parser_free($this->parser
);
182 // done... send the results back
183 return $this->result
;
186 // ###################################################################
188 * Process the opening location of an XML tag
192 * @param integer XML parser
193 * @param string Tag name
194 * @param array Tag attributes
196 function handle_tag_start(&$parser, $name, $attrs)
198 // we need to keep track of indicies to monitor the last key in $this->attribs
202 array_walk($attrs, 'trim');
205 if (isset($this->attribs
["$name"]))
207 // node set has < 1 child nodes
208 if (!isset($this->attribs
["$name"][0]))
210 $tmp = $this->attribs
["$name"];
211 unset($this->attribs
["$name"]);
212 $this->attribs
["$name"][0] = $tmp;
215 // create a new child node
216 $this->attribs
["$name"][ $index["$name"] ] = (array)$attrs;
217 $this->attach_node($this->attribs
["$name"][ $index["$name"] ]);
220 // node set doesn't exist, so create it
223 $this->attribs
["$name"] = (array)$attrs;
224 $this->attach_node($this->attribs
["$name"]);
229 // ###################################################################
235 * @param integer XML parser
236 * @param string CDATA from tag
238 function handle_cdata(&$parser, $data)
240 $this->cdata
.= $data;
243 // ###################################################################
245 * Process the closing of an XML tag
249 * @param integer XML parser
250 * @param string Tag name
252 function handle_tag_end(&$parser, $name)
254 // attach data to the node
255 if (($this->cdata
= trim($this->cdata
)) != '')
257 // if we have a data handler, operate it now
258 if (isset($this->taghandler
["$name"]))
260 $this->registry
->debug("handler: " . $this->taghandler
["$name"]);
261 if (function_exists($this->taghandler
["$name"]))
263 $this->cdata
= $this->taghandler
["$name"]($this->cdata
, $this);
267 trigger_error('Could not find the function [' . $this->taghandler
["$name"] . '()] for the XML tag "' . $name . '"', E_USER_ERROR
);
271 $this->attribs
['value'] = $this->cdata
;
277 $this->detach_node();
280 // ###################################################################
282 * Shifts the node tree
286 * @param array Node to place into the stack
288 function attach_node(&$node)
291 $this->stack
[ sizeof($this->stack
) ] =& $node;
293 // new attributes to work with
294 $this->attribs
=& $node;
297 // ###################################################################
299 * Unshifts the node tree
303 function detach_node()
305 // drop the newest node
306 unset($this->stack
[ sizeof($this->stack
) - 1 ]);
308 // assign the attributes to the next newest node
309 $this->attribs
=& $this->stack
[ sizeof($this->stack
) - 1 ];
312 // ###################################################################
314 * Unless a node has multiple children, there will not be a numerical
315 * index for the child node. So this means that if you have variable
316 * XML structure with some of the same types of nodes having one children
317 * or multiple children, you'd have different things to parse. If
318 * you want the node unified (as in, all single-children would be indexed
319 * numerically, run this function on the node. It works on references.
323 * @param array The node to int-index
325 function unify_node(&$node)
327 if (!isset($node[0]))
329 $node = array($node);
334 /*=====================================================================*\
335 || ###################################################################
338 || ###################################################################
339 \*=====================================================================*/