/web/htdocs/www.euroroma.net/home/public/gallery2/lib/pear/HTMLSax3.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
//
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to either version 3.0 of the PHP license |
// | or version 3.0 of the GNU Lesser General Public License.             |
// | The PHP license 3.0 is bundled with this package in the file LICENSE,|
// | and is available at  through the world-wide-web at                   |
// | http://www.php.net/license/3_0.txt. If you did not receive a copy of |
// | the PHP license and are unable to obtain it through the              |
// | world-wide-web, please send a note to license@php.net so we can mail |
// | you a copy immediately.                                              |
// | You should have received a copy of the GNU Lesser General Public     |
// | License along with this library.  If not, see                        |
// | <http://www.gnu.org/licenses/>.                                      |
// +----------------------------------------------------------------------+
// | Authors: Alexander Zhukov <alex@veresk.ru> Original port from Python |
// | Authors: Harry Fuecks <hfuecks@phppatterns.com> Port to PEAR + more  |
// | Authors: Many @ Sitepointforums Advanced PHP Forums                  |
// +----------------------------------------------------------------------+
//
// PEAR  Id: HTMLSax3.php,v 1.2 2007/10/29 21:41:34 hfuecks
//   G2 $Id: HTMLSax3.php 20960 2009-12-16 06:54:53Z mindless $
//
/**
* Main parser components
* @package XML_HTMLSax3
* @version  Id: HTMLSax3.php,v 1.2 2007/10/29 21:41:34 hfuecks
*/
/**
* Required classes
*/
//if (!defined('XML_HTMLSAX3')) {
//    define('XML_HTMLSAX3', 'XML/');
//}
//require_once(XML_HTMLSAX3 . 'HTMLSax3/States.php');
//require_once(XML_HTMLSAX3 . 'HTMLSax3/Decorators.php');

