]>
src.bluestatic.org Git - isso.git/blob - xml.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
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 XML file data
140 * @return array Array with all the XML data parsed
142 function parse($data)
144 $this->stack
= array();
145 $this->attribs
= array();
146 $this->result
= array();
149 // create a new parser
150 $this->parser
= xml_parser_create();
151 xml_set_object($this->parser
, $this);
152 xml_parser_set_option($this->parser
, XML_OPTION_CASE_FOLDING
, 0);
153 xml_set_element_handler($this->parser
, 'handle_tag_start', 'handle_tag_end');
154 xml_set_character_data_handler($this->parser
, 'handle_cdata');
156 $this->attach_node($this->result
);
158 // parse the data and check for errors
159 if (!xml_parse($this->parser
, $data))
161 $error['code'] = xml_get_error_code($this->parser
);
162 $error['string'] = xml_error_string($error['code']);
163 $error['line'] = xml_get_current_line_number($this->parser
);
164 $error['column'] = xml_get_current_column_number($this->parser
);
165 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]", E_USER_ERROR
);
169 // destroy the parser
170 xml_parser_free($this->parser
);
172 // done... send the results back
173 return $this->result
;
176 // ###################################################################
178 * Process the opening location of an XML tag
182 * @param integer XML parser
183 * @param string Tag name
184 * @param array Tag attributes
186 function handle_tag_start(&$parser, $name, $attrs)
189 array_walk($attrs, 'trim');
192 if (isset($this->attribs
["$name"]))
194 // node set has < 1 child nodes
195 if (!isset($this->attribs["$name"][0]))
197 $tmp = $this->attribs
["$name"];
198 unset($this->attribs["$name"]);
199 $this->attribs
["$name"][0] = $tmp;
202 // create a new child node
203 $this->attach_node($this->attribs["$name"][] = (array)$attrs);
205 // node set doesn't exist, so create it
208 $this->attach_node($this->attribs
["$name"] = (array)$attrs);
212 // ###################################################################
218 * @param integer XML parser
219 * @param string CDATA from tag
221 function handle_cdata(&$parser, $data)
223 $this->cdata .= $data;
226 // ###################################################################
228 * Process the closing of an XML tag
232 * @param integer XML parser
233 * @param string Tag name
235 function handle_tag_end(&$parser, $name)
237 // attach data to the node
238 if (($this->cdata = trim($this->cdata)) != '')
240 // if we have a data handler, operate it now
241 if (isset($this->taghandler["$name"]))
243 $this->registry
->debug("handler: " . $this->taghandler
["$name"]);
244 if (function_exists($this->taghandler["$name"]))
246 $this->cdata
= $this->taghandler
["$name"]($this->cdata, $this);
250 trigger_error('Could not find the function [' . $this->taghandler["$name"] . '()] for the XML tag "' . $name . '"', E_USER_ERROR
);
254 $this->attribs
['value'] = $this->cdata
;
260 $this->detach_node();
263 // ###################################################################
265 * Shifts the node tree
269 * @param array Node to place into the stack
271 function attach_node(&$node)
274 $this->stack
[ count($this->stack
) ] =& $node;
276 // new attributes to work with
277 $this->attribs
=& $node;
280 // ###################################################################
282 * Unshifts the node tree
286 function detach_node()
288 // drop the newest node
289 unset($this->stack
[ count($this->stack
) - 1 ]);
291 // assign the attributes to the next newest node
292 $this->attribs
=& $this->stack
[ count($this->stack
) - 1 ];
296 /*=====================================================================*\
297 || ###################################################################
300 || ###################################################################
301 \*=====================================================================*/