]>
src.bluestatic.org Git - isso.git/blob - xml.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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.
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
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 \*=====================================================================*/
32 * This framework is a wrapper for a robust XML parser.
34 * @author Iris Studios, Inc.
35 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
43 * Framework registry object
55 * An array of function names that are to be executed for each tag name (name => function)
58 var $taghandler = array();
67 * Tag stack of all open nodes
73 * Node list for all open tag attributes
76 var $attribs = array();
79 * Resulting parsed array
82 var $result = array();
87 function XML(&$registry)
89 $this->registry
=& $registry;
95 * @param string XML file data
97 * @return array Array with all the XML data parsed
101 $this->stack
= array();
102 $this->attribs
= array();
103 $this->result
= array();
106 // create a new parser
107 $this->parser
= xml_parser_create();
108 xml_set_object($this->parser
, $this);
109 xml_parser_set_option($this->parser
, XML_OPTION_CASE_FOLDING
, 0);
110 xml_set_element_handler($this->parser
, 'handle_tag_start', 'handle_tag_end');
111 xml_set_character_data_handler($this->parser
, 'handle_cdata');
113 $this->attach_node($this->result
);
115 // parse the data and check for errors
116 if (!xml_parse($this->parser
, $data))
118 $error['code'] = xml_get_error_code($this->parser
);
119 $error['string'] = xml_error_string($error['code']);
120 $error['line'] = xml_get_current_line_number($this->parser
);
121 $error['column'] = xml_get_current_column_number($this->parser
);
122 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]", E_USER_ERROR
);
126 // destroy the parser
127 xml_parser_free($this->parser
);
129 // done... send the results back
130 return $this->result
;
134 * Process the opening location of an XML tag
136 * @param integer XML parser
137 * @param string Tag name
138 * @param array Tag attributes
140 function handle_tag_start(&$parser, $name, $attrs)
143 array_walk($attrs, 'trim');
146 if (isset($this->attribs
["$name"]))
148 // node set has < 1 child nodes
149 if (!isset($this->attribs["$name"][0]))
151 $tmp = $this->attribs
["$name"];
152 unset($this->attribs["$name"]);
153 $this->attribs
["$name"][0] = $tmp;
156 // create a new child node
157 $this->attach_node($this->attribs["$name"][] = (array)$attrs);
159 // node set doesn't exist, so create it
162 $this->attach_node($this->attribs
["$name"] = (array)$attrs);
169 * @param integer XML parser
170 * @param string CDATA from tag
172 function handle_cdata(&$parser, $data)
174 $this->cdata .= $data;
178 * Process the closing of an XML tag
180 * @param integer XML parser
181 * @param string Tag name
183 function handle_tag_end(&$parser, $name)
185 // attach data to the node
186 if (($this->cdata = trim($this->cdata)) != '')
188 // if we have a data handler, operate it now
189 if (isset($this->taghandler["$name"]))
191 $this->registry
->debug("handler: " . $this->taghandler
["$name"]);
192 if (function_exists($this->taghandler["$name"]))
194 $this->cdata
= $this->taghandler
["$name"]($this->cdata, $this);
198 trigger_error('Could not find the function [' . $this->taghandler["$name"] . '()] for the XML tag "' . $name . '"', E_USER_ERROR
);
202 $this->attribs
['value'] = $this->cdata
;
208 $this->detach_node();
212 * Shifts the node tree
214 * @param array Node to place into the stack
216 function attach_node(&$node)
219 $this->stack
[ count($this->stack
) ] =& $node;
221 // new attributes to work with
222 $this->attribs
=& $node;
226 * Unshifts the node tree
228 function detach_node()
230 // drop the newest node
231 unset($this->stack
[ count($this->stack
) - 1 ]);
233 // assign the attributes to the next newest node
234 $this->attribs
=& $this->stack
[ count($this->stack
) - 1 ];
238 /*=====================================================================*\
239 || ###################################################################
242 || ###################################################################
243 \*=====================================================================*/