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();
91 // ###################################################################
95 function __construct(&$registry)
97 $this->registry
=& $registry;
100 // ###################################################################
102 * (PHP 4) Constructor
104 function XML(&$registry)
106 $this->__construct($registry);
109 // ###################################################################
115 * @param string XML file data
117 * @return array Array with all the XML data parsed
119 function parse($data)
121 $this->stack
= array();
122 $this->attribs
= array();
123 $this->result
= array();
126 // create a new parser
127 $this->parser
= xml_parser_create();
128 xml_set_object($this->parser
, $this);
129 xml_parser_set_option($this->parser
, XML_OPTION_CASE_FOLDING
, 0);
130 xml_set_element_handler($this->parser
, 'handle_tag_start', 'handle_tag_end');
131 xml_set_character_data_handler($this->parser
, 'handle_cdata');
133 $this->attach_node($this->result
);
135 // parse the data and check for errors
136 if (!xml_parse($this->parser
, $data))
138 $error['code'] = xml_get_error_code($this->parser
);
139 $error['string'] = xml_error_string($error['code']);
140 $error['line'] = xml_get_current_line_number($this->parser
);
141 $error['column'] = xml_get_current_column_number($this->parser
);
142 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]", E_USER_ERROR
);
146 // destroy the parser
147 xml_parser_free($this->parser
);
149 // done... send the results back
150 return $this->result
;
153 // ###################################################################
155 * Process the opening location of an XML tag
159 * @param integer XML parser
160 * @param string Tag name
161 * @param array Tag attributes
163 function handle_tag_start(&$parser, $name, $attrs)
166 array_walk($attrs, 'trim');
169 if (isset($this->attribs
["$name"]))
171 // node set has < 1 child nodes
172 if (!isset($this->attribs
["$name"][0]))
174 $tmp = $this->attribs
["$name"];
175 unset($this->attribs
["$name"]);
176 $this->attribs
["$name"][0] = $tmp;
179 // create a new child node
180 $this->attach_node($this->attribs
["$name"][] = (array)$attrs);
182 // node set doesn't exist, so create it
185 $this->attach_node($this->attribs
["$name"] = (array)$attrs);
189 // ###################################################################
195 * @param integer XML parser
196 * @param string CDATA from tag
198 function handle_cdata(&$parser, $data)
200 $this->cdata
.= $data;
203 // ###################################################################
205 * Process the closing of an XML tag
209 * @param integer XML parser
210 * @param string Tag name
212 function handle_tag_end(&$parser, $name)
214 // attach data to the node
215 if (($this->cdata
= trim($this->cdata
)) != '')
217 // if we have a data handler, operate it now
218 if (isset($this->taghandler
["$name"]))
220 $this->registry
->debug("handler: " . $this->taghandler
["$name"]);
221 if (function_exists($this->taghandler
["$name"]))
223 $this->cdata
= $this->taghandler
["$name"]($this->cdata
, $this);
227 trigger_error('Could not find the function [' . $this->taghandler
["$name"] . '()] for the XML tag "' . $name . '"', E_USER_ERROR
);
231 $this->attribs
['value'] = $this->cdata
;
237 $this->detach_node();
240 // ###################################################################
242 * Shifts the node tree
246 * @param array Node to place into the stack
248 function attach_node(&$node)
251 $this->stack
[ count($this->stack
) ] =& $node;
253 // new attributes to work with
254 $this->attribs
=& $node;
257 // ###################################################################
259 * Unshifts the node tree
263 function detach_node()
265 // drop the newest node
266 unset($this->stack
[ count($this->stack
) - 1 ]);
268 // assign the attributes to the next newest node
269 $this->attribs
=& $this->stack
[ count($this->stack
) - 1 ];
273 /*=====================================================================*\
274 || ###################################################################
277 || ###################################################################
278 \*=====================================================================*/