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