]> src.bluestatic.org Git - isso.git/blob - xml.php
Forgot to change this instance of a method name that was changed
[isso.git] / xml.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
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 /**
30 * XML Parser
31 *
32 * This framework is a wrapper for a robust XML parser.
33 *
34 * @author Blue Static
35 * @copyright Copyright ©2002 - [#]year[#], Blue Static
36 * @version $Revision$
37 * @package ISSO
38 *
39 */
40 class XML
41 {
42 /**
43 * Framework registry object
44 * @var object
45 */
46 private $registry = null;
47
48 /**
49 * Parser resource
50 * @var integer
51 */
52 private $parser = null;
53
54 /**
55 * An array of function names that are to be executed for each tag name (name => function)
56 * @var array()
57 */
58 private $taghandler = array();
59
60 /**
61 * Current CDATA value
62 * @var string
63 */
64 private $cdata = '';
65
66 /**
67 * Tag stack of all open nodes
68 * @var array
69 */
70 private $stack = array();
71
72 /**
73 * Node list for all open tag attributes
74 * @var array
75 */
76 private $attribs = array();
77
78 /**
79 * Resulting parsed array
80 * @var array
81 */
82 private $result = array();
83
84 /**
85 * Fields array that is used in this module
86 * @var array
87 */
88 private $fields = array(
89 'taghandler' => array(REQ_NO, null, false)
90 );
91
92 // ###################################################################
93 /**
94 * Constructor
95 */
96 public function __construct(&$registry)
97 {
98 $this->registry =& $registry;
99 }
100
101 // ###################################################################
102 /**
103 * Sets an ISSO field
104 *
105 * @param string Field name
106 * @param mixed Value of the field
107 */
108 public function set($name, $value)
109 {
110 $this->registry->do_set($name, $value, 'xml');
111 }
112
113 // ###################################################################
114 /**
115 * Gets an ISSO field
116 *
117 * @param string Field name
118 *
119 * @return mixed Value of the field
120 */
121 public function get($fieldname)
122 {
123 return $this->registry->do_get($fieldname, 'xml');
124 }
125
126 // ###################################################################
127 /**
128 * Parse an XML file
129 *
130 * @param string XML file data
131 * @param string Parse file as UTF-8 instead of ISSO-8859-1
132 *
133 * @return array Array with all the XML data parsed
134 */
135 public function parse($data, $utf8 = false)
136 {
137 $this->registry->check_isso_fields(get_class($this));
138
139 $this->stack = array();
140 $this->attribs = array();
141 $this->result = array();
142 $this->cdata = '';
143
144 if ($utf8)
145 {
146 $data = utf8_encode($data);
147 $this->parser = xml_parser_create('UTF-8');
148 }
149 else
150 {
151 $this->parser = xml_parser_create('ISO-8859-1');
152 }
153
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');
159
160 $this->attach_node($this->result);
161
162 // parse the data and check for errors
163 if (!xml_parse($this->parser, $data))
164 {
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);
170 exit;
171 }
172
173 // destroy the parser
174 xml_parser_free($this->parser);
175
176 // done... send the results back
177 return $this->result;
178 }
179
180 // ###################################################################
181 /**
182 * Process the opening location of an XML tag
183 *
184 * @param integer XML parser
185 * @param string Tag name
186 * @param array Tag attributes
187 */
188 private function handle_tag_start(&$parser, $name, $attrs)
189 {
190 // we need to keep track of indicies to monitor the last key in $this->attribs
191 static $index;
192
193 // trim attributes
194 array_walk($attrs, 'trim');
195
196 // existing node set
197 if (isset($this->attribs["$name"]))
198 {
199 // node set has < 1 child nodes
200 if (!isset($this->attribs["$name"][0]))
201 {
202 $tmp = $this->attribs["$name"];
203 unset($this->attribs["$name"]);
204 $this->attribs["$name"][0] = $tmp;
205 }
206
207 // create a new child node
208 $this->attribs["$name"][ $index["$name"] ] = (array)$attrs;
209 $this->attach_node($this->attribs["$name"][ $index["$name"] ]);
210 $index["$name"]++;
211 }
212 // node set doesn't exist, so create it
213 else
214 {
215 $this->attribs["$name"] = (array)$attrs;
216 $this->attach_node($this->attribs["$name"]);
217 $index["$name"] = 1;
218 }
219 }
220
221 // ###################################################################
222 /**
223 * Process XML CDATA
224 *
225 * @param integer XML parser
226 * @param string CDATA from tag
227 */
228 private function handle_cdata(&$parser, $data)
229 {
230 $this->cdata .= $data;
231 }
232
233 // ###################################################################
234 /**
235 * Process the closing of an XML tag
236 *
237 * @param integer XML parser
238 * @param string Tag name
239 */
240 private function handle_tag_end(&$parser, $name)
241 {
242 // attach data to the node
243 if (($this->cdata = trim($this->cdata)) != '')
244 {
245 // if we have a data handler, operate it now
246 if (isset($this->taghandler["$name"]))
247 {
248 $this->registry->debug("handler: " . $this->taghandler["$name"]);
249 if (function_exists($this->taghandler["$name"]))
250 {
251 $this->cdata = $this->taghandler["$name"]($this->cdata, $this);
252 }
253 else
254 {
255 trigger_error('Could not find the function [' . $this->taghandler["$name"] . '()] for the XML tag "' . $name . '"', E_USER_ERROR);
256 }
257 }
258
259 $this->attribs['value'] = $this->cdata;
260 }
261
262 $this->cdata = '';
263
264 // remove the node
265 $this->detach_node();
266 }
267
268 // ###################################################################
269 /**
270 * Shifts the node tree
271 *
272 * @param array Node to place into the stack
273 */
274 private function attach_node(&$node)
275 {
276 // create a new node
277 $this->stack[ sizeof($this->stack) ] =& $node;
278
279 // new attributes to work with
280 $this->attribs =& $node;
281 }
282
283 // ###################################################################
284 /**
285 * Unshifts the node tree
286 */
287 private function detach_node()
288 {
289 // drop the newest node
290 unset($this->stack[ sizeof($this->stack) - 1 ]);
291
292 // assign the attributes to the next newest node
293 $this->attribs =& $this->stack[ sizeof($this->stack) - 1 ];
294 }
295
296 // ###################################################################
297 /**
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.
304 *
305 * @param array The node to int-index
306 */
307 public function unify_node(&$node)
308 {
309 if (!isset($node[0]))
310 {
311 $node = array($node);
312 }
313 }
314 }
315
316 /*=====================================================================*\
317 || ###################################################################
318 || # $HeadURL$
319 || # $Id$
320 || ###################################################################
321 \*=====================================================================*/
322 ?>