Back-porting the trunk back onto branch
[isso.git] / xml.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 /**
14 * XML Parser
15 * xml.php
16 *
17 * @package ISSO
18 */
19
20 $OBJECT = 'XML Parser';
21 $CLASS = 'XML_Parser';
22 $OBJ = 'xml';
23
24 /**
25 * XML Parser
26 *
27 * This framework is a wrapper for a robust XML parser.
28 *
29 * @author Iris Studios, Inc.
30 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
31 * @version $Revision$
32 * @package ISSO
33 *
34 */
35 class XML_Parser
36 {
37 /**
38 * Parser resource
39 * @var res
40 * @see parse()
41 */
42 var $parser = null;
43
44 /**
45 * An array of function names that are to be executed for each tag name (name => function)
46 * @var array()
47 * @see parse()
48 */
49 var $taghandler = array();
50
51 /**
52 * Current CDATA value
53 * @var string
54 */
55 var $cdata = '';
56
57 /**
58 * Tag stack of all open nodes
59 * @var array
60 */
61 var $stack = array();
62
63 /**
64 * Node list for all open tag attributes
65 * @var array
66 */
67 var $attribs = array();
68
69 /**
70 * Resulting parsed array
71 * @var array
72 */
73 var $result = array();
74
75 /**
76 * Parse an XML file
77 *
78 * @param str XML file data
79 *
80 * @return array Array with all the XML data parsed
81 */
82 function parse($data)
83 {
84 $this->stack = array();
85 $this->attribs = array();
86 $this->result = array();
87 $this->cdata = '';
88
89 // create a new parser
90 $this->parser = xml_parser_create();
91 xml_set_object($this->parser, $this);
92 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
93 xml_set_element_handler($this->parser, 'handle_tag_start', 'handle_tag_end');
94 xml_set_character_data_handler($this->parser, 'handle_cdata');
95
96 $this->attach_node($this->result);
97
98 // parse the data and check for errors
99 if (!xml_parse($this->parser, $data))
100 {
101 $error['code'] = xml_get_error_code($this->parser);
102 $error['string'] = xml_error_string($error['code']);
103 $error['line'] = xml_get_current_line_number($this->parser);
104 $error['column'] = xml_get_current_column_number($this->parser);
105 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]", E_USER_ERROR);
106 exit;
107 }
108
109 // destroy the parser
110 xml_parser_free($this->parser);
111
112 // done... send the results back
113 return $this->result;
114 }
115
116 /**
117 * Process the opening location of an XML tag
118 *
119 * @param res XML parser
120 * @param str Tag name
121 * @param array Tag attributes
122 */
123 function handle_tag_start(&$parser, $name, $attrs)
124 {
125 // trim attributes
126 array_walk($attrs, 'trim');
127
128 // existing node set
129 if (isset($this->attribs["$name"]))
130 {
131 // node set has < 1 child nodes
132 if (!isset($this->attribs["$name"][0]))
133 {
134 $tmp = $this->attribs["$name"];
135 unset($this->attribs["$name"]);
136 $this->attribs["$name"][0] = $tmp;
137 }
138
139 // create a new child node
140 $this->attach_node($this->attribs["$name"][] = (array)$attrs);
141 }
142 // node set doesn't exist, so create it
143 else
144 {
145 $this->attach_node($this->attribs["$name"] = (array)$attrs);
146 }
147 }
148
149 /**
150 * Process XML CDATA
151 *
152 * @param res XML parser
153 * @param str CDATA from tag
154 */
155 function handle_cdata(&$parser, $data)
156 {
157 $this->cdata .= $data;
158 }
159
160 /**
161 * Process the closing of an XML tag
162 *
163 * @param res XML parser
164 * @param str Tag name
165 */
166 function handle_tag_end(&$parser, $name)
167 {
168 global $_isso;
169
170 // attach data to the node
171 if (($this->cdata = trim($this->cdata)) != '')
172 {
173 // if we have a data handler, operate it now
174 if (isset($this->taghandler["$name"]))
175 {
176 $_isso->debug("handler: " . $this->taghandler["$name"]);
177 if (function_exists($this->taghandler["$name"]))
178 {
179 $this->cdata = $this->taghandler["$name"]($this->cdata, $this);
180 }
181 else
182 {
183 trigger_error('Could not find the function [' . $this->taghandler["$name"] . '()] for the XML tag "' . $name . '"', E_USER_ERROR);
184 }
185 }
186
187 $this->attribs['value'] = $this->cdata;
188 }
189
190 $this->cdata = '';
191
192 // remove the node
193 $this->detach_node();
194 }
195
196 /**
197 * Shifts the node tree
198 *
199 * @param array Node to place into the stack
200 */
201 function attach_node(&$node)
202 {
203 // create a new node
204 $this->stack[ count($this->stack) ] =& $node;
205
206 // new attributes to work with
207 $this->attribs =& $node;
208 }
209
210 /**
211 * Unshifts the node tree
212 */
213 function detach_node()
214 {
215 // drop the newest node
216 unset($this->stack[ count($this->stack) - 1 ]);
217
218 // assign the attributes to the next newest node
219 $this->attribs =& $this->stack[ count($this->stack) - 1 ];
220 }
221 }
222
223 /*=====================================================================*\
224 || ###################################################################
225 || # $HeadURL$
226 || # $Id$
227 || ###################################################################
228 \*=====================================================================*/
229 ?>