/**
* Base State Parser
* @package XML_HTMLSax3
* @access protected
* @abstract
*/
class XML_HTMLSax3_StateParser {
    
/**
    * Instance of user front end class to be passed to callbacks
    * @var XML_HTMLSax3
    * @access private
    */
    
var $htmlsax;
    
/**
    * User defined object for handling elements
    * @var object
    * @access private
    */
    
var $handler_object_element;
    
/**
    * User defined open tag handler method
    * @var string
    * @access private
    */
    
var $handler_method_opening;
    
/**
    * User defined close tag handler method
    * @var string
    * @access private
    */
    
var $handler_method_closing;
    
/**
    * User defined object for handling data in elements
    * @var object
    * @access private
    */
    
var $handler_object_data;
    
/**
    * User defined data handler method
    * @var string
    * @access private
    */
    
var $handler_method_data;
    
/**
    * User defined object for handling processing instructions
    * @var object
    * @access private
    */
    
var $handler_object_pi;
    
/**
    * User defined processing instruction handler method
    * @var string
    * @access private
    */
    
var $handler_method_pi;
    
/**
    * User defined object for handling JSP/ASP tags
    * @var object
    * @access private
    */
    
var $handler_object_jasp;
    
/**
    * User defined JSP/ASP handler method
    * @var string
    * @access private
    */
    
var $handler_method_jasp;
    
/**
    * User defined object for handling XML escapes
    * @var object
    * @access private
    */
    
var $handler_object_escape;
    
/**
    * User defined XML escape handler method
    * @var string
    * @access private
    */
    
var $handler_method_escape;
    
/**
    * User defined handler object or NullHandler
    * @var object
    * @access private
    */
    
var $handler_default;
    
/**
    * Parser options determining parsing behavior
    * @var array
    * @access private
    */
    
var $parser_options = array();
    
/**
    * XML document being parsed
    * @var string
    * @access private
    */
    
var $rawtext;
    
/**
    * Position in XML document relative to start (0)
    * @var int
    * @access private
    */
    
var $position;
    
/**
    * Length of the XML document in characters
    * @var int
    * @access private
    */
    
var $length;
    
/**
    * Array of state objects
    * @var array
    * @access private
    */
    
var $State = array();

    
/**
    * Constructs XML_HTMLSax3_StateParser setting up states
    * @var XML_HTMLSax3 instance of user front end class
    * @access protected
    */
    
function XML_HTMLSax3_StateParser (& $htmlsax) {
        
$this->htmlsax = & $htmlsax;
        
$this->State[XML_HTMLSAX3_STATE_START] = new XML_HTMLSax3_StartingState();

        
$this->State[XML_HTMLSAX3_STATE_CLOSING_TAG] = new XML_HTMLSax3_ClosingTagState();
        
$this->State[XML_HTMLSAX3_STATE_TAG] = new XML_HTMLSax3_TagState();
        
$this->State[XML_HTMLSAX3_STATE_OPENING_TAG] = new XML_HTMLSax3_OpeningTagState();

        
$this->State[XML_HTMLSAX3_STATE_PI] = new XML_HTMLSax3_PiState();
        
$this->State[XML_HTMLSAX3_STATE_JASP] = new XML_HTMLSax3_JaspState();
        
$this->State[XML_HTMLSAX3_STATE_ESCAPE] = new XML_HTMLSax3_EscapeState();
    }

    
/**
    * Moves the position back one character
    * @access protected
    * @return void
    */
    
function unscanCharacter() {
        
$this->position -= 1;
    }

    
/**
    * Moves the position forward one character
    * @access protected
    * @return void
    */
    
function ignoreCharacter() {
        
$this->position += 1;
    }

    
/**
    * Returns the next character from the XML document or void if at end
    * @access protected
    * @return mixed
    */
    
function scanCharacter() {
        if (
$this->position $this->length) {
            return 
$this->rawtext{$this->position++};
        }
    }

    
/**
    * Returns a string from the current position to the next occurance
    * of the supplied string
    * @param string string to search until
    * @access protected
    * @return string
    */
    
function scanUntilString($string) {
        
$start $this->position;
        
$this->position strpos($this->rawtext$string$start);
        if (
$this->position === FALSE) {
            
$this->position $this->length;
        }
        return 
substr($this->rawtext$start$this->position $start);
    }

    
/**
    * Returns a string from the current position until the first instance of
    * one of the characters in the supplied string argument
    * @param string string to search until
    * @access protected
    * @return string
    * @abstract
    */
    
function scanUntilCharacters($string) {}

    
/**
    * Moves the position forward past any whitespace characters
    * @access protected
    * @return void
    * @abstract
    */
    
function ignoreWhitespace() {}

    
/**
    * Begins the parsing operation, setting up any decorators, depending on
    * parse options invoking _parse() to execute parsing
    * @param string XML document to parse
    * @access protected
    * @return void
    */
    
function parse($data) {
        if (
$this->parser_options['XML_OPTION_TRIM_DATA_NODES']==1) {
            
$decorator = new XML_HTMLSax3_Trim(
                
$this->handler_object_data,
                
$this->handler_method_data);
            
$this->handler_object_data =& $decorator;
            
$this->handler_method_data 'trimData';
        }
        if (
$this->parser_options['XML_OPTION_CASE_FOLDING']==1) {
            
$open_decor = new XML_HTMLSax3_CaseFolding(
                
$this->handler_object_element,
                
$this->handler_method_opening,
                
$this->handler_method_closing);
            
$this->handler_object_element =& $open_decor;
            
$this->handler_method_opening ='foldOpen';
            
$this->handler_method_closing ='foldClose';
        }
        if (
$this->parser_options['XML_OPTION_LINEFEED_BREAK']==1) {
            
$decorator = new XML_HTMLSax3_Linefeed(
                
$this->handler_object_data,
                
$this->handler_method_data);
            
$this->handler_object_data =& $decorator;
            
$this->handler_method_data 'breakData';
        }
        if (
$this->parser_options['XML_OPTION_TAB_BREAK']==1) {
            
$decorator = new XML_HTMLSax3_Tab(
                
$this->handler_object_data,
                
$this->handler_method_data);
            
$this->handler_object_data =& $decorator;
            
$this->handler_method_data 'breakData';
        }
        if (
$this->parser_options['XML_OPTION_ENTITIES_UNPARSED']==1) {
            
$decorator = new XML_HTMLSax3_Entities_Unparsed(
                
$this->handler_object_data,
                
$this->handler_method_data);
            
$this->handler_object_data =& $decorator;
            
$this->handler_method_data 'breakData';
        }
        if (
$this->parser_options['XML_OPTION_ENTITIES_PARSED']==1) {
            
$decorator = new XML_HTMLSax3_Entities_Parsed(
                
$this->handler_object_data,
                
$this->handler_method_data);
            
$this->handler_object_data =& $decorator;
            
$this->handler_method_data 'breakData';
        }
        
// Note switched on by default
        
if ($this->parser_options['XML_OPTION_STRIP_ESCAPES']==1) {
            
$decorator = new XML_HTMLSax3_Escape_Stripper(
                
$this->handler_object_escape,
                
$this->handler_method_escape);
            
$this->handler_object_escape =& $decorator;
            
$this->handler_method_escape 'strip';
        }
        
$this->rawtext $data;
        
$this->length strlen($data);
        
$this->position 0;
        
$this->_parse();
    }

    
/**
    * Performs the parsing itself, delegating calls to a specific parser
    * state
    * @param constant state object to parse with
    * @access protected
    * @return void
    */
    
function _parse($state XML_HTMLSAX3_STATE_START) {
        do {
            
$state $this->State[$state]->parse($this);
        } while (
$state != XML_HTMLSAX3_STATE_STOP &&
                    
$this->position $this->length);
    }
}

