Renaming classes to match file names
[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
45 {
46 /**
47 * Framework registry object
48 * @var object
49 */
50 var $registry = null;
51
52 /**
53 * Parser resource
54 * @var integer
55 */
56 var $parser = null;
57
58 /**
59 * An array of function names that are to be executed for each tag name (name => function)
60 * @var array()
61 */
62 var $taghandler = array();
63
64 /**
65 * Current CDATA value
66 * @var string
67 */
68 var $cdata = '';
69
70 /**
71 * Tag stack of all open nodes
72 * @var array
73 */
74 var $stack = array();
75
76 /**
77 * Node list for all open tag attributes
78 * @var array
79 */
80 var $attribs = array();
81
82 /**
83 * Resulting parsed array
84 * @var array
85 */
86 var $result = array();
87
88 /**
89 * Constructor
90 */
91 function XML_Parser(&$registry)
92 {
93 $this->registry =& $registry;
94 }
95
96 /**
97 * Parse an XML file
98 *
99 * @param string XML file data
100 *
101 * @return array Array with all the XML data parsed
102 */
103 function parse($data)
104 {
105 $this->stack = array();
106 $this->attribs = array();
107 $this->result = array();
108 $this->cdata = '';
109
110 // create a new parser
111 $this->parser = xml_parser_create();
112 xml_set_object($this->parser, $this);
113 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
114 xml_set_element_handler($this->parser, 'handle_tag_start', 'handle_tag_end');
115 xml_set_character_data_handler($this->parser, 'handle_cdata');
116
117 $this->attach_node($this->result);
118
119 // parse the data and check for errors
120 if (!xml_parse($this->parser, $data))
121 {
122 $error['code'] = xml_get_error_code($this->parser);
123 $error['string'] = xml_error_string($error['code']);
124 $error['line'] = xml_get_current_line_number($this->parser);
125 $error['column'] = xml_get_current_column_number($this->parser);
126 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]", E_USER_ERROR);
127 exit;
128 }
129
130 // destroy the parser
131 xml_parser_free($this->parser);
132
133 // done... send the results back
134 return $this->result;
135 }
136
137 /**
138 * Process the opening location of an XML tag
139 *
140 * @param integer XML parser
141 * @param string Tag name
142 * @param array Tag attributes
143 */
144 function handle_tag_start(&$parser, $name, $attrs)
145 {
146 // trim attributes
147 array_walk($attrs, 'trim');
148
149 // existing node set
150 if (isset($this->attribs["$name"]))
151 {
152 // node set has < 1 child nodes
153 if (!isset($this->attribs["$name"][0]))
154 {
155 $tmp = $this->attribs["$name"];
156 unset($this->attribs["$name"]);
157 $this->attribs["$name"][0] = $tmp;
158 }
159
160 // create a new child node
161 $this->attach_node($this->attribs["$name"][] = (array)$attrs);
162 }
163 // node set doesn't exist, so create it
164 else
165 {
166 $this->attach_node($this->attribs["$name"] = (array)$attrs);
167 }
168 }
169
170 /**
171 * Process XML CDATA
172 *
173 * @param integer XML parser
174 * @param string CDATA from tag
175 */
176 function handle_cdata(&$parser, $data)
177 {
178 $this->cdata .= $data;
179 }
180
181 /**
182 * Process the closing of an XML tag
183 *
184 * @param integer XML parser
185 * @param string Tag name
186 */
187 function handle_tag_end(&$parser, $name)
188 {
189 // attach data to the node
190 if (($this->cdata = trim($this->cdata)) != '')
191 {
192 // if we have a data handler, operate it now
193 if (isset($this->taghandler["$name"]))
194 {
195 $this->registry->debug("handler: " . $this->taghandler["$name"]);
196 if (function_exists($this->taghandler["$name"]))
197 {
198 $this->cdata = $this->taghandler["$name"]($this->cdata, $this);
199 }
200 else
201 {
202 trigger_error('Could not find the function [' . $this->taghandler["$name"] . '()] for the XML tag "' . $name . '"', E_USER_ERROR);
203 }
204 }
205
206 $this->attribs['value'] = $this->cdata;
207 }
208
209 $this->cdata = '';
210
211 // remove the node
212 $this->detach_node();
213 }
214
215 /**
216 * Shifts the node tree
217 *
218 * @param array Node to place into the stack
219 */
220 function attach_node(&$node)
221 {
222 // create a new node
223 $this->stack[ count($this->stack) ] =& $node;
224
225 // new attributes to work with
226 $this->attribs =& $node;
227 }
228
229 /**
230 * Unshifts the node tree
231 */
232 function detach_node()
233 {
234 // drop the newest node
235 unset($this->stack[ count($this->stack) - 1 ]);
236
237 // assign the attributes to the next newest node
238 $this->attribs =& $this->stack[ count($this->stack) - 1 ];
239 }
240 }
241
242 /*=====================================================================*\
243 || ###################################################################
244 || # $HeadURL$
245 || # $Id$
246 || ###################################################################
247 \*=====================================================================*/
248 ?>