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