]>
src.bluestatic.org Git - isso.git/blob - xml.php
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 public function __construct(&$registry)
98 $this->registry
=& $registry;
101 // ###################################################################
105 * @param string Field name
106 * @param mixed Value of the field
108 public function set($name, $value)
110 $this->registry
->do_set($name, $value, 'xml');
113 // ###################################################################
117 * @param string Field name
119 * @return mixed Value of the field
121 public function get($fieldname)
123 return $this->registry
->do_get($fieldname, 'xml');
126 // ###################################################################
130 * @param string XML file data
131 * @param string Parse file as UTF-8 instead of ISSO-8859-1
133 * @return array Array with all the XML data parsed
135 public function parse($data, $utf8 = false)
137 $this->registry
->check_isso_fields(get_class($this));
139 $this->stack
= array();
140 $this->attribs
= array();
141 $this->result
= array();
146 $data = utf8_encode($data);
147 $this->parser
= xml_parser_create('UTF-8');
151 $this->parser
= xml_parser_create('ISO-8859-1');
154 // create a new parser
155 xml_set_object($this->parser
, $this);
156 xml_parser_set_option($this->parser
, XML_OPTION_CASE_FOLDING
, 0);
157 xml_set_element_handler($this->parser
, 'handle_tag_start', 'handle_tag_end');
158 xml_set_character_data_handler($this->parser
, 'handle_cdata');
160 $this->attach_node($this->result
);
162 // parse the data and check for errors
163 if (!xml_parse($this->parser
, $data))
165 $error['code'] = xml_get_error_code($this->parser
);
166 $error['string'] = xml_error_string($error['code']);
167 $error['line'] = xml_get_current_line_number($this->parser
);
168 $error['column'] = xml_get_current_column_number($this->parser
);
169 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]", E_USER_ERROR
);
173 // destroy the parser
174 xml_parser_free($this->parser
);
176 // done... send the results back
177 return $this->result
;
180 // ###################################################################
182 * Process the opening location of an XML tag
184 * @param integer XML parser
185 * @param string Tag name
186 * @param array Tag attributes
188 private function handle_tag_start(&$parser, $name, $attrs)
190 // we need to keep track of indicies to monitor the last key in $this->attribs
194 array_walk($attrs, 'trim');
197 if (isset($this->attribs
["$name"]))
199 // node set has < 1 child nodes
200 if (!isset($this->attribs["$name"][0]))
202 $tmp = $this->attribs
["$name"];
203 unset($this->attribs["$name"]);
204 $this->attribs
["$name"][0] = $tmp;
207 // create a new child node
208 $this->attribs["$name"][ $index["$name"] ] = (array)$attrs;
209 $this->attach_node($this->attribs["$name"][ $index["$name"] ]);
212 // node set doesn't exist, so create it
215 $this->attribs
["$name"] = (array)$attrs;
216 $this->attach_node($this->attribs["$name"]);
221 // ###################################################################
225 * @param integer XML parser
226 * @param string CDATA from tag
228 private function handle_cdata(&$parser, $data)
230 $this->cdata .= $data;
233 // ###################################################################
235 * Process the closing of an XML tag
237 * @param integer XML parser
238 * @param string Tag name
240 private function handle_tag_end(&$parser, $name)
242 // attach data to the node
243 if (($this->cdata = trim($this->cdata)) != '')
245 // if we have a data handler, operate it now
246 if (isset($this->taghandler["$name"]))
248 $this->registry
->debug("handler: " . $this->taghandler
["$name"]);
249 if (function_exists($this->taghandler["$name"]))
251 $this->cdata
= $this->taghandler
["$name"]($this->cdata, $this);
255 trigger_error('Could not find the function [' . $this->taghandler["$name"] . '()] for the XML tag "' . $name . '"', E_USER_ERROR
);
259 $this->attribs
['value'] = $this->cdata
;
265 $this->detach_node();
268 // ###################################################################
270 * Shifts the node tree
272 * @param array Node to place into the stack
274 private function attach_node(&$node)
277 $this->stack
[ sizeof($this->stack
) ] =& $node;
279 // new attributes to work with
280 $this->attribs
=& $node;
283 // ###################################################################
285 * Unshifts the node tree
287 private function detach_node()
289 // drop the newest node
290 unset($this->stack
[ sizeof($this->stack
) - 1 ]);
292 // assign the attributes to the next newest node
293 $this->attribs
=& $this->stack
[ sizeof($this->stack
) - 1 ];
296 // ###################################################################
298 * Unless a node has multiple children, there will not be a numerical
299 * index for the child node. So this means that if you have variable
300 * XML structure with some of the same types of nodes having one children
301 * or multiple children, you'd have different things to parse. If
302 * you want the node unified (as in, all single-children would be indexed
303 * numerically, run this function on the node. It works on references.
305 * @param array The node to int-index
307 public function unify_node(&$node)
309 if (!isset($node[0]))
311 $node = array($node);
316 /*=====================================================================*\
317 || ###################################################################
320 || ###################################################################
321 \*=====================================================================*/