Source for file Simple.php

Documentation is available at Simple.php

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

Documentation generated on Mon, 04 Dec 2006 11:10:10 -0500 by phpDocumentor 1.3.0RC3