/**
* Parser for PHP Versions below 4.3.0. Uses a slower parsing mechanism than
* the equivalent PHP 4.3.0+  subclass of StateParser
* @package XML_HTMLSax3
* @access protected
* @see XML_HTMLSax3_StateParser_Gtet430
*/
class XML_HTMLSax3_StateParser_Lt430 extends XML_HTMLSax3_StateParser {
    
/**
    * Constructs XML_HTMLSax3_StateParser_Lt430 defining available
    * parser options
    * @var XML_HTMLSax3 instance of user front end class
    * @access protected
    */
    
function XML_HTMLSax3_StateParser_Lt430(& $htmlsax) {
        
parent::XML_HTMLSax3_StateParser($htmlsax);
        
$this->parser_options['XML_OPTION_TRIM_DATA_NODES'] = 0;
        
$this->parser_options['XML_OPTION_CASE_FOLDING'] = 0;
        
$this->parser_options['XML_OPTION_LINEFEED_BREAK'] = 0;
        
$this->parser_options['XML_OPTION_TAB_BREAK'] = 0;
        
$this->parser_options['XML_OPTION_ENTITIES_PARSED'] = 0;
        
$this->parser_options['XML_OPTION_ENTITIES_UNPARSED'] = 0;
        
$this->parser_options['XML_OPTION_STRIP_ESCAPES'] = 0;
    }

    
/**
    * Returns a string from the current position until the first instance of
    * one of the characters in the supplied string argument
    * @param string string to search until
    * @access protected
    * @return string
    */
    
function scanUntilCharacters($string) {
        
$startpos $this->position;
        while (
$this->position $this->length && strpos($string$this->rawtext{$this->position}) === FALSE) {
            
$this->position++;
        }
        return 
substr($this->rawtext$startpos$this->position $startpos);
    }

    
/**
    * Moves the position forward past any whitespace characters
    * @access protected
    * @return void
    */
    
function ignoreWhitespace() {
        while (
$this->position $this->length && 
            
strpos(" \n\r\t"$this->rawtext{$this->position}) !== FALSE) {
            
$this->position++;
        }
    }

    
/**
    * Begins the parsing operation, setting up the unparsed XML entities
    * decorator if necessary then delegating further work to parent
    * @param string XML document to parse
    * @access protected
    * @return void
    */
    
function parse($data) {
        
parent::parse($data);
    }
}

/**
* Parser for PHP Versions equal to or greater than 4.3.0. Uses a faster
* parsing mechanism than the equivalent PHP < 4.3.0 subclass of StateParser
* @package XML_HTMLSax3
* @access protected
* @see XML_HTMLSax3_StateParser_Lt430
*/
class XML_HTMLSax3_StateParser_Gtet430 extends XML_HTMLSax3_StateParser {
    
/**
    * Constructs XML_HTMLSax3_StateParser_Gtet430 defining available
    * parser options
    * @var XML_HTMLSax3 instance of user front end class
    * @access protected
    */
    
function XML_HTMLSax3_StateParser_Gtet430(& $htmlsax) {
        
parent::XML_HTMLSax3_StateParser($htmlsax);
        
$this->parser_options['XML_OPTION_TRIM_DATA_NODES'] = 0;
        
$this->parser_options['XML_OPTION_CASE_FOLDING'] = 0;
        
$this->parser_options['XML_OPTION_LINEFEED_BREAK'] = 0;
        
$this->parser_options['XML_OPTION_TAB_BREAK'] = 0;
        
$this->parser_options['XML_OPTION_ENTITIES_PARSED'] = 0;
        
$this->parser_options['XML_OPTION_ENTITIES_UNPARSED'] = 0;
        
$this->parser_options['XML_OPTION_STRIP_ESCAPES'] = 0;
    }
    
/**
    * Returns a string from the current position until the first instance of
    * one of the characters in the supplied string argument.
    * @param string string to search until
    * @access protected
    * @return string
    */
    
function scanUntilCharacters($string) {
        
$startpos $this->position;
        
$length strcspn($this->rawtext$string$startpos);
        
$this->position += $length;
        return 
substr($this->rawtext$startpos$length);
    }

    
/**
    * Moves the position forward past any whitespace characters
    * @access protected
    * @return void
    */
    
function ignoreWhitespace() {
        
$this->position += strspn($this->rawtext" \n\r\t"$this->position);
    }

    
/**
    * Begins the parsing operation, setting up the parsed and unparsed
    * XML entity decorators if necessary then delegating further work
    * to parent
    * @param string XML document to parse
    * @access protected
    * @return void
    */
    
function parse($data) {
        
parent::parse($data);
    }
}

/**
* Default NullHandler for methods which were not set by user
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_NullHandler {
    
/**
    * Generic handler method which does nothing
    * @access protected
    * @return void
    */
    
function DoNothing() {
    }
}

