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 \*=====================================================================*/
29 $OBJECT = 'XML Parser';
30 $CLASS = 'XML_Parser';
36 * This framework is a wrapper for a robust XML parser.
38 * @author Iris Studios, Inc.
39 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
54 * An array of function names that are to be executed for each tag name (name => function)
58 var $taghandler = array();
67 * Tag stack of all open nodes
73 * Node list for all open tag attributes
76 var $attribs = array();
79 * Resulting parsed array
82 var $result = array();
87 * @param string XML file data
89 * @return array Array with all the XML data parsed
93 $this->stack
= array();
94 $this->attribs
= array();
95 $this->result
= array();
98 // create a new parser
99 $this->parser
= xml_parser_create();
100 xml_set_object($this->parser
, $this);
101 xml_parser_set_option($this->parser
, XML_OPTION_CASE_FOLDING
, 0);
102 xml_set_element_handler($this->parser
, 'handle_tag_start', 'handle_tag_end');
103 xml_set_character_data_handler($this->parser
, 'handle_cdata');
105 $this->attach_node($this->result
);
107 // parse the data and check for errors
108 if (!xml_parse($this->parser
, $data))
110 $error['code'] = xml_get_error_code($this->parser
);
111 $error['string'] = xml_error_string($error['code']);
112 $error['line'] = xml_get_current_line_number($this->parser
);
113 $error['column'] = xml_get_current_column_number($this->parser
);
114 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]", E_USER_ERROR
);
118 // destroy the parser
119 xml_parser_free($this->parser
);
121 // done... send the results back
122 return $this->result
;
126 * Process the opening location of an XML tag
128 * @param integer XML parser
129 * @param string Tag name
130 * @param array Tag attributes
132 function handle_tag_start(&$parser, $name, $attrs)
135 array_walk($attrs, 'trim');
138 if (isset($this->attribs
["$name"]))
140 // node set has < 1 child nodes
141 if (!isset($this->attribs
["$name"][0]))
143 $tmp = $this->attribs
["$name"];
144 unset($this->attribs
["$name"]);
145 $this->attribs
["$name"][0] = $tmp;
148 // create a new child node
149 $this->attach_node($this->attribs
["$name"][] = (array)$attrs);
151 // node set doesn't exist, so create it
154 $this->attach_node($this->attribs
["$name"] = (array)$attrs);
161 * @param integer XML parser
162 * @param string CDATA from tag
164 function handle_cdata(&$parser, $data)
166 $this->cdata
.= $data;
170 * Process the closing of an XML tag
172 * @param integer XML parser
173 * @param string Tag name
175 function handle_tag_end(&$parser, $name)
179 // attach data to the node
180 if (($this->cdata
= trim($this->cdata
)) != '')
182 // if we have a data handler, operate it now
183 if (isset($this->taghandler
["$name"]))
185 $_isso->debug("handler: " . $this->taghandler
["$name"]);
186 if (function_exists($this->taghandler
["$name"]))
188 $this->cdata
= $this->taghandler
["$name"]($this->cdata
, $this);
192 trigger_error('Could not find the function [' . $this->taghandler
["$name"] . '()] for the XML tag "' . $name . '"', E_USER_ERROR
);
196 $this->attribs
['value'] = $this->cdata
;
202 $this->detach_node();
206 * Shifts the node tree
208 * @param array Node to place into the stack
210 function attach_node(&$node)
213 $this->stack
[ count($this->stack
) ] =& $node;
215 // new attributes to work with
216 $this->attribs
=& $node;
220 * Unshifts the node tree
222 function detach_node()
224 // drop the newest node
225 unset($this->stack
[ count($this->stack
) - 1 ]);
227 // assign the attributes to the next newest node
228 $this->attribs
=& $this->stack
[ count($this->stack
) - 1 ];
232 /*=====================================================================*\
233 || ###################################################################
236 || ###################################################################
237 \*=====================================================================*/