2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
4 // +----------------------------------------------------------------------+
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 1997-2002 The PHP Group |
8 // +----------------------------------------------------------------------+
9 // | This source file is subject toversion 3.0 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 // +----------------------------------------------------------------------+
22 // $Id: HTMLSax3.php,v 1.2 2007/10/29 21:41:34 hfuecks Exp $
25 * Main parser components
26 * @package XML_HTMLSax3
27 * @version $Id: HTMLSax3.php,v 1.2 2007/10/29 21:41:34 hfuecks Exp $
32 if (!defined('XML_HTMLSAX3')) {
33 define('XML_HTMLSAX3', 'XML/');
35 require_once(XML_HTMLSAX3
. 'HTMLSax3/States.php');
36 require_once(XML_HTMLSAX3
. 'HTMLSax3/Decorators.php');
40 * @package XML_HTMLSax3
44 class XML_HTMLSax3_StateParser
{
46 * Instance of user front end class to be passed to callbacks
52 * User defined object for handling elements
56 var $handler_object_element;
58 * User defined open tag handler method
62 var $handler_method_opening;
64 * User defined close tag handler method
68 var $handler_method_closing;
70 * User defined object for handling data in elements
74 var $handler_object_data;
76 * User defined data handler method
80 var $handler_method_data;
82 * User defined object for handling processing instructions
86 var $handler_object_pi;
88 * User defined processing instruction handler method
92 var $handler_method_pi;
94 * User defined object for handling JSP/ASP tags
98 var $handler_object_jasp;
100 * User defined JSP/ASP handler method
104 var $handler_method_jasp;
106 * User defined object for handling XML escapes
110 var $handler_object_escape;
112 * User defined XML escape handler method
116 var $handler_method_escape;
118 * User defined handler object or NullHandler
122 var $handler_default;
124 * Parser options determining parsing behavior
128 var $parser_options = [];
130 * XML document being parsed
136 * Position in XML document relative to start (0)
142 * Length of the XML document in characters
148 * Array of state objects
155 * Constructs XML_HTMLSax3_StateParser setting up states
156 * @var XML_HTMLSax3 instance of user front end class
159 function __construct (& $htmlsax) {
160 $this->htmlsax
= & $htmlsax;
161 $this->State
[XML_HTMLSAX3_STATE_START
] = new XML_HTMLSax3_StartingState();
163 $this->State
[XML_HTMLSAX3_STATE_CLOSING_TAG
] = new XML_HTMLSax3_ClosingTagState();
164 $this->State
[XML_HTMLSAX3_STATE_TAG
] = new XML_HTMLSax3_TagState();
165 $this->State
[XML_HTMLSAX3_STATE_OPENING_TAG
] = new XML_HTMLSax3_OpeningTagState();
167 $this->State
[XML_HTMLSAX3_STATE_PI
] = new XML_HTMLSax3_PiState();
168 $this->State
[XML_HTMLSAX3_STATE_JASP
] = new XML_HTMLSax3_JaspState();
169 $this->State
[XML_HTMLSAX3_STATE_ESCAPE
] = new XML_HTMLSax3_EscapeState();
173 * Moves the position back one character
177 function unscanCharacter() {
178 $this->position
-= 1;
182 * Moves the position forward one character
186 function ignoreCharacter() {
187 $this->position +
= 1;
191 * Returns the next character from the XML document or void if at end
195 function scanCharacter() {
196 if ($this->position
< $this->length
) {
197 return $this->rawtext
[$this->position++
];
202 * Returns a string from the current position to the next occurance
203 * of the supplied string
204 * @param string string to search until
208 function scanUntilString($string) {
209 $start = $this->position
;
210 $this->position
= strpos($this->rawtext
, $string, $start);
211 if ($this->position
=== FALSE) {
212 $this->position
= $this->length
;
214 return substr($this->rawtext
, $start, $this->position
- $start);
218 * Returns a string from the current position until the first instance of
219 * one of the characters in the supplied string argument
220 * @param string string to search until
225 function scanUntilCharacters($string) {}
228 * Moves the position forward past any whitespace characters
233 function ignoreWhitespace() {}
236 * Begins the parsing operation, setting up any decorators, depending on
237 * parse options invoking _parse() to execute parsing
238 * @param string XML document to parse
242 function parse($data) {
243 if ($this->parser_options
['XML_OPTION_TRIM_DATA_NODES']==1) {
244 $decorator = new XML_HTMLSax3_Trim(
245 $this->handler_object_data
,
246 $this->handler_method_data
);
247 $this->handler_object_data
=& $decorator;
248 $this->handler_method_data
= 'trimData';
250 if ($this->parser_options
['XML_OPTION_CASE_FOLDING']==1) {
251 $open_decor = new XML_HTMLSax3_CaseFolding(
252 $this->handler_object_element
,
253 $this->handler_method_opening
,
254 $this->handler_method_closing
);
255 $this->handler_object_element
=& $open_decor;
256 $this->handler_method_opening
='foldOpen';
257 $this->handler_method_closing
='foldClose';
259 if ($this->parser_options
['XML_OPTION_LINEFEED_BREAK']==1) {
260 $decorator = new XML_HTMLSax3_Linefeed(
261 $this->handler_object_data
,
262 $this->handler_method_data
);
263 $this->handler_object_data
=& $decorator;
264 $this->handler_method_data
= 'breakData';
266 if ($this->parser_options
['XML_OPTION_TAB_BREAK']==1) {
267 $decorator = new XML_HTMLSax3_Tab(
268 $this->handler_object_data
,
269 $this->handler_method_data
);
270 $this->handler_object_data
=& $decorator;
271 $this->handler_method_data
= 'breakData';
273 if ($this->parser_options
['XML_OPTION_ENTITIES_UNPARSED']==1) {
274 $decorator = new XML_HTMLSax3_Entities_Unparsed(
275 $this->handler_object_data
,
276 $this->handler_method_data
);
277 $this->handler_object_data
=& $decorator;
278 $this->handler_method_data
= 'breakData';
280 if ($this->parser_options
['XML_OPTION_ENTITIES_PARSED']==1) {
281 $decorator = new XML_HTMLSax3_Entities_Parsed(
282 $this->handler_object_data
,
283 $this->handler_method_data
);
284 $this->handler_object_data
=& $decorator;
285 $this->handler_method_data
= 'breakData';
287 // Note switched on by default
288 if ($this->parser_options
['XML_OPTION_STRIP_ESCAPES']==1) {
289 $decorator = new XML_HTMLSax3_Escape_Stripper(
290 $this->handler_object_escape
,
291 $this->handler_method_escape
);
292 $this->handler_object_escape
=& $decorator;
293 $this->handler_method_escape
= 'strip';
295 $this->rawtext
= $data;
296 $this->length
= strlen($data);
302 * Performs the parsing itself, delegating calls to a specific parser
304 * @param constant state object to parse with
308 function _parse($state = XML_HTMLSAX3_STATE_START
) {
310 $state = $this->State
[$state]->parse($this);
311 } while ($state != XML_HTMLSAX3_STATE_STOP
&&
312 $this->position
< $this->length
);
317 * Parser for PHP Versions equal to or greater than 4.3.0. Uses a faster
318 * parsing mechanism than the equivalent PHP < 4.3.0 subclass of StateParser
319 * @package XML_HTMLSax3
322 class XML_HTMLSax3_StateParser_Gtet430
extends XML_HTMLSax3_StateParser
{
324 * Constructs XML_HTMLSax3_StateParser_Gtet430 defining available
326 * @var XML_HTMLSax3 instance of user front end class
329 function __construct(& $htmlsax) {
330 parent
::__construct($htmlsax);
331 $this->parser_options
['XML_OPTION_TRIM_DATA_NODES'] = 0;
332 $this->parser_options
['XML_OPTION_CASE_FOLDING'] = 0;
333 $this->parser_options
['XML_OPTION_LINEFEED_BREAK'] = 0;
334 $this->parser_options
['XML_OPTION_TAB_BREAK'] = 0;
335 $this->parser_options
['XML_OPTION_ENTITIES_PARSED'] = 0;
336 $this->parser_options
['XML_OPTION_ENTITIES_UNPARSED'] = 0;
337 $this->parser_options
['XML_OPTION_STRIP_ESCAPES'] = 0;
340 * Returns a string from the current position until the first instance of
341 * one of the characters in the supplied string argument.
342 * @param string string to search until
346 function scanUntilCharacters($string) {
347 $startpos = $this->position
;
348 $length = strcspn($this->rawtext
, $string, $startpos);
349 $this->position +
= $length;
350 return substr($this->rawtext
, $startpos, $length);
354 * Moves the position forward past any whitespace characters
358 function ignoreWhitespace() {
359 $this->position +
= strspn($this->rawtext
, " \n\r\t", $this->position
);
363 * Begins the parsing operation, setting up the parsed and unparsed
364 * XML entity decorators if necessary then delegating further work
366 * @param string XML document to parse
370 function parse($data) {
371 parent
::parse($data);
376 * Default NullHandler for methods which were not set by user
377 * @package XML_HTMLSax3
380 class XML_HTMLSax3_NullHandler
{
382 * Generic handler method which does nothing
386 function DoNothing() {
391 * User interface class. All user calls should only be made to this class
392 * @package XML_HTMLSax3
397 * Instance of concrete subclass of XML_HTMLSax3_StateParser
398 * @var XML_HTMLSax3_StateParser
404 * Constructs XML_HTMLSax3 selecting concrete StateParser subclass
405 * depending on PHP version being used as well as setting the default
406 * NullHandler for all callbacks<br />
409 * $myHandler = new MyHandler();
410 * $parser = new XML_HTMLSax3();
411 * $parser->set_object($myHandler);
412 * $parser->set_option('XML_OPTION_CASE_FOLDING');
413 * $parser->set_element_handler('myOpenHandler','myCloseHandler');
414 * $parser->set_data_handler('myDataHandler');
415 * $parser->parser($xml);
419 function __construct() {
420 $this->state_parser
= new XML_HTMLSax3_StateParser_Gtet430($this);
421 $nullhandler = new XML_HTMLSax3_NullHandler();
422 $this->set_object($nullhandler);
423 $this->set_element_handler('DoNothing', 'DoNothing');
424 $this->set_data_handler('DoNothing');
425 $this->set_pi_handler('DoNothing');
426 $this->set_jasp_handler('DoNothing');
427 $this->set_escape_handler('DoNothing');
431 * Sets the user defined handler object. Returns a PEAR Error
432 * if supplied argument is not an object.
433 * @param object handler object containing SAX callback methods
437 function set_object(&$object) {
438 if ( is_object($object) ) {
439 $this->state_parser
->handler_default
=& $object;
442 require_once('PEAR.php');
443 PEAR
::raiseError('XML_HTMLSax3::set_object requires '.
444 'an object instance');
449 * Sets a parser option. By default all options are switched off.
450 * Returns a PEAR Error if option is invalid<br />
451 * <b>Available options:</b>
453 * <li>XML_OPTION_TRIM_DATA_NODES: trim whitespace off the beginning
454 * and end of data passed to the data handler</li>
455 * <li>XML_OPTION_LINEFEED_BREAK: linefeeds result in additional data
457 * <li>XML_OPTION_TAB_BREAK: tabs result in additional data handler
459 * <li>XML_OPTION_ENTITIES_UNPARSED: XML entities are returned as
460 * seperate data handler calls in unparsed form</li>
461 * <li>XML_OPTION_ENTITIES_PARSED: (PHP 4.3.0+ only) XML entities are
462 * returned as seperate data handler calls and are parsed with
463 * PHP's html_entity_decode() function</li>
464 * <li>XML_OPTION_STRIP_ESCAPES: strips out the -- -- comment markers
465 * or CDATA markup inside an XML escape, if found.</li>
467 * To get HTMLSax to behave in the same way as the native PHP SAX parser,
468 * using it's default state, you need to switch on XML_OPTION_LINEFEED_BREAK,
469 * XML_OPTION_ENTITIES_PARSED and XML_OPTION_CASE_FOLDING
470 * @param string name of parser option
471 * @param int (optional) 1 to switch on, 0 for off
475 function set_option($name, $value=1) {
476 if ( array_key_exists($name,$this->state_parser
->parser_options
) ) {
477 $this->state_parser
->parser_options
[$name] = $value;
480 require_once('PEAR.php');
481 PEAR
::raiseError('XML_HTMLSax3::set_option('.$name.') illegal');
486 * Sets the data handler method which deals with the contents of XML
488 * The handler method must accept two arguments, the first being an
489 * instance of XML_HTMLSax3 and the second being the contents of an
492 * function myDataHander(& $parser,$data){}
494 * @param string name of method
499 function set_data_handler($data_method) {
500 $this->state_parser
->handler_object_data
=& $this->state_parser
->handler_default
;
501 $this->state_parser
->handler_method_data
= $data_method;
505 * Sets the open and close tag handlers
506 * <br />The open handler method must accept three arguments; the parser,
507 * the tag name and an array of attributes e.g.
509 * function myOpenHander(& $parser,$tagname,$attrs=array()){}
511 * The close handler method must accept two arguments; the parser and
514 * function myCloseHander(& $parser,$tagname){}
516 * @param string name of open method
517 * @param string name of close method
522 function set_element_handler($opening_method, $closing_method) {
523 $this->state_parser
->handler_object_element
=& $this->state_parser
->handler_default
;
524 $this->state_parser
->handler_method_opening
= $opening_method;
525 $this->state_parser
->handler_method_closing
= $closing_method;
529 * Sets the processing instruction handler method e.g. for PHP open
530 * and close tags<br />
531 * The handler method must accept three arguments; the parser, the
532 * PI target and data inside the PI
534 * function myPIHander(& $parser,$target, $data){}
536 * @param string name of method
541 function set_pi_handler($pi_method) {
542 $this->state_parser
->handler_object_pi
=& $this->state_parser
->handler_default
;
543 $this->state_parser
->handler_method_pi
= $pi_method;
547 * Sets the XML escape handler method e.g. for comments and doctype
549 * The handler method must accept two arguments; the parser and the
550 * contents of the escaped section
552 * function myEscapeHander(& $parser, $data){}
554 * @param string name of method
559 function set_escape_handler($escape_method) {
560 $this->state_parser
->handler_object_escape
=& $this->state_parser
->handler_default
;
561 $this->state_parser
->handler_method_escape
= $escape_method;
565 * Sets the JSP/ASP markup handler<br />
566 * The handler method must accept two arguments; the parser and
567 * body of the JASP tag
569 * function myJaspHander(& $parser, $data){}
571 * @param string name of method
576 function set_jasp_handler ($jasp_method) {
577 $this->state_parser
->handler_object_jasp
=& $this->state_parser
->handler_default
;
578 $this->state_parser
->handler_method_jasp
= $jasp_method;
582 * Returns the current string position of the "cursor" inside the XML
584 * <br />Intended for use from within a user defined handler called
585 * via the $parser reference e.g.
587 * function myDataHandler(& $parser,$data) {
588 * echo( 'Current position: '.$parser->get_current_position() );
595 function get_current_position() {
596 return $this->state_parser
->position
;
600 * Returns the string length of the XML document being parsed
604 function get_length() {
605 return $this->state_parser
->length
;
609 * Start parsing some XML
610 * @param string XML document
614 function parse($data) {
615 $this->state_parser
->parse($data);