[ Index ]
 

Code source de SPIP 1.9.2c

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/ecrire/safehtml/classes/ -> HTMLSax3.php (source)

   1  <?php
   2  // +----------------------------------------------------------------------+

   3  // | Copyright (c) 1997-2002 The PHP Group                                |

   4  // +----------------------------------------------------------------------+

   5  // | This source file is subject to version 2.02 of the PHP license,      |

   6  // | that is bundled with this package in the file LICENSE, and is        |

   7  // | available at through the world-wide-web at                           |

   8  // | http://www.php.net/license/3_0.txt.                                  |

   9  // | If you did not receive a copy of the PHP license and are unable to   |

  10  // | obtain it through the world-wide-web, please send a note to          |

  11  // | license@php.net so we can mail you a copy immediately.               |

  12  // +----------------------------------------------------------------------+

  13  // | Authors: Alexander Zhukov <alex@veresk.ru> Original port from Python |

  14  // | Authors: Harry Fuecks <hfuecks@phppatterns.com> Port to PEAR + more  |

  15  // | Authors: Many @ Sitepointforums Advanced PHP Forums                  |

  16  // +----------------------------------------------------------------------+

  17  //

  18  if (!defined('XML_HTMLSAX3')) {
  19   define('XML_HTMLSAX3', 'XML/');
  20  }
  21  require_once (XML_HTMLSAX3 . 'HTMLSax3/States.php');
  22  require_once (XML_HTMLSAX3 . 'HTMLSax3/Decorators.php');
  23  
  24  class XML_HTMLSax3_StateParser {
  25   var $htmlsax;
  26   var $handler_object_element;
  27   var $handler_method_opening;
  28   var $handler_method_closing;
  29   var $handler_object_data;
  30   var $handler_method_data;
  31   var $handler_object_pi;
  32   var $handler_method_pi;
  33   var $handler_object_jasp;
  34   var $handler_method_jasp;
  35   var $handler_object_escape;
  36   var $handler_method_escape;
  37   var $handler_default;
  38   var $parser_options = array();
  39   var $rawtext;
  40   var $position;
  41   var $length;
  42   var $State = array();
  43  
  44   function XML_HTMLSax3_StateParser (& $htmlsax) {
  45    $this->htmlsax = & $htmlsax;
  46    $this->State[XML_HTMLSAX3_STATE_START] =& new XML_HTMLSax3_StartingState();
  47  
  48    $this->State[XML_HTMLSAX3_STATE_CLOSING_TAG] =& new XML_HTMLSax3_ClosingTagState();
  49    $this->State[XML_HTMLSAX3_STATE_TAG] =& new XML_HTMLSax3_TagState();
  50    $this->State[XML_HTMLSAX3_STATE_OPENING_TAG] =& new XML_HTMLSax3_OpeningTagState();
  51  
  52    $this->State[XML_HTMLSAX3_STATE_PI] =& new XML_HTMLSax3_PiState();
  53    $this->State[XML_HTMLSAX3_STATE_JASP] =& new XML_HTMLSax3_JaspState();
  54    $this->State[XML_HTMLSAX3_STATE_ESCAPE] =& new XML_HTMLSax3_EscapeState();
  55   }
  56  
  57   function unscanCharacter() {
  58    $this->position -= 1;
  59   }
  60  
  61   function ignoreCharacter() {
  62    $this->position += 1;
  63   }
  64  
  65   function scanCharacter() {
  66    if ($this->position < $this->length) {
  67     return $this->rawtext{$this->position++};
  68    }
  69   }
  70  
  71   function scanUntilString($string) {
  72    $start = $this->position;
  73    $this->position = strpos($this->rawtext, $string, $start);
  74    if ($this->position === FALSE) {
  75     $this->position = $this->length;
  76    }
  77    return substr($this->rawtext, $start, $this->position - $start);
  78   }
  79  
  80   function scanUntilCharacters($string) {}
  81  
  82   function ignoreWhitespace() {}
  83  
  84   function parse($data) {
  85    if ($this->parser_options['XML_OPTION_TRIM_DATA_NODES']==1) {
  86     $decorator =& new XML_HTMLSax3_Trim(
  87      $this->handler_object_data,
  88      $this->handler_method_data);
  89     $this->handler_object_data =& $decorator;
  90     $this->handler_method_data = 'trimData';
  91    }
  92    if ($this->parser_options['XML_OPTION_CASE_FOLDING']==1) {
  93     $open_decor =& new XML_HTMLSax3_CaseFolding(
  94      $this->handler_object_element,
  95      $this->handler_method_opening,
  96      $this->handler_method_closing);
  97     $this->handler_object_element =& $open_decor;
  98     $this->handler_method_opening ='foldOpen';
  99     $this->handler_method_closing ='foldClose';
 100    }
 101    if ($this->parser_options['XML_OPTION_LINEFEED_BREAK']==1) {
 102     $decorator =& new XML_HTMLSax3_Linefeed(
 103      $this->handler_object_data,
 104      $this->handler_method_data);
 105     $this->handler_object_data =& $decorator;
 106     $this->handler_method_data = 'breakData';
 107    }
 108    if ($this->parser_options['XML_OPTION_TAB_BREAK']==1) {
 109     $decorator =& new XML_HTMLSax3_Tab(
 110      $this->handler_object_data,
 111      $this->handler_method_data);
 112     $this->handler_object_data =& $decorator;
 113     $this->handler_method_data = 'breakData';
 114    }
 115    if ($this->parser_options['XML_OPTION_ENTITIES_UNPARSED']==1) {
 116     $decorator =& new XML_HTMLSax3_Entities_Unparsed(
 117      $this->handler_object_data,
 118      $this->handler_method_data);
 119     $this->handler_object_data =& $decorator;
 120     $this->handler_method_data = 'breakData';
 121    }
 122    if ($this->parser_options['XML_OPTION_ENTITIES_PARSED']==1) {
 123     $decorator =& new XML_HTMLSax3_Entities_Parsed(
 124      $this->handler_object_data,
 125      $this->handler_method_data);
 126     $this->handler_object_data =& $decorator;
 127     $this->handler_method_data = 'breakData';
 128    }
 129    // Note switched on by default

 130    if ($this->parser_options['XML_OPTION_STRIP_ESCAPES']==1) {
 131     $decorator =& new XML_HTMLSax3_Escape_Stripper(
 132      $this->handler_object_escape,
 133      $this->handler_method_escape);
 134     $this->handler_object_escape =& $decorator;
 135     $this->handler_method_escape = 'strip';
 136    }
 137    $this->rawtext = $data;
 138    $this->length = strlen($data);
 139    $this->position = 0;
 140    $this->_parse();
 141   }
 142  
 143   function _parse($state = XML_HTMLSAX3_STATE_START) {
 144    do {
 145     $state = $this->State[$state]->parse($this);
 146    } while ($state != XML_HTMLSAX3_STATE_STOP &&
 147       $this->position < $this->length);
 148   }
 149  }
 150  
 151  class XML_HTMLSax3_StateParser_Lt430 extends XML_HTMLSax3_StateParser {
 152   function XML_HTMLSax3_StateParser_Lt430(& $htmlsax) {
 153    parent::XML_HTMLSax3_StateParser($htmlsax);
 154    $this->parser_options['XML_OPTION_TRIM_DATA_NODES'] = 0;
 155    $this->parser_options['XML_OPTION_CASE_FOLDING'] = 0;
 156    $this->parser_options['XML_OPTION_LINEFEED_BREAK'] = 0;
 157    $this->parser_options['XML_OPTION_TAB_BREAK'] = 0;
 158    $this->parser_options['XML_OPTION_ENTITIES_PARSED'] = 0;
 159    $this->parser_options['XML_OPTION_ENTITIES_UNPARSED'] = 0;
 160    $this->parser_options['XML_OPTION_STRIP_ESCAPES'] = 0;
 161   }
 162  
 163   function scanUntilCharacters($string) {
 164    $startpos = $this->position;
 165    while ($this->position < $this->length && strpos($string, $this->rawtext{$this->position}) === FALSE) {
 166     $this->position++;
 167    }
 168    return substr($this->rawtext, $startpos, $this->position - $startpos);
 169   }
 170  
 171   function ignoreWhitespace() {
 172    while ($this->position < $this->length && 
 173     strpos(" \n\r\t", $this->rawtext{$this->position}) !== FALSE) {
 174     $this->position++;
 175    }
 176   }
 177  
 178   function parse($data) {
 179    parent::parse($data);
 180   }
 181  }
 182  
 183  class XML_HTMLSax3_StateParser_Gtet430 extends XML_HTMLSax3_StateParser {
 184   function XML_HTMLSax3_StateParser_Gtet430(& $htmlsax) {
 185    parent::XML_HTMLSax3_StateParser($htmlsax);
 186    $this->parser_options['XML_OPTION_TRIM_DATA_NODES'] = 0;
 187    $this->parser_options['XML_OPTION_CASE_FOLDING'] = 0;
 188    $this->parser_options['XML_OPTION_LINEFEED_BREAK'] = 0;
 189    $this->parser_options['XML_OPTION_TAB_BREAK'] = 0;
 190    $this->parser_options['XML_OPTION_ENTITIES_PARSED'] = 0;
 191    $this->parser_options['XML_OPTION_ENTITIES_UNPARSED'] = 0;
 192    $this->parser_options['XML_OPTION_STRIP_ESCAPES'] = 0;
 193   }
 194   function scanUntilCharacters($string) {
 195    $startpos = $this->position;
 196    $length = strcspn($this->rawtext, $string, $startpos);
 197    $this->position += $length;
 198    return substr($this->rawtext, $startpos, $length);
 199   }
 200  
 201   function ignoreWhitespace() {
 202    $this->position += strspn($this->rawtext, " \n\r\t", $this->position);
 203   }
 204  
 205   function parse($data) {
 206    parent::parse($data);
 207   }
 208  }
 209  
 210  class XML_HTMLSax3_NullHandler {
 211   function DoNothing() {
 212   }
 213  }
 214  
 215  class XML_HTMLSax3 {
 216   var $state_parser;
 217  
 218   function XML_HTMLSax3() {
 219    if (version_compare(phpversion(), '4.3', 'ge')) {
 220     $this->state_parser =& new XML_HTMLSax3_StateParser_Gtet430($this);
 221    } else {
 222     $this->state_parser =& new XML_HTMLSax3_StateParser_Lt430($this);
 223    }
 224    $nullhandler =& new XML_HTMLSax3_NullHandler();
 225    $this->set_object($nullhandler);
 226    $this->set_element_handler('DoNothing', 'DoNothing');
 227    $this->set_data_handler('DoNothing');
 228    $this->set_pi_handler('DoNothing');
 229    $this->set_jasp_handler('DoNothing');
 230    $this->set_escape_handler('DoNothing');
 231   }
 232  
 233   function set_object(&$object) {
 234    if ( is_object($object) ) {
 235     $this->state_parser->handler_default =& $object;
 236     return true;
 237    } else {
 238     require_once('PEAR.php');
 239     PEAR::raiseError('XML_HTMLSax3::set_object requires '.
 240      'an object instance');
 241    }
 242   }
 243  
 244   function set_option($name, $value=1) {
 245    if ( array_key_exists($name,$this->state_parser->parser_options) ) {
 246     $this->state_parser->parser_options[$name] = $value;
 247     return true;
 248    } else {
 249     require_once('PEAR.php');
 250     PEAR::raiseError('XML_HTMLSax3::set_option('.$name.') illegal');
 251    }
 252   }
 253  
 254   function set_data_handler($data_method) {
 255    $this->state_parser->handler_object_data =& $this->state_parser->handler_default;
 256    $this->state_parser->handler_method_data = $data_method;
 257   }
 258  
 259   function set_element_handler($opening_method, $closing_method) {
 260    $this->state_parser->handler_object_element =& $this->state_parser->handler_default;
 261    $this->state_parser->handler_method_opening = $opening_method;
 262    $this->state_parser->handler_method_closing = $closing_method;
 263   }
 264  
 265   function set_pi_handler($pi_method) {
 266    $this->state_parser->handler_object_pi =& $this->state_parser->handler_default;
 267    $this->state_parser->handler_method_pi = $pi_method;
 268   }
 269  
 270   function set_escape_handler($escape_method) {
 271    $this->state_parser->handler_object_escape =& $this->state_parser->handler_default;
 272    $this->state_parser->handler_method_escape = $escape_method;
 273   }
 274  
 275   function set_jasp_handler ($jasp_method) {
 276    $this->state_parser->handler_object_jasp =& $this->state_parser->handler_default;
 277    $this->state_parser->handler_method_jasp = $jasp_method;
 278   }
 279  
 280   function get_current_position() {
 281    return $this->state_parser->position;
 282   }
 283  
 284   function get_length() {
 285    return $this->state_parser->length;
 286   }
 287  
 288   function parse($data) {
 289    $this->state_parser->parse($data);
 290   }
 291  }
 292  ?>


Généré le : Wed Nov 21 10:20:27 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics