/saf/lib/PEAR/XML/HTMLSax/XML_HTMLSax_States.php

https://github.com/durand54/sitellite · PHP · 307 lines · 178 code · 0 blank · 129 comment · 42 complexity · 0190135afa3fdd1cf07cf3d59a6914c8 MD5 · raw file

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. //
  4. // +----------------------------------------------------------------------+
  5. // | PHP Version 4 |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2002 The PHP Group |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.02 of the PHP license, |
  10. // | that is bundled with this package in the file LICENSE, and is |
  11. // | available at through the world-wide-web at |
  12. // | http://www.php.net/license/3_0.txt. |
  13. // | If you did not receive a copy of the PHP license and are unable to |
  14. // | obtain it through the world-wide-web, please send a note to |
  15. // | license@php.net so we can mail you a copy immediately. |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Alexander Zhukov <alex@veresk.ru> Original port from Python |
  18. // | Authors: Harry Fuecks <hfuecks@phppatterns.com> Port to PEAR + more |
  19. // | Authors: Many @ Sitepointforums Advanced PHP Forums |
  20. // +----------------------------------------------------------------------+
  21. //
  22. // $Id: XML_HTMLSax_States.php,v 1.1.1.1 2005/04/29 04:44:43 lux Exp $
  23. //
  24. /**
  25. * Main parser components
  26. * @package XML_HTMLSax
  27. * @version $Id: XML_HTMLSax_States.php,v 1.1.1.1 2005/04/29 04:44:43 lux Exp $
  28. */
  29. /**
  30. * Define parser states
  31. */
  32. define('XML_HTMLSAX_STATE_STOP', 0);
  33. define('XML_HTMLSAX_STATE_START', 1);
  34. define('XML_HTMLSAX_STATE_TAG', 2);
  35. define('XML_HTMLSAX_STATE_OPENING_TAG', 3);
  36. define('XML_HTMLSAX_STATE_CLOSING_TAG', 4);
  37. define('XML_HTMLSAX_STATE_ATTRIBUTE', 5);
  38. define('XML_HTMLSAX_STATE_ESCAPE', 6);
  39. define('XML_HTMLSAX_STATE_JASP', 7);
  40. define('XML_HTMLSAX_STATE_PI', 8);
  41. /**
  42. * StartingState searches for the start of any XML tag
  43. * @package XML_HTMLSax
  44. * @access protected
  45. */
  46. class XML_HTMLSax_StartingState {
  47. /**
  48. * @param XML_HTMLSax_StateParser subclass
  49. * @return constant XML_HTMLSAX_STATE_TAG
  50. * @access protected
  51. */
  52. function parse(&$context) {
  53. $data = $context->scanUntilString('<');
  54. if ($data != '') {
  55. $context->handler_object_data->
  56. {$context->handler_method_data}($context->htmlsax, $data);
  57. }
  58. $context->IgnoreCharacter();
  59. return XML_HTMLSAX_STATE_TAG;
  60. }
  61. }
  62. /**
  63. * Decides which state to move one from after StartingState
  64. * @package XML_HTMLSax
  65. * @access protected
  66. */
  67. class XML_HTMLSax_TagState {
  68. /**
  69. * @param XML_HTMLSax_StateParser subclass
  70. * @return constant the next state to move into
  71. * @access protected
  72. */
  73. function parse(&$context) {
  74. switch($context->ScanCharacter()) {
  75. case '/':
  76. return XML_HTMLSAX_STATE_CLOSING_TAG;
  77. break;
  78. case '?':
  79. return XML_HTMLSAX_STATE_PI;
  80. break;
  81. case '%':
  82. return XML_HTMLSAX_STATE_JASP;
  83. break;
  84. case '!':
  85. return XML_HTMLSAX_STATE_ESCAPE;
  86. break;
  87. default:
  88. $context->unscanCharacter();
  89. return XML_HTMLSAX_STATE_OPENING_TAG;
  90. }
  91. }
  92. }
  93. /**
  94. * Dealing with closing XML tags
  95. * @package XML_HTMLSax
  96. * @access protected
  97. */
  98. class XML_HTMLSax_ClosingTagState {
  99. /**
  100. * @param XML_HTMLSax_StateParser subclass
  101. * @return constant XML_HTMLSAX_STATE_START
  102. * @access protected
  103. */
  104. function parse(&$context) {
  105. $tag = $context->scanUntilCharacters('/>');
  106. if ($tag != '') {
  107. $char = $context->scanCharacter();
  108. if ($char == '/') {
  109. $char = $context->scanCharacter();
  110. if ($char != '>') {
  111. $context->unscanCharacter();
  112. }
  113. }
  114. $context->handler_object_element->
  115. {$context->handler_method_closing}($context->htmlsax, $tag);
  116. }
  117. return XML_HTMLSAX_STATE_START;
  118. }
  119. }
  120. /**
  121. * Dealing with opening XML tags
  122. * @package XML_HTMLSax
  123. * @access protected
  124. */
  125. class XML_HTMLSax_OpeningTagState {
  126. /**
  127. * Array of tag attributes
  128. * @var array
  129. * @access private
  130. */
  131. var $attrs = array();
  132. /**
  133. * Handles attributes
  134. * @param string attribute name
  135. * @param string attribute value
  136. * @return void
  137. * @access protected
  138. * @see XML_HTMLSax_AttributeStartState
  139. */
  140. function attributeHandler($attributename, $attributevalue) {
  141. $this->attrs[$attributename] = $attributevalue;
  142. }
  143. /**
  144. * @param XML_HTMLSax_StateParser subclass
  145. * @return constant XML_HTMLSAX_STATE_START
  146. * @access protected
  147. */
  148. function parse(&$context) {
  149. $tag = $context->scanUntilCharacters("/> \n\r\t");
  150. if ($tag != '') {
  151. $this->attrs = array();
  152. $context->_parse(XML_HTMLSAX_STATE_ATTRIBUTE);
  153. $context->handler_object_element->
  154. {$context->handler_method_opening}($context->htmlsax, $tag, $this->attrs);
  155. $char = $context->scanCharacter();
  156. if ($char == '/') {
  157. $char = $context->scanCharacter();
  158. if ($char != '>') {
  159. $context->unscanCharacter();
  160. }
  161. $context->handler_object_element->
  162. {$context->handler_method_closing}($context->htmlsax, $tag);
  163. }
  164. }
  165. return XML_HTMLSAX_STATE_START;
  166. }
  167. }
  168. /**
  169. * Deals with opening tag attributes
  170. * @package XML_HTMLSax
  171. * @access protected
  172. * @see XML_HTMLSax_OpeningTagState
  173. */
  174. class XML_HTMLSax_AttributeStartState {
  175. /**
  176. * The opening state to pass attributes back to
  177. * @var XML_HTMLSax_OpeningTagState
  178. * @access private
  179. */
  180. var $attribute_handler;
  181. /**
  182. * @param XML_HTMLSax_StateParser subclass
  183. * @return constant XML_HTMLSAX_STATE_ATTRIBUTE
  184. * @access protected
  185. */
  186. function parse(&$context) {
  187. $context->ignoreWhitespace();
  188. $attributename = $context->scanUntilCharacters("=/> \n\r\t");
  189. if ($attributename == '') {
  190. return XML_HTMLSAX_STATE_STOP;
  191. } else {
  192. $attributevalue = NULL;
  193. $context->ignoreWhitespace();
  194. $char = $context->scanCharacter();
  195. if ($char == '=') {
  196. $context->ignoreWhitespace();
  197. $char = $context->ScanCharacter();
  198. if ($char == '"') {
  199. $attributevalue= $context->scanUntilString('"');
  200. $context->IgnoreCharacter();
  201. } else if ($char == "'") {
  202. $attributevalue = $context->scanUntilString("'");
  203. $context->IgnoreCharacter();
  204. } else {
  205. $context->unscanCharacter();
  206. $attributevalue =
  207. $context->scanUntilCharacters("> \n\r\t");
  208. }
  209. } else {
  210. $attributevalue = true;
  211. $context->unscanCharacter();
  212. }
  213. $this->attribute_handler->
  214. attributeHandler($attributename, $attributevalue);
  215. return XML_HTMLSAX_STATE_ATTRIBUTE;
  216. }
  217. }
  218. }
  219. /**
  220. * Deals with XML escapes handling comments and CDATA correctly
  221. * @package XML_HTMLSax
  222. * @access protected
  223. */
  224. class XML_HTMLSax_EscapeState {
  225. /**
  226. * @param XML_HTMLSax_StateParser subclass
  227. * @return constant XML_HTMLSAX_STATE_START
  228. * @access protected
  229. */
  230. function parse(&$context) {
  231. $char = $context->ScanCharacter();
  232. if ($char == '-') {
  233. $char = $context->ScanCharacter();
  234. if ($char == '-') {
  235. $text = $context->scanUntilString('-->');
  236. $context->IgnoreCharacter();
  237. $context->IgnoreCharacter();
  238. } else {
  239. $context->unscanCharacter();
  240. $text = $context->scanUntilString('>');
  241. }
  242. } else if ( $char == '[') {
  243. $context->scanUntilString('CDATA[');
  244. for ( $i=0;$i<6;$i++ ) {
  245. $context->IgnoreCharacter();
  246. }
  247. $text = $context->scanUntilString(']]>');
  248. $context->IgnoreCharacter();
  249. $context->IgnoreCharacter();
  250. } else {
  251. $context->unscanCharacter();
  252. $text = $context->scanUntilString('>');
  253. }
  254. $context->IgnoreCharacter();
  255. if ($text != '') {
  256. $context->handler_object_escape->
  257. {$context->handler_method_escape}($context->htmlsax, $text);
  258. }
  259. return XML_HTMLSAX_STATE_START;
  260. }
  261. }
  262. /**
  263. * Deals with JASP/ASP markup
  264. * @package XML_HTMLSax
  265. * @access protected
  266. */
  267. class XML_HTMLSax_JaspState {
  268. /**
  269. * @param XML_HTMLSax_StateParser subclass
  270. * @return constant XML_HTMLSAX_STATE_START
  271. * @access protected
  272. */
  273. function parse(&$context) {
  274. $text = $context->scanUntilString('%>');
  275. if ($text != '') {
  276. $context->handler_object_jasp->
  277. {$context->handler_method_jasp}($context->htmlsax, $text);
  278. }
  279. $context->IgnoreCharacter();
  280. $context->IgnoreCharacter();
  281. return XML_HTMLSAX_STATE_START;
  282. }
  283. }
  284. /**
  285. * Deals with XML processing instructions
  286. * @package XML_HTMLSax
  287. * @access protected
  288. */
  289. class XML_HTMLSax_PiState {
  290. /**
  291. * @param XML_HTMLSax_StateParser subclass
  292. * @return constant XML_HTMLSAX_STATE_START
  293. * @access protected
  294. */
  295. function parse(&$context) {
  296. $target = $context->scanUntilCharacters(" \n\r\t");
  297. $data = $context->scanUntilString('?>');
  298. if ($data != '') {
  299. $context->handler_object_pi->
  300. {$context->handler_method_pi}($context->htmlsax, $target, $data);
  301. }
  302. $context->IgnoreCharacter();
  303. $context->IgnoreCharacter();
  304. return XML_HTMLSAX_STATE_START;
  305. }
  306. }
  307. ?>