summaryrefslogtreecommitdiffstats
path: root/pts-core/objects/tandem_Xml
diff options
context:
space:
mode:
authorMichael Larabel <michael@phx-laptop.(none)>2008-11-27 11:02:12 -0500
committerMichael Larabel <michael@phx-laptop.(none)>2008-11-27 11:02:12 -0500
commit41c3d53a7f3e5bd1f62164e927fd77ef92f3de59 (patch)
treed34e29c75e7e594a5d3cb9074d96602661f6a2d9 /pts-core/objects/tandem_Xml
parenta60a7fe1ee47c0175d30184029fc3ddbfd52a81b (diff)
downloadphoronix-test-suite-upstream-41c3d53a7f3e5bd1f62164e927fd77ef92f3de59.tar.gz
phoronix-test-suite-upstream-41c3d53a7f3e5bd1f62164e927fd77ef92f3de59.tar.xz
phoronix-test-suite-upstream-41c3d53a7f3e5bd1f62164e927fd77ef92f3de59.zip
pts-core: Add support for dynamically loading objects from
sub-directories of pts-core/objects/ (Forgot to git-add to previous commit)
Diffstat (limited to 'pts-core/objects/tandem_Xml')
-rw-r--r--pts-core/objects/tandem_Xml/pts_test_tandem_XmlReader.php53
-rw-r--r--pts-core/objects/tandem_Xml/tandem_XmlReader.php309
-rw-r--r--pts-core/objects/tandem_Xml/tandem_XmlWriter.php199
3 files changed, 561 insertions, 0 deletions
diff --git a/pts-core/objects/tandem_Xml/pts_test_tandem_XmlReader.php b/pts-core/objects/tandem_Xml/pts_test_tandem_XmlReader.php
new file mode 100644
index 0000000..096b356
--- /dev/null
+++ b/pts-core/objects/tandem_Xml/pts_test_tandem_XmlReader.php
@@ -0,0 +1,53 @@
+<?php
+
+/*
+ Phoronix Test Suite
+ URLs: http://www.phoronix.com, http://www.phoronix-test-suite.com/
+ Copyright (C) 2008, Phoronix Media
+ Copyright (C) 2004-2008, Michael Larabel
+ pts_test_tandem_XmlReader.php: The XML reading object for the Phoronix Test Suite with optimizations for handling test profiles
+
+ Additional Notes: A very simple XML parser with a few extras... Does not currently support attributes on tags, etc.
+ A work in progress. This was originally designed for just some select needs in the past. No XML validation is done with this parser, etc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+class pts_test_tandem_XmlReader extends tandem_XmlReader
+{
+ function handleXmlZeroTagFallback($xml_tag)
+ {
+ // Cascading Test Profiles for finding a tag within an XML file being extended by another XML file
+ $fallback_value = $this->tag_fallback_value;
+
+ if(!empty($this->xml_file_name))
+ {
+ $test_extends = $this->getValue(P_TEST_CTPEXTENDS, null, null, true, true);
+
+ if(!empty($test_extends) && pts_is_test($test_extends))
+ {
+ $test_below_parser = new pts_test_tandem_XmlReader(pts_location_test($test_extends));
+ $test_below_tag = $test_below_parser->getXMLValue($xml_tag);
+
+ if(!empty($test_below_tag))
+ {
+ $fallback_value = $test_below_tag;
+ }
+ }
+ }
+
+ return $fallback_value;
+ }
+}
+?>
diff --git a/pts-core/objects/tandem_Xml/tandem_XmlReader.php b/pts-core/objects/tandem_Xml/tandem_XmlReader.php
new file mode 100644
index 0000000..7bc6707
--- /dev/null
+++ b/pts-core/objects/tandem_Xml/tandem_XmlReader.php
@@ -0,0 +1,309 @@
+<?php
+
+/*
+ Phoronix Test Suite
+ URLs: http://www.phoronix.com, http://www.phoronix-test-suite.com/
+ Copyright (C) 2008, Phoronix Media
+ Copyright (C) 2004-2008, Michael Larabel
+ tandem_XmlReader.php: The XML reading object for the Phoronix Test Suite.
+
+ Additional Notes: A very simple XML parser with a few extras... Does not currently support attributes on tags, etc.
+ A work in progress. This was originally designed for just some select needs in the past. No XML validation is done with this parser, etc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+class tandem_XmlReader
+{
+ var $xml_data = ""; // XML contents
+ var $xml_file_time = null; // File modification time
+ var $xml_file_name = null; // File name
+
+ var $xml_cache_file = false; // Cache the entire XML file being parsed?
+ var $xml_cache_tags = true; // Cache the tags that are being called?
+
+ var $tag_fallback_value = null; // Fallback value if tag is not present
+
+ static $tag_cache = null; // The cache for the tag cache
+ static $file_cache = null; // The cache for the file cache
+
+ function __construct($read_xml, $cache_support = true)
+ {
+ if(is_readable($read_xml))
+ {
+ if($cache_support)
+ {
+ $this->xml_cache_file = false;
+ $this->xml_cache_tags = false;
+ }
+
+ // If you are going to be banging XML files hard through the course of the script, you will want to flush the PHP file cache
+ // clearstatcache();
+
+ $this->xml_file_time = filemtime($read_xml);
+ $this->xml_file_name = $read_xml;
+
+ if($this->xml_cache_file && isset(self::$file_cache[$this->xml_file_name][$this->xml_file_time]))
+ {
+ $this->xml_data = self::$file_cache[$this->xml_file_name][$this->xml_file_time];
+ }
+
+ if(empty($this->xml_data))
+ {
+ $this->xml_data = file_get_contents($read_xml);
+
+ if($this->xml_cache_file)
+ {
+ self::$file_cache[$this->xml_file_name][$this->xml_file_time] = $this->xml_data;
+ }
+ }
+ }
+ else
+ {
+ $this->xml_cache_file = false;
+ $this->xml_cache_tags = false;
+ $this->xml_data = $read_xml;
+ }
+ }
+ function getStatement($statement)
+ {
+ return $this->listStatements(true, $statement);
+ }
+ function listStatements($perform_search = false, $search_query = "")
+ {
+ $return_r = array();
+ $comment_statements = $this->parseLineCommentsFromFile();
+
+ foreach($comment_statements as $statement)
+ {
+ $name = substr($statement, 0, strpos($statement, ":"));
+ $name = trim(strstr($name, " "));
+
+ if($perform_search && !empty($search_query))
+ {
+ if($name == $search_query)
+ {
+ $value = trim(substr(strstr($statement, ":"), 1));
+ array_push($return_r, $value);
+ }
+ }
+ else
+ {
+ array_push($return_r, $name);
+ }
+ }
+
+ return $return_r;
+ }
+ function getXMLValue($xml_tag)
+ {
+ return $this->getValue($xml_tag);
+ }
+ function isDefined($xml_tag)
+ {
+ return $this->getValue($xml_tag) != null;
+ }
+ function getValue($xml_path, $xml_tag = null, $xml_match = null, $cache_tag = true, $is_fallback_call = false)
+ {
+ if($xml_tag == null)
+ {
+ $xml_tag = $xml_path;
+ }
+ if($xml_match == null)
+ {
+ $xml_match = $this->xml_data;
+ }
+
+ if($this->xml_cache_tags && $cache_tag && isset(self::$tag_cache[$this->xml_file_name][$this->xml_file_time][$xml_tag]))
+ {
+ $xml_match = self::$tag_cache[$this->xml_file_name][$this->xml_file_time][$xml_tag];
+ }
+ else
+ {
+ foreach(explode("/", $xml_tag) as $xml_step)
+ {
+ $xml_match = $this->parseXMLString($xml_step, $xml_match, false);
+
+ if($xml_match == false)
+ {
+ if($is_fallback_call != true)
+ {
+ $xml_match = $this->handleXmlZeroTagFallback($xml_path);
+ }
+ else
+ {
+ $xml_match = $this->tag_fallback_value;
+ }
+ }
+ }
+
+ if($this->xml_cache_tags && $cache_tag)
+ {
+ self::$tag_cache[$this->xml_file_name][$this->xml_file_time][$xml_tag] = $xml_match;
+ }
+ }
+
+ return $xml_match;
+ }
+ function parseXMLString($xml_tag, $to_parse, $multi_search = true)
+ {
+ $return = false;
+ $temp_r = array();
+ $temp = $to_parse;
+
+ $open_tag = "<" . $xml_tag . ">";
+ $close_tag = "</" . $xml_tag . ">";
+ $open_tag_length = strlen($open_tag);
+ $close_tag_length = strlen($close_tag);
+
+ do
+ {
+ $return = null;
+
+ if(($start = strpos($temp, $open_tag)) !== false)
+ {
+ $temp = substr($temp, $start + $open_tag_length);
+
+ if(($end = strpos($temp, $close_tag)) !== false)
+ {
+ $return = substr($temp, 0, $end);
+ $temp = substr($temp, strlen($return) + $close_tag_length);
+ }
+ }
+
+ if($return != null)
+ {
+ array_push($temp_r, $return);
+ }
+ }
+ while($return != null && $multi_search);
+
+ if(count($temp_r) > 0)
+ {
+ $return = $temp_r;
+
+ if($multi_search == false)
+ {
+ $return = $return[0];
+ }
+ }
+
+ return $return;
+ }
+ function parseLineCommentsFromFile($read_from = null)
+ {
+ if(empty($read_from))
+ {
+ $read_from = $this->xml_data;
+ }
+
+ $return_r = array();
+ $temp = $read_from;
+
+ do
+ {
+ $return = null;
+
+ if(($start = strpos($temp, "<!--")) !== false)
+ {
+ $temp = substr($temp, $start + 4);
+
+ if(($end = strpos($temp, "-->")) !== false)
+ {
+ $return = substr($temp, 0, $end);
+ $temp = substr($temp, strlen($return) + 3);
+ }
+ }
+
+ if($return != null)
+ {
+ array_push($return_r, $return);
+ }
+ }
+ while($return != null);
+
+ return $return_r;
+ }
+ function handleXmlZeroTagFallback($xml_tag)
+ {
+ return $this->tag_fallback_value;
+ }
+ function getXMLValues($xml_tag)
+ {
+ return $this->getXMLArrayValues($xml_tag);
+ }
+ function getXMLArrayValues($xml_tag)
+ {
+ return $this->getArrayValues($xml_tag, $this->xml_data);
+ }
+ function getArrayValues($xml_tag, $xml_match)
+ {
+ $xml_steps = explode("/", $xml_tag);
+ $xml_steps_count = count($xml_steps);
+ $this_xml = $xml_match;
+
+ for($i = 0; $i < ($xml_steps_count - 2); $i++)
+ {
+ $this_xml = $this->getValue($xml_tag, $xml_steps[$i], $this_xml, false);
+ }
+
+ $xml_matches = $this->parseXMLString($xml_steps[($xml_steps_count - 2)], $this_xml);
+ $xml_matches_count = count($xml_matches);
+
+ $return_r = array();
+ $extraction_tags = explode(",", end($xml_steps));
+ $extraction_tags_count = count($extraction_tags);
+
+ for($i = 0; $i < $xml_matches_count; $i++)
+ {
+ if($extraction_tags_count == 1)
+ {
+ $this_item = $this->getValue($xml_tag, $extraction_tags[0], $xml_matches[$i], false);
+ array_push($return_r, $this_item);
+ }
+ else
+ {
+ if($i == 0)
+ {
+ foreach($extraction_tags as $extract)
+ {
+ $return_r[$extract] = array();
+ }
+ }
+
+ foreach($extraction_tags as $extract)
+ {
+ $this_item = $this->getValue($xml_tag, $extract, $xml_matches[$i], false);
+ array_push($return_r[$extract], $this_item);
+ }
+ }
+ }
+ return $return_r;
+ }
+ function setFileCaching($do_cache)
+ {
+ $this->xml_cache_file = ($do_cache == true);
+ }
+ function setTagCaching($do_cache)
+ {
+ $this->xml_cache_tags = ($do_cache == true);
+ }
+ function setCaching($do_cache)
+ {
+ $this->setFileCaching($do_cache);
+ $this->setTagCaching($do_cache);
+ }
+}
+
+?>
diff --git a/pts-core/objects/tandem_Xml/tandem_XmlWriter.php b/pts-core/objects/tandem_Xml/tandem_XmlWriter.php
new file mode 100644
index 0000000..78a8c5e
--- /dev/null
+++ b/pts-core/objects/tandem_Xml/tandem_XmlWriter.php
@@ -0,0 +1,199 @@
+<?php
+
+/*
+ Phoronix Test Suite
+ URLs: http://www.phoronix.com, http://www.phoronix-test-suite.com/
+ Copyright (C) 2008, Phoronix Media
+ Copyright (C) 2004-2008, Michael Larabel
+ tandem_XmlReader.php: The XML writing object for the Phoronix Test Suite.
+
+ Additional Notes: A very simple XML writer with a few extras... Does not support attributes on tags, etc.
+ A work in progress. This was originally designed for just some select needs in the past. It does support linking to an XSL as
+ well as whether to format the XML or not, etc. Also provides a MD5 checksum of the XML body.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+class tandem_XmlWriter
+{
+ var $xml_objects = array();
+ var $xml_string_paths = array();
+ var $xml_statements = array();
+
+ var $xml_checksum = false;
+ var $format_xml = true;
+ var $xsl_binding = null;
+
+ function __construct($nice_formatting = true)
+ {
+ $this->format_xml = ($nice_formatting == true);
+ }
+ function setXslBinding($url)
+ {
+ $this->xsl_binding = $url;
+ }
+ function writeXmlCheckSum()
+ {
+ $this->xml_checksum = true;
+ }
+ function addXmlObject($xml_location, $unique_identifier = 0, $xml_value = "", $std_step = null, $step_id = null)
+ {
+ $xml_array = array();
+ $alt_step = -1;
+ $steps = 0;
+
+ if($std_step == null)
+ {
+ $std_step = 2;
+ }
+ if($step_id == null)
+ {
+ $step_id = $unique_identifier;
+ }
+
+ if(array_search($unique_identifier . "," . $xml_location, $this->xml_string_paths) !== false)
+ {
+ $alt_step = 2;
+ }
+ else
+ {
+ array_push($this->xml_string_paths, $unique_identifier . "," . $xml_location);
+ }
+
+ $xml_steps = explode('/', $xml_location);
+ foreach(array_reverse($xml_steps) as $current_tag)
+ {
+ $steps++;
+
+ if(empty($xml_array))
+ {
+ $xml_array = $xml_value;
+ }
+ if(!empty($current_tag))
+ {
+ $xml_array = array("$current_tag" => $xml_array);
+ }
+
+ if($steps == $std_step)
+ {
+ $xml_array = array("id_" . $unique_identifier => $xml_array);
+ }
+ if($steps == $alt_step)
+ {
+ $xml_array = array("id_" . $step_id => $xml_array);
+ }
+ }
+
+ $this->xml_objects = array_merge_recursive($this->xml_objects, $xml_array);
+ }
+ function addStatement($name, $value)
+ {
+ array_push($this->xml_statements, trim($name . ": " . $value));
+ }
+ function getXMLStatements()
+ {
+ $return_string = "";
+ $statements_to_print = array_reverse($this->xml_statements);
+
+ foreach($statements_to_print as $statement)
+ {
+ $return_string .= "<!-- " . $statement . " -->\n";
+ }
+
+ return $return_string;
+ }
+ function getXML()
+ {
+ $formatted_xml = $this->getXMLBelow($this->xml_objects, 0);
+
+ $this->addStatement("Generated", date("Y-m-d H:i:s"));
+
+ if($this->xml_checksum)
+ {
+ $this->addStatement("Checksum", md5($formatted_xml));
+ }
+
+ return "<?xml version=\"1.0\"?>\n" . $this->getXSL() . $this->getXMLStatements() . $formatted_xml;
+ }
+ function getXSL()
+ {
+ $str = "";
+
+ if($this->xsl_binding != null)
+ {
+ $str = "<?xml-stylesheet type=\"text/xsl\" href=\"" . $this->xsl_binding . "\" ?>\n";
+ }
+
+ return $str;
+ }
+ function getJustXML()
+ {
+ return $this->getXMLBelow($this->xml_objects, 0);
+ }
+ function getXMLBelow($statement_name, $times_deep)
+ {
+ $formatted_xml = "";
+
+ foreach($statement_name as $key => $value)
+ {
+ if(!is_array($value))
+ {
+ $formatted_xml .= $this->getXMLTabs($times_deep) . "<" . $key . ">" . $value . "</" . $key . ">" . $this->getXMLBreaks();
+ }
+ else
+ {
+ if(substr($key, 0, 3) === "id_")
+ {
+ $formatted_xml .= $this->getXMLBelow($value, $times_deep);
+ }
+ else
+ {
+ $formatted_xml .= $this->getXMLTabs($times_deep) . "<" . $key . ">" . $this->getXMLBreaks();
+ $formatted_xml .= $this->getXMLBelow($value, $times_deep + 1);
+ $formatted_xml .= $this->getXMLTabs($times_deep) . "</" . $key . ">" . $this->getXMLBreaks();
+ }
+ }
+ }
+
+ return $formatted_xml;
+ }
+ function getXMLTabs($times_deep)
+ {
+ $format = "";
+
+ if($this->format_xml)
+ {
+ $format = str_repeat("\t", $times_deep);
+ }
+
+ return $format;
+ }
+ function getXMLBreaks()
+ {
+ $format = "";
+
+ if($this->format_xml)
+ {
+ $format = "\n";
+ }
+
+ return $format;
+ }
+ function debugDumpArray()
+ {
+ return $this->xml_objects;
+ }
+}
+
+?>