trigger_error() should kill the script: don't exit on our own accord
[isso.git] / Xml.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright ©2002-[#]year[#] Blue Static
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 * Static XML parser (Xml.php)
24 *
25 * @package ISSO
26 */
27
28 /**
29 * XML Parser
30 *
31 * A simple XML parser that is run by calling BSXml::Parse( String xmlData) and an
32 * array is returned with the parsed information.
33 *
34 * @author Blue Static
35 * @copyright Copyright ©2002 - [#]year[#], Blue Static
36 * @version $Revision$
37 * @package ISSO
38 *
39 */
40 class BSXml
41 {
42 /**
43 * Parser resource
44 * @var integer
45 */
46 private $parser = null;
47
48 /**
49 * An array of function names that are to be executed for each tag name (name => function)
50 * @var array()
51 */
52 private $taghandler = array();
53
54 /**
55 * Current CDATA value
56 * @var string
57 */
58 private $cdata = '';
59
60 /**
61 * Tag stack of all open nodes
62 * @var array
63 */
64 private $stack = array();
65
66 /**
67 * Node list for all open tag attributes
68 * @var array
69 */
70 private $attribs = array();
71
72 /**
73 * Resulting parsed array
74 * @var array
75 */
76 private $result = array();
77
78 // ###################################################################
79 /**
80 * Constructor
81 */
82 private function __construct() {}
83
84 // ###################################################################
85 /**
86 * Parse an XML file
87 *
88 * @param string XML file data
89 * @param string Parse file as UTF-8 instead of ISSO-8859-1
90 *
91 * @return array Array with all the XML data parsed
92 */
93 public static function Parse($data, $utf8 = false)
94 {
95 $parser = new BSXml();
96
97 if ($utf8)
98 {
99 $data = utf8_encode($data);
100 $parser->parser = xml_parser_create('UTF-8');
101 }
102 else
103 {
104 $parser->parser = xml_parser_create('ISO-8859-1');
105 }
106
107 // create a new parser
108 xml_set_object($parser->parser, $parser);
109 xml_parser_set_option($parser->parser, XML_OPTION_CASE_FOLDING, 0);
110 xml_set_element_handler($parser->parser, '_handleStartTag', '_handleEndTag');
111 xml_set_character_data_handler($parser->parser, '_handleCData');
112
113 $parser->_attachNode($parser->result);
114
115 // parse the data and check for errors
116 if (!xml_parse($parser->parser, $data))
117 {
118 $error['code'] = xml_get_error_code($parser->parser);
119 $error['string'] = xml_error_string($error['code']);
120 $error['line'] = xml_get_current_line_number($parser->parser);
121 $error['column'] = xml_get_current_column_number($parser->parser);
122 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]");
123 }
124
125 // destroy the parser
126 xml_parser_free($parser->parser);
127
128 // done... send the results back
129 return $parser->result;
130 }
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 private function _handleStartTag(&$parser, $name, $attrs)
141 {
142 // we need to keep track of indicies to monitor the last key in $this->attribs
143 static $index;
144
145 // trim attributes
146 array_walk($attrs, 'trim');
147
148 // existing node set
149 if (isset($this->attribs["$name"]))
150 {
151 // node set has < 1 child nodes
152 if (!isset($this->attribs["$name"][0]))
153 {
154 $tmp = $this->attribs["$name"];
155 unset($this->attribs["$name"]);
156 $this->attribs["$name"][0] = $tmp;
157 }
158
159 // create a new child node
160 $this->attribs["$name"][ $index["$name"] ] = (array)$attrs;
161 $this->_attachNode($this->attribs["$name"][ $index["$name"] ]);
162 $index["$name"]++;
163 }
164 // node set doesn't exist, so create it
165 else
166 {
167 $this->attribs["$name"] = (array)$attrs;
168 $this->_attachNode($this->attribs["$name"]);
169 $index["$name"] = 1;
170 }
171 }
172
173 // ###################################################################
174 /**
175 * Process XML CDATA
176 *
177 * @param integer XML parser
178 * @param string CDATA from tag
179 */
180 private function _handleCData(&$parser, $data)
181 {
182 $this->cdata .= $data;
183 }
184
185 // ###################################################################
186 /**
187 * Process the closing of an XML tag
188 *
189 * @param integer XML parser
190 * @param string Tag name
191 */
192 private function _handleEndTag(&$parser, $name)
193 {
194 // attach data to the node
195 if (($this->cdata = trim($this->cdata)) != '')
196 {
197 $this->attribs['value'] = $this->cdata;
198 }
199
200 $this->cdata = '';
201
202 // remove the node
203 $this->_detachNode();
204 }
205
206 // ###################################################################
207 /**
208 * Shifts the node tree
209 *
210 * @param array Node to place into the stack
211 */
212 private function _attachNode(&$node)
213 {
214 // create a new node
215 $this->stack[ sizeof($this->stack) ] =& $node;
216
217 // new attributes to work with
218 $this->attribs =& $node;
219 }
220
221 // ###################################################################
222 /**
223 * Unshifts the node tree
224 */
225 private function _detachNode()
226 {
227 // drop the newest node
228 unset($this->stack[ sizeof($this->stack) - 1 ]);
229
230 // assign the attributes to the next newest node
231 $this->attribs =& $this->stack[ sizeof($this->stack) - 1 ];
232 }
233
234 // ###################################################################
235 /**
236 * Unless a node has multiple children, there will not be a numerical
237 * index for the child node. So this means that if you have variable
238 * XML structure with some of the same types of nodes having one children
239 * or multiple children, you'd have different things to parse. If
240 * you want the node unified (as in, all single-children would be indexed
241 * numerically, run this function on the node. It works on references.
242 *
243 * @param array The node to int-index
244 */
245 public static function UnifyNode(&$node)
246 {
247 if (!isset($node[0]))
248 {
249 $node = array($node);
250 }
251 }
252 }
253
254 /*=====================================================================*\
255 || ###################################################################
256 || # $HeadURL$
257 || # $Id$
258 || ###################################################################
259 \*=====================================================================*/
260 ?>