]>
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 print(sprintf("XML Error: %s (%d) at line %d colunn %d", $error['string'], $error['code'], $error['line'], $error['column']));
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 // done... send the results back
176 * Process the opening location of an XML tag
178 * @param res XML parser
179 * @param str Tag name
180 * @param array Tag attributes
182 function _tag_start($parser, $name, $attributes)
184 // we're opening a new tag
186 $this->tagname = $name;
188 // copy all tag attributes
189 array_walk($attributes, 'trim');
190 $this->attrdata[ $this->tagid ] = $attributes;
195 // calculate the parent
196 $this->parent = current($this->tree);
197 $this->parent = (($this->parent === false) ? -1 : $this->parent);
199 // add the current element onto the array
200 array_push($this->tree, $this->tagid);
202 // initialize the data set
203 $this->taginfo["$name"][ $this->parent
][ $this->tagid
] = '';
209 * @param res XML parser
210 * @param str CDATA from tag
212 function _cdata($parser, $data)
215 if ($this->parent
== $this->tagid
AND !isset($this->taginfo
[ $this->tagname
][ $this->parent
][ $this->tagid
]))
217 $this->taginfo
[ $this->tagname
][ $this->parent
][ $this->tagid
] = '';
219 $this->taginfo
[ $this->tagname
][ $this->parent
][ $this->tagid
] .= $data;
223 * Process the closing of an XML tag
225 * @param res XML parser
226 * @param str Tag name
228 function _tag_end($parser, $name)
230 // pop off the end element
231 array_pop($this->tree
);
235 /*=====================================================================*\
236 || ###################################################################
239 || ###################################################################
240 \*=====================================================================*/