PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/xml.class.php

http://wikiplot.googlecode.com/
PHP | 397 lines | 76 code | 17 blank | 304 comment | 6 complexity | 160894f49feeebd79aadd07902c32d5a MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. Copyright (C) 2006 by the WikiPlot project authors (See http://code.google.com/p/WikiPlot).
  4. 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 2 of the License, or (at your option) any later version.
  5. 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.
  6. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7. */
  8. /**
  9. * The file contains XMLParser class
  10. *
  11. * This file contains the XMLParser class which parses the XML data to
  12. * a multidimensional array.
  13. *
  14. * @package WikiPlot
  15. * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License
  16. * @author WikiPlot development team.
  17. * @copyright Copyright 2006, WikiPlot development team.
  18. */
  19. /**
  20. * XMLParser class
  21. *
  22. * This class parses a given XML data to a multidimensional array by using
  23. * a user-defined tag. The default tag is <graph>. The example below explains
  24. * how the class works.
  25. * <code>
  26. * <?php
  27. * $xml_data = "<root>
  28. * <graph color='234,234,233' label='string'>x^2+5</graph>
  29. * <another_tag name='tag'>This tag</another_tag>
  30. * <graph>x^2+5</graph>
  31. * </root>";
  32. *
  33. * $xml = new XMLParser($xml_data);
  34. * print_r($xml->CreateInputArray());
  35. * ?>
  36. * OUTPUT:
  37. * Array
  38. * (
  39. * [0] => Array
  40. * (
  41. * [0] => Array
  42. * (
  43. * [color] => 234,234,233
  44. * [label] => string
  45. * )
  46. * [1] => x^2+5
  47. * )
  48. * [1] => Array
  49. * (
  50. * [0] => x^2+5
  51. * )
  52. * )
  53. * </code>
  54. *
  55. * @package WikiPlot
  56. * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License
  57. * @author WikiPlot development team.
  58. * @copyright Copyright 2006, WikiPlot development team.
  59. */
  60. class XMLParser {
  61. /**
  62. * Created XML Parser
  63. *
  64. * Is a resource handle and referenced to be used by athor XML functions
  65. * @access private
  66. */
  67. var $Parser;
  68. /**
  69. * XML data given by user
  70. *
  71. * Stores the XML data given by user as it is
  72. *
  73. * @var string
  74. * @access private
  75. */
  76. var $Input;
  77. /**
  78. * An interested tag in given XML data
  79. *
  80. * The variable stores attribute(s) and data of an interested tag not
  81. * the tag it selv <tag>. For example:
  82. * <code>
  83. * If this is an interested tag
  84. * <graph color='23,25,200' lable='string'>2x^3+3x</graph> the variable
  85. * Variable $Tag will look like this:
  86. * Array
  87. * (
  88. * [0] => Array
  89. * (
  90. * [color] => 23,25,200
  91. * [lable] => string
  92. * )
  93. * [1] => 2x^3+3x
  94. * )
  95. * </code>
  96. * As you can see the first element in the array is an array and it
  97. * will always be an array if the interested tag has attribute(s). The second
  98. * element in the array will be the data of the tag as string. One more thing
  99. * to be notes is that the array can not contain more then two elements, while one
  100. * element is possible.
  101. *
  102. * @var array
  103. * @access private
  104. */
  105. var $Tag;
  106. /**
  107. * Attributes of interested tag
  108. *
  109. * The variable will always be an array whether the interested tag has any
  110. * attributes or not. If the interested tag has any attribute the $Attributes
  111. * variable will be used otherwise it will be ignored.
  112. *
  113. * @var array
  114. * @access private
  115. */
  116. var $Attributes;
  117. /**
  118. * Data of the tag
  119. *
  120. * The variable will store the data of the tag. For example
  121. * <tag> tag data </tag>
  122. * $TagData = "tag data";
  123. *
  124. * @var array
  125. * @access private
  126. */
  127. var $TagData;
  128. /**
  129. * All interested tags
  130. *
  131. * The variable will store alle the interested tags found in the
  132. * given XML data.
  133. *
  134. * @var array
  135. * @access private
  136. */
  137. var $Tags;
  138. /**
  139. * The interested tag
  140. *
  141. * The variable is our iterested tag. It means the tag that we are
  142. * iterested to finde in the given XML data.
  143. * The way you should definde your interested tag is as follows:
  144. * If your interested tag is <Tag> than you should change the
  145. * $Separator variable to XMLParser::Separator = "<Tag" not "<Tag>"
  146. * or something else!
  147. *
  148. * @var string
  149. * @access public
  150. */
  151. var $Separator;
  152. /**
  153. * Constructor of XMLParser class
  154. *
  155. * The function initializes the fallowing variables:
  156. * $Parser, $Input, $Tags, $Attributes and $Separator.
  157. * It makes it possible to use XML Parser within an object
  158. * by using the function xml_set_object. Besides it uses also
  159. * two more XML Parser Functions xml_set_element_handler(),
  160. * xml_set_character_data_handler() and xml_parser_free().
  161. *
  162. * @access private
  163. * @param string $Data XML Input Data from user
  164. * @return XMLParser
  165. * @uses $Parser
  166. * @uses $Input
  167. * @uses $Tags
  168. * @uses $Attributes
  169. * @uses ExplodeInputData()
  170. * @uses Parse()
  171. * @uses OpenTag()
  172. * @uses CloseTag()
  173. * @uses GetCharData()
  174. */
  175. function XMLParser($Data)
  176. {
  177. //Initialize $Parser and creat an XML Parser to use later on
  178. $this->Parser = xml_parser_create();
  179. //Initialize $Input and set it equal to $Data (XML from user)
  180. $this->Input = $Data;
  181. //Initialize $Tags to be an array
  182. $this->Tags = array();
  183. //Initialize $Attributes to be an array
  184. $this->Attributes = array();
  185. //Initialize $Separator to be an array
  186. $this->Separator = "<graph";
  187. //Set XML Parser to use it within object
  188. xml_set_object($this->Parser, $this);
  189. //Set up start and end element handlers for the parser
  190. xml_set_element_handler($this->Parser, "OpenTag", "CloseTag");
  191. //Set up character data handler for the parser
  192. xml_set_character_data_handler($this->Parser, "GetCharData");
  193. //Call ExplodeInputData() to get the interested tags
  194. $this->ExplodeInputData();
  195. //Call Parse() to parse the $Input
  196. $this->Parse($this->Input);
  197. //Free the XML parser to later use
  198. xml_parser_free($this->Parser);
  199. }
  200. /**
  201. * Parses the given XML data
  202. *
  203. * The function uses xml_parse() function from XML Parser Functions in PHP
  204. * and parses only the first tag in the given XML data and ignores
  205. * everything else. So you can not use it for multitag XML data.
  206. * The function also calls CreateTagArray() to generate tag attribute(s)
  207. * and data to an array.
  208. *
  209. * @access private
  210. * @param string $Data
  211. * @uses CreateTagArray()
  212. */
  213. function Parse($Data)
  214. {
  215. //Parse XML Data using the $Parser
  216. xml_parse($this->Parser, $Data);
  217. //Put returned values (Attribute(s) and TagData)
  218. //from XML praser into an array called $Tag
  219. $this->CreateTagArray();
  220. }
  221. /**
  222. * Puts parsed data into an array
  223. *
  224. * The function takes the variables $Attributes and $TagData and
  225. * puts them into an array called $Tag. The first element in the
  226. * array will be Attribute(s) of the interested tag and the second
  227. * element will be the data of the tag. If Attribute does not exist
  228. * the first element will then be the data of the tag.
  229. *
  230. * @access private
  231. * @uses $Attributes
  232. * @uses $TagData
  233. * @uses $Tag
  234. */
  235. function CreateTagArray()
  236. {
  237. if (!empty($this->Attributes) && !empty($this->TagData))
  238. {
  239. $this->Tag = array($this->Attributes, $this->TagData);
  240. }
  241. else
  242. {
  243. $this->Tag = array($this->TagData);
  244. }
  245. }
  246. /**
  247. * Findes the interested tag in XML Data
  248. *
  249. * The function uses explode() function and the $Separator to finde
  250. * the interested tag in the given XML Data. When the tags are found
  251. * it puts them into array called $Tags.
  252. *
  253. * @access private
  254. * @uses $Separator
  255. * @uses $Input
  256. * @uses $Tags
  257. */
  258. function ExplodeInputData()
  259. {
  260. //Split the given XML data by using $Separator
  261. $InterestedTags = explode($this->Separator , $this->Input);
  262. //Go through the array containing the interesting tags
  263. //NOTICE: $i must be = 1 because the array contains
  264. //nothing on 0 position
  265. //NOTICE: $ must be < lenght of the array and not <= because
  266. //the last element in the array is not interesting.
  267. for ($i=1; $i < count($InterestedTags); $i++)
  268. {
  269. //Put the $Separator into the tag
  270. //(the separator vanishes when exploding the data)
  271. //fx. If the separator is <tag. The following will take place.
  272. //<tag>Hello</tag> will be exploded by <tag and
  273. //returned as >Hello</tag>. To complete the tag
  274. //we put the separator back on place. <tag + >Hello</tag>
  275. //this will return the complete tag = <tag>Hello</tag>
  276. array_push($this->Tags, $this->Separator . $InterestedTags[$i]);
  277. }
  278. }
  279. /**
  280. * Handles attribute(s) of a tag
  281. *
  282. * The function gets the value of the attribute(s) of a tag using the
  283. * $Parser. It is used by xml_set_element_handler() function in the
  284. * constructor.
  285. *
  286. * @access private
  287. * @param mixed $Parser
  288. * @param string $Tag
  289. * @param array $Attributes
  290. * @uses $Parser
  291. * @uses $Attributes
  292. */
  293. function OpenTag($Parser, $Tag, $Attributes)
  294. {
  295. //Check whether $Attributes is an array and is not an empty array
  296. if (is_array($Attributes) && count($Attributes) > 0)
  297. {
  298. //Put $his->Attributes equal to $Attributes while changing the
  299. //case of its key(s) to lowercase. The case of the key(s) is
  300. //importan due to avoid error later on.
  301. $this->Attributes = array_change_key_case($Attributes, CASE_LOWER);
  302. }
  303. else
  304. {
  305. //$this->Attributes will be an empty array() which is ignored
  306. //when adding it to the general array which is return by the
  307. //class!
  308. }
  309. }
  310. /**
  311. * Gets data of the tag
  312. *
  313. * The function gets the data of an interesting tag by using the
  314. * $Parser. It is used by xml_set_character_data_handler() function
  315. * in the constructer.
  316. *
  317. * @access private
  318. * @param mixed $Parser
  319. * @param string $CharData
  320. * @uses $Parser
  321. * @uses $TagData
  322. */
  323. function GetCharData($Parser, $CharData)
  324. {
  325. //Set $this->TagData equal to $CharData
  326. //for later use.
  327. $this->TagData = $CharData;
  328. }
  329. /**
  330. * Handles end/closing tag
  331. *
  332. * The function gets the end/closing tag using the $Parser.
  333. * It is used by xml_set_element_handler() function in the
  334. * constructer.
  335. *
  336. * @access private
  337. * @param mixed $Parser
  338. * @param string $Tag
  339. * @uses $Parser
  340. */
  341. function CloseTag($Parser, $Tag)
  342. {
  343. //Have nothing do to! :(
  344. //But must be present.
  345. }
  346. /**
  347. * Creates an array containing all parsed XML data
  348. *
  349. * The function runs each and every tag in the $Tags array
  350. * through the XMLParser object. The parsed data is then
  351. * stored in the $Graph which is returned at the end of the
  352. * proces.
  353. *
  354. * @access public
  355. * @return $Graph
  356. * @uses $Tags
  357. * @uses XMLParser
  358. */
  359. function CreateInputArray()
  360. {
  361. //Create an array to store the parsed XML data in it
  362. //and then return it at the end of the proces.
  363. $Graph = array();
  364. //Get each interested tag from $Tags
  365. foreach( $this->Tags as $Tag )
  366. {
  367. //Create instance of XMLParser and parse the
  368. //single tag to it
  369. $XMLParser = new XMLParser($Tag);
  370. //Store the data parsed by the XMLParser in the $Graph
  371. array_push($Graph, $XMLParser->Tag);
  372. }
  373. //Return the $Graph to user
  374. return $Graph;
  375. }
  376. }
  377. ?>