summaryrefslogtreecommitdiffstats
path: root/pts-core/objects/tandem_Xml/tandem_XmlReader.php
blob: 7bc6707eaeac1c65768bfafd5f50a3e59fedd99f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
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);
	}
}

?>