| [ Index ] |
|
Code source de Joomla 1.0.13 |
1 <?php 2 /** 3 * SAXY_Parser_Base is a base class for SAXY and SAXY Lite 4 * @package saxy-xmlparser 5 * @version 1.0 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 13 /** the initial characters of a cdata section */ 14 define('SAXY_SEARCH_CDATA', '![CDATA['); 15 /** the length of the initial characters of a cdata section */ 16 define('SAXY_CDATA_LEN', 8); 17 /** the initial characters of a notation */ 18 define('SAXY_SEARCH_NOTATION', '!NOTATION'); 19 /** the initial characters of a doctype */ 20 define('SAXY_SEARCH_DOCTYPE', '!DOCTYPE'); 21 /** saxy parse state, just before parsing an attribute */ 22 define('SAXY_STATE_ATTR_NONE', 0); 23 /** saxy parse state, parsing an attribute key */ 24 define('SAXY_STATE_ATTR_KEY', 1); 25 /** saxy parse state, parsing an attribute value */ 26 define('SAXY_STATE_ATTR_VALUE', 2); 27 28 /** 29 * The base SAX Parser class 30 * 31 * @package saxy-xmlparser 32 * @author John Heinstein <johnkarl@nbnet.nb.ca> 33 */ 34 class SAXY_Parser_Base { 35 /** @var int The current state of the parser */ 36 var $state; 37 /** @var int A temporary container for parsed characters */ 38 var $charContainer; 39 /** @var Object A reference to the start event handler */ 40 var $startElementHandler; 41 /** @var Object A reference to the end event handler */ 42 var $endElementHandler; 43 /** @var Object A reference to the data event handler */ 44 var $characterDataHandler; 45 /** @var Object A reference to the CDATA Section event handler */ 46 var $cDataSectionHandler = null; 47 /** @var boolean True if predefined entities are to be converted into characters */ 48 var $convertEntities = true; 49 /** @var Array Translation table for predefined entities */ 50 var $predefinedEntities = array('&' => '&', '<' => '<', '>' => '>', 51 '"' => '"', ''' => "'"); 52 /** @var Array User defined translation table for entities */ 53 var $definedEntities = array(); 54 /** @var boolean True if whitespace is to be preserved during parsing. NOT YET IMPLEMENTED! */ 55 var $preserveWhitespace = false; 56 57 58 /** 59 * Constructor for SAX parser 60 */ 61 function SAXY_Parser_Base() { 62 $this->charContainer = ''; 63 } //SAXY_Parser_Base 64 65 /** 66 * Sets a reference to the handler for the start element event 67 * @param mixed A reference to the start element handler 68 */ 69 function xml_set_element_handler($startHandler, $endHandler) { 70 $this->startElementHandler = $startHandler; 71 $this->endElementHandler = $endHandler; 72 } //xml_set_element_handler 73 74 /** 75 * Sets a reference to the handler for the data event 76 * @param mixed A reference to the data handler 77 */ 78 function xml_set_character_data_handler($handler) { 79 $this->characterDataHandler =& $handler; 80 } //xml_set_character_data_handler 81 82 /** 83 * Sets a reference to the handler for the CDATA Section event 84 * @param mixed A reference to the CDATA Section handler 85 */ 86 function xml_set_cdata_section_handler($handler) { 87 $this->cDataSectionHandler =& $handler; 88 } //xml_set_cdata_section_handler 89 90 /** 91 * Sets whether predefined entites should be replaced with their equivalent characters during parsing 92 * @param boolean True if entity replacement is to occur 93 */ 94 function convertEntities($truthVal) { 95 $this->convertEntities = $truthVal; 96 } //convertEntities 97 98 /** 99 * Appends an array of entity mappings to the existing translation table 100 * 101 * Intended mainly to facilitate the conversion of non-ASCII entities into equivalent characters 102 * 103 * @param array A list of entity mappings in the format: array('&' => '&'); 104 */ 105 function appendEntityTranslationTable($table) { 106 $this->definedEntities = $table; 107 } //appendEntityTranslationTable 108 109 110 /** 111 * Gets the nth character from the end of the string 112 * @param string The text to be queried 113 * @param int The index from the end of the string 114 * @return string The found character 115 */ 116 function getCharFromEnd($text, $index) { 117 $len = strlen($text); 118 $char = $text{($len - 1 - $index)}; 119 120 return $char; 121 } //getCharFromEnd 122 123 /** 124 * Parses the attributes string into an array of key / value pairs 125 * @param string The attribute text 126 * @return Array An array of key / value pairs 127 */ 128 function parseAttributes($attrText) { 129 $attrText = trim($attrText); 130 $attrArray = array(); 131 $maybeEntity = false; 132 133 $total = strlen($attrText); 134 $keyDump = ''; 135 $valueDump = ''; 136 $currentState = SAXY_STATE_ATTR_NONE; 137 $quoteType = ''; 138 139 for ($i = 0; $i < $total; $i++) { 140 $currentChar = $attrText{$i}; 141 142 if ($currentState == SAXY_STATE_ATTR_NONE) { 143 if (trim($currentChar != '')) { 144 $currentState = SAXY_STATE_ATTR_KEY; 145 } 146 } 147 148 switch ($currentChar) { 149 case "\t": 150 if ($currentState == SAXY_STATE_ATTR_VALUE) { 151 $valueDump .= $currentChar; 152 } 153 else { 154 $currentChar = ''; 155 } 156 break; 157 158 case "\x0B": //vertical tab 159 case "\n": 160 case "\r": 161 $currentChar = ''; 162 break; 163 164 case '=': 165 if ($currentState == SAXY_STATE_ATTR_VALUE) { 166 $valueDump .= $currentChar; 167 } 168 else { 169 $currentState = SAXY_STATE_ATTR_VALUE; 170 $quoteType = ''; 171 $maybeEntity = false; 172 } 173 break; 174 175 case '"': 176 if ($currentState == SAXY_STATE_ATTR_VALUE) { 177 if ($quoteType == '') { 178 $quoteType = '"'; 179 } 180 else { 181 if ($quoteType == $currentChar) { 182 // Joomla! hack 183 if (isset( $this ) && $this->convertEntities && $maybeEntity) { 184 $valueDump = strtr($valueDump, $this->predefinedEntities); 185 $valueDump = strtr($valueDump, $this->definedEntities); 186 } 187 188 $keyDump = trim($keyDump); 189 $attrArray[$keyDump] = $valueDump; 190 $keyDump = $valueDump = $quoteType = ''; 191 $currentState = SAXY_STATE_ATTR_NONE; 192 } 193 else { 194 $valueDump .= $currentChar; 195 } 196 } 197 } 198 break; 199 200 case "'": 201 if ($currentState == SAXY_STATE_ATTR_VALUE) { 202 if ($quoteType == '') { 203 $quoteType = "'"; 204 } 205 else { 206 if ($quoteType == $currentChar) { 207 // Joomla! hack 208 if (isset( $this ) && $this->convertEntities && $maybeEntity) { 209 $valueDump = strtr($valueDump, $this->predefinedEntities); 210 $valueDump = strtr($valueDump, $this->definedEntities); 211 } 212 213 $keyDump = trim($keyDump); 214 $attrArray[$keyDump] = $valueDump; 215 $keyDump = $valueDump = $quoteType = ''; 216 $currentState = SAXY_STATE_ATTR_NONE; 217 } 218 else { 219 $valueDump .= $currentChar; 220 } 221 } 222 } 223 break; 224 225 case '&': 226 //might be an entity 227 $maybeEntity = true; 228 $valueDump .= $currentChar; 229 break; 230 231 default: 232 if ($currentState == SAXY_STATE_ATTR_KEY) { 233 $keyDump .= $currentChar; 234 } 235 else { 236 $valueDump .= $currentChar; 237 } 238 } 239 } 240 241 return $attrArray; 242 } //parseAttributes 243 244 /** 245 * Parses character data 246 * @param string The character data 247 */ 248 function parseBetweenTags($betweenTagText) { 249 if (trim($betweenTagText) != ''){ 250 $this->fireCharacterDataEvent($betweenTagText); 251 } 252 } //parseBetweenTags 253 254 /** 255 * Fires a start element event 256 * @param string The start element tag name 257 * @param Array The start element attributes 258 */ 259 function fireStartElementEvent($tagName, $attributes) { 260 call_user_func($this->startElementHandler, $this, $tagName, $attributes); 261 } //fireStartElementEvent 262 263 /** 264 * Fires an end element event 265 * @param string The end element tag name 266 */ 267 function fireEndElementEvent($tagName) { 268 call_user_func($this->endElementHandler, $this, $tagName); 269 } //fireEndElementEvent 270 271 /** 272 * Fires a character data event 273 * @param string The character data 274 */ 275 function fireCharacterDataEvent($data) { 276 if ($this->convertEntities && ((strpos($data, "&") != -1))) { 277 $data = strtr($data, $this->predefinedEntities); 278 $data = strtr($data, $this->definedEntities); 279 } 280 281 call_user_func($this->characterDataHandler, $this, $data); 282 } //fireCharacterDataEvent 283 284 /** 285 * Fires a CDATA Section event 286 * @param string The CDATA Section data 287 */ 288 function fireCDataSectionEvent($data) { 289 call_user_func($this->cDataSectionHandler, $this, $data); 290 } //fireCDataSectionEvent 291 } //SAXY_Parser_Base 292 293 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Wed Nov 21 14:43:32 2007 | par Balluche grâce à PHPXref 0.7 |
|