PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/Quản lý website tuyển dụng việc làm PHP/webcaoca/public_html/vlv/libraries/domit/xml_saxy_lite_parser.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 243 lines | 170 code | 26 blank | 47 comment | 38 complexity | 5f0a6c53900eff81585f7fa64bb5d71b MD5 | raw file
  1. <?php
  2. /**
  3. * SAXY Lite is a non-validating, but lightweight and fast SAX parser for PHP, modelled on the Expat parser
  4. * @package saxy-xmlparser
  5. * @subpackage saxy-xmlparser-lite
  6. * @version 1.0
  7. * @copyright (C) 2004 John Heinstein. All rights reserved
  8. * @license http://www.gnu.org/copyleft/lesser.html LGPL License
  9. * @author John Heinstein <johnkarl@nbnet.nb.ca>
  10. * @link http://www.engageinteractive.com/saxy/ SAXY Home Page
  11. * SAXY is Free Software
  12. **/
  13. if (!defined('SAXY_INCLUDE_PATH')) {
  14. define('SAXY_INCLUDE_PATH', (dirname(__FILE__) . "/"));
  15. }
  16. /** current version of SAXY Lite */
  17. define ('SAXY_LITE_VERSION', '1.0');
  18. /** initial saxy lite parse state, before anything is encountered */
  19. define('SAXY_STATE_NONE', 0);
  20. /** saxy lite parse state, processing main document */
  21. define('SAXY_STATE_PARSING', 1);
  22. require_once(SAXY_INCLUDE_PATH . 'xml_saxy_shared.php');
  23. /**
  24. * The SAX Parser class
  25. *
  26. * @package saxy-xmlparser
  27. * @subpackage saxy-xmlparser-lite
  28. * @author John Heinstein <johnkarl@nbnet.nb.ca>
  29. */
  30. class SAXY_Lite_Parser extends SAXY_Parser_Base {
  31. /**
  32. * Constructor for SAX parser
  33. */
  34. function SAXY_Lite_Parser() {
  35. $this->SAXY_Parser_Base();
  36. $this->state = SAXY_STATE_NONE;
  37. } //SAXY_Lite_Parser
  38. /**
  39. * Returns the current version of SAXY Lite
  40. * @return Object The current version of SAXY Lite
  41. */
  42. function getVersion() {
  43. return SAXY_LITE_VERSION;
  44. } //getVersion
  45. /**
  46. * Processes the xml prolog, doctype, and any other nodes that exist outside of the main xml document
  47. * @param string The xml text to be processed
  48. * @return string The preprocessed xml text
  49. */
  50. function preprocessXML($xmlText) {
  51. //strip prolog
  52. $xmlText = trim($xmlText);
  53. $total = strlen($xmlText);
  54. for ($i = 0; $i < $total; $i++) {
  55. // if ($xmlText{$i} == '<') {
  56. if (substr($xmlText, $i, 1) == '<') {
  57. switch ($xmlText{($i + 1)}) {
  58. case '?':
  59. case '!':
  60. break;
  61. default:
  62. $this->state = SAXY_STATE_PARSING;
  63. return (substr($xmlText, $i));
  64. }
  65. }
  66. }
  67. } //preprocessXML
  68. /**
  69. * The controlling method for the parsing process
  70. * @param string The xml text to be processed
  71. * @return boolean True if parsing is successful
  72. */
  73. function parse ($xmlText) {
  74. $xmlText = $this->preprocessXML($xmlText);
  75. $total = strlen($xmlText);
  76. for ($i = 0; $i < $total; $i++) {
  77. // $currentChar = $xmlText{$i};
  78. $currentChar = substr($xmlText, $i, 1);
  79. switch ($this->state) {
  80. case SAXY_STATE_PARSING:
  81. switch ($currentChar) {
  82. case '<':
  83. if (substr($this->charContainer, 0, SAXY_CDATA_LEN) == SAXY_SEARCH_CDATA) {
  84. $this->charContainer .= $currentChar;
  85. }
  86. else {
  87. $this->parseBetweenTags($this->charContainer);
  88. $this->charContainer = '';
  89. }
  90. break;
  91. case '>':
  92. if ((substr($this->charContainer, 0, SAXY_CDATA_LEN) == SAXY_SEARCH_CDATA) &&
  93. !(($this->getCharFromEnd($this->charContainer, 0) == ']') &&
  94. ($this->getCharFromEnd($this->charContainer, 1) == ']'))) {
  95. $this->charContainer .= $currentChar;
  96. }
  97. else {
  98. $this->parseTag($this->charContainer);
  99. $this->charContainer = '';
  100. }
  101. break;
  102. default:
  103. $this->charContainer .= $currentChar;
  104. }
  105. break;
  106. }
  107. }
  108. return true;
  109. } //parse
  110. /**
  111. * Parses an element tag
  112. * @param string The interior text of the element tag
  113. */
  114. function parseTag($tagText) {
  115. $tagText = trim($tagText);
  116. $firstChar = $tagText{0};
  117. $myAttributes = array();
  118. switch ($firstChar) {
  119. case '/':
  120. $tagName = substr($tagText, 1);
  121. $this->fireEndElementEvent($tagName);
  122. break;
  123. case '!':
  124. $upperCaseTagText = strtoupper($tagText);
  125. if (strpos($upperCaseTagText, SAXY_SEARCH_CDATA) !== false) { //CDATA Section
  126. $total = strlen($tagText);
  127. $openBraceCount = 0;
  128. $textNodeText = '';
  129. for ($i = 0; $i < $total; $i++) {
  130. // $currentChar = $tagText{$i};
  131. $currentChar = substr($tagText, $i, 1);
  132. if (($currentChar == ']') && ($tagText{($i + 1)} == ']')) {
  133. break;
  134. }
  135. else if ($openBraceCount > 1) {
  136. $textNodeText .= $currentChar;
  137. }
  138. else if ($currentChar == '[') { //this won't be reached after the first open brace is found
  139. $openBraceCount ++;
  140. }
  141. }
  142. if ($this->cDataSectionHandler == null) {
  143. $this->fireCharacterDataEvent($textNodeText);
  144. }
  145. else {
  146. $this->fireCDataSectionEvent($textNodeText);
  147. }
  148. }
  149. else if (strpos($upperCaseTagText, SAXY_SEARCH_NOTATION) !== false) { //NOTATION node, discard
  150. return;
  151. }
  152. else if (substr($tagText, 0, 2) == '!-') { //comment node, discard
  153. return;
  154. }
  155. break;
  156. case '?':
  157. //Processing Instruction node, discard
  158. return;
  159. default:
  160. if ((strpos($tagText, '"') !== false) || (strpos($tagText, "'") !== false)) {
  161. $total = strlen($tagText);
  162. $tagName = '';
  163. for ($i = 0; $i < $total; $i++) {
  164. // $currentChar = $tagText{$i};
  165. $currentChar = substr($tagText, $i, 1);
  166. if (($currentChar == ' ') || ($currentChar == "\t") ||
  167. ($currentChar == "\n") || ($currentChar == "\r") ||
  168. ($currentChar == "\x0B")) {
  169. $myAttributes = $this->parseAttributes(substr($tagText, $i));
  170. break;
  171. }
  172. else {
  173. $tagName .= $currentChar;
  174. }
  175. }
  176. if (strrpos($tagText, '/') == (strlen($tagText) - 1)) { //check $tagText, but send $tagName
  177. $this->fireStartElementEvent($tagName, $myAttributes);
  178. $this->fireEndElementEvent($tagName);
  179. }
  180. else {
  181. $this->fireStartElementEvent($tagName, $myAttributes);
  182. }
  183. }
  184. else {
  185. if (strpos($tagText, '/') !== false) {
  186. $tagText = trim(substr($tagText, 0, (strrchr($tagText, '/') - 1)));
  187. $this->fireStartElementEvent($tagText, $myAttributes);
  188. $this->fireEndElementEvent($tagText);
  189. }
  190. else {
  191. $this->fireStartElementEvent($tagText, $myAttributes);
  192. }
  193. }
  194. }
  195. } //parseTag
  196. /**
  197. * Returns the current error code (non-functional for SAXY Lite)
  198. * @return int The current error code
  199. */
  200. function xml_get_error_code() {
  201. return -1;
  202. } //xml_get_error_code
  203. /**
  204. * Returns a textual description of the error code (non-functional for SAXY Lite)
  205. * @param int The error code
  206. * @return string The error message
  207. */
  208. function xml_error_string($code) {
  209. return "";
  210. } //xml_error_string
  211. } //SAXY_Lite_Parser
  212. ?>