Converting to GPL
[isso.git] / xml.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
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.
10 || #
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
14 || # more details.
15 || #
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 \*=====================================================================*/
21
22 /**
23 * XML Parser
24 * xml.php
25 *
26 * @package ISSO
27 */
28
29 $OBJECT = 'XML Parser';
30 $CLASS = 'XML_Parser';
31 $OBJ = 'xml';
32
33 /**
34 * XML Parser
35 *
36 * This framework is a wrapper for a robust XML parser.
37 *
38 * @author Iris Studios, Inc.
39 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
40 * @version $Revision$
41 * @package ISSO
42 *
43 */
44 class XML_Parser
45 {
46 /**
47 * Parser resource
48 * @var res
49 * @see parse()
50 */
51 var $parser = null;
52
53 /**
54 * An array of function names that are to be executed for each tag name (name => function)
55 * @var array()
56 * @see parse()
57 */
58 var $taghandler = array();
59
60 /**
61 * Current CDATA value
62 * @var string
63 */
64 var $cdata = '';
65
66 /**
67 * Tag stack of all open nodes
68 * @var array
69 */
70 var $stack = array();
71
72 /**
73 * Node list for all open tag attributes
74 * @var array
75 */
76 var $attribs = array();
77
78 /**
79 * Resulting parsed array
80 * @var array
81 */
82 var $result = array();
83
84 /**
85 * Parse an XML file
86 *
87 * @param str XML file data
88 *
89 * @return array Array with all the XML data parsed
90 */
91 function parse($data)
92 {
93 $this->stack = array();
94 $this->attribs = array();
95 $this->result = array();
96 $this->cdata = '';
97
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');
104
105 $this->attach_node($this->result);
106
107 // parse the data and check for errors
108 if (!xml_parse($this->parser, $data))
109 {
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);
115 exit;
116 }
117
118 // destroy the parser
119 xml_parser_free($this->parser);
120
121 // done... send the results back
122 return $this->result;
123 }
124
125 /**
126 * Process the opening location of an XML tag
127 *
128 * @param res XML parser
129 * @param str Tag name
130 * @param array Tag attributes
131 */
132 function handle_tag_start(&$parser, $name, $attrs)
133 {
134 // trim attributes
135 array_walk($attrs, 'trim');
136
137 // existing node set
138 if (isset($this->attribs["$name"]))
139 {
140 // node set has < 1 child nodes
141 if (!isset($this->attribs["$name"][0]))
142 {
143 $tmp = $this->attribs["$name"];
144 unset($this->attribs["$name"]);
145 $this->attribs["$name"][0] = $tmp;
146 }
147
148 // create a new child node
149 $this->attach_node($this->attribs["$name"][] = (array)$attrs);
150 }
151 // node set doesn't exist, so create it
152 else
153 {
154 $this->attach_node($this->attribs["$name"] = (array)$attrs);
155 }
156 }
157
158 /**
159 * Process XML CDATA
160 *
161 * @param res XML parser
162 * @param str CDATA from tag
163 */
164 function handle_cdata(&$parser, $data)
165 {
166 $this->cdata .= $data;
167 }
168
169 /**
170 * Process the closing of an XML tag
171 *
172 * @param res XML parser
173 * @param str Tag name
174 */
175 function handle_tag_end(&$parser, $name)
176 {
177 global $_isso;
178
179 // attach data to the node
180 if (($this->cdata = trim($this->cdata)) != '')
181 {
182 // if we have a data handler, operate it now
183 if (isset($this->taghandler["$name"]))
184 {
185 $_isso->debug("handler: " . $this->taghandler["$name"]);
186 if (function_exists($this->taghandler["$name"]))
187 {
188 $this->cdata = $this->taghandler["$name"]($this->cdata, $this);
189 }
190 else
191 {
192 trigger_error('Could not find the function [' . $this->taghandler["$name"] . '()] for the XML tag "' . $name . '"', E_USER_ERROR);
193 }
194 }
195
196 $this->attribs['value'] = $this->cdata;
197 }
198
199 $this->cdata = '';
200
201 // remove the node
202 $this->detach_node();
203 }
204
205 /**
206 * Shifts the node tree
207 *
208 * @param array Node to place into the stack
209 */
210 function attach_node(&$node)
211 {
212 // create a new node
213 $this->stack[ count($this->stack) ] =& $node;
214
215 // new attributes to work with
216 $this->attribs =& $node;
217 }
218
219 /**
220 * Unshifts the node tree
221 */
222 function detach_node()
223 {
224 // drop the newest node
225 unset($this->stack[ count($this->stack) - 1 ]);
226
227 // assign the attributes to the next newest node
228 $this->attribs =& $this->stack[ count($this->stack) - 1 ];
229 }
230 }
231
232 /*=====================================================================*\
233 || ###################################################################
234 || # $HeadURL$
235 || # $Id$
236 || ###################################################################
237 \*=====================================================================*/
238 ?>