/**
* User interface class. All user calls should only be made to this class
* @package XML_HTMLSax3
* @access public
*/
class XML_HTMLSax3 {
    
/**
    * Instance of concrete subclass of XML_HTMLSax3_StateParser
    * @var XML_HTMLSax3_StateParser
    * @access private
    */
    
var $state_parser;

    
/**
    * Constructs XML_HTMLSax3 selecting concrete StateParser subclass
    * depending on PHP version being used as well as setting the default
    * NullHandler for all callbacks<br />
    * <b>Example:</b>
    * <pre>
    * $myHandler = & new MyHandler();
    * $parser = new XML_HTMLSax3();
    * $parser->set_object($myHandler);
    * $parser->set_option('XML_OPTION_CASE_FOLDING');
    * $parser->set_element_handler('myOpenHandler','myCloseHandler');
    * $parser->set_data_handler('myDataHandler');
    * $parser->parser($xml);
    * </pre>
    * @access public
    */
    
function XML_HTMLSax3() {
        if (
version_compare(phpversion(), '4.3''ge')) {
            
$this->state_parser = new XML_HTMLSax3_StateParser_Gtet430($this);
        } else {
            
$this->state_parser = new XML_HTMLSax3_StateParser_Lt430($this);
        }
        
$nullhandler = new XML_HTMLSax3_NullHandler();
        
$this->set_object($nullhandler);
        
$this->set_element_handler('DoNothing''DoNothing');
        
$this->set_data_handler('DoNothing');
        
$this->set_pi_handler('DoNothing');
        
$this->set_jasp_handler('DoNothing');
        
$this->set_escape_handler('DoNothing');
    }

    
/**
    * Sets the user defined handler object. Returns a PEAR Error
    * if supplied argument is not an object.
    * @param object handler object containing SAX callback methods
    * @access public
    * @return mixed
    */
    
function set_object(&$object) {
        if ( 
is_object($object) ) {
            
$this->state_parser->handler_default =& $object;
            return 
true;
        } else {
            
//require_once('PEAR.php');
            //PEAR::raiseError('XML_HTMLSax3::set_object requires '.
            //    'an object instance');
        
}
    }

    
/**
    * Sets a parser option. By default all options are switched off.
    * Returns a PEAR Error if option is invalid<br />
    * <b>Available options:</b>
    * <ul>
    * <li>XML_OPTION_TRIM_DATA_NODES: trim whitespace off the beginning
    * and end of data passed to the data handler</li>
    * <li>XML_OPTION_LINEFEED_BREAK: linefeeds result in additional data
    * handler calls</li>
    * <li>XML_OPTION_TAB_BREAK: tabs result in additional data handler
    * calls</li>
    * <li>XML_OPTION_ENTITIES_UNPARSED: XML entities are returned as
    * seperate data handler calls in unparsed form</li>
    * <li>XML_OPTION_ENTITIES_PARSED: (PHP 4.3.0+ only) XML entities are
    * returned as seperate data handler calls and are parsed with 
    * PHP's html_entity_decode() function</li>
    * <li>XML_OPTION_STRIP_ESCAPES: strips out the -- -- comment markers
    * or CDATA markup inside an XML escape, if found.</li>
    * </ul>
    * To get HTMLSax to behave in the same way as the native PHP SAX parser,
    * using it's default state, you need to switch on XML_OPTION_LINEFEED_BREAK,
    * XML_OPTION_ENTITIES_PARSED and XML_OPTION_CASE_FOLDING
    * @param string name of parser option
    * @param int (optional) 1 to switch on, 0 for off
    * @access public
    * @return boolean
    */
    
function set_option($name$value=1) {
        if ( 
array_key_exists($name,$this->state_parser->parser_options) ) {
            
$this->state_parser->parser_options[$name] = $value;
            return 
true;
        } else {
            
//require_once('PEAR.php');
            //PEAR::raiseError('XML_HTMLSax3::set_option('.$name.') illegal');
        
}
    }

    
/**
    * Sets the data handler method which deals with the contents of XML
    * elements.<br />
    * The handler method must accept two arguments, the first being an
    * instance of XML_HTMLSax3 and the second being the contents of an
    * XML element e.g.
    * <pre>
    * function myDataHander(& $parser,$data){}
    * </pre>
    * @param string name of method
    * @access public
    * @return void
    * @see set_object
    */
    
function set_data_handler($data_method) {
        
$this->state_parser->handler_object_data =& $this->state_parser->handler_default;
        
$this->state_parser->handler_method_data $data_method;
    }

    
/**
    * Sets the open and close tag handlers
    * <br />The open handler method must accept three arguments; the parser,
    * the tag name and an array of attributes e.g.
    * <pre>
    * function myOpenHander(& $parser,$tagname,$attrs=array()){}
    * </pre>
    * The close handler method must accept two arguments; the parser and
    * the tag name e.g.
    * <pre>
    * function myCloseHander(& $parser,$tagname){}
    * </pre>
    * @param string name of open method
    * @param string name of close method
    * @access public
    * @return void
    * @see set_object
    */
    
function set_element_handler($opening_method$closing_method) {
        
$this->state_parser->handler_object_element =& $this->state_parser->handler_default;
        
$this->state_parser->handler_method_opening $opening_method;
        
$this->state_parser->handler_method_closing $closing_method;
    }

    
/**
    * Sets the processing instruction handler method e.g. for PHP open
    * and close tags<br />
    * The handler method must accept three arguments; the parser, the
    * PI target and data inside the PI
    * <pre>
    * function myPIHander(& $parser,$target, $data){}
    * </pre>
    * @param string name of method
    * @access public
    * @return void
    * @see set_object
    */
    
function set_pi_handler($pi_method) {
        
$this->state_parser->handler_object_pi =& $this->state_parser->handler_default;
        
$this->state_parser->handler_method_pi $pi_method;
    }

    
/**
    * Sets the XML escape handler method e.g. for comments and doctype
    * declarations<br />
    * The handler method must accept two arguments; the parser and the
    * contents of the escaped section
    * <pre>
    * function myEscapeHander(& $parser, $data){}
    * </pre>
    * @param string name of method
    * @access public
    * @return void
    * @see set_object
    */
    
function set_escape_handler($escape_method) {
        
$this->state_parser->handler_object_escape =& $this->state_parser->handler_default;
        
$this->state_parser->handler_method_escape $escape_method;
    }

    
/**
    * Sets the JSP/ASP markup handler<br />
    * The handler method must accept two arguments; the parser and
    * body of the JASP tag
    * <pre>
    * function myJaspHander(& $parser, $data){}
    * </pre>
    * @param string name of method
    * @access public
    * @return void
    * @see set_object
    */
    
function set_jasp_handler ($jasp_method) {
        
$this->state_parser->handler_object_jasp =& $this->state_parser->handler_default;
        
$this->state_parser->handler_method_jasp $jasp_method;
    }

    
/**
    * Returns the current string position of the "cursor" inside the XML
    * document
    * <br />Intended for use from within a user defined handler called
    * via the $parser reference e.g.
    * <pre>
    * function myDataHandler(& $parser,$data) {
    *     echo( 'Current position: '.$parser->get_current_position() );
    * }
    * </pre>
    * @access public
    * @return int
    * @see get_length
    */
    
function get_current_position() {
        return 
$this->state_parser->position;
    }

    
/**
    * Returns the string length of the XML document being parsed
    * @access public
    * @return int
    */
    
function get_length() {
        return 
$this->state_parser->length;
    }

    
/**
    * Start parsing some XML
    * @param string XML document
    * @access public
    * @return void
    */
    
function parse($data) {
        
$this->state_parser->parse($data);
    }
}
?>
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
//
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to either version 3.0 of the PHP license |
// | or version 3.0 of the GNU Lesser General Public License.             |
// | The PHP license 3.0 is bundled with this package in the file LICENSE,|
// | and is available at  through the world-wide-web at                   |
// | http://www.php.net/license/3_0.txt. If you did not receive a copy of |
// | the PHP license and are unable to obtain it through the              |
// | world-wide-web, please send a note to license@php.net so we can mail |
// | you a copy immediately.                                              |
// | You should have received a copy of the GNU Lesser General Public     |
// | License along with this library.  If not, see                        |
// | <http://www.gnu.org/licenses/>.                                      |
// +----------------------------------------------------------------------+
// | Authors: Alexander Zhukov <alex@veresk.ru> Original port from Python |
// | Authors: Harry Fuecks <hfuecks@phppatterns.com> Port to PEAR + more  |
// | Authors: Many @ Sitepointforums Advanced PHP Forums                  |
// +----------------------------------------------------------------------+
//
// PEAR  Id: States.php,v 1.3 2007/10/29 21:41:35 hfuecks
//
/**
* Parsing states.
* @package XML_HTMLSax3
* @version  Id: States.php,v 1.3 2007/10/29 21:41:35 hfuecks
*/
/**
* Define parser states
*/
define('XML_HTMLSAX3_STATE_STOP'0);
define('XML_HTMLSAX3_STATE_START'1);
define('XML_HTMLSAX3_STATE_TAG'2);
define('XML_HTMLSAX3_STATE_OPENING_TAG'3);
define('XML_HTMLSAX3_STATE_CLOSING_TAG'4);
define('XML_HTMLSAX3_STATE_ESCAPE'6);
define('XML_HTMLSAX3_STATE_JASP'7);
define('XML_HTMLSAX3_STATE_PI'8);
/**
* StartingState searches for the start of any XML tag
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_StartingState  {
    
/**
    * @param XML_HTMLSax3_StateParser subclass
    * @return constant XML_HTMLSAX3_STATE_TAG
    * @access protected
    */
    
