1
|
|
|
<?php |
2
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
3
|
|
|
// |
4
|
|
|
// +----------------------------------------------------------------------+ |
5
|
|
|
// | PHP Version 4 | |
6
|
|
|
// +----------------------------------------------------------------------+ |
7
|
|
|
// | Copyright (c) 1997-2002 The PHP Group | |
8
|
|
|
// +----------------------------------------------------------------------+ |
9
|
|
|
// | This source file is subject to version 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
|
|
|
// | [email protected] so we can mail you a copy immediately. | |
16
|
|
|
// +----------------------------------------------------------------------+ |
17
|
|
|
// | Authors: Alexander Zhukov <[email protected]> Original port from Python | |
18
|
|
|
// | Authors: Harry Fuecks <[email protected]> Port to PEAR + more | |
19
|
|
|
// | Authors: Many @ Sitepointforums Advanced PHP Forums | |
20
|
|
|
// +----------------------------------------------------------------------+ |
21
|
|
|
// |
22
|
|
|
|
23
|
|
|
// |
24
|
|
|
/** |
25
|
|
|
* Main parser components |
26
|
|
|
* @package XML_HTMLSax3 |
27
|
|
|
|
28
|
|
|
*/ |
29
|
|
|
/** |
30
|
|
|
* Required classes |
31
|
|
|
*/ |
32
|
|
|
if (!defined('XML_HTMLSAX3')) { |
33
|
|
|
define('XML_HTMLSAX3', 'include/Pear/XML_HTMLSax3/'); |
34
|
|
|
} |
35
|
|
|
require_once(XML_HTMLSAX3 . 'HTMLSax3/States.php'); |
36
|
|
|
require_once(XML_HTMLSAX3 . 'HTMLSax3/Decorators.php'); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Base State Parser |
40
|
|
|
* @package XML_HTMLSax3 |
41
|
|
|
* @access protected |
42
|
|
|
* @abstract |
43
|
|
|
*/ |
44
|
|
|
class XML_HTMLSax3_StateParser { |
45
|
|
|
/** |
46
|
|
|
* Instance of user front end class to be passed to callbacks |
47
|
|
|
* @var XML_HTMLSax3 |
48
|
|
|
* @access private |
49
|
|
|
*/ |
50
|
|
|
var $htmlsax; |
51
|
|
|
/** |
52
|
|
|
* User defined object for handling elements |
53
|
|
|
* @var object |
54
|
|
|
* @access private |
55
|
|
|
*/ |
56
|
|
|
var $handler_object_element; |
57
|
|
|
/** |
58
|
|
|
* User defined open tag handler method |
59
|
|
|
* @var string |
60
|
|
|
* @access private |
61
|
|
|
*/ |
62
|
|
|
var $handler_method_opening; |
63
|
|
|
/** |
64
|
|
|
* User defined close tag handler method |
65
|
|
|
* @var string |
66
|
|
|
* @access private |
67
|
|
|
*/ |
68
|
|
|
var $handler_method_closing; |
69
|
|
|
/** |
70
|
|
|
* User defined object for handling data in elements |
71
|
|
|
* @var object |
72
|
|
|
* @access private |
73
|
|
|
*/ |
74
|
|
|
var $handler_object_data; |
75
|
|
|
/** |
76
|
|
|
* User defined data handler method |
77
|
|
|
* @var string |
78
|
|
|
* @access private |
79
|
|
|
*/ |
80
|
|
|
var $handler_method_data; |
81
|
|
|
/** |
82
|
|
|
* User defined object for handling processing instructions |
83
|
|
|
* @var object |
84
|
|
|
* @access private |
85
|
|
|
*/ |
86
|
|
|
var $handler_object_pi; |
87
|
|
|
/** |
88
|
|
|
* User defined processing instruction handler method |
89
|
|
|
* @var string |
90
|
|
|
* @access private |
91
|
|
|
*/ |
92
|
|
|
var $handler_method_pi; |
93
|
|
|
/** |
94
|
|
|
* User defined object for handling JSP/ASP tags |
95
|
|
|
* @var object |
96
|
|
|
* @access private |
97
|
|
|
*/ |
98
|
|
|
var $handler_object_jasp; |
99
|
|
|
/** |
100
|
|
|
* User defined JSP/ASP handler method |
101
|
|
|
* @var string |
102
|
|
|
* @access private |
103
|
|
|
*/ |
104
|
|
|
var $handler_method_jasp; |
105
|
|
|
/** |
106
|
|
|
* User defined object for handling XML escapes |
107
|
|
|
* @var object |
108
|
|
|
* @access private |
109
|
|
|
*/ |
110
|
|
|
var $handler_object_escape; |
111
|
|
|
/** |
112
|
|
|
* User defined XML escape handler method |
113
|
|
|
* @var string |
114
|
|
|
* @access private |
115
|
|
|
*/ |
116
|
|
|
var $handler_method_escape; |
117
|
|
|
/** |
118
|
|
|
* User defined handler object or NullHandler |
119
|
|
|
* @var object |
120
|
|
|
* @access private |
121
|
|
|
*/ |
122
|
|
|
var $handler_default; |
123
|
|
|
/** |
124
|
|
|
* Parser options determining parsing behavior |
125
|
|
|
* @var array |
126
|
|
|
* @access private |
127
|
|
|
*/ |
128
|
|
|
var $parser_options = array(); |
129
|
|
|
/** |
130
|
|
|
* XML document being parsed |
131
|
|
|
* @var string |
132
|
|
|
* @access private |
133
|
|
|
*/ |
134
|
|
|
var $rawtext; |
135
|
|
|
/** |
136
|
|
|
* Position in XML document relative to start (0) |
137
|
|
|
* @var int |
138
|
|
|
* @access private |
139
|
|
|
*/ |
140
|
|
|
var $position; |
141
|
|
|
/** |
142
|
|
|
* Length of the XML document in characters |
143
|
|
|
* @var int |
144
|
|
|
* @access private |
145
|
|
|
*/ |
146
|
|
|
var $length; |
147
|
|
|
/** |
148
|
|
|
* Array of state objects |
149
|
|
|
* @var array |
150
|
|
|
* @access private |
151
|
|
|
*/ |
152
|
|
|
var $State = array(); |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Constructs XML_HTMLSax3_StateParser setting up states |
156
|
|
|
* @var XML_HTMLSax3 instance of user front end class |
157
|
|
|
* @access protected |
158
|
|
|
*/ |
159
|
|
|
function __construct (& $htmlsax) { |
160
|
|
|
$this->htmlsax = & $htmlsax; |
161
|
|
|
$this->State[XML_HTMLSAX3_STATE_START] = new XML_HTMLSax3_StartingState(); |
162
|
|
|
|
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(); |
166
|
|
|
|
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(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @deprecated deprecated since version 7.6, PHP4 Style Constructors are deprecated and will be remove in 7.8, please update your code, use __construct instead |
174
|
|
|
*/ |
175
|
|
|
function XML_HTMLSax3_StateParser(& $htmlsax){ |
176
|
|
|
$deprecatedMessage = 'PHP4 Style Constructors are deprecated and will be remove in 7.8, please update your code'; |
177
|
|
|
if(isset($GLOBALS['log'])) { |
178
|
|
|
$GLOBALS['log']->deprecated($deprecatedMessage); |
179
|
|
|
} |
180
|
|
|
else { |
181
|
|
|
trigger_error($deprecatedMessage, E_USER_DEPRECATED); |
182
|
|
|
} |
183
|
|
|
self::__construct( $htmlsax); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Moves the position back one character |
189
|
|
|
* @access protected |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
function unscanCharacter() { |
193
|
|
|
$this->position -= 1; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Moves the position forward one character |
198
|
|
|
* @access protected |
199
|
|
|
* @return void |
200
|
|
|
*/ |
201
|
|
|
function ignoreCharacter() { |
202
|
|
|
$this->position += 1; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Returns the next character from the XML document or void if at end |
207
|
|
|
* @access protected |
208
|
|
|
* @return mixed |
209
|
|
|
*/ |
210
|
|
|
function scanCharacter() { |
211
|
|
|
if ($this->position < $this->length) { |
212
|
|
|
return $this->rawtext{$this->position++}; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Returns a string from the current position to the next occurance |
218
|
|
|
* of the supplied string |
219
|
|
|
* @param string string to search until |
220
|
|
|
* @access protected |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
|
|
function scanUntilString($string) { |
224
|
|
|
$start = $this->position; |
225
|
|
|
$this->position = strpos($this->rawtext, $string, $start); |
226
|
|
|
if ($this->position === FALSE) { |
227
|
|
|
$this->position = $this->length; |
228
|
|
|
} |
229
|
|
|
return substr($this->rawtext, $start, $this->position - $start); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Returns a string from the current position until the first instance of |
234
|
|
|
* one of the characters in the supplied string argument |
235
|
|
|
* @param string string to search until |
236
|
|
|
* @access protected |
237
|
|
|
* @return string |
238
|
|
|
* @abstract |
239
|
|
|
*/ |
240
|
|
|
function scanUntilCharacters($string) {} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Moves the position forward past any whitespace characters |
244
|
|
|
* @access protected |
245
|
|
|
* @return void |
246
|
|
|
* @abstract |
247
|
|
|
*/ |
248
|
|
|
function ignoreWhitespace() {} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Begins the parsing operation, setting up any decorators, depending on |
252
|
|
|
* parse options invoking _parse() to execute parsing |
253
|
|
|
* @param string XML document to parse |
254
|
|
|
* @access protected |
255
|
|
|
* @return void |
256
|
|
|
*/ |
257
|
|
|
function parse($data) { |
258
|
|
|
if ($this->parser_options['XML_OPTION_TRIM_DATA_NODES']==1) { |
259
|
|
|
$decorator = new XML_HTMLSax3_Trim( |
260
|
|
|
$this->handler_object_data, |
261
|
|
|
$this->handler_method_data); |
262
|
|
|
$this->handler_object_data =& $decorator; |
263
|
|
|
$this->handler_method_data = 'trimData'; |
264
|
|
|
} |
265
|
|
|
if ($this->parser_options['XML_OPTION_CASE_FOLDING']==1) { |
266
|
|
|
$open_decor = new XML_HTMLSax3_CaseFolding( |
267
|
|
|
$this->handler_object_element, |
268
|
|
|
$this->handler_method_opening, |
269
|
|
|
$this->handler_method_closing); |
270
|
|
|
$this->handler_object_element =& $open_decor; |
271
|
|
|
$this->handler_method_opening ='foldOpen'; |
272
|
|
|
$this->handler_method_closing ='foldClose'; |
273
|
|
|
} |
274
|
|
|
if ($this->parser_options['XML_OPTION_LINEFEED_BREAK']==1) { |
275
|
|
|
$decorator = new XML_HTMLSax3_Linefeed( |
276
|
|
|
$this->handler_object_data, |
277
|
|
|
$this->handler_method_data); |
278
|
|
|
$this->handler_object_data =& $decorator; |
279
|
|
|
$this->handler_method_data = 'breakData'; |
280
|
|
|
} |
281
|
|
|
if ($this->parser_options['XML_OPTION_TAB_BREAK']==1) { |
282
|
|
|
$decorator = new XML_HTMLSax3_Tab( |
283
|
|
|
$this->handler_object_data, |
284
|
|
|
$this->handler_method_data); |
285
|
|
|
$this->handler_object_data =& $decorator; |
286
|
|
|
$this->handler_method_data = 'breakData'; |
287
|
|
|
} |
288
|
|
|
if ($this->parser_options['XML_OPTION_ENTITIES_UNPARSED']==1) { |
289
|
|
|
$decorator = new XML_HTMLSax3_Entities_Unparsed( |
290
|
|
|
$this->handler_object_data, |
291
|
|
|
$this->handler_method_data); |
292
|
|
|
$this->handler_object_data =& $decorator; |
293
|
|
|
$this->handler_method_data = 'breakData'; |
294
|
|
|
} |
295
|
|
|
if ($this->parser_options['XML_OPTION_ENTITIES_PARSED']==1) { |
296
|
|
|
$decorator = new XML_HTMLSax3_Entities_Parsed( |
297
|
|
|
$this->handler_object_data, |
298
|
|
|
$this->handler_method_data); |
299
|
|
|
$this->handler_object_data =& $decorator; |
300
|
|
|
$this->handler_method_data = 'breakData'; |
301
|
|
|
} |
302
|
|
|
// Note switched on by default |
303
|
|
|
if ($this->parser_options['XML_OPTION_STRIP_ESCAPES']==1) { |
304
|
|
|
$decorator = new XML_HTMLSax3_Escape_Stripper( |
305
|
|
|
$this->handler_object_escape, |
306
|
|
|
$this->handler_method_escape); |
307
|
|
|
$this->handler_object_escape =& $decorator; |
308
|
|
|
$this->handler_method_escape = 'strip'; |
309
|
|
|
} |
310
|
|
|
$this->rawtext = $data; |
311
|
|
|
$this->length = strlen($data); |
312
|
|
|
$this->position = 0; |
313
|
|
|
$this->_parse(); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Performs the parsing itself, delegating calls to a specific parser |
318
|
|
|
* state |
319
|
|
|
* @param constant state object to parse with |
320
|
|
|
* @access protected |
321
|
|
|
* @return void |
322
|
|
|
*/ |
323
|
|
|
function _parse($state = XML_HTMLSAX3_STATE_START) { |
324
|
|
|
do { |
325
|
|
|
$state = $this->State[$state]->parse($this); |
326
|
|
|
} while ($state != XML_HTMLSAX3_STATE_STOP && |
327
|
|
|
$this->position < $this->length); |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Parser for PHP Versions below 4.3.0. Uses a slower parsing mechanism than |
333
|
|
|
* the equivalent PHP 4.3.0+ subclass of StateParser |
334
|
|
|
* @package XML_HTMLSax3 |
335
|
|
|
* @access protected |
336
|
|
|
* @see XML_HTMLSax3_StateParser_Gtet430 |
337
|
|
|
*/ |
338
|
|
|
class XML_HTMLSax3_StateParser_Lt430 extends XML_HTMLSax3_StateParser { |
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Constructs XML_HTMLSax3_StateParser_Lt430 defining available |
341
|
|
|
* parser options |
342
|
|
|
* @var XML_HTMLSax3 instance of user front end class |
343
|
|
|
* @access protected |
344
|
|
|
*/ |
345
|
|
|
function __construct(& $htmlsax) { |
346
|
|
|
parent::__construct($htmlsax); |
347
|
|
|
$this->parser_options['XML_OPTION_TRIM_DATA_NODES'] = 0; |
348
|
|
|
$this->parser_options['XML_OPTION_CASE_FOLDING'] = 0; |
349
|
|
|
$this->parser_options['XML_OPTION_LINEFEED_BREAK'] = 0; |
350
|
|
|
$this->parser_options['XML_OPTION_TAB_BREAK'] = 0; |
351
|
|
|
$this->parser_options['XML_OPTION_ENTITIES_PARSED'] = 0; |
352
|
|
|
$this->parser_options['XML_OPTION_ENTITIES_UNPARSED'] = 0; |
353
|
|
|
$this->parser_options['XML_OPTION_STRIP_ESCAPES'] = 0; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Returns a string from the current position until the first instance of |
358
|
|
|
* one of the characters in the supplied string argument |
359
|
|
|
* @param string string to search until |
360
|
|
|
* @access protected |
361
|
|
|
* @return string |
362
|
|
|
*/ |
363
|
|
|
function scanUntilCharacters($string) { |
364
|
|
|
$startpos = $this->position; |
365
|
|
|
while ($this->position < $this->length && strpos($string, $this->rawtext{$this->position}) === FALSE) { |
366
|
|
|
$this->position++; |
367
|
|
|
} |
368
|
|
|
return substr($this->rawtext, $startpos, $this->position - $startpos); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Moves the position forward past any whitespace characters |
373
|
|
|
* @access protected |
374
|
|
|
* @return void |
375
|
|
|
*/ |
376
|
|
|
function ignoreWhitespace() { |
377
|
|
|
while ($this->position < $this->length && |
378
|
|
|
strpos(" \n\r\t", $this->rawtext{$this->position}) !== FALSE) { |
379
|
|
|
$this->position++; |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Begins the parsing operation, setting up the unparsed XML entities |
385
|
|
|
* decorator if necessary then delegating further work to parent |
386
|
|
|
* @param string XML document to parse |
387
|
|
|
* @access protected |
388
|
|
|
* @return void |
389
|
|
|
*/ |
390
|
|
|
function parse($data) { |
391
|
|
|
parent::parse($data); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Parser for PHP Versions equal to or greater than 4.3.0. Uses a faster |
397
|
|
|
* parsing mechanism than the equivalent PHP < 4.3.0 subclass of StateParser |
398
|
|
|
* @package XML_HTMLSax3 |
399
|
|
|
* @access protected |
400
|
|
|
* @see XML_HTMLSax3_StateParser_Lt430 |
401
|
|
|
*/ |
402
|
|
|
class XML_HTMLSax3_StateParser_Gtet430 extends XML_HTMLSax3_StateParser { |
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Constructs XML_HTMLSax3_StateParser_Gtet430 defining available |
405
|
|
|
* parser options |
406
|
|
|
* @var XML_HTMLSax3 instance of user front end class |
407
|
|
|
* @access protected |
408
|
|
|
*/ |
409
|
|
|
function __construct(& $htmlsax) { |
410
|
|
|
parent::__construct($htmlsax); |
411
|
|
|
$this->parser_options['XML_OPTION_TRIM_DATA_NODES'] = 0; |
412
|
|
|
$this->parser_options['XML_OPTION_CASE_FOLDING'] = 0; |
413
|
|
|
$this->parser_options['XML_OPTION_LINEFEED_BREAK'] = 0; |
414
|
|
|
$this->parser_options['XML_OPTION_TAB_BREAK'] = 0; |
415
|
|
|
$this->parser_options['XML_OPTION_ENTITIES_PARSED'] = 0; |
416
|
|
|
$this->parser_options['XML_OPTION_ENTITIES_UNPARSED'] = 0; |
417
|
|
|
$this->parser_options['XML_OPTION_STRIP_ESCAPES'] = 0; |
418
|
|
|
} |
419
|
|
|
/** |
420
|
|
|
* Returns a string from the current position until the first instance of |
421
|
|
|
* one of the characters in the supplied string argument. |
422
|
|
|
* @param string string to search until |
423
|
|
|
* @access protected |
424
|
|
|
* @return string |
425
|
|
|
*/ |
426
|
|
|
function scanUntilCharacters($string) { |
427
|
|
|
$startpos = $this->position; |
428
|
|
|
$length = strcspn($this->rawtext, $string, $startpos); |
429
|
|
|
$this->position += $length; |
430
|
|
|
return substr($this->rawtext, $startpos, $length); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Moves the position forward past any whitespace characters |
435
|
|
|
* @access protected |
436
|
|
|
* @return void |
437
|
|
|
*/ |
438
|
|
|
function ignoreWhitespace() { |
439
|
|
|
$this->position += strspn($this->rawtext, " \n\r\t", $this->position); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Begins the parsing operation, setting up the parsed and unparsed |
444
|
|
|
* XML entity decorators if necessary then delegating further work |
445
|
|
|
* to parent |
446
|
|
|
* @param string XML document to parse |
447
|
|
|
* @access protected |
448
|
|
|
* @return void |
449
|
|
|
*/ |
450
|
|
|
function parse($data) { |
451
|
|
|
parent::parse($data); |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Default NullHandler for methods which were not set by user |
457
|
|
|
* @package XML_HTMLSax3 |
458
|
|
|
* @access protected |
459
|
|
|
*/ |
460
|
|
|
class XML_HTMLSax3_NullHandler { |
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* Generic handler method which does nothing |
463
|
|
|
* @access protected |
464
|
|
|
* @return void |
465
|
|
|
*/ |
466
|
|
|
function DoNothing() { |
467
|
|
|
} |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* User interface class. All user calls should only be made to this class |
472
|
|
|
* @package XML_HTMLSax3 |
473
|
|
|
* @access public |
474
|
|
|
*/ |
475
|
|
|
class XML_HTMLSax3 { |
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* Instance of concrete subclass of XML_HTMLSax3_StateParser |
478
|
|
|
* @var XML_HTMLSax3_StateParser |
479
|
|
|
* @access private |
480
|
|
|
*/ |
481
|
|
|
var $state_parser; |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Constructs XML_HTMLSax3 selecting concrete StateParser subclass |
485
|
|
|
* depending on PHP version being used as well as setting the default |
486
|
|
|
* NullHandler for all callbacks<br /> |
487
|
|
|
* <b>Example:</b> |
488
|
|
|
* <pre> |
489
|
|
|
* $myHandler = new MyHandler(); |
490
|
|
|
* $parser = new XML_HTMLSax3(); |
491
|
|
|
* $parser->set_object($myHandler); |
492
|
|
|
* $parser->set_option('XML_OPTION_CASE_FOLDING'); |
493
|
|
|
* $parser->set_element_handler('myOpenHandler','myCloseHandler'); |
494
|
|
|
* $parser->set_data_handler('myDataHandler'); |
495
|
|
|
* $parser->parser($xml); |
496
|
|
|
* </pre> |
497
|
|
|
* @access public |
498
|
|
|
*/ |
499
|
|
|
function XML_HTMLSax3() { |
500
|
|
|
if (version_compare(phpversion(), '4.3', 'ge')) { |
501
|
|
|
$this->state_parser = new XML_HTMLSax3_StateParser_Gtet430($this); |
502
|
|
|
} else { |
503
|
|
|
$this->state_parser = new XML_HTMLSax3_StateParser_Lt430($this); |
504
|
|
|
} |
505
|
|
|
$nullhandler = new XML_HTMLSax3_NullHandler(); |
506
|
|
|
$this->set_object($nullhandler); |
507
|
|
|
$this->set_element_handler('DoNothing', 'DoNothing'); |
508
|
|
|
$this->set_data_handler('DoNothing'); |
509
|
|
|
$this->set_pi_handler('DoNothing'); |
510
|
|
|
$this->set_jasp_handler('DoNothing'); |
511
|
|
|
$this->set_escape_handler('DoNothing'); |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* Sets the user defined handler object. Returns a PEAR Error |
516
|
|
|
* if supplied argument is not an object. |
517
|
|
|
* @param object handler object containing SAX callback methods |
518
|
|
|
* @access public |
519
|
|
|
* @return mixed |
520
|
|
|
*/ |
521
|
|
|
function set_object(&$object) { |
522
|
|
|
if ( is_object($object) ) { |
523
|
|
|
$this->state_parser->handler_default =& $object; |
524
|
|
|
return true; |
525
|
|
|
} else { |
526
|
|
|
$GLOBALS['log']->info('XML_HTMLSax3::set_object requires '. |
527
|
|
|
'an object instance'); |
528
|
|
|
} |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Sets a parser option. By default all options are switched off. |
533
|
|
|
* Returns a PEAR Error if option is invalid<br /> |
534
|
|
|
* <b>Available options:</b> |
535
|
|
|
* <ul> |
536
|
|
|
* <li>XML_OPTION_TRIM_DATA_NODES: trim whitespace off the beginning |
537
|
|
|
* and end of data passed to the data handler</li> |
538
|
|
|
* <li>XML_OPTION_LINEFEED_BREAK: linefeeds result in additional data |
539
|
|
|
* handler calls</li> |
540
|
|
|
* <li>XML_OPTION_TAB_BREAK: tabs result in additional data handler |
541
|
|
|
* calls</li> |
542
|
|
|
* <li>XML_OPTION_ENTITIES_UNPARSED: XML entities are returned as |
543
|
|
|
* seperate data handler calls in unparsed form</li> |
544
|
|
|
* <li>XML_OPTION_ENTITIES_PARSED: (PHP 4.3.0+ only) XML entities are |
545
|
|
|
* returned as seperate data handler calls and are parsed with |
546
|
|
|
* PHP's html_entity_decode() function</li> |
547
|
|
|
* <li>XML_OPTION_STRIP_ESCAPES: strips out the -- -- comment markers |
548
|
|
|
* or CDATA markup inside an XML escape, if found.</li> |
549
|
|
|
* </ul> |
550
|
|
|
* To get HTMLSax to behave in the same way as the native PHP SAX parser, |
551
|
|
|
* using it's default state, you need to switch on XML_OPTION_LINEFEED_BREAK, |
552
|
|
|
* XML_OPTION_ENTITIES_PARSED and XML_OPTION_CASE_FOLDING |
553
|
|
|
* @param string name of parser option |
554
|
|
|
* @param int (optional) 1 to switch on, 0 for off |
555
|
|
|
* @access public |
556
|
|
|
* @return boolean |
557
|
|
|
*/ |
558
|
|
|
function set_option($name, $value=1) { |
559
|
|
|
if ( array_key_exists($name,$this->state_parser->parser_options) ) { |
560
|
|
|
$this->state_parser->parser_options[$name] = $value; |
561
|
|
|
return true; |
562
|
|
|
} else { |
563
|
|
|
$GLOBALS['log']->info('XML_HTMLSax3::set_option('.$name.') illegal'); |
564
|
|
|
} |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
/** |
568
|
|
|
* Sets the data handler method which deals with the contents of XML |
569
|
|
|
* elements.<br /> |
570
|
|
|
* The handler method must accept two arguments, the first being an |
571
|
|
|
* instance of XML_HTMLSax3 and the second being the contents of an |
572
|
|
|
* XML element e.g. |
573
|
|
|
* <pre> |
574
|
|
|
* function myDataHander(& $parser,$data){} |
575
|
|
|
* </pre> |
576
|
|
|
* @param string name of method |
577
|
|
|
* @access public |
578
|
|
|
* @return void |
579
|
|
|
* @see set_object |
580
|
|
|
*/ |
581
|
|
|
function set_data_handler($data_method) { |
582
|
|
|
$this->state_parser->handler_object_data =& $this->state_parser->handler_default; |
583
|
|
|
$this->state_parser->handler_method_data = $data_method; |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
/** |
587
|
|
|
* Sets the open and close tag handlers |
588
|
|
|
* <br />The open handler method must accept three arguments; the parser, |
589
|
|
|
* the tag name and an array of attributes e.g. |
590
|
|
|
* <pre> |
591
|
|
|
* function myOpenHander(& $parser,$tagname,$attrs=array()){} |
592
|
|
|
* </pre> |
593
|
|
|
* The close handler method must accept two arguments; the parser and |
594
|
|
|
* the tag name e.g. |
595
|
|
|
* <pre> |
596
|
|
|
* function myCloseHander(& $parser,$tagname){} |
597
|
|
|
* </pre> |
598
|
|
|
* @param string name of open method |
599
|
|
|
* @param string name of close method |
600
|
|
|
* @access public |
601
|
|
|
* @return void |
602
|
|
|
* @see set_object |
603
|
|
|
*/ |
604
|
|
|
function set_element_handler($opening_method, $closing_method) { |
605
|
|
|
$this->state_parser->handler_object_element =& $this->state_parser->handler_default; |
606
|
|
|
$this->state_parser->handler_method_opening = $opening_method; |
607
|
|
|
$this->state_parser->handler_method_closing = $closing_method; |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* Sets the processing instruction handler method e.g. for PHP open |
612
|
|
|
* and close tags<br /> |
613
|
|
|
* The handler method must accept three arguments; the parser, the |
614
|
|
|
* PI target and data inside the PI |
615
|
|
|
* <pre> |
616
|
|
|
* function myPIHander(& $parser,$target, $data){} |
617
|
|
|
* </pre> |
618
|
|
|
* @param string name of method |
619
|
|
|
* @access public |
620
|
|
|
* @return void |
621
|
|
|
* @see set_object |
622
|
|
|
*/ |
623
|
|
|
function set_pi_handler($pi_method) { |
624
|
|
|
$this->state_parser->handler_object_pi =& $this->state_parser->handler_default; |
625
|
|
|
$this->state_parser->handler_method_pi = $pi_method; |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
/** |
629
|
|
|
* Sets the XML escape handler method e.g. for comments and doctype |
630
|
|
|
* declarations<br /> |
631
|
|
|
* The handler method must accept two arguments; the parser and the |
632
|
|
|
* contents of the escaped section |
633
|
|
|
* <pre> |
634
|
|
|
* function myEscapeHander(& $parser, $data){} |
635
|
|
|
* </pre> |
636
|
|
|
* @param string name of method |
637
|
|
|
* @access public |
638
|
|
|
* @return void |
639
|
|
|
* @see set_object |
640
|
|
|
*/ |
641
|
|
|
function set_escape_handler($escape_method) { |
642
|
|
|
$this->state_parser->handler_object_escape =& $this->state_parser->handler_default; |
643
|
|
|
$this->state_parser->handler_method_escape = $escape_method; |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
/** |
647
|
|
|
* Sets the JSP/ASP markup handler<br /> |
648
|
|
|
* The handler method must accept two arguments; the parser and |
649
|
|
|
* body of the JASP tag |
650
|
|
|
* <pre> |
651
|
|
|
* function myJaspHander(& $parser, $data){} |
652
|
|
|
* </pre> |
653
|
|
|
* @param string name of method |
654
|
|
|
* @access public |
655
|
|
|
* @return void |
656
|
|
|
* @see set_object |
657
|
|
|
*/ |
658
|
|
|
function set_jasp_handler ($jasp_method) { |
659
|
|
|
$this->state_parser->handler_object_jasp =& $this->state_parser->handler_default; |
660
|
|
|
$this->state_parser->handler_method_jasp = $jasp_method; |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
/** |
664
|
|
|
* Returns the current string position of the "cursor" inside the XML |
665
|
|
|
* document |
666
|
|
|
* <br />Intended for use from within a user defined handler called |
667
|
|
|
* via the $parser reference e.g. |
668
|
|
|
* <pre> |
669
|
|
|
* function myDataHandler(& $parser,$data) { |
670
|
|
|
* echo( 'Current position: '.$parser->get_current_position() ); |
671
|
|
|
* } |
672
|
|
|
* </pre> |
673
|
|
|
* @access public |
674
|
|
|
* @return int |
675
|
|
|
* @see get_length |
676
|
|
|
*/ |
677
|
|
|
function get_current_position() { |
678
|
|
|
return $this->state_parser->position; |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
/** |
682
|
|
|
* Returns the string length of the XML document being parsed |
683
|
|
|
* @access public |
684
|
|
|
* @return int |
685
|
|
|
*/ |
686
|
|
|
function get_length() { |
687
|
|
|
return $this->state_parser->length; |
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
/** |
691
|
|
|
* Start parsing some XML |
692
|
|
|
* @param string XML document |
693
|
|
|
* @access public |
694
|
|
|
* @return void |
695
|
|
|
*/ |
696
|
|
|
function parse($data) { |
697
|
|
|
$this->state_parser->parse($data); |
698
|
|
|
} |
699
|
|
|
} |
700
|
|
|
?> |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.