Delineating functions
[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 /**
30 * XML Parser
31 *
32 * This framework is a wrapper for a robust XML parser.
33 *
34 * @author Iris Studios, Inc.
35 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
36 * @version $Revision$
37 * @package ISSO
38 *
39 */
40 class XML
41 {
42 /**
43 * Framework registry object
44 * @var object
45 */
46 var $registry = null;
47
48 /**
49 * Parser resource
50 * @var integer
51 */
52 var $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 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 /**
86 * Constructor
87 */
88 function __construct(&$registry)
89 {
90 $this->registry =& $registry;
91 }
92
93 // ###################################################################
94 /**
95 * (PHP 4) Constructor
96 */
97 function XML(&$registry)
98 {
99 $this->__construct($registry);
100 }
101
102 // ###################################################################
103 /**
104 * Parse an XML file
105 *
106 * @access public
107 *
108 * @param string XML file data
109 *
110 * @return array Array with all the XML data parsed
111 */
112 function parse($data)
113 {
114 $this->stack = array();
115 $this->attribs = array();
116 $this->result = array();
117 $this->cdata = '';
118
119 // create a new parser
120 $this->parser = xml_parser_create();
121 xml_set_object($this->parser, $this);
122 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
123 xml_set_element_handler($this->parser, 'handle_tag_start', 'handle_tag_end');
124 xml_set_character_data_handler($this->parser, 'handle_cdata');
125
126 $this->attach_node($this->result);
127
128 // parse the data and check for errors
129 if (!xml_parse($this->parser, $data))
130 {
131 $error['code'] = xml_get_error_code($this->parser);
132 $error['string'] = xml_error_string($error['code']);
133 $error['line'] = xml_get_current_line_number($this->parser);
134 $error['column'] = xml_get_current_column_number($this->parser);
135 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]", E_USER_ERROR);
136 exit;
137 }
138
139 // destroy the parser
140 xml_parser_free($this->parser);
141
142 // done... send the results back
143 return $this->result;
144 }
145
146 // ###################################################################
147 /**
148 * Process the opening location of an XML tag
149 *
150 * @access private
151 *
152 * @param integer XML parser
153 * @param string Tag name
154 * @param array Tag attributes
155 */
156 function handle_tag_start(&$parser, $name, $attrs)
157 {
158 // trim attributes
159 array_walk($attrs, 'trim');
160
161 // existing node set
162 if (isset($this->attribs["$name"]))
163 {
164 // node set has < 1 child nodes
165 if (!isset($this->attribs["$name"][0]))
166 {
167 $tmp = $this->attribs["$name"];
168 unset($this->attribs["$name"]);
169 $this->attribs["$name"][0] = $tmp;
170 }
171
172 // create a new child node
173 $this->attach_node($this->attribs["$name"][] = (array)$attrs);
174 }
175 // node set doesn't exist, so create it
176 else
177 {
178 $this->attach_node($this->attribs["$name"] = (array)$attrs);
179 }
180 }
181
182 // ###################################################################
183 /**
184 * Process XML CDATA
185 *
186 * @access private
187 *
188 * @param integer XML parser
189 * @param string CDATA from tag
190 */
191 function handle_cdata(&$parser, $data)
192 {
193 $this->cdata .= $data;
194 }
195
196 // ###################################################################
197 /**
198 * Process the closing of an XML tag
199 *
200 * @access private
201 *
202 * @param integer XML parser
203 * @param string Tag name
204 */
205 function handle_tag_end(&$parser, $name)
206 {
207 // attach data to the node
208 if (($this->cdata = trim($this->cdata)) != '')
209 {
210 // if we have a data handler, operate it now
211 if (isset($this->taghandler["$name"]))
212 {
213 $this->registry->debug("handler: " . $this->taghandler["$name"]);
214 if (function_exists($this->taghandler["$name"]))
215 {
216 $this->cdata = $this->taghandler["$name"]($this->cdata, $this);
217 }
218 else
219 {
220 trigger_error('Could not find the function [' . $this->taghandler["$name"] . '()] for the XML tag "' . $name . '"', E_USER_ERROR);
221 }
222 }
223
224 $this->attribs['value'] = $this->cdata;
225 }
226
227 $this->cdata = '';
228
229 // remove the node
230 $this->detach_node();
231 }
232
233 // ###################################################################
234 /**
235 * Shifts the node tree
236 *
237 * @access private
238 *
239 * @param array Node to place into the stack
240 */
241 function attach_node(&$node)
242 {
243 // create a new node
244 $this->stack[ count($this->stack) ] =& $node;
245
246 // new attributes to work with
247 $this->attribs =& $node;
248 }
249
250 // ###################################################################
251 /**
252 * Unshifts the node tree
253 *
254 * @access private
255 */
256 function detach_node()
257 {
258 // drop the newest node
259 unset($this->stack[ count($this->stack) - 1 ]);
260
261 // assign the attributes to the next newest node
262 $this->attribs =& $this->stack[ count($this->stack) - 1 ];
263 }
264 }
265
266 /*=====================================================================*\
267 || ###################################################################
268 || # $HeadURL$
269 || # $Id$
270 || ###################################################################
271 \*=====================================================================*/
272 ?>