function parse(&$context) {
        
$data $context->scanUntilString('<');
        if (
$data != '') {
            
$context->handler_object_data->
                {
$context->handler_method_data}($context->htmlsax$data);
        }
        
$context->IgnoreCharacter();
        return 
XML_HTMLSAX3_STATE_TAG;
    }
}
/**
* Decides which state to move one from after StartingState
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_TagState {
    
/**
    * @param XML_HTMLSax3_StateParser subclass
    * @return constant the next state to move into
    * @access protected
    */
    
function parse(&$context) {
        switch(
$context->ScanCharacter()) {
        case 
'/':
            return 
XML_HTMLSAX3_STATE_CLOSING_TAG;
            break;
        case 
'?':
            return 
XML_HTMLSAX3_STATE_PI;
            break;
        case 
'%':
            return 
XML_HTMLSAX3_STATE_JASP;
            break;
        case 
'!':
            return 
XML_HTMLSAX3_STATE_ESCAPE;
            break;
        default:
            
$context->unscanCharacter();
            return 
XML_HTMLSAX3_STATE_OPENING_TAG;
        }
    }
}
/**
* Dealing with closing XML tags
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_ClosingTagState {
    
/**
    * @param XML_HTMLSax3_StateParser subclass
    * @return constant XML_HTMLSAX3_STATE_START
    * @access protected
    */
    
