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