]>
src.bluestatic.org Git - isso.git/blob - xml.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 \*=====================================================================*/
20 $OBJECT = 'XML Parser';
21 $CLASS = 'XML_Parser';
27 * This framework is a wrapper for a robust XML parser.
29 * @author Iris Studios, Inc.
30 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
45 * Array of all parsed tag data
48 var $taginfo = array();
51 * An array of function names that are to be executed for each tag name (name => function)
55 var $taghandler = array();
58 * Whether we should trim CDATA elements
65 * Attribute data parsed from the tags
69 var $attrdata = array();
72 * Internal tracker for the current tag
78 * Internal tracker for the name of the current tag
84 * An internal tree to sort out the nesting of elements
90 * Internal tracker for the tag above the current one
98 * @param str XML file data
100 * @return array Array with all the XML data parsed
102 function parse($data)
106 // reset all the parser data before starting
107 $this->taginfo
= array();
108 $this->attrdata
= array();
111 $this->tree
= array();
112 $this->parentid
= -1;
114 // create a new parser
115 $this->parser
= xml_parser_create();
116 xml_set_object($this->parser
, $this);
117 xml_parser_set_option($this->parser
, XML_OPTION_CASE_FOLDING
, 0);
118 xml_set_element_handler($this->parser
, '_tag_start', '_tag_end');
119 xml_set_character_data_handler($this->parser
, '_cdata');
121 // parse the data and check for errors
122 if (!xml_parse($this->parser
, $data))
124 $error['code'] = xml_get_error_code($this->parser
);
125 $error['string'] = xml_error_string($error['code']);
126 $error['line'] = xml_get_current_line_number($this->parser
);
127 $error['column'] = xml_get_current_column_number($this->parser
);
128 trigger_error(sprintf("XML Error: %s (%d) at line %d colunn %d", $error['string'], $error['code'], $error['line'], $error['column']), E_USER_ERROR
);
132 // destroy the parser
133 xml_parser_free($this->parser
);
135 // data manipulation -- kind of resource intensive :-/
136 foreach ($this->taginfo
AS $tagname => $parents)
138 foreach ($parents AS $parentid => $ids)
140 foreach ($ids AS $tagid => $data)
142 // trim data if necessary
145 $_isso->debug("data trim: on");
146 if (($trimmed = trim($data)) != '')
152 // if we have a data handler, operate it now
153 if (isset($this->taghandler
["$tagname"]))
155 $_isso->debug("handler
: " . $this->taghandler["$tagname"]);
156 if (function_exists($this->taghandler
["$tagname"]))
158 $data = $this->taghandler["$tagname"]($data, $this);
162 trigger_error('Could not find the function [' . $this->taghandler
["$tagname"] . '()] for the XML tag "' . $tagname . '"', E_USER_ERROR);
166 $return["$tagid"] = array('tagid' => $tagid, 'tagname' => $tagname, 'parentid' => $parentid, 'attributes' => $this->attrdata
["$tagid"], 'data' => $data);
171 // place the normal order on things
174 // done... send the results back
179 * Process the opening location of an XML tag
181 * @param res XML parser
182 * @param str Tag name
183 * @param array Tag attributes
185 function _tag_start($parser, $name, $attributes)
187 // we're opening a new tag
189 $this->tagname = $name;
191 // copy all tag attributes
192 array_walk($attributes, 'trim');
193 $this->attrdata[ $this->tagid ] = $attributes;
198 // calculate the parent
199 $this->parent = current($this->tree);
200 $this->parent = (($this->parent === false) ? -1 : $this->parent);
202 // add the current element onto the array
203 array_push($this->tree, $this->tagid);
205 // initialize the data set
206 $this->taginfo["$name"][ $this->parent
][ $this->tagid
] = '';
212 * @param res XML parser
213 * @param str CDATA from tag
215 function _cdata($parser, $data)
218 if ($this->parent
== $this->tagid
AND !isset($this->taginfo
[ $this->tagname
][ $this->parent
][ $this->tagid
]))
220 $this->taginfo
[ $this->tagname
][ $this->parent
][ $this->tagid
] = '';
222 $this->taginfo
[ $this->tagname
][ $this->parent
][ $this->tagid
] .= $data;
226 * Process the closing of an XML tag
228 * @param res XML parser
229 * @param str Tag name
231 function _tag_end($parser, $name)
233 // pop off the end element
234 array_pop($this->tree
);
238 /*=====================================================================*\
239 || ###################################################################
242 || ###################################################################
243 \*=====================================================================*/