function parse(&$context) {
        
$tag $context->scanUntilCharacters('/>');
        if (
$tag != '') {
            
$char $context->scanCharacter();
            if (
$char == '/') {
                
$char $context->scanCharacter();
                if (
$char != '>') {
                    
$context->unscanCharacter();
                }
            }
            
$context->handler_object_element->
                {
$context->handler_method_closing}($context->htmlsax$tagFALSE);
        }
        return 
XML_HTMLSAX3_STATE_START;
    }
}
/**
* Dealing with opening XML tags
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_OpeningTagState {
    
/**
    * Handles attributes
    * @param string attribute name
    * @param string attribute value
    * @return void
    * @access protected
    * @see XML_HTMLSax3_AttributeStartState
    */
    
function parseAttributes(&$context) {
        
$Attributes = array();
    
        
$context->ignoreWhitespace();
        
$attributename $context->scanUntilCharacters("=/> \n\r\t");
        while (
$attributename != '') {
            
$attributevalue NULL;
            
$context->ignoreWhitespace();
            
$char $context->scanCharacter();
            if (
$char == '=') {
                
$context->ignoreWhitespace();
                
$char $context->ScanCharacter();
                if (
$char == '"') {
                    
$attributevalue$context->scanUntilString('"');
                    
$context->IgnoreCharacter();
                } else if (
$char == "'") {
                    
$attributevalue $context->scanUntilString("'");
                    
$context->IgnoreCharacter();
                } else {
                    
$context->unscanCharacter();
                    
$attributevalue =
                        
$context->scanUntilCharacters("> \n\r\t");
                }
            } else if (
$char !== NULL) {
                
$attributevalue NULL;
                
$context->unscanCharacter();
            }
            
$Attributes[$attributename] = $attributevalue;
            
            
$context->ignoreWhitespace();
            
$attributename $context->scanUntilCharacters("=/> \n\r\t");
        }
        return 
$Attributes;
    }

    
/**
    * @param XML_HTMLSax3_StateParser subclass
    * @return constant XML_HTMLSAX3_STATE_START
    * @access protected
    */
    
function parse(&$context) {
        
$tag $context->scanUntilCharacters("/> \n\r\t");
        if (
$tag != '') {
            
$this->attrs = array();
            
$Attributes $this->parseAttributes($context);
            
$char $context->scanCharacter();
            if (
$char == '/') {
                
$char $context->scanCharacter();
                if (
$char != '>') {
                    
$context->unscanCharacter();
                }
                
$context->handler_object_element->
                    {
$context->handler_method_opening}($context->htmlsax$tag
                    
$AttributesTRUE);
                
$context->handler_object_element->
                    {
$context->handler_method_closing}($context->htmlsax$tag
                    
TRUE);
            } else {
                
$context->handler_object_element->
                    {
$context->handler_method_opening}($context->htmlsax$tag
                    
$AttributesFALSE);
            }
        }
        return 
XML_HTMLSAX3_STATE_START;
    }
}

/**
* Deals with XML escapes handling comments and CDATA correctly
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_EscapeState {
    
/**
    * @param XML_HTMLSax3_StateParser subclass
    * @return constant XML_HTMLSAX3_STATE_START
    * @access protected
    */
    
function parse(&$context) {
        
$char $context->ScanCharacter();
        if (
$char == '-') {
            
$char $context->ScanCharacter();
            if (
$char == '-') {
                
$context->unscanCharacter();
                
$context->unscanCharacter();
                
$text $context->scanUntilString('-->');
                
$text .= $context->scanCharacter();
                
$text .= $context->scanCharacter();
            } else {
                
$context->unscanCharacter();
                
$text $context->scanUntilString('>');
            }
        } else if ( 
$char == '[') {
            
$context->unscanCharacter();
            
$text $context->scanUntilString(']>');
            
$text.= $context->scanCharacter();
        } else {
            
$context->unscanCharacter();
            
$text $context->scanUntilString('>');
        }

        
$context->IgnoreCharacter();
        if (
$text != '') {
            
$context->handler_object_escape->
            {
$context->handler_method_escape}($context->htmlsax$text);
        }
        return 
XML_HTMLSAX3_STATE_START;
    }
}
/**
* Deals with JASP/ASP markup
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_JaspState {
    
/**
    * @param XML_HTMLSax3_StateParser subclass
    * @return constant XML_HTMLSAX3_STATE_START
    * @access protected
    */
    
function parse(&$context) {
        
$text $context->scanUntilString('%>');
        if (
$text != '') {
            
$context->handler_object_jasp->
                {
$context->handler_method_jasp}($context->htmlsax$text);
        }
        
$context->IgnoreCharacter();
        
$context->IgnoreCharacter();
        return 
XML_HTMLSAX3_STATE_START;
    }
}
/**
* Deals with XML processing instructions
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_PiState {
    
/**
    * @param XML_HTMLSax3_StateParser subclass
    * @return constant XML_HTMLSAX3_STATE_START
    * @access protected
    */
    
