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