diff options
author | Ermal Luçi <eri@pfsense.org> | 2009-08-12 17:55:18 +0000 |
---|---|---|
committer | Ermal Luçi <eri@pfsense.org> | 2009-08-12 17:55:18 +0000 |
commit | d7b6c8424d47f72d95a8e6cbaf6825a6c1a210ae (patch) | |
tree | e7ebcbfbbf4eddd5192a56721af185903de2a79e /etc/inc/xmlparse.inc | |
parent | 85250056e0554f572cfeca746f58ed5dc40c63cc (diff) | |
download | pfsense-d7b6c8424d47f72d95a8e6cbaf6825a6c1a210ae.zip pfsense-d7b6c8424d47f72d95a8e6cbaf6825a6c1a210ae.tar.gz |
Change the parser to xmlreader instead of the current parser. This brings speed improvements and reduces some of the errors of the previous parser.
Diffstat (limited to 'etc/inc/xmlparse.inc')
-rw-r--r-- | etc/inc/xmlparse.inc | 134 |
1 files changed, 38 insertions, 96 deletions
diff --git a/etc/inc/xmlparse.inc b/etc/inc/xmlparse.inc index cb55117..4522145 100644 --- a/etc/inc/xmlparse.inc +++ b/etc/inc/xmlparse.inc @@ -59,77 +59,37 @@ function listtags_pkg() { return $ret; } -function startElement($parser, $name, $attrs) { - global $parsedcfg, $depth, $curpath, $havedata, $listtags; +function add_elements(&$cfgarray, &$parser) { + global $listtags; + while ($parser->read()) { + switch ($parser->nodeType) { + case XMLReader::WHITESPACE: + //$type = "WHITESPACE"; + break; + case XMLReader::SIGNIFICANT_WHITESPACE: + //$type = "SIGNIFICANT_WHITESPACE"; + break; + case XMLReader::ELEMENT: + if ($parser->isEmptyElement) { + $cfgarray[$parser->name] = ""; + } else { + if (in_array($parser->name, $listtags)) + add_elements($cfgarray[$parser->name][count($cfgarray[$parser->name])], $parser); + else + add_elements($cfgarray[$parser->name], $parser); + } + break; + case XMLReader::TEXT: + $cfgarray = $parser->value; + break; + case XMLReader::END_ELEMENT: + return; + break; + default: + break; + } - array_push($curpath, strtolower($name)); - - $ptr =& $parsedcfg; - foreach ($curpath as $path) { - $ptr =& $ptr[$path]; - } - - /* is it an element that belongs to a list? */ - if (in_array(strtolower($name), $listtags)) { - - /* is there an array already? */ - if (!is_array($ptr)) { - /* make an array */ - $ptr = array(); - } - - array_push($curpath, count($ptr)); - - } else if (isset($ptr)) { - /* multiple entries not allowed for this element, bail out */ - die(sprintf("XML error: %s at line %d cannot occur more than once\n", - $name, - xml_get_current_line_number($parser))); - } - - $depth++; - $havedata = $depth; -} - -function endElement($parser, $name) { - global $depth, $curpath, $parsedcfg, $havedata, $listtags; - - if ($havedata == $depth) { - $ptr =& $parsedcfg; - foreach ($curpath as $path) { - $ptr =& $ptr[$path]; - } - $ptr = ""; - } - - array_pop($curpath); - - if (in_array(strtolower($name), $listtags)) - array_pop($curpath); - - $depth--; -} - -function cData($parser, $data) { - global $depth, $curpath, $parsedcfg, $havedata; - - $data = trim($data, "\t\n\r"); - - if ($data != "") { - $ptr =& $parsedcfg; - foreach ($curpath as $path) { - $ptr =& $ptr[$path]; - } - - if (is_string($ptr)) { - $ptr .= $data; - } else { - if (trim($data, " ") != "") { - $ptr = $data; - $havedata++; - } - } - } + } } function parse_xml_config($cffile, $rootobj, $isstring = "false") { @@ -140,7 +100,7 @@ function parse_xml_config($cffile, $rootobj, $isstring = "false") { $listtags[] = $tag; } } - return parse_xml_config_raw($cffile, $rootobj, $isstring); + return parse_xml_config_raw($cffile, $rootobj); } function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") { @@ -156,34 +116,16 @@ function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") { function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") { - global $depth, $curpath, $parsedcfg, $havedata, $listtags; - $parsedcfg = array(); - $curpath = array(); - $depth = 0; - $havedata = 0; - - $xml_parser = xml_parser_create(); - - xml_set_element_handler($xml_parser, "startElement", "endElement"); - xml_set_character_data_handler($xml_parser, "cdata"); + global $listtags; - if (!($fp = fopen($cffile, "r"))) { - die("Error: could not open XML input\n"); - } + $parsedcfg = array(); - while ($data = fread($fp, 4096)) { - if (!xml_parse($xml_parser, $data, feof($fp))) { - log_error(sprintf("XML error: %s at line %d\n", - xml_error_string(xml_get_error_code($xml_parser)), - xml_get_current_line_number($xml_parser))); - return -1; - } - } - xml_parser_free($xml_parser); + $par = new XMLReader(); + $par->open($cffile); - if (!$parsedcfg[$rootobj]) { - die("XML error: no $rootobj object found!\n"); - } + add_elements($parsedcfg, $par); + + $par->close(); return $parsedcfg[$rootobj]; } |