function parse(&$context) {
        
$target $context->scanUntilCharacters(" \n\r\t");
        
$data $context->scanUntilString('?>');
        if (
$data != '') {
            
$context->handler_object_pi->
            {
$context->handler_method_pi}($context->htmlsax$target$data);
        }
        
$context->IgnoreCharacter();
        
$context->IgnoreCharacter();
        return 
XML_HTMLSAX3_STATE_START;
    }
}
?>
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
//
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to either version 3.0 of the PHP license |
// | or version 3.0 of the GNU Lesser General Public License.             |
// | The PHP license 3.0 is bundled with this package in the file LICENSE,|
// | and is available at  through the world-wide-web at                   |
// | http://www.php.net/license/3_0.txt. If you did not receive a copy of |
// | the PHP license and are unable to obtain it through the              |
// | world-wide-web, please send a note to license@php.net so we can mail |
// | you a copy immediately.                                              |
// | You should have received a copy of the GNU Lesser General Public     |
// | License along with this library.  If not, see                        |
// | <http://www.gnu.org/licenses/>.                                      |
// +----------------------------------------------------------------------+
// | Authors: Alexander Zhukov <alex@veresk.ru> Original port from Python |
// | Authors: Harry Fuecks <hfuecks@phppatterns.com> Port to PEAR + more  |
// | Authors: Many @ Sitepointforums Advanced PHP Forums                  |
// +----------------------------------------------------------------------+
//
// PEAR  Id: Decorators.php,v 1.2 2007/10/29 21:41:35 hfuecks
//
/**
* Decorators for dealing with parser options
* @package XML_HTMLSax3
* @version  Id: Decorators.php,v 1.2 2007/10/29 21:41:35 hfuecks
* @see XML_HTMLSax3::set_option
*/
/**
* Trims the contents of element data from whitespace at start and end
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_Trim {
    
/**
    * Original handler object
    * @var object
    * @access private
    */
    
var $orig_obj;
    
/**
    * Original handler method
    * @var string
    * @access private
    */
    
var $orig_method;
    
/**
    * Constructs XML_HTMLSax3_Trim
    * @param object handler object being decorated
    * @param string original handler method
    * @access protected
    */
    
function XML_HTMLSax3_Trim(&$orig_obj$orig_method) {
        
$this->orig_obj =& $orig_obj;
        
$this->orig_method $orig_method;
    }
    
/**
    * Trims the data
    * @param XML_HTMLSax3
    * @param string element data
    * @access protected
    */
    
function trimData(&$parser$data) {
        
$data trim($data);
        if (
$data != '') {
            
$this->orig_obj->{$this->orig_method}($parser$data);
        }
    }
}
/**
* Coverts tag names to upper case
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_CaseFolding {
    
/**
    * Original handler object
    * @var object
    * @access private
    */
    
var $orig_obj;
    
/**
    * Original open handler method
    * @var string
    * @access private
    */
    
var $orig_open_method;
    
/**
    * Original close handler method
    * @var string
    * @access private
    */
    
var $orig_close_method;
    
/**
    * Constructs XML_HTMLSax3_CaseFolding
    * @param object handler object being decorated
    * @param string original open handler method
    * @param string original close handler method
    * @access protected
    */
    
function XML_HTMLSax3_CaseFolding(&$orig_obj$orig_open_method$orig_close_method) {
        
$this->orig_obj =& $orig_obj;
        
$this->orig_open_method $orig_open_method;
        
$this->orig_close_method $orig_close_method;
    }
    
/**
    * Folds up open tag callbacks
    * @param XML_HTMLSax3
    * @param string tag name
    * @param array tag attributes
    * @access protected
    */
    
function foldOpen(&$parser$tag$attrs=array(), $empty FALSE) {
        
$this->orig_obj->{$this->orig_open_method}($parserstrtoupper($tag), $attrs$empty);
    }
    
/**
    * Folds up close tag callbacks
    * @param XML_HTMLSax3
    * @param string tag name
    * @access protected
    */
    
function foldClose(&$parser$tag$empty FALSE) {
        
$this->orig_obj->{$this->orig_close_method}($parserstrtoupper($tag), $empty);
    }
}
/**
* Breaks up data by linefeed characters, resulting in additional
* calls to the data handler
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_Linefeed {
    
/**
    * Original handler object
    * @var object
    * @access private
    */
    
var $orig_obj;
    
/**
    * Original handler method
    * @var string
    * @access private
    */
    
var $orig_method;
    
/**
    * Constructs XML_HTMLSax3_LineFeed
    * @param object handler object being decorated
    * @param string original handler method
    * @access protected
    */
    
function XML_HTMLSax3_LineFeed(&$orig_obj$orig_method) {
        
$this->orig_obj =& $orig_obj;
        
$this->orig_method $orig_method;
    }
    
/**
    * Breaks the data up by linefeeds
    * @param XML_HTMLSax3
    * @param string element data
    * @access protected
    */
    
function breakData(&$parser$data) {
        
$data explode("\n",$data);
        foreach ( 
$data as $chunk ) {
            
$this->orig_obj->{$this->orig_method}($parser$chunk);
        }
    }
}
/**
* Breaks up data by tab characters, resulting in additional
* calls to the data handler
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_Tab {
    
/**
    * Original handler object
    * @var object
    * @access private
    */
    
var $orig_obj;
    
/**
    * Original handler method
    * @var string
    * @access private
    */
    
var $orig_method;
    
