PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/www/devel/upgrade/upms_server/lib/pear/XML/Parser/Simple.php

https://bitbucket.org/valmy/openx
PHP | 297 lines | 94 code | 20 blank | 183 comment | 10 complexity | 23d62aa7b6114d8c8b15abb8f369b77f MD5 | raw file
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/3_0.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stephan Schmidt <schst@php-tools.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id$
  20. /**
  21. * Simple XML parser class.
  22. *
  23. * This class is a simplified version of XML_Parser.
  24. * In most XML applications the real action is executed,
  25. * when a closing tag is found.
  26. *
  27. * XML_Parser_Simple allows you to just implement one callback
  28. * for each tag that will receive the tag with its attributes
  29. * and CData
  30. *
  31. * @category XML
  32. * @package XML_Parser
  33. * @author Stephan Schmidt <schst@php-tools.net>
  34. */
  35. /**
  36. * built on XML_Parser
  37. */
  38. require_once 'XML/Parser.php';
  39. /**
  40. * Simple XML parser class.
  41. *
  42. * This class is a simplified version of XML_Parser.
  43. * In most XML applications the real action is executed,
  44. * when a closing tag is found.
  45. *
  46. * XML_Parser_Simple allows you to just implement one callback
  47. * for each tag that will receive the tag with its attributes
  48. * and CData.
  49. *
  50. * <code>
  51. * require_once '../Parser/Simple.php';
  52. *
  53. * class myParser extends XML_Parser_Simple
  54. * {
  55. * function myParser()
  56. * {
  57. * $this->XML_Parser_Simple();
  58. * }
  59. *
  60. * function handleElement($name, $attribs, $data)
  61. * {
  62. * printf('handle %s<br>', $name);
  63. * }
  64. * }
  65. *
  66. * $p = &new myParser();
  67. *
  68. * $result = $p->setInputFile('myDoc.xml');
  69. * $result = $p->parse();
  70. * </code>
  71. *
  72. * @category XML
  73. * @package XML_Parser
  74. * @author Stephan Schmidt <schst@php-tools.net>
  75. */
  76. class XML_Parser_Simple extends XML_Parser
  77. {
  78. /**
  79. * element stack
  80. *
  81. * @access private
  82. * @var array
  83. */
  84. var $_elStack = array();
  85. /**
  86. * all character data
  87. *
  88. * @access private
  89. * @var array
  90. */
  91. var $_data = array();
  92. /**
  93. * element depth
  94. *
  95. * @access private
  96. * @var integer
  97. */
  98. var $_depth = 0;
  99. /**
  100. * Mapping from expat handler function to class method.
  101. *
  102. * @var array
  103. */
  104. var $handler = array(
  105. 'default_handler' => 'defaultHandler',
  106. 'processing_instruction_handler' => 'piHandler',
  107. 'unparsed_entity_decl_handler' => 'unparsedHandler',
  108. 'notation_decl_handler' => 'notationHandler',
  109. 'external_entity_ref_handler' => 'entityrefHandler'
  110. );
  111. /**
  112. * Creates an XML parser.
  113. *
  114. * This is needed for PHP4 compatibility, it will
  115. * call the constructor, when a new instance is created.
  116. *
  117. * @param string $srcenc source charset encoding, use NULL (default) to use
  118. * whatever the document specifies
  119. * @param string $mode how this parser object should work, "event" for
  120. * handleElement(), "func" to have it call functions
  121. * named after elements (handleElement_$name())
  122. * @param string $tgenc a valid target encoding
  123. */
  124. function XML_Parser_Simple($srcenc = null, $mode = 'event', $tgtenc = null)
  125. {
  126. $this->XML_Parser($srcenc, $mode, $tgtenc);
  127. }
  128. /**
  129. * inits the handlers
  130. *
  131. * @access private
  132. */
  133. function _initHandlers()
  134. {
  135. if (!is_object($this->_handlerObj)) {
  136. $this->_handlerObj = &$this;
  137. }
  138. if ($this->mode != 'func' && $this->mode != 'event') {
  139. return $this->raiseError('Unsupported mode given', XML_PARSER_ERROR_UNSUPPORTED_MODE);
  140. }
  141. xml_set_object($this->parser, $this->_handlerObj);
  142. xml_set_element_handler($this->parser, array(&$this, 'startHandler'), array(&$this, 'endHandler'));
  143. xml_set_character_data_handler($this->parser, array(&$this, 'cdataHandler'));
  144. /**
  145. * set additional handlers for character data, entities, etc.
  146. */
  147. foreach ($this->handler as $xml_func => $method) {
  148. if (method_exists($this->_handlerObj, $method)) {
  149. $xml_func = 'xml_set_' . $xml_func;
  150. $xml_func($this->parser, $method);
  151. }
  152. }
  153. }
  154. /**
  155. * Reset the parser.
  156. *
  157. * This allows you to use one parser instance
  158. * to parse multiple XML documents.
  159. *
  160. * @access public
  161. * @return boolean|object true on success, PEAR_Error otherwise
  162. */
  163. function reset()
  164. {
  165. $this->_elStack = array();
  166. $this->_data = array();
  167. $this->_depth = 0;
  168. $result = $this->_create();
  169. if ($this->isError( $result )) {
  170. return $result;
  171. }
  172. return true;
  173. }
  174. /**
  175. * start handler
  176. *
  177. * Pushes attributes and tagname onto a stack
  178. *
  179. * @access private
  180. * @final
  181. * @param resource xml parser resource
  182. * @param string element name
  183. * @param array attributes
  184. */
  185. function startHandler($xp, $elem, &$attribs)
  186. {
  187. array_push($this->_elStack, array(
  188. 'name' => $elem,
  189. 'attribs' => $attribs
  190. )
  191. );
  192. $this->_depth++;
  193. $this->_data[$this->_depth] = '';
  194. }
  195. /**
  196. * end handler
  197. *
  198. * Pulls attributes and tagname from a stack
  199. *
  200. * @access private
  201. * @final
  202. * @param resource xml parser resource
  203. * @param string element name
  204. */
  205. function endHandler($xp, $elem)
  206. {
  207. $el = array_pop($this->_elStack);
  208. $data = $this->_data[$this->_depth];
  209. $this->_depth--;
  210. switch ($this->mode) {
  211. case 'event':
  212. $this->_handlerObj->handleElement($el['name'], $el['attribs'], $data);
  213. break;
  214. case 'func':
  215. $func = 'handleElement_' . $elem;
  216. if (strchr($func, '.')) {
  217. $func = str_replace('.', '_', $func);
  218. }
  219. if (method_exists($this->_handlerObj, $func)) {
  220. call_user_func(array(&$this->_handlerObj, $func), $el['name'], $el['attribs'], $data);
  221. }
  222. break;
  223. }
  224. }
  225. /**
  226. * handle character data
  227. *
  228. * @access private
  229. * @final
  230. * @param resource xml parser resource
  231. * @param string data
  232. */
  233. function cdataHandler($xp, $data)
  234. {
  235. $this->_data[$this->_depth] .= $data;
  236. }
  237. /**
  238. * handle a tag
  239. *
  240. * Implement this in your parser
  241. *
  242. * @access public
  243. * @abstract
  244. * @param string element name
  245. * @param array attributes
  246. * @param string character data
  247. */
  248. function handleElement($name, $attribs, $data)
  249. {
  250. }
  251. /**
  252. * get the current tag depth
  253. *
  254. * The root tag is in depth 0.
  255. *
  256. * @access public
  257. * @return integer
  258. */
  259. function getCurrentDepth()
  260. {
  261. return $this->_depth;
  262. }
  263. /**
  264. * add some string to the current ddata.
  265. *
  266. * This is commonly needed, when a document is parsed recursively.
  267. *
  268. * @access public
  269. * @param string data to add
  270. * @return void
  271. */
  272. function addToData( $data )
  273. {
  274. $this->_data[$this->_depth] .= $data;
  275. }
  276. }
  277. ?>