From f6cf4339d9699a779d4f6700c3f34e1c21136cde Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 22 Jan 2005 02:29:38 +0000 Subject: [PATCH] Broken initial XML parser. Just getting it in here so we can preserve debug stuff. --- xml.php | 224 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 xml.php diff --git a/xml.php b/xml.php new file mode 100644 index 0000000..42e5aa2 --- /dev/null +++ b/xml.php @@ -0,0 +1,224 @@ +parser = xml_parser_create(); + xml_set_object($this->parser, $this); + xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); + xml_set_element_handler($this->parser, '_tag_start', '_tag_end'); + xml_set_character_data_handler($this->parser, '_cdata'); + + if (!xml_parse($this->parser, $data)) + { + $error['code'] = xml_get_error_code($this->parser); + $error['string'] = xml_error_string($error['code']); + $error['line'] = xml_get_current_line_number($this->parser); + $error['column'] = xml_get_current_column_number($this->parser); + print(sprintf("XML Error: %s (%d) at line %d colunn %d", $error['string'], $error['code'], $error['line'], $error['column'])); + exit; + } + + xml_parser_free($this->parser); + + return array(); + } + + /** + * Process the opening location of an XML tag + * + * @param res XML parser + * @param str Tag name + * @param array Tag attributes + */ + function _tag_start($parser, $name, $attributes) + { + // we're opening a new tag + $this->tagid++; + $this->tagname = $name; + + // copy all tag attributes + array_walk($attributes, 'trim'); + $this->attrdata[ $this->tagid ] = $attributes; + + $this->parent = current($this->tree); + if ($this->parent === false) + { + $this->parent = -1; + } + // advance the parent counter because the tree is yet to be updated + else + { + $this->parent++; + } + + // initialize the data set + $this->taginfo["$name"][ $this->parent ][ $this->tagid ] = ''; + + // add the current tag into the tree + $this->tree[] = $this->tagid; + } + + /** + * Process XML CDATA + * + * @param res XML parser + * @param str CDATA from tag + */ + function _cdata($parser, $data) + { + global $_isso; + static $count; + + /*if (preg_replace('#(^[[:space:]]+|[[:space:]]+$)#', '', trim($data)) == '') + { + $count++; + $datanew = str_replace(array("\t", "\n", "\r", " "), array("{t}", "{n}", "{r}", "{s}"), $data); + $_isso->debug("strangechars[" . $this->tagid . "] = '$datanew'"); + $data = $datanew . "||$count||"; + //return; + }*/ + + $_isso->debug("cdata[] = '$data'"); + // read in the CDATA + if ($this->parent == $this->tagid) + { + if (!isset($this->taginfo[ $this->tagname ][ $this->parent - 1 ][ $this->tagid ])) + { + $this->taginfo[ $this->tagname ][ $this->parent - 1 ][ $this->tagid ] = ''; + } + + $this->taginfo[ $this->tagname ][ $this->parent - 1 ][ $this->tagid ] .= $data; + } + else + { + $this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ] .= $data; + } + } + + /** + * Process the closing of an XML tag + * + * @param res XML parser + * @param str Tag name + */ + function _tag_end($parser, $name) + { + global $_isso; + + // fetch the data + if (isset($this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ])) + { + $data = $this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ]; + } + else + { + return; + } + + if (trim($data) == '') + { + $_isso->debug("not going to bother with '" . $this->tagid . "'"); + } + + // if we have a data handler, operate it now + if (isset($this->taghandler[ $this->tagname ])) + { + $_isso->debug("handler: {$this->taghandler[ " . $this->tagname . " ]}"); + if (function_exists($this->taghandler[ $this->tagname ])) + { + $data = $this->taghandler[ $this->tagname ]($data); + } + } + + // check for necessary trims + if ($this->trimdata) + { + // trim() doesn't take off the edge for more than a few white characters, preg_replace does; don't use [:space:] either... + //$data = preg_replace('#(^\s+|\s+$)#', '', $data); + //$data = preg_replace('#(^[[:space:]]+|[[:space:]]+$)#', '', trim($data)); + $data = preg_replace('#^\s+#', '', $data); + $data = preg_replace('#\s+$#', '', $data); + $_isso->debug("trimming data ('" . $this->tagid . "'): '" . $data . "'"); + //$data = trim($data); + } + + // don't want blank fields + if (trim($data) != '') + { + $this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ] = $data; + } + else + { + unset($this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ]); + } + + $_isso->debug("tree[] = " . current($this->tree)); + + // make sure that we're always at the end of the tree + end($this->tree); + } +} + +/*=====================================================================*\ +|| ################################################################### +|| # $HeadURL$ +|| # $Id$ +|| ################################################################### +\*=====================================================================*/ +?> \ No newline at end of file -- 2.43.5