/**
    * Constructs XML_HTMLSax3_Tab
    * @param object handler object being decorated
    * @param string original handler method
    * @access protected
    */
    
function XML_HTMLSax3_Tab(&$orig_obj$orig_method) {
        
$this->orig_obj =& $orig_obj;
        
$this->orig_method $orig_method;
    }
    
/**
    * Breaks the data up by linefeeds
    * @param XML_HTMLSax3
    * @param string element data
    * @access protected
    */
    
function breakData(&$parser$data) {
        
$data explode("\t",$data);
        foreach ( 
$data as $chunk ) {
            
$this->orig_obj->{$this->orig_method}($this$chunk);
        }
    }
}
/**
* Breaks up data by XML entities and parses them with html_entity_decode(),
* resulting in additional calls to the data handler<br />
* Requires PHP 4.3.0+
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_Entities_Parsed {
    
/**
    * Original handler object
    * @var object
    * @access private
    */
    
var $orig_obj;
    
/**
    * Original handler method
    * @var string
    * @access private
    */
    
var $orig_method;
    
/**
    * Constructs XML_HTMLSax3_Entities_Parsed
    * @param object handler object being decorated
    * @param string original handler method
    * @access protected
    */
    
function XML_HTMLSax3_Entities_Parsed(&$orig_obj$orig_method) {
        
$this->orig_obj =& $orig_obj;
        
$this->orig_method $orig_method;
    }
    
/**
    * Breaks the data up by XML entities
    * @param XML_HTMLSax3
    * @param string element data
    * @access protected
    */
    
function breakData(&$parser$data) {
        
$data preg_split('/(&.+?;)/',$data,-1,PREG_SPLIT_DELIM_CAPTURE PREG_SPLIT_NO_EMPTY);
        foreach ( 
$data as $chunk ) {
            
$chunk html_entity_decode($chunk,ENT_NOQUOTES);
            
$this->orig_obj->{$this->orig_method}($this$chunk);
        }
    }
}
/**
* Compatibility with older PHP versions
*/
if (version_compare(phpversion(), '4.3''<') && !function_exists('html_entity_decode') ) {
    function 
html_entity_decode($str$style=ENT_NOQUOTES) {
        return 
strtr($str,
            
array_flip(get_html_translation_table(HTML_ENTITIES,$style)));
    }
}
/**
* Breaks up data by XML entities but leaves them unparsed,
* resulting in additional calls to the data handler<br />
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_Entities_Unparsed {
    
/**
    * Original handler object
    * @var object
    * @access private
    */
    
var $orig_obj;
    
/**
    * Original handler method
    * @var string
    * @access private
    */
    
var $orig_method;
    
/**
    * Constructs XML_HTMLSax3_Entities_Unparsed
    * @param object handler object being decorated
    * @param string original handler method
    * @access protected
    */
    
function XML_HTMLSax3_Entities_Unparsed(&$orig_obj$orig_method) {
        
$this->orig_obj =& $orig_obj;
        
$this->orig_method $orig_method;
    }
    
/**
    * Breaks the data up by XML entities
    * @param XML_HTMLSax3
    * @param string element data
    * @access protected
    */
    
function breakData(&$parser$data) {
        
$data preg_split('/(&.+?;)/',$data,-1,PREG_SPLIT_DELIM_CAPTURE PREG_SPLIT_NO_EMPTY);
        foreach ( 
$data as $chunk ) {
            
$this->orig_obj->{$this->orig_method}($this$chunk);
        }
    }
}

/**
* Strips the HTML comment markers or CDATA sections from an escape.
* If XML_OPTIONS_FULL_ESCAPES is on, this decorator is not used.<br />
* @package XML_HTMLSax3
* @access protected
*/
class XML_HTMLSax3_Escape_Stripper {
    
/**
    * Original handler object
    * @var object
    * @access private
    */
    
var $orig_obj;
    
/**
    * Original handler method
    * @var string
    * @access private
    */
    
var $orig_method;
    
/**
    * Constructs XML_HTMLSax3_Entities_Unparsed
    * @param object handler object being decorated
    * @param string original handler method
    * @access protected
    */
    
function XML_HTMLSax3_Escape_Stripper(&$orig_obj$orig_method) {
        
$this->orig_obj =& $orig_obj;
        
$this->orig_method $orig_method;
    }
    
/**
    * Breaks the data up by XML entities
    * @param XML_HTMLSax3
    * @param string element data
    * @access protected
    */
    
function strip(&$parser$data) {
        
// Check for HTML comments first
        
if ( substr($data,0,2) == '--' ) {
            
$patterns = array(
                
'/^\-\-/',          // Opening comment: --
                
'/\-\-$/',          // Closing comment: --
            
);
            
$data preg_replace($patterns,'',$data);

        
// Check for XML CDATA sections (note: don't do both!)
        
} else if ( substr($data,0,1) == '[' ) {
            
$patterns = array(
                
'/^\[.*CDATA.*\[/s'// Opening CDATA
                
'/\].*\]$/s',       // Closing CDATA
                
);
            
$data preg_replace($patterns,'',$data);
        }

        
$this->orig_obj->{$this->orig_method}($this$data);
    }
}
?>