PageRenderTime 64ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/kerphi/contrib/pfcInstaller2/PEAR/XMLParser.php

https://bitbucket.org/shashwat_dinasource/bitscentral
PHP | 261 lines | 133 code | 24 blank | 104 comment | 24 complexity | 4708bbff52de958814b6ac2e22070e56 MD5 | raw file
  1. <?php
  2. /**
  3. * PEAR_FTP
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: This source file is subject to version 3.0 of the PHP license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  10. * the PHP License and are unable to obtain it through the web, please
  11. * send a note to license@php.net so we can mail you a copy immediately.
  12. *
  13. * @category pear
  14. * @package PEAR
  15. * @author Greg Beaver <cellog@php.net>
  16. * @author Stephan Schmidt (original XML_Unserializer code)
  17. * @copyright 1997-2006 The PHP Group
  18. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  19. * @version CVS: $Id: XMLParser.php,v 1.11 2006/01/06 04:47:36 cellog Exp $
  20. * @link http://pear.php.net/package/PEAR
  21. * @since File available since Release 1.4.0a1
  22. */
  23. /**
  24. * Parser for any xml file
  25. * @category pear
  26. * @package PEAR
  27. * @author Greg Beaver <cellog@php.net>
  28. * @author Stephan Schmidt (original XML_Unserializer code)
  29. * @copyright 1997-2006 The PHP Group
  30. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  31. * @version Release: 1.4.11
  32. * @link http://pear.php.net/package/PEAR
  33. * @since Class available since Release 1.4.0a1
  34. */
  35. class PEAR_XMLParser
  36. {
  37. /**
  38. * unserilialized data
  39. * @var string $_serializedData
  40. */
  41. var $_unserializedData = null;
  42. /**
  43. * name of the root tag
  44. * @var string $_root
  45. */
  46. var $_root = null;
  47. /**
  48. * stack for all data that is found
  49. * @var array $_dataStack
  50. */
  51. var $_dataStack = array();
  52. /**
  53. * stack for all values that are generated
  54. * @var array $_valStack
  55. */
  56. var $_valStack = array();
  57. /**
  58. * current tag depth
  59. * @var int $_depth
  60. */
  61. var $_depth = 0;
  62. /**
  63. * @return array
  64. */
  65. function getData()
  66. {
  67. return $this->_unserializedData;
  68. }
  69. /**
  70. * @param string xml content
  71. * @return true|PEAR_Error
  72. */
  73. function parse($data)
  74. {
  75. if (!extension_loaded('xml')) {
  76. include_once 'PEAR.php';
  77. return PEAR::raiseError("XML Extension not found", 1);
  78. }
  79. $this->_valStack = array();
  80. $this->_dataStack = array();
  81. $this->_depth = 0;
  82. if (version_compare(phpversion(), '5.0.0', 'lt')) {
  83. if (strpos($data, 'encoding="UTF-8"')) {
  84. $data = utf8_decode($data);
  85. }
  86. $xp = @xml_parser_create('ISO-8859-1');
  87. } else {
  88. if (strpos($data, 'encoding="UTF-8"')) {
  89. $xp = @xml_parser_create('UTF-8');
  90. } else {
  91. $xp = @xml_parser_create('ISO-8859-1');
  92. }
  93. }
  94. xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, 0);
  95. xml_set_object($xp, $this);
  96. xml_set_element_handler($xp, 'startHandler', 'endHandler');
  97. xml_set_character_data_handler($xp, 'cdataHandler');
  98. if (!xml_parse($xp, $data)) {
  99. $msg = xml_error_string(xml_get_error_code($xp));
  100. $line = xml_get_current_line_number($xp);
  101. xml_parser_free($xp);
  102. include_once 'PEAR.php';
  103. return PEAR::raiseError("XML Error: '$msg' on line '$line'", 2);
  104. }
  105. xml_parser_free($xp);
  106. return true;
  107. }
  108. /**
  109. * Start element handler for XML parser
  110. *
  111. * @access private
  112. * @param object $parser XML parser object
  113. * @param string $element XML element
  114. * @param array $attribs attributes of XML tag
  115. * @return void
  116. */
  117. function startHandler($parser, $element, $attribs)
  118. {
  119. $type = 'string';
  120. $this->_depth++;
  121. $this->_dataStack[$this->_depth] = null;
  122. $val = array(
  123. 'name' => $element,
  124. 'value' => null,
  125. 'type' => $type,
  126. 'childrenKeys' => array(),
  127. 'aggregKeys' => array()
  128. );
  129. if (count($attribs) > 0) {
  130. $val['children'] = array();
  131. $val['type'] = 'array';
  132. $val['children']['attribs'] = $attribs;
  133. }
  134. array_push($this->_valStack, $val);
  135. }
  136. /**
  137. * post-process data
  138. *
  139. * @param string $data
  140. * @param string $element element name
  141. */
  142. function postProcess($data, $element)
  143. {
  144. return trim($data);
  145. }
  146. /**
  147. * End element handler for XML parser
  148. *
  149. * @access private
  150. * @param object XML parser object
  151. * @param string
  152. * @return void
  153. */
  154. function endHandler($parser, $element)
  155. {
  156. $value = array_pop($this->_valStack);
  157. $data = $this->postProcess($this->_dataStack[$this->_depth], $element);
  158. // adjust type of the value
  159. switch(strtolower($value['type'])) {
  160. /*
  161. * unserialize an array
  162. */
  163. case 'array':
  164. if ($data !== '') {
  165. $value['children']['_content'] = $data;
  166. }
  167. if (isset($value['children'])) {
  168. $value['value'] = $value['children'];
  169. } else {
  170. $value['value'] = array();
  171. }
  172. break;
  173. /*
  174. * unserialize a null value
  175. */
  176. case 'null':
  177. $data = null;
  178. break;
  179. /*
  180. * unserialize any scalar value
  181. */
  182. default:
  183. settype($data, $value['type']);
  184. $value['value'] = $data;
  185. break;
  186. }
  187. $parent = array_pop($this->_valStack);
  188. if ($parent === null) {
  189. $this->_unserializedData = &$value['value'];
  190. $this->_root = &$value['name'];
  191. return true;
  192. } else {
  193. // parent has to be an array
  194. if (!isset($parent['children']) || !is_array($parent['children'])) {
  195. $parent['children'] = array();
  196. if ($parent['type'] != 'array') {
  197. $parent['type'] = 'array';
  198. }
  199. }
  200. if (!empty($value['name'])) {
  201. // there already has been a tag with this name
  202. if (in_array($value['name'], $parent['childrenKeys'])) {
  203. // no aggregate has been created for this tag
  204. if (!in_array($value['name'], $parent['aggregKeys'])) {
  205. if (isset($parent['children'][$value['name']])) {
  206. $parent['children'][$value['name']] = array($parent['children'][$value['name']]);
  207. } else {
  208. $parent['children'][$value['name']] = array();
  209. }
  210. array_push($parent['aggregKeys'], $value['name']);
  211. }
  212. array_push($parent['children'][$value['name']], $value['value']);
  213. } else {
  214. $parent['children'][$value['name']] = &$value['value'];
  215. array_push($parent['childrenKeys'], $value['name']);
  216. }
  217. } else {
  218. array_push($parent['children'],$value['value']);
  219. }
  220. array_push($this->_valStack, $parent);
  221. }
  222. $this->_depth--;
  223. }
  224. /**
  225. * Handler for character data
  226. *
  227. * @access private
  228. * @param object XML parser object
  229. * @param string CDATA
  230. * @return void
  231. */
  232. function cdataHandler($parser, $cdata)
  233. {
  234. $this->_dataStack[$this->_depth] .= $cdata;
  235. }
  236. }
  237. ?>