PageRenderTime 51ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/domit/xml_saxy_shared.php

http://akelosframework.googlecode.com/
PHP | 287 lines | 254 code | 2 blank | 31 comment | 4 complexity | 147360917b6c491653a981e45e24ea7a MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * SAXY_Parser_Base is a base class for SAXY and SAXY Lite
  4. * @package saxy-xmlparser
  5. * @version 0.87
  6. * @copyright (C) 2004 John Heinstein. All rights reserved
  7. * @license http://www.gnu.org/copyleft/lesser.html LGPL License
  8. * @author John Heinstein <johnkarl@nbnet.nb.ca>
  9. * @link http://www.engageinteractive.com/saxy/ SAXY Home Page
  10. * SAXY is Free Software
  11. **/
  12. /** the initial characters of a cdata section */
  13. define('SAXY_SEARCH_CDATA', '![CDATA[');
  14. /** the length of the initial characters of a cdata section */
  15. define('SAXY_CDATA_LEN', 8);
  16. /** the initial characters of a notation */
  17. define('SAXY_SEARCH_NOTATION', '!NOTATION');
  18. /** the initial characters of a doctype */
  19. define('SAXY_SEARCH_DOCTYPE', '!DOCTYPE');
  20. /** saxy parse state, just before parsing an attribute */
  21. define('SAXY_STATE_ATTR_NONE', 0);
  22. /** saxy parse state, parsing an attribute key */
  23. define('SAXY_STATE_ATTR_KEY', 1);
  24. /** saxy parse state, parsing an attribute value */
  25. define('SAXY_STATE_ATTR_VALUE', 2);
  26. /**
  27. * The base SAX Parser class
  28. *
  29. * @package saxy-xmlparser
  30. * @author John Heinstein <johnkarl@nbnet.nb.ca>
  31. */
  32. class SAXY_Parser_Base {
  33. /** @var int The current state of the parser */
  34. var $state;
  35. /** @var int A temporary container for parsed characters */
  36. var $charContainer;
  37. /** @var Object A reference to the start event handler */
  38. var $startElementHandler;
  39. /** @var Object A reference to the end event handler */
  40. var $endElementHandler;
  41. /** @var Object A reference to the data event handler */
  42. var $characterDataHandler;
  43. /** @var Object A reference to the CDATA Section event handler */
  44. var $cDataSectionHandler = null;
  45. /** @var boolean True if predefined entities are to be converted into characters */
  46. var $convertEntities = true;
  47. /** @var Array Translation table for predefined entities */
  48. var $predefinedEntities = array('&amp;' => '&', '&lt;' => '<', '&gt;' => '>',
  49. '&quot;' => '"', '&apos;' => "'");
  50. /** @var Array User defined translation table for entities */
  51. var $definedEntities = array();
  52. /**
  53. * Constructor for SAX parser
  54. */
  55. function SAXY_Parser_Base() {
  56. $this->charContainer = '';
  57. } //SAXY_Parser_Base
  58. /**
  59. * Sets a reference to the handler for the start element event
  60. * @param mixed A reference to the start element handler
  61. */
  62. function xml_set_element_handler($startHandler, $endHandler) {
  63. $this->startElementHandler = $startHandler;
  64. $this->endElementHandler = $endHandler;
  65. } //xml_set_element_handler
  66. /**
  67. * Sets a reference to the handler for the data event
  68. * @param mixed A reference to the data handler
  69. */
  70. function xml_set_character_data_handler($handler) {
  71. $this->characterDataHandler =& $handler;
  72. } //xml_set_character_data_handler
  73. /**
  74. * Sets a reference to the handler for the CDATA Section event
  75. * @param mixed A reference to the CDATA Section handler
  76. */
  77. function xml_set_cdata_section_handler($handler) {
  78. $this->cDataSectionHandler =& $handler;
  79. } //xml_set_cdata_section_handler
  80. /**
  81. * Sets whether predefined entites should be replaced with their equivalent characters during parsing
  82. * @param boolean True if entity replacement is to occur
  83. */
  84. function convertEntities($truthVal) {
  85. $this->convertEntities = $truthVal;
  86. } //convertEntities
  87. /**
  88. * Appends an array of entity mappings to the existing translation table
  89. *
  90. * Intended mainly to facilitate the conversion of non-ASCII entities into equivalent characters
  91. *
  92. * @param array A list of entity mappings in the format: array('&amp;' => '&');
  93. */
  94. function appendEntityTranslationTable($table) {
  95. $this->definedEntities = $table;
  96. } //appendEntityTranslationTable
  97. /**
  98. * Gets the nth character from the end of the string
  99. * @param string The text to be queried
  100. * @param int The index from the end of the string
  101. * @return string The found character
  102. */
  103. function getCharFromEnd($text, $index) {
  104. $len = strlen($text);
  105. $char = $text{($len - 1 - $index)};
  106. return $char;
  107. } //getCharFromEnd
  108. /**
  109. * Parses the attributes string into an array of key / value pairs
  110. * @param string The attribute text
  111. * @return Array An array of key / value pairs
  112. */
  113. function parseAttributes($attrText) {
  114. $attrText = trim($attrText);
  115. $attrArray = array();
  116. $maybeEntity = false;
  117. $total = strlen($attrText);
  118. $keyDump = '';
  119. $valueDump = '';
  120. $currentState = SAXY_STATE_ATTR_NONE;
  121. $quoteType = '';
  122. for ($i = 0; $i < $total; $i++) {
  123. $currentChar = $attrText{$i};
  124. if ($currentState == SAXY_STATE_ATTR_NONE) {
  125. if (trim($currentChar != '')) {
  126. $currentState = SAXY_STATE_ATTR_KEY;
  127. }
  128. }
  129. switch ($currentChar) {
  130. case "\t":
  131. if ($currentState == SAXY_STATE_ATTR_VALUE) {
  132. $valueDump .= $currentChar;
  133. }
  134. else {
  135. $currentChar = '';
  136. }
  137. break;
  138. case "\x0B": //vertical tab
  139. case "\n":
  140. case "\r":
  141. $currentChar = '';
  142. break;
  143. case '=':
  144. if ($currentState == SAXY_STATE_ATTR_VALUE) {
  145. $valueDump .= $currentChar;
  146. }
  147. else {
  148. $currentState = SAXY_STATE_ATTR_VALUE;
  149. $quoteType = '';
  150. $maybeEntity = false;
  151. }
  152. break;
  153. case '"':
  154. if ($currentState == SAXY_STATE_ATTR_VALUE) {
  155. if ($quoteType == '') {
  156. $quoteType = '"';
  157. }
  158. else {
  159. if ($quoteType == $currentChar) {
  160. if ($this->convertEntities && $maybeEntity) {
  161. $valueDump = strtr($valueDump, $this->predefinedEntities);
  162. $valueDump = strtr($valueDump, $this->definedEntities);
  163. }
  164. $attrArray[trim($keyDump)] = $valueDump;
  165. $keyDump = $valueDump = $quoteType = '';
  166. $currentState = SAXY_STATE_ATTR_NONE;
  167. }
  168. else {
  169. $valueDump .= $currentChar;
  170. }
  171. }
  172. }
  173. break;
  174. case "'":
  175. if ($currentState == SAXY_STATE_ATTR_VALUE) {
  176. if ($quoteType == '') {
  177. $quoteType = "'";
  178. }
  179. else {
  180. if ($quoteType == $currentChar) {
  181. if ($this->convertEntities && $maybeEntity) {
  182. $valueDump = strtr($valueDump, $this->predefinedEntities);
  183. $valueDump = strtr($valueDump, $this->definedEntities);
  184. }
  185. $attrArray[trim($keyDump)] = $valueDump;
  186. $keyDump = $valueDump = $quoteType = '';
  187. $currentState = SAXY_STATE_ATTR_NONE;
  188. }
  189. else {
  190. $valueDump .= $currentChar;
  191. }
  192. }
  193. }
  194. break;
  195. case '&':
  196. //might be an entity
  197. $maybeEntity = true;
  198. $valueDump .= $currentChar;
  199. break;
  200. default:
  201. if ($currentState == SAXY_STATE_ATTR_KEY) {
  202. $keyDump .= $currentChar;
  203. }
  204. else {
  205. $valueDump .= $currentChar;
  206. }
  207. }
  208. }
  209. return $attrArray;
  210. } //parseAttributes
  211. /**
  212. * Parses character data
  213. * @param string The character data
  214. */
  215. function parseBetweenTags($betweenTagText) {
  216. if (trim($betweenTagText) != ''){
  217. $this->fireCharacterDataEvent($betweenTagText);
  218. }
  219. } //parseBetweenTags
  220. /**
  221. * Fires a start element event
  222. * @param string The start element tag name
  223. * @param Array The start element attributes
  224. */
  225. function fireStartElementEvent($tagName, $attributes) {
  226. call_user_func($this->startElementHandler, $this, $tagName, $attributes);
  227. } //fireStartElementEvent
  228. /**
  229. * Fires an end element event
  230. * @param string The end element tag name
  231. */
  232. function fireEndElementEvent($tagName) {
  233. call_user_func($this->endElementHandler, $this, $tagName);
  234. } //fireEndElementEvent
  235. /**
  236. * Fires a character data event
  237. * @param string The character data
  238. */
  239. function fireCharacterDataEvent($data) {
  240. if ($this->convertEntities && ((strpos($data, "&") != -1))) {
  241. $data = strtr($data, $this->predefinedEntities);
  242. $data = strtr($data, $this->definedEntities);
  243. }
  244. call_user_func($this->characterDataHandler, $this, $data);
  245. } //fireCharacterDataEvent
  246. /**
  247. * Fires a CDATA Section event
  248. * @param string The CDATA Section data
  249. */
  250. function fireCDataSectionEvent($data) {
  251. call_user_func($this->cDataSectionHandler, $this, $data);
  252. } //fireCDataSectionEvent
  253. } //SAXY_Parser_Base
  254. ?>