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
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
|
package XML::Parser::Expat;
use strict;
#use warnings; No warnings numeric??
use XSLoader;
use Carp;
our $VERSION = '2.47';
our ( %Encoding_Table, @Encoding_Path, $have_File_Spec );
use File::Spec ();
%Encoding_Table = ();
if ($have_File_Spec) {
@Encoding_Path = (
grep( -d $_,
map( File::Spec->catdir( $_, qw(XML Parser Encodings) ),
@INC ) ),
File::Spec->curdir
);
}
else {
@Encoding_Path = ( grep( -d $_, map( $_ . '/XML/Parser/Encodings', @INC ) ), '.' );
}
XSLoader::load( 'XML::Parser::Expat', $VERSION );
our %Handler_Setters = (
Start => \&SetStartElementHandler,
End => \&SetEndElementHandler,
Char => \&SetCharacterDataHandler,
Proc => \&SetProcessingInstructionHandler,
Comment => \&SetCommentHandler,
CdataStart => \&SetStartCdataHandler,
CdataEnd => \&SetEndCdataHandler,
Default => \&SetDefaultHandler,
Unparsed => \&SetUnparsedEntityDeclHandler,
Notation => \&SetNotationDeclHandler,
ExternEnt => \&SetExternalEntityRefHandler,
ExternEntFin => \&SetExtEntFinishHandler,
Entity => \&SetEntityDeclHandler,
Element => \&SetElementDeclHandler,
Attlist => \&SetAttListDeclHandler,
Doctype => \&SetDoctypeHandler,
DoctypeFin => \&SetEndDoctypeHandler,
XMLDecl => \&SetXMLDeclHandler
);
sub new {
my ( $class, %args ) = @_;
my $self = bless \%args, $_[0];
$args{_State_} = 0;
$args{Context} = [];
$args{Namespaces} ||= 0;
$args{ErrorMessage} ||= '';
if ( $args{Namespaces} ) {
$args{Namespace_Table} = {};
$args{Namespace_List} = [undef];
$args{Prefix_Table} = {};
$args{New_Prefixes} = [];
}
$args{_Setters} = \%Handler_Setters;
$args{Parser} = ParserCreate(
$self, $args{ProtocolEncoding},
$args{Namespaces}
);
$self;
}
sub load_encoding {
my ($file) = @_;
$file =~ s!([^/]+)$!\L$1\E!;
$file .= '.enc' unless $file =~ /\.enc$/;
unless ( $file =~ m!^/! ) {
foreach (@Encoding_Path) {
my $tmp = (
$have_File_Spec
? File::Spec->catfile( $_, $file )
: "$_/$file"
);
if ( -e $tmp ) {
$file = $tmp;
last;
}
}
}
open( my $fh, '<', $file ) or croak("Couldn't open encmap $file:\n$!\n");
binmode($fh);
my $data;
my $br = sysread( $fh, $data, -s $file );
croak("Trouble reading $file:\n$!\n")
unless defined($br);
close($fh);
my $name = LoadEncoding( $data, $br );
croak("$file isn't an encmap file")
unless defined($name);
$name;
} # End load_encoding
sub setHandlers {
my ( $self, @handler_pairs ) = @_;
croak("Uneven number of arguments to setHandlers method")
if ( int(@handler_pairs) & 1 );
my @ret;
while (@handler_pairs) {
my $type = shift @handler_pairs;
my $handler = shift @handler_pairs;
croak 'Handler for $type not a Code ref'
unless ( !defined($handler) or !$handler or ref($handler) eq 'CODE' );
my $hndl = $self->{_Setters}->{$type};
unless ( defined($hndl) ) {
my @types = sort keys %{ $self->{_Setters} };
croak("Unknown Expat handler type: $type\n Valid types: @types");
}
my $old = &$hndl( $self->{Parser}, $handler );
push( @ret, $type, $old );
}
return @ret;
}
sub xpcroak {
my ( $self, $message ) = @_;
my $eclines = $self->{ErrorContext};
my $line = GetCurrentLineNumber( $_[0]->{Parser} );
$message .= " at line $line";
$message .= ":\n" . $self->position_in_context($eclines)
if defined($eclines);
croak $message;
}
sub xpcarp {
my ( $self, $message ) = @_;
my $eclines = $self->{ErrorContext};
my $line = GetCurrentLineNumber( $_[0]->{Parser} );
$message .= ' at line $line';
$message .= ":\n" . $self->position_in_context($eclines)
if defined($eclines);
carp $message;
}
sub default_current {
my $self = shift;
if ( $self->{_State_} == 1 ) {
return DefaultCurrent( $self->{Parser} );
}
}
sub recognized_string {
my $self = shift;
if ( $self->{_State_} == 1 ) {
return RecognizedString( $self->{Parser} );
}
}
sub original_string {
my $self = shift;
if ( $self->{_State_} == 1 ) {
return OriginalString( $self->{Parser} );
}
}
sub current_line {
my $self = shift;
if ( $self->{_State_} == 1 ) {
return GetCurrentLineNumber( $self->{Parser} );
}
}
sub current_column {
my $self = shift;
if ( $self->{_State_} == 1 ) {
return GetCurrentColumnNumber( $self->{Parser} );
}
}
sub current_byte {
my $self = shift;
if ( $self->{_State_} == 1 ) {
return GetCurrentByteIndex( $self->{Parser} );
}
}
sub base {
my ( $self, $newbase ) = @_;
my $p = $self->{Parser};
my $oldbase = GetBase($p);
SetBase( $p, $newbase ) if @_ > 1;
return $oldbase;
}
sub context {
my $ctx = $_[0]->{Context};
@$ctx;
}
sub current_element {
my ($self) = @_;
@{ $self->{Context} } ? $self->{Context}->[-1] : undef;
}
sub in_element {
my ( $self, $element ) = @_;
@{ $self->{Context} }
? $self->eq_name( $self->{Context}->[-1], $element )
: undef;
}
sub within_element {
my ( $self, $element ) = @_;
my $cnt = 0;
foreach ( @{ $self->{Context} } ) {
$cnt++ if $self->eq_name( $_, $element );
}
return $cnt;
}
sub depth {
my ($self) = @_;
int( @{ $self->{Context} } );
}
sub element_index {
my ($self) = @_;
if ( $self->{_State_} == 1 ) {
return ElementIndex( $self->{Parser} );
}
}
################
# Namespace methods
sub namespace {
my ( $self, $name ) = @_;
local ($^W) = 0;
$self->{Namespace_List}->[ int($name) ];
}
sub eq_name {
my ( $self, $nm1, $nm2 ) = @_;
local ($^W) = 0;
int($nm1) == int($nm2) and $nm1 eq $nm2;
}
sub generate_ns_name {
my ( $self, $name, $namespace ) = @_;
$namespace
? GenerateNSName(
$name, $namespace, $self->{Namespace_Table},
$self->{Namespace_List}
)
: $name;
}
sub new_ns_prefixes {
my ($self) = @_;
if ( $self->{Namespaces} ) {
return @{ $self->{New_Prefixes} };
}
return ();
}
sub expand_ns_prefix {
my ( $self, $prefix ) = @_;
if ( $self->{Namespaces} ) {
my $stack = $self->{Prefix_Table}->{$prefix};
return ( defined($stack) and @$stack ) ? $stack->[-1] : undef;
}
return undef;
}
sub current_ns_prefixes {
my ($self) = @_;
if ( $self->{Namespaces} ) {
my %set = %{ $self->{Prefix_Table} };
if ( exists $set{'#default'} and not defined( $set{'#default'}->[-1] ) ) {
delete $set{'#default'};
}
return keys %set;
}
return ();
}
################################################################
# Namespace declaration handlers
#
sub NamespaceStart {
my ( $self, $prefix, $uri ) = @_;
$prefix = '#default' unless defined $prefix;
my $stack = $self->{Prefix_Table}->{$prefix};
if ( defined $stack ) {
push( @$stack, $uri );
}
else {
$self->{Prefix_Table}->{$prefix} = [$uri];
}
# The New_Prefixes list gets emptied at end of startElement function
# in Expat.xs
push( @{ $self->{New_Prefixes} }, $prefix );
}
sub NamespaceEnd {
my ( $self, $prefix ) = @_;
$prefix = '#default' unless defined $prefix;
my $stack = $self->{Prefix_Table}->{$prefix};
if ( @$stack > 1 ) {
pop(@$stack);
}
else {
delete $self->{Prefix_Table}->{$prefix};
}
}
################
sub specified_attr {
my $self = shift;
if ( $self->{_State_} == 1 ) {
return GetSpecifiedAttributeCount( $self->{Parser} );
}
}
sub finish {
my ($self) = @_;
if ( $self->{_State_} == 1 ) {
my $parser = $self->{Parser};
UnsetAllHandlers($parser);
}
}
sub position_in_context {
my ( $self, $lines ) = @_;
if ( $self->{_State_} == 1 ) {
my $parser = $self->{Parser};
my ( $string, $linepos ) = PositionContext( $parser, $lines );
return '' unless defined($string);
my $col = GetCurrentColumnNumber($parser);
my $ptr = ( '=' x ( $col - 1 ) ) . '^' . "\n";
my $ret;
my $dosplit = $linepos < length($string);
$string .= "\n" unless $string =~ /\n$/;
if ($dosplit) {
$ret = substr( $string, 0, $linepos ) . $ptr . substr( $string, $linepos );
}
else {
$ret = $string . $ptr;
}
return $ret;
}
}
sub xml_escape {
my $self = shift;
my $text = shift;
study $text;
$text =~ s/\&/\&/g;
$text =~ s/</\</g;
foreach (@_) {
croak "xml_escape: '$_' isn't a single character" if length($_) > 1;
if ( $_ eq '>' ) {
$text =~ s/>/\>/g;
}
elsif ( $_ eq '"' ) {
$text =~ s/\"/\"/;
}
elsif ( $_ eq "'" ) {
$text =~ s/\'/\'/;
}
else {
my $rep = '&#' . sprintf( 'x%X', ord($_) ) . ';';
if (/\W/) {
my $ptrn = "\\$_";
$text =~ s/$ptrn/$rep/g;
}
else {
$text =~ s/$_/$rep/g;
}
}
}
$text;
}
sub skip_until {
my $self = shift;
if ( $self->{_State_} <= 1 ) {
SkipUntil( $self->{Parser}, $_[0] );
}
}
sub release {
my $self = shift;
ParserRelease( $self->{Parser} );
}
sub DESTROY {
my $self = shift;
ParserFree( $self->{Parser} );
}
sub parse {
my $self = shift;
my $arg = shift;
croak 'Parse already in progress (Expat)' if $self->{_State_};
$self->{_State_} = 1;
my $parser = $self->{Parser};
my $ioref;
my $result = 0;
if ( defined $arg ) {
local *@;
if ( ref($arg) and UNIVERSAL::isa( $arg, 'IO::Handle' ) ) {
$ioref = $arg;
}
elsif ( $] < 5.008 and defined tied($arg) ) {
require IO::Handle;
$ioref = $arg;
}
else {
require IO::Handle;
eval {
no strict 'refs';
$ioref = *{$arg}{IO} if defined *{$arg};
};
if ( ref($ioref) eq 'FileHandle' ) {
#for perl 5.10.x and possibly earlier, see t/file_open_scalar.t
require FileHandle;
}
}
}
if ( defined($ioref) ) {
my $delim = $self->{Stream_Delimiter};
my $prev_rs;
my $ioclass = ref $ioref;
$ioclass = 'IO::Handle' if !length $ioclass;
$prev_rs = $ioclass->input_record_separator("\n$delim\n")
if defined($delim);
$result = ParseStream( $parser, $ioref, $delim );
$ioclass->input_record_separator($prev_rs)
if defined($delim);
}
else {
$result = ParseString( $parser, $arg );
}
$self->{_State_} = 2;
$result or croak $self->{ErrorMessage};
}
sub parsestring {
my $self = shift;
$self->parse(@_);
}
sub parsefile {
my $self = shift;
croak 'Parser has already been used' if $self->{_State_};
open( my $fh, '<', $_[0] ) or croak "Couldn't open $_[0]:\n$!";
binmode($fh);
my $ret = $self->parse($fh);
close($fh);
$ret;
}
################################################################
package #hide from PAUSE
XML::Parser::ContentModel;
use overload '""' => \&asString, 'eq' => \&thiseq;
sub EMPTY () { 1 }
sub ANY () { 2 }
sub MIXED () { 3 }
sub NAME () { 4 }
sub CHOICE () { 5 }
sub SEQ () { 6 }
sub isempty {
return $_[0]->{Type} == EMPTY;
}
sub isany {
return $_[0]->{Type} == ANY;
}
sub ismixed {
return $_[0]->{Type} == MIXED;
}
sub isname {
return $_[0]->{Type} == NAME;
}
sub name {
return $_[0]->{Tag};
}
sub ischoice {
return $_[0]->{Type} == CHOICE;
}
sub isseq {
return $_[0]->{Type} == SEQ;
}
sub quant {
return $_[0]->{Quant};
}
sub children {
my $children = $_[0]->{Children};
if ( defined $children ) {
return @$children;
}
return undef;
}
sub asString {
my ($self) = @_;
my $ret;
if ( $self->{Type} == NAME ) {
$ret = $self->{Tag};
}
elsif ( $self->{Type} == EMPTY ) {
return 'EMPTY';
}
elsif ( $self->{Type} == ANY ) {
return 'ANY';
}
elsif ( $self->{Type} == MIXED ) {
$ret = '(#PCDATA';
foreach ( @{ $self->{Children} } ) {
$ret .= '|' . $_;
}
$ret .= ')';
}
else {
my $sep = $self->{Type} == CHOICE ? '|' : ',';
$ret = '(' . join( $sep, map { $_->asString } @{ $self->{Children} } ) . ')';
}
$ret .= $self->{Quant} if $self->{Quant};
return $ret;
}
sub thiseq {
my $self = shift;
return $self->asString eq $_[0];
}
################################################################
package #hide from PAUSE
XML::Parser::ExpatNB;
use Carp;
our @ISA = qw(XML::Parser::Expat);
sub parse {
my $self = shift;
my $class = ref($self);
croak "parse method not supported in $class";
}
sub parsestring {
my $self = shift;
my $class = ref($self);
croak "parsestring method not supported in $class";
}
sub parsefile {
my $self = shift;
my $class = ref($self);
croak "parsefile method not supported in $class";
}
sub parse_more {
my ( $self, $data ) = @_;
$self->{_State_} = 1;
my $ret = XML::Parser::Expat::ParsePartial( $self->{Parser}, $data );
croak $self->{ErrorMessage} unless $ret;
}
sub parse_done {
my $self = shift;
my $ret = XML::Parser::Expat::ParseDone( $self->{Parser} );
unless ($ret) {
my $msg = $self->{ErrorMessage};
$self->release;
croak $msg;
}
$self->{_State_} = 2;
my $result = $ret;
my @result = ();
my $final = $self->{FinalHandler};
if ( defined $final ) {
if (wantarray) {
@result = &$final($self);
}
else {
$result = &$final($self);
}
}
$self->release;
return unless defined wantarray;
return wantarray ? @result : $result;
}
################################################################
package #hide from PAUSE
XML::Parser::Encinfo;
sub DESTROY {
my $self = shift;
XML::Parser::Expat::FreeEncoding($self);
}
1;
__END__
=head1 NAME
XML::Parser::Expat - Lowlevel access to James Clark's expat XML parser
=head1 SYNOPSIS
use XML::Parser::Expat;
$parser = XML::Parser::Expat->new;
$parser->setHandlers('Start' => \&sh,
'End' => \&eh,
'Char' => \&ch);
open(my $fh, '<', 'info.xml') or die "Couldn't open";
$parser->parse($fh);
close($fh);
# $parser->parse('<foo id="me"> here <em>we</em> go </foo>');
sub sh
{
my ($p, $el, %atts) = @_;
$p->setHandlers('Char' => \&spec)
if ($el eq 'special');
...
}
sub eh
{
my ($p, $el) = @_;
$p->setHandlers('Char' => \&ch) # Special elements won't contain
if ($el eq 'special'); # other special elements
...
}
=head1 DESCRIPTION
This module provides an interface to James Clark's XML parser, expat. As in
expat, a single instance of the parser can only parse one document. Calls
to parsestring after the first for a given instance will die.
Expat (and XML::Parser::Expat) are event based. As the parser recognizes
parts of the document (say the start or end of an XML element), then any
handlers registered for that type of an event are called with suitable
parameters.
=head1 METHODS
=over 4
=item new
This is a class method, the constructor for XML::Parser::Expat. Options are
passed as keyword value pairs. The recognized options are:
=over 4
=item * ProtocolEncoding
The protocol encoding name. The default is none. The expat built-in
encodings are: C<UTF-8>, C<ISO-8859-1>, C<UTF-16>, and C<US-ASCII>.
Other encodings may be used if they have encoding maps in one of the
directories in the @Encoding_Path list. Setting the protocol encoding
overrides any encoding in the XML declaration.
=item * Namespaces
When this option is given with a true value, then the parser does namespace
processing. By default, namespace processing is turned off. When it is
turned on, the parser consumes I<xmlns> attributes and strips off prefixes
from element and attributes names where those prefixes have a defined
namespace. A name's namespace can be found using the L<"namespace"> method
and two names can be checked for absolute equality with the L<"eq_name">
method.
=item * NoExpand
Normally, the parser will try to expand references to entities defined in
the internal subset. If this option is set to a true value, and a default
handler is also set, then the default handler will be called when an
entity reference is seen in text. This has no effect if a default handler
has not been registered, and it has no effect on the expansion of entity
references inside attribute values.
=item * Stream_Delimiter
This option takes a string value. When this string is found alone on a line
while parsing from a stream, then the parse is ended as if it saw an end of
file. The intended use is with a stream of xml documents in a MIME multipart
format. The string should not contain a trailing newline.
=item * ErrorContext
When this option is defined, errors are reported in context. The value
of ErrorContext should be the number of lines to show on either side of
the line in which the error occurred.
=item * ParseParamEnt
Unless standalone is set to "yes" in the XML declaration, setting this to
a true value allows the external DTD to be read, and parameter entities
to be parsed and expanded.
=item * Base
The base to use for relative pathnames or URLs. This can also be done by
using the base method.
=back
=item setHandlers(TYPE, HANDLER [, TYPE, HANDLER [...]])
This method registers handlers for the various events. If no handlers are
registered, then a call to parsestring or parsefile will only determine if
the corresponding XML document is well formed (by returning without error.)
This may be called from within a handler, after the parse has started.
Setting a handler to something that evaluates to false unsets that
handler.
This method returns a list of type, handler pairs corresponding to the
input. The handlers returned are the ones that were in effect before the
call to setHandlers.
The recognized events and the parameters passed to the corresponding
handlers are:
=over 4
=item * Start (Parser, Element [, Attr, Val [,...]])
This event is generated when an XML start tag is recognized. Parser is
an XML::Parser::Expat instance. Element is the name of the XML element that
is opened with the start tag. The Attr & Val pairs are generated for each
attribute in the start tag.
=item * End (Parser, Element)
This event is generated when an XML end tag is recognized. Note that
an XML empty tag (<foo/>) generates both a start and an end event.
There is always a lower level start and end handler installed that wrap
the corresponding callbacks. This is to handle the context mechanism.
A consequence of this is that the default handler (see below) will not
see a start tag or end tag unless the default_current method is called.
=item * Char (Parser, String)
This event is generated when non-markup is recognized. The non-markup
sequence of characters is in String. A single non-markup sequence of
characters may generate multiple calls to this handler. Whatever the
encoding of the string in the original document, this is given to the
handler in UTF-8.
=item * Proc (Parser, Target, Data)
This event is generated when a processing instruction is recognized.
=item * Comment (Parser, String)
This event is generated when a comment is recognized.
=item * CdataStart (Parser)
This is called at the start of a CDATA section.
=item * CdataEnd (Parser)
This is called at the end of a CDATA section.
=item * Default (Parser, String)
This is called for any characters that don't have a registered handler.
This includes both characters that are part of markup for which no
events are generated (markup declarations) and characters that
could generate events, but for which no handler has been registered.
Whatever the encoding in the original document, the string is returned to
the handler in UTF-8.
=item * Unparsed (Parser, Entity, Base, Sysid, Pubid, Notation)
This is called for a declaration of an unparsed entity. Entity is the name
of the entity. Base is the base to be used for resolving a relative URI.
Sysid is the system id. Pubid is the public id. Notation is the notation
name. Base and Pubid may be undefined.
=item * Notation (Parser, Notation, Base, Sysid, Pubid)
This is called for a declaration of notation. Notation is the notation name.
Base is the base to be used for resolving a relative URI. Sysid is the system
id. Pubid is the public id. Base, Sysid, and Pubid may all be undefined.
=item * ExternEnt (Parser, Base, Sysid, Pubid)
This is called when an external entity is referenced. Base is the base to be
used for resolving a relative URI. Sysid is the system id. Pubid is the public
id. Base, and Pubid may be undefined.
This handler should either return a string, which represents the contents of
the external entity, or return an open filehandle that can be read to obtain
the contents of the external entity, or return undef, which indicates the
external entity couldn't be found and will generate a parse error.
If an open filehandle is returned, it must be returned as either a glob
(*FOO) or as a reference to a glob (e.g. an instance of IO::Handle).
=item * ExternEntFin (Parser)
This is called after an external entity has been parsed. It allows
applications to perform cleanup on actions performed in the above
ExternEnt handler.
=item * Entity (Parser, Name, Val, Sysid, Pubid, Ndata, IsParam)
This is called when an entity is declared. For internal entities, the Val
parameter will contain the value and the remaining three parameters will
be undefined. For external entities, the Val parameter
will be undefined, the Sysid parameter will have the system id, the Pubid
parameter will have the public id if it was provided (it will be undefined
otherwise), the Ndata parameter will contain the notation for unparsed
entities. If this is a parameter entity declaration, then the IsParam
parameter is true.
Note that this handler and the Unparsed handler above overlap. If both are
set, then this handler will not be called for unparsed entities.
=item * Element (Parser, Name, Model)
The element handler is called when an element declaration is found. Name is
the element name, and Model is the content model as an
XML::Parser::ContentModel object. See L<"XML::Parser::ContentModel Methods">
for methods available for this class.
=item * Attlist (Parser, Elname, Attname, Type, Default, Fixed)
This handler is called for each attribute in an ATTLIST declaration.
So an ATTLIST declaration that has multiple attributes
will generate multiple calls to this handler. The Elname parameter is the
name of the element with which the attribute is being associated. The Attname
parameter is the name of the attribute. Type is the attribute type, given as
a string. Default is the default value, which will either be "#REQUIRED",
"#IMPLIED" or a quoted string (i.e. the returned string will begin and end
with a quote character). If Fixed is true, then this is a fixed attribute.
=item * Doctype (Parser, Name, Sysid, Pubid, Internal)
This handler is called for DOCTYPE declarations. Name is the document type
name. Sysid is the system id of the document type, if it was provided,
otherwise it's undefined. Pubid is the public id of the document type,
which will be undefined if no public id was given. Internal will be
true or false, indicating whether or not the doctype declaration contains
an internal subset.
=item * DoctypeFin (Parser)
This handler is called after parsing of the DOCTYPE declaration has finished,
including any internal or external DTD declarations.
=item * XMLDecl (Parser, Version, Encoding, Standalone)
This handler is called for XML declarations. Version is a string containing
the version. Encoding is either undefined or contains an encoding string.
Standalone is either undefined, or true or false. Undefined indicates
that no standalone parameter was given in the XML declaration. True or
false indicates "yes" or "no" respectively.
=back
=item namespace(name)
Return the URI of the namespace that the name belongs to. If the name doesn't
belong to any namespace, an undef is returned. This is only valid on names
received through the Start or End handlers from a single document, or through
a call to the generate_ns_name method. In other words, don't use names
generated from one instance of XML::Parser::Expat with other instances.
=item eq_name(name1, name2)
Return true if name1 and name2 are identical (i.e. same name and from
the same namespace.) This is only meaningful if both names were obtained
through the Start or End handlers from a single document, or through
a call to the generate_ns_name method.
=item generate_ns_name(name, namespace)
Return a name, associated with a given namespace, good for using with the
above 2 methods. The namespace argument should be the namespace URI, not
a prefix.
=item new_ns_prefixes
When called from a start tag handler, returns namespace prefixes declared
with this start tag. If called elsewhere (or if there were no namespace
prefixes declared), it returns an empty list. Setting of the default
namespace is indicated with '#default' as a prefix.
=item expand_ns_prefix(prefix)
Return the uri to which the given prefix is currently bound. Returns
undef if the prefix isn't currently bound. Use '#default' to find the
current binding of the default namespace (if any).
=item current_ns_prefixes
Return a list of currently bound namespace prefixes. The order of the
the prefixes in the list has no meaning. If the default namespace is
currently bound, '#default' appears in the list.
=item recognized_string
Returns the string from the document that was recognized in order to call
the current handler. For instance, when called from a start handler, it
will give us the start-tag string. The string is encoded in UTF-8.
This method doesn't return a meaningful string inside declaration handlers.
=item original_string
Returns the verbatim string from the document that was recognized in
order to call the current handler. The string is in the original document
encoding. This method doesn't return a meaningful string inside declaration
handlers.
=item default_current
When called from a handler, causes the sequence of characters that generated
the corresponding event to be sent to the default handler (if one is
registered). Use of this method is deprecated in favor the recognized_string
method, which you can use without installing a default handler. This
method doesn't deliver a meaningful string to the default handler when
called from inside declaration handlers.
=item xpcroak(message)
Concatenate onto the given message the current line number within the
XML document plus the message implied by ErrorContext. Then croak with
the formed message.
=item xpcarp(message)
Concatenate onto the given message the current line number within the
XML document plus the message implied by ErrorContext. Then carp with
the formed message.
=item current_line
Returns the line number of the current position of the parse.
=item current_column
Returns the column number of the current position of the parse.
=item current_byte
Returns the current position of the parse.
=item base([NEWBASE]);
Returns the current value of the base for resolving relative URIs. If
NEWBASE is supplied, changes the base to that value.
=item context
Returns a list of element names that represent open elements, with the
last one being the innermost. Inside start and end tag handlers, this
will be the tag of the parent element.
=item current_element
Returns the name of the innermost currently opened element. Inside
start or end handlers, returns the parent of the element associated
with those tags.
=item in_element(NAME)
Returns true if NAME is equal to the name of the innermost currently opened
element. If namespace processing is being used and you want to check
against a name that may be in a namespace, then use the generate_ns_name
method to create the NAME argument.
=item within_element(NAME)
Returns the number of times the given name appears in the context list.
If namespace processing is being used and you want to check
against a name that may be in a namespace, then use the generate_ns_name
method to create the NAME argument.
=item depth
Returns the size of the context list.
=item element_index
Returns an integer that is the depth-first visit order of the current
element. This will be zero outside of the root element. For example,
this will return 1 when called from the start handler for the root element
start tag.
=item skip_until(INDEX)
INDEX is an integer that represents an element index. When this method
is called, all handlers are suspended until the start tag for an element
that has an index number equal to INDEX is seen. If a start handler has
been set, then this is the first tag that the start handler will see
after skip_until has been called.
=item position_in_context(LINES)
Returns a string that shows the current parse position. LINES should be
an integer >= 0 that represents the number of lines on either side of the
current parse line to place into the returned string.
=item xml_escape(TEXT [, CHAR [, CHAR ...]])
Returns TEXT with markup characters turned into character entities. Any
additional characters provided as arguments are also turned into character
references where found in TEXT.
=item parse (SOURCE)
The SOURCE parameter should either be a string containing the whole XML
document, or it should be an open IO::Handle. Only a single document
may be parsed for a given instance of XML::Parser::Expat, so this will croak
if it's been called previously for this instance.
=item parsestring(XML_DOC_STRING)
Parses the given string as an XML document. Only a single document may be
parsed for a given instance of XML::Parser::Expat, so this will die if either
parsestring or parsefile has been called for this instance previously.
This method is deprecated in favor of the parse method.
=item parsefile(FILENAME)
Parses the XML document in the given file. Will die if parsestring or
parsefile has been called previously for this instance.
=item is_defaulted(ATTNAME)
NO LONGER WORKS. To find out if an attribute is defaulted please use
the specified_attr method.
=item specified_attr
When the start handler receives lists of attributes and values, the
non-defaulted (i.e. explicitly specified) attributes occur in the list
first. This method returns the number of specified items in the list.
So if this number is equal to the length of the list, there were no
defaulted values. Otherwise the number points to the index of the
first defaulted attribute name.
=item finish
Unsets all handlers (including internal ones that set context), but expat
continues parsing to the end of the document or until it finds an error.
It should finish up a lot faster than with the handlers set.
=item release
There are data structures used by XML::Parser::Expat that have circular
references. This means that these structures will never be garbage
collected unless these references are explicitly broken. Calling this
method breaks those references (and makes the instance unusable.)
Normally, higher level calls handle this for you, but if you are using
XML::Parser::Expat directly, then it's your responsibility to call it.
=back
=head2 XML::Parser::ContentModel Methods
The element declaration handlers are passed objects of this class as the
content model of the element declaration. They also represent content
particles, components of a content model.
When referred to as a string, these objects are automagically converted to a
string representation of the model (or content particle).
=over 4
=item isempty
This method returns true if the object is "EMPTY", false otherwise.
=item isany
This method returns true if the object is "ANY", false otherwise.
=item ismixed
This method returns true if the object is "(#PCDATA)" or "(#PCDATA|...)*",
false otherwise.
=item isname
This method returns if the object is an element name.
=item ischoice
This method returns true if the object is a choice of content particles.
=item isseq
This method returns true if the object is a sequence of content particles.
=item quant
This method returns undef or a string representing the quantifier
('?', '*', '+') associated with the model or particle.
=item children
This method returns undef or (for mixed, choice, and sequence types)
an array of component content particles. There will always be at least
one component for choices and sequences, but for a mixed content model
of pure PCDATA, "(#PCDATA)", then an undef is returned.
=back
=head2 XML::Parser::ExpatNB Methods
The class XML::Parser::ExpatNB is a subclass of XML::Parser::Expat used
for non-blocking access to the expat library. It does not support the parse,
parsestring, or parsefile methods, but it does have these additional methods:
=over 4
=item parse_more(DATA)
Feed expat more text to munch on.
=item parse_done
Tell expat that it's gotten the whole document.
=back
=head1 FUNCTIONS
=over 4
=item XML::Parser::Expat::load_encoding(ENCODING)
Load an external encoding. ENCODING is either the name of an encoding or
the name of a file. The basename is converted to lowercase and a '.enc'
extension is appended unless there's one already there. Then, unless
it's an absolute pathname (i.e. begins with '/'), the first file by that
name discovered in the @Encoding_Path path list is used.
The encoding in the file is loaded and kept in the %Encoding_Table
table. Earlier encodings of the same name are replaced.
This function is automatically called by expat when it encounters an encoding
it doesn't know about. Expat shouldn't call this twice for the same
encoding name. The only reason users should use this function is to
explicitly load an encoding not contained in the @Encoding_Path list.
=back
=head1 AUTHORS
Larry Wall <F<larry@wall.org>> wrote version 1.0.
Clark Cooper <F<coopercc@netheaven.com>> picked up support, changed the API
for this version (2.x), provided documentation, and added some standard
package features.
=cut
/*****************************************************************
** Expat.xs
**
** Copyright 1998 Larry Wall and Clark Cooper
** All rights reserved.
**
** This program is free software; you can redistribute it and/or
** modify it under the same terms as Perl itself.
**
*/
#include <expat.h>
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#undef convert
#include "patchlevel.h"
#include "encoding.h"
/* Version 5.005_5x (Development version for 5.006) doesn't like sv_...
anymore, but 5.004 doesn't know about PL_sv..
Don't want to push up required version just for this. */
#if PATCHLEVEL < 5
#define PL_sv_undef sv_undef
#define PL_sv_no sv_no
#define PL_sv_yes sv_yes
#define PL_na na
#endif
#define BUFSIZE 32768
#define NSDELIM '|'
/* Macro to update handler fields. Used in the various handler setting
XSUBS */
#define XMLP_UPD(fld) \
RETVAL = cbv->fld ? newSVsv(cbv->fld) : &PL_sv_undef;\
if (cbv->fld) {\
if (cbv->fld != fld)\
sv_setsv(cbv->fld, fld);\
}\
else\
cbv->fld = newSVsv(fld)
/* Macro to push old handler value onto return stack. This is done here
to get around a bug in 5.004 sv_2mortal function. */
#define PUSHRET \
ST(0) = RETVAL;\
if (RETVAL != &PL_sv_undef && SvREFCNT(RETVAL)) sv_2mortal(RETVAL)
typedef struct {
SV* self_sv;
XML_Parser p;
AV* context;
AV* new_prefix_list;
HV *nstab;
AV *nslst;
unsigned int st_serial;
unsigned int st_serial_stackptr;
unsigned int st_serial_stacksize;
unsigned int * st_serial_stack;
unsigned int skip_until;
SV *recstring;
char * delim;
STRLEN delimlen;
unsigned ns:1;
unsigned no_expand:1;
unsigned parseparam:1;
/* Callback handlers */
SV* start_sv;
SV* end_sv;
SV* char_sv;
SV* proc_sv;
SV* cmnt_sv;
SV* dflt_sv;
SV* entdcl_sv;
SV* eledcl_sv;
SV* attdcl_sv;
SV* doctyp_sv;
SV* doctypfin_sv;
SV* xmldec_sv;
SV* unprsd_sv;
SV* notation_sv;
SV* extent_sv;
SV* extfin_sv;
SV* startcd_sv;
SV* endcd_sv;
} CallbackVector;
static HV* EncodingTable = NULL;
static XML_Char nsdelim[] = {NSDELIM, '\0'};
static char *QuantChar[] = {"", "?", "*", "+"};
/* Forward declarations */
static void suspend_callbacks(CallbackVector *);
static void resume_callbacks(CallbackVector *);
#if PATCHLEVEL < 5 && SUBVERSION < 5
/* ================================================================
** This is needed where the length is explicitly given. The expat
** library may sometimes give us zero-length strings. Perl's newSVpv
** interprets a zero length as a directive to do a strlen. This
** function is used when we want to force length to mean length, even
** if zero.
*/
static SV *
newSVpvn(char *s, STRLEN len)
{
register SV *sv;
sv = newSV(0);
sv_setpvn(sv, s, len);
return sv;
} /* End newSVpvn */
#define ERRSV GvSV(errgv)
#endif
#ifdef SvUTF8_on
static SV *
newUTF8SVpv(char *s, STRLEN len) {
register SV *sv;
sv = newSVpv(s, len);
SvUTF8_on(sv);
return sv;
} /* End new UTF8SVpv */
static SV *
newUTF8SVpvn(char *s, STRLEN len) {
register SV *sv;
sv = newSV(0);
sv_setpvn(sv, s, len);
SvUTF8_on(sv);
return sv;
}
#else /* SvUTF8_on not defined */
#define newUTF8SVpv newSVpv
#define newUTF8SVpvn newSVpvn
#endif
static void*
mymalloc(size_t size) {
#ifndef LEAKTEST
return safemalloc(size);
#else
return safexmalloc(328,size);
#endif
}
static void*
myrealloc(void *p, size_t s) {
#ifndef LEAKTEST
return saferealloc(p, s);
#else
return safexrealloc(p, s);
#endif
}
static void
myfree(void *p) {
Safefree(p);
}
static XML_Memory_Handling_Suite ms = {mymalloc, myrealloc, myfree};
static void
append_error(XML_Parser parser, char * err)
{
dSP;
CallbackVector * cbv;
SV ** errstr;
cbv = (CallbackVector*) XML_GetUserData(parser);
errstr = hv_fetch((HV*)SvRV(cbv->self_sv),
"ErrorMessage", 12, 0);
if (errstr && SvPOK(*errstr)) {
SV ** errctx = hv_fetch((HV*) SvRV(cbv->self_sv),
"ErrorContext", 12, 0);
int dopos = !err && errctx && SvOK(*errctx);
if (! err)
err = (char *) XML_ErrorString(XML_GetErrorCode(parser));
sv_catpvf(*errstr, "\n%s at line %ld, column %ld, byte %ld%s",
err,
(long)XML_GetCurrentLineNumber(parser),
(long)XML_GetCurrentColumnNumber(parser),
(long)XML_GetCurrentByteIndex(parser),
dopos ? ":\n" : "");
/* See https://rt.cpan.org/Ticket/Display.html?id=92030
It explains why type conversion is used. */
if (dopos)
{
int count;
ENTER ;
SAVETMPS ;
PUSHMARK(sp);
XPUSHs(cbv->self_sv);
XPUSHs(*errctx);
PUTBACK ;
count = perl_call_method("position_in_context", G_SCALAR);
SPAGAIN ;
if (count >= 1) {
sv_catsv(*errstr, POPs);
}
PUTBACK ;
FREETMPS ;
LEAVE ;
}
}
} /* End append_error */
static SV *
generate_model(XML_Content *model) {
HV * hash = newHV();
SV * obj = newRV_noinc((SV *) hash);
sv_bless(obj, gv_stashpv("XML::Parser::ContentModel", 1));
hv_store(hash, "Type", 4, newSViv(model->type), 0);
if (model->quant != XML_CQUANT_NONE) {
hv_store(hash, "Quant", 5, newSVpv(QuantChar[model->quant], 1), 0);
}
switch(model->type) {
case XML_CTYPE_NAME:
hv_store(hash, "Tag", 3, newUTF8SVpv((char *)model->name, 0), 0);
break;
case XML_CTYPE_MIXED:
case XML_CTYPE_CHOICE:
case XML_CTYPE_SEQ:
if (model->children && model->numchildren)
{
AV * children = newAV();
int i;
for (i = 0; i < model->numchildren; i++) {
av_push(children, generate_model(&model->children[i]));
}
hv_store(hash, "Children", 8, newRV_noinc((SV *) children), 0);
}
break;
}
return obj;
} /* End generate_model */
static int
parse_stream(XML_Parser parser, SV * ioref)
{
dSP;
SV * tbuff;
SV * tsiz;
char * linebuff;
STRLEN lblen;
STRLEN br = 0;
int buffsize;
int done = 0;
int ret = 1;
char * msg = NULL;
CallbackVector * cbv;
char *buff = (char *) 0;
cbv = (CallbackVector*) XML_GetUserData(parser);
ENTER;
SAVETMPS;
if (cbv->delim) {
int cnt;
SV * tline;
PUSHMARK(SP);
XPUSHs(ioref);
PUTBACK ;
cnt = perl_call_method("getline", G_SCALAR);
SPAGAIN;
if (cnt != 1)
croak("getline method call failed");
tline = POPs;
if (! SvOK(tline)) {
lblen = 0;
}
else {
char * chk;
linebuff = SvPV(tline, lblen);
chk = &linebuff[lblen - cbv->delimlen - 1];
if (lblen > cbv->delimlen + 1
&& *chk == *cbv->delim
&& chk[cbv->delimlen] == '\n'
&& strnEQ(++chk, cbv->delim + 1, cbv->delimlen - 1))
lblen -= cbv->delimlen + 1;
}
PUTBACK ;
buffsize = lblen;
done = lblen == 0;
}
else {
tbuff = newSV(0);
tsiz = newSViv(BUFSIZE); /* in UTF-8 characters */
buffsize = BUFSIZE * 6; /* in bytes that encode an UTF-8 string */
}
while (! done)
{
char *buffer = XML_GetBuffer(parser, buffsize);
if (! buffer)
croak("Ran out of memory for input buffer");
SAVETMPS;
if (cbv->delim) {
Copy(linebuff, buffer, lblen, char);
br = lblen;
done = 1;
}
else {
int cnt;
SV * rdres;
char * tb;
PUSHMARK(SP);
EXTEND(SP, 3);
PUSHs(ioref);
PUSHs(tbuff);
PUSHs(tsiz);
PUTBACK ;
cnt = perl_call_method("read", G_SCALAR);
SPAGAIN ;
if (cnt != 1)
croak("read method call failed");
rdres = POPs;
if (! SvOK(rdres))
croak("read error");
tb = SvPV(tbuff, br);
if (br > 0) {
if (br > buffsize)
croak("The input buffer is not large enough for read UTF-8 decoded string");
Copy(tb, buffer, br, char);
} else
done = 1;
PUTBACK ;
}
ret = XML_ParseBuffer(parser, br, done);
SPAGAIN; /* resync local SP in case callbacks changed global stack */
if (! ret)
break;
FREETMPS;
}
if (! ret)
append_error(parser, msg);
if (! cbv->delim) {
SvREFCNT_dec(tsiz);
SvREFCNT_dec(tbuff);
}
FREETMPS;
LEAVE;
return ret;
} /* End parse_stream */
static SV *
gen_ns_name(const char * name, HV * ns_table, AV * ns_list)
{
char *pos = strchr(name, NSDELIM);
SV * ret;
if (pos && pos > name)
{
SV ** name_ent = hv_fetch(ns_table, (char *) name,
pos - name, TRUE);
ret = newUTF8SVpv(&pos[1], 0);
if (name_ent)
{
int index;
if (SvOK(*name_ent))
{
index = SvIV(*name_ent);
}
else
{
av_push(ns_list, newUTF8SVpv((char *) name, pos - name));
index = av_len(ns_list);
sv_setiv(*name_ent, (IV) index);
}
sv_setiv(ret, (IV) index);
SvPOK_on(ret);
}
}
else
ret = newUTF8SVpv((char *) name, 0);
return ret;
} /* End gen_ns_name */
static void
characterData(void *userData, const char *s, int len)
{
dSP;
CallbackVector* cbv = (CallbackVector*) userData;
ENTER;
SAVETMPS;
PUSHMARK(sp);
EXTEND(sp, 2);
PUSHs(cbv->self_sv);
PUSHs(sv_2mortal(newUTF8SVpvn((char*)s,len)));
PUTBACK;
perl_call_sv(cbv->char_sv, G_DISCARD);
FREETMPS;
LEAVE;
} /* End characterData */
static void
startElement(void *userData, const char *name, const char **atts)
{
dSP;
CallbackVector* cbv = (CallbackVector*) userData;
SV ** pcontext;
unsigned do_ns = cbv->ns;
unsigned skipping = 0;
SV ** pnstab;
SV ** pnslst;
SV * elname;
cbv->st_serial++;
if (cbv->skip_until) {
skipping = cbv->st_serial < cbv->skip_until;
if (! skipping) {
resume_callbacks(cbv);
cbv->skip_until = 0;
}
}
if (cbv->st_serial_stackptr >= cbv->st_serial_stacksize) {
unsigned int newsize = cbv->st_serial_stacksize + 512;
Renew(cbv->st_serial_stack, newsize, unsigned int);
cbv->st_serial_stacksize = newsize;
}
cbv->st_serial_stack[++cbv->st_serial_stackptr] = cbv->st_serial;
if (do_ns)
elname = gen_ns_name(name, cbv->nstab, cbv->nslst);
else
elname = newUTF8SVpv((char *)name, 0);
if (! skipping && SvTRUE(cbv->start_sv))
{
const char **attlim = atts;
while (*attlim)
attlim++;
ENTER;
SAVETMPS;
PUSHMARK(sp);
EXTEND(sp, attlim - atts + 2);
PUSHs(cbv->self_sv);
PUSHs(elname);
while (*atts)
{
SV * attname;
attname = (do_ns ? gen_ns_name(*atts, cbv->nstab, cbv->nslst)
: newUTF8SVpv((char *) *atts, 0));
atts++;
PUSHs(sv_2mortal(attname));
if (*atts)
PUSHs(sv_2mortal(newUTF8SVpv((char*)*atts++,0)));
}
PUTBACK;
perl_call_sv(cbv->start_sv, G_DISCARD);
FREETMPS;
LEAVE;
}
av_push(cbv->context, elname);
if (cbv->ns) {
av_clear(cbv->new_prefix_list);
}
} /* End startElement */
static void
endElement(void *userData, const char *name)
{
dSP;
CallbackVector* cbv = (CallbackVector*) userData;
SV *elname;
elname = av_pop(cbv->context);
if (! cbv->st_serial_stackptr) {
croak("endElement: Start tag serial number stack underflow");
}
if (! cbv->skip_until && SvTRUE(cbv->end_sv))
{
ENTER;
SAVETMPS;
PUSHMARK(sp);
EXTEND(sp, 2);
PUSHs(cbv->self_sv);
PUSHs(elname);
PUTBACK;
perl_call_sv(cbv->end_sv, G_DISCARD);
FREETMPS;
LEAVE;
}
cbv->st_serial_stackptr--;
SvREFCNT_dec(elname);
} /* End endElement */
static void
processingInstruction(void *userData, const char *target, const char *data)
{
dSP;
CallbackVector* cbv = (CallbackVector*) userData;
ENTER;
SAVETMPS;
PUSHMARK(sp);
EXTEND(sp, 3);
PUSHs(cbv->self_sv);
PUSHs(sv_2mortal(newUTF8SVpv((char*)target,0)));
PUSHs(sv_2mortal(newUTF8SVpv((char*)data,0)));
PUTBACK;
perl_call_sv(cbv->proc_sv, G_DISCARD);
FREETMPS;
LEAVE;
} /* End processingInstruction */
static void
commenthandle(void *userData, const char *string)
{
dSP;
CallbackVector * cbv = (CallbackVector*) userData;
ENTER;
SAVETMPS;
PUSHMARK(sp);
EXTEND(sp, 2);
PUSHs(cbv->self_sv);
PUSHs(sv_2mortal(newUTF8SVpv((char*) string, 0)));
PUTBACK;
perl_call_sv(cbv->cmnt_sv, G_DISCARD);
FREETMPS;
LEAVE;
} /* End commenthandler */
static void
startCdata(void *userData)
{
dSP;
CallbackVector* cbv = (CallbackVector*) userData;
if (cbv->startcd_sv) {
ENTER;
SAVETMPS;
PUSHMARK(sp);
XPUSHs(cbv->self_sv);
PUTBACK;
perl_call_sv(cbv->startcd_sv, G_DISCARD);
FREETMPS;
LEAVE;
}
} /* End startCdata */
static void
endCdata(void *userData)
{
dSP;
CallbackVector* cbv = (CallbackVector*) userData;
if (cbv->endcd_sv) {
ENTER;
SAVETMPS;
PUSHMARK(sp);
XPUSHs(cbv->self_sv);
PUTBACK;
perl_call_sv(cbv->endcd_sv, G_DISCARD);
FREETMPS;
LEAVE;
}
} /* End endCdata */
static void
nsStart(void *userdata, const XML_Char *prefix, const XML_Char *uri){
dSP;
CallbackVector* cbv = (CallbackVector*) userdata;
ENTER;
SAVETMPS;
PUSHMARK(sp);
EXTEND(sp, 3);
PUSHs(cbv->self_sv);
PUSHs(prefix ? sv_2mortal(newUTF8SVpv((char *)prefix, 0)) : &PL_sv_undef);
PUSHs(uri ? sv_2mortal(newUTF8SVpv((char *)uri, 0)) : &PL_sv_undef);
PUTBACK;
perl_call_method("NamespaceStart", G_DISCARD);
FREETMPS;
LEAVE;
} /* End nsStart */
static void
nsEnd(void *userdata, const XML_Char *prefix) {
dSP;
CallbackVector* cbv = (CallbackVector*) userdata;
ENTER;
SAVETMPS;
PUSHMARK(sp);
EXTEND(sp, 2);
PUSHs(cbv->self_sv);
PUSHs(prefix ? sv_2mortal(newUTF8SVpv((char *)prefix, 0)) : &PL_sv_undef);
PUTBACK;
perl_call_method("NamespaceEnd", G_DISCARD);
FREETMPS;
LEAVE;
} /* End nsEnd */
static void
defaulthandle(void *userData, const char *string, int len)
{
dSP;
CallbackVector* cbv = (CallbackVector*) userData;
ENTER;
SAVETMPS;
PUSHMARK(sp);
EXTEND(sp, 2);
PUSHs(cbv->self_sv);
PUSHs(sv_2mortal(newUTF8SVpvn((char*)string, len)));
PUTBACK;
perl_call_sv(cbv->dflt_sv, G_DISCARD);
FREETMPS;
LEAVE;
} /* End defaulthandle */
static void
elementDecl(void *data,
const char *name,
XML_Content *model) {
dSP;
CallbackVector *cbv = (CallbackVector*) data;
SV *cmod;
ENTER;
SAVETMPS;
cmod = generate_model(model);
Safefree(model);
PUSHMARK(sp);
EXTEND(sp, 3);
PUSHs(cbv->self_sv);
PUSHs(sv_2mortal(newUTF8SVpv((char *)name, 0)));
PUSHs(sv_2mortal(cmod));
PUTBACK;
perl_call_sv(cbv->eledcl_sv, G_DISCARD);
FREETMPS;
LEAVE;
} /* End elementDecl */
static void
attributeDecl(void *data,
const char * elname,
const char * attname,
const char * att_type,
const char * dflt,
int reqorfix) {
dSP;
CallbackVector *cbv = (CallbackVector*) data;
SV * dfltsv;
if (dflt) {
dfltsv = newUTF8SVpv("'", 1);
sv_catpv(dfltsv, (char *) dflt);
sv_catpv(dfltsv, "'");
}
else {
dfltsv = newUTF8SVpv(reqorfix ? "#REQUIRED" : "#IMPLIED", 0);
}
ENTER;
SAVETMPS;
PUSHMARK(sp);
EXTEND(sp, 5);
PUSHs(cbv->self_sv);
PUSHs(sv_2mortal(newUTF8SVpv((char *)elname, 0)));
PUSHs(sv_2mortal(newUTF8SVpv((char *)attname, 0)));
PUSHs(sv_2mortal(newUTF8SVpv((char *)att_type, 0)));
PUSHs(sv_2mortal(dfltsv));
if (dflt && reqorfix)
XPUSHs(&PL_sv_yes);
PUTBACK;
perl_call_sv(cbv->attdcl_sv, G_DISCARD);
FREETMPS;
LEAVE;
} /* End attributeDecl */
static void
entityDecl(void *data,
const char *name,
int isparam,
const char *value,
int vlen,
const char *base,
const char *sysid,
const char *pubid,
const char *notation) {
dSP;
CallbackVector *cbv = (CallbackVector*) data;
ENTER;
SAVETMPS;
PUSHMARK(sp);
EXTEND(sp, 6);
PUSHs(cbv->self_sv);
PUSHs(sv_2mortal(newUTF8SVpv((char*)name, 0)));
PUSHs(value ? sv_2mortal(newUTF8SVpvn((char*)value, vlen)) : &PL_sv_undef);
PUSHs(sysid ? sv_2mortal(newUTF8SVpv((char *)sysid, 0)) : &PL_sv_undef);
PUSHs(pubid ? sv_2mortal(newUTF8SVpv((char *)pubid, 0)) : &PL_sv_undef);
PUSHs(notation ? sv_2mortal(newUTF8SVpv((char *)notation, 0)) : &PL_sv_undef);
if (isparam)
XPUSHs(&PL_sv_yes);
PUTBACK;
perl_call_sv(cbv->entdcl_sv, G_DISCARD);
FREETMPS;
LEAVE;
} /* End entityDecl */
static void
doctypeStart(void *userData,
const char* name,
const char* sysid,
const char* pubid,
int hasinternal) {
dSP;
CallbackVector *cbv = (CallbackVector*) userData;
ENTER;
SAVETMPS;
PUSHMARK(sp);
EXTEND(sp, 5);
PUSHs(cbv->self_sv);
PUSHs(sv_2mortal(newUTF8SVpv((char*)name, 0)));
PUSHs(sysid ? sv_2mortal(newUTF8SVpv((char*)sysid, 0)) : &PL_sv_undef);
PUSHs(pubid ? sv_2mortal(newUTF8SVpv((char*)pubid, 0)) : &PL_sv_undef);
PUSHs(hasinternal ? &PL_sv_yes : &PL_sv_no);
PUTBACK;
perl_call_sv(cbv->doctyp_sv, G_DISCARD);
FREETMPS;
LEAVE;
} /* End doctypeStart */
static void
doctypeEnd(void *userData) {
dSP;
CallbackVector *cbv = (CallbackVector*) userData;
ENTER;
SAVETMPS;
PUSHMARK(sp);
EXTEND(sp, 1);
PUSHs(cbv->self_sv);
PUTBACK;
perl_call_sv(cbv->doctypfin_sv, G_DISCARD);
FREETMPS;
LEAVE;
} /* End doctypeEnd */
static void
xmlDecl(void *userData,
const char *version,
const char *encoding,
int standalone) {
dSP;
CallbackVector *cbv = (CallbackVector*) userData;
ENTER;
SAVETMPS;
PUSHMARK(sp);
EXTEND(sp, 4);
PUSHs(cbv->self_sv);
PUSHs(version ? sv_2mortal(newUTF8SVpv((char *)version, 0))
: &PL_sv_undef);
PUSHs(encoding ? sv_2mortal(newUTF8SVpv((char *)encoding, 0))
: &PL_sv_undef);
PUSHs(standalone == -1 ? &PL_sv_undef
: (standalone ? &PL_sv_yes : &PL_sv_no));
PUTBACK;
perl_call_sv(cbv->xmldec_sv, G_DISCARD);
FREETMPS;
LEAVE;
} /* End xmlDecl */
static void
unparsedEntityDecl(void *userData,
const char* entity,
const char* base,
const char* sysid,
const char* pubid,
const char* notation)
{
dSP;
CallbackVector* cbv = (CallbackVector*) userData;
ENTER;
SAVETMPS;
PUSHMARK(sp);
EXTEND(sp, 6);
PUSHs(cbv->self_sv);
PUSHs(sv_2mortal(newUTF8SVpv((char*) entity, 0)));
PUSHs(base ? sv_2mortal(newUTF8SVpv((char*) base, 0)) : &PL_sv_undef);
PUSHs(sv_2mortal(newUTF8SVpv((char*) sysid, 0)));
PUSHs(pubid ? sv_2mortal(newUTF8SVpv((char*) pubid, 0)) : &PL_sv_undef);
PUSHs(sv_2mortal(newUTF8SVpv((char*) notation, 0)));
PUTBACK;
perl_call_sv(cbv->unprsd_sv, G_DISCARD);
FREETMPS;
LEAVE;
} /* End unparsedEntityDecl */
static void
notationDecl(void *userData,
const char *name,
const char *base,
const char *sysid,
const char *pubid)
{
dSP;
CallbackVector* cbv = (CallbackVector*) userData;
PUSHMARK(sp);
XPUSHs(cbv->self_sv);
XPUSHs(sv_2mortal(newUTF8SVpv((char*) name, 0)));
if (base)
{
XPUSHs(sv_2mortal(newUTF8SVpv((char *) base, 0)));
}
else if (sysid || pubid)
{
XPUSHs(&PL_sv_undef);
}
if (sysid)
{
XPUSHs(sv_2mortal(newUTF8SVpv((char *) sysid, 0)));
}
else if (pubid)
{
XPUSHs(&PL_sv_undef);
}
if (pubid)
XPUSHs(sv_2mortal(newUTF8SVpv((char *) pubid, 0)));
PUTBACK;
perl_call_sv(cbv->notation_sv, G_DISCARD);
} /* End notationDecl */
static int
externalEntityRef(XML_Parser parser,
const char* open,
const char* base,
const char* sysid,
const char* pubid)
{
dSP;
#if defined(USE_THREADS) && PATCHLEVEL==6
dTHX;
#endif
int count;
int ret = 0;
int parse_done = 0;
CallbackVector* cbv = (CallbackVector*) XML_GetUserData(parser);
if (! cbv->extent_sv)
return 0;
ENTER ;
SAVETMPS ;
PUSHMARK(sp);
EXTEND(sp, pubid ? 4 : 3);
PUSHs(cbv->self_sv);
PUSHs(base ? sv_2mortal(newUTF8SVpv((char*) base, 0)) : &PL_sv_undef);
PUSHs(sv_2mortal(newSVpv((char*) sysid, 0)));
if (pubid)
PUSHs(sv_2mortal(newUTF8SVpv((char*) pubid, 0)));
PUTBACK ;
count = perl_call_sv(cbv->extent_sv, G_SCALAR);
SPAGAIN ;
if (count >= 1) {
SV * result = POPs;
int type;
if (result && (type = SvTYPE(result)) > 0) {
SV **pval = hv_fetch((HV*) SvRV(cbv->self_sv), "Parser", 6, 0);
if (! pval || ! SvIOK(*pval))
append_error(parser, "Can't find parser entry in XML::Parser object");
else {
XML_Parser entpar;
char *errmsg = (char *) 0;
entpar = XML_ExternalEntityParserCreate(parser, open, 0);
XML_SetBase(entpar, XML_GetBase(parser));
sv_setiv(*pval, (IV) entpar);
cbv->p = entpar;
PUSHMARK(sp);
EXTEND(sp, 2);
PUSHs(*pval);
PUSHs(result);
PUTBACK;
count = perl_call_pv("XML::Parser::Expat::Do_External_Parse",
G_SCALAR | G_EVAL);
SPAGAIN;
if (SvTRUE(ERRSV)) {
char *hold;
STRLEN len;
POPs;
hold = SvPV(ERRSV, len);
New(326, errmsg, len + 1, char);
if (len)
Copy(hold, errmsg, len, char);
goto Extparse_Cleanup;
}
if (count > 0)
ret = POPi;
parse_done = 1;
Extparse_Cleanup:
cbv->p = parser;
sv_setiv(*pval, (IV) parser);
XML_ParserFree(entpar);
if (cbv->extfin_sv) {
PUSHMARK(sp);
PUSHs(cbv->self_sv);
PUTBACK;
perl_call_sv(cbv->extfin_sv, G_DISCARD);
SPAGAIN;
}
if (SvTRUE(ERRSV))
append_error(parser, SvPV_nolen(ERRSV));
}
}
}
if (! ret && ! parse_done)
append_error(parser, "Handler couldn't resolve external entity");
PUTBACK ;
FREETMPS ;
LEAVE ;
return ret;
} /* End externalEntityRef */
/*================================================================
** This is the function that expat calls to convert multi-byte sequences
** for external encodings. Each byte in the sequence is used to index
** into the current map to either set the next map or, in the case of
** the final byte, to get the corresponding Unicode scalar, which is
** returned.
*/
static int
convert_to_unicode(void *data, const char *seq) {
Encinfo *enc = (Encinfo *) data;
PrefixMap *curpfx;
int count;
int index = 0;
for (count = 0; count < 4; count++) {
unsigned char byte = (unsigned char) seq[count];
unsigned char bndx;
unsigned char bmsk;
int offset;
curpfx = &enc->prefixes[index];
offset = ((int) byte) - curpfx->min;
if (offset < 0)
break;
if (offset >= curpfx->len && curpfx->len != 0)
break;
bndx = byte >> 3;
bmsk = 1 << (byte & 0x7);
if (curpfx->ispfx[bndx] & bmsk) {
index = enc->bytemap[curpfx->bmap_start + offset];
}
else if (curpfx->ischar[bndx] & bmsk) {
return enc->bytemap[curpfx->bmap_start + offset];
}
else
break;
}
return -1;
} /* End convert_to_unicode */
static int
unknownEncoding(void *unused, const char *name, XML_Encoding *info)
{
SV ** encinfptr;
Encinfo *enc;
int namelen;
int i;
char buff[42];
namelen = strlen(name);
if (namelen > 40)
return 0;
/* Make uppercase */
for (i = 0; i < namelen; i++) {
char c = name[i];
if (c >= 'a' && c <= 'z')
c -= 'a' - 'A';
buff[i] = c;
}
if (! EncodingTable) {
EncodingTable = perl_get_hv("XML::Parser::Expat::Encoding_Table", FALSE);
if (! EncodingTable)
croak("Can't find XML::Parser::Expat::Encoding_Table");
}
encinfptr = hv_fetch(EncodingTable, buff, namelen, 0);
if (! encinfptr || ! SvOK(*encinfptr)) {
/* Not found, so try to autoload */
dSP;
int count;
ENTER;
SAVETMPS;
PUSHMARK(sp);
XPUSHs(sv_2mortal(newSVpvn(buff,namelen)));
PUTBACK;
perl_call_pv("XML::Parser::Expat::load_encoding", G_DISCARD);
encinfptr = hv_fetch(EncodingTable, buff, namelen, 0);
FREETMPS;
LEAVE;
if (! encinfptr || ! SvOK(*encinfptr))
return 0;
}
if (! sv_derived_from(*encinfptr, "XML::Parser::Encinfo"))
croak("Entry in XML::Parser::Expat::Encoding_Table not an Encinfo object");
enc = (Encinfo *) SvIV((SV*)SvRV(*encinfptr));
Copy(enc->firstmap, info->map, 256, int);
info->release = NULL;
if (enc->prefixes_size) {
info->data = (void *) enc;
info->convert = convert_to_unicode;
}
else {
info->data = NULL;
info->convert = NULL;
}
return 1;
} /* End unknownEncoding */
static void
recString(void *userData, const char *string, int len)
{
CallbackVector *cbv = (CallbackVector*) userData;
if (cbv->recstring) {
sv_catpvn(cbv->recstring, (char *) string, len);
}
else {
cbv->recstring = newUTF8SVpvn((char *) string, len);
}
} /* End recString */
static void
suspend_callbacks(CallbackVector *cbv) {
if (SvTRUE(cbv->char_sv)) {
XML_SetCharacterDataHandler(cbv->p,
(XML_CharacterDataHandler) 0);
}
if (SvTRUE(cbv->proc_sv)) {
XML_SetProcessingInstructionHandler(cbv->p,
(XML_ProcessingInstructionHandler) 0);
}
if (SvTRUE(cbv->cmnt_sv)) {
XML_SetCommentHandler(cbv->p,
(XML_CommentHandler) 0);
}
if (SvTRUE(cbv->startcd_sv)
|| SvTRUE(cbv->endcd_sv)) {
XML_SetCdataSectionHandler(cbv->p,
(XML_StartCdataSectionHandler) 0,
(XML_EndCdataSectionHandler) 0);
}
if (SvTRUE(cbv->unprsd_sv)) {
XML_SetUnparsedEntityDeclHandler(cbv->p,
(XML_UnparsedEntityDeclHandler) 0);
}
if (SvTRUE(cbv->notation_sv)) {
XML_SetNotationDeclHandler(cbv->p,
(XML_NotationDeclHandler) 0);
}
if (SvTRUE(cbv->extent_sv)) {
XML_SetExternalEntityRefHandler(cbv->p,
(XML_ExternalEntityRefHandler) 0);
}
} /* End suspend_callbacks */
static void
resume_callbacks(CallbackVector *cbv) {
if (SvTRUE(cbv->char_sv)) {
XML_SetCharacterDataHandler(cbv->p, characterData);
}
if (SvTRUE(cbv->proc_sv)) {
XML_SetProcessingInstructionHandler(cbv->p, processingInstruction);
}
if (SvTRUE(cbv->cmnt_sv)) {
XML_SetCommentHandler(cbv->p, commenthandle);
}
if (SvTRUE(cbv->startcd_sv)
|| SvTRUE(cbv->endcd_sv)) {
XML_SetCdataSectionHandler(cbv->p, startCdata, endCdata);
}
if (SvTRUE(cbv->unprsd_sv)) {
XML_SetUnparsedEntityDeclHandler(cbv->p, unparsedEntityDecl);
}
if (SvTRUE(cbv->notation_sv)) {
XML_SetNotationDeclHandler(cbv->p, notationDecl);
}
if (SvTRUE(cbv->extent_sv)) {
XML_SetExternalEntityRefHandler(cbv->p, externalEntityRef);
}
} /* End resume_callbacks */
MODULE = XML::Parser::Expat PACKAGE = XML::Parser::Expat PREFIX = XML_
XML_Parser
XML_ParserCreate(self_sv, enc_sv, namespaces)
SV * self_sv
SV * enc_sv
int namespaces
CODE:
{
CallbackVector *cbv;
enum XML_ParamEntityParsing pep = XML_PARAM_ENTITY_PARSING_NEVER;
char *enc = (char *) (SvTRUE(enc_sv) ? SvPV_nolen(enc_sv) : 0);
SV ** spp;
Newz(320, cbv, 1, CallbackVector);
cbv->self_sv = SvREFCNT_inc(self_sv);
Newz(325, cbv->st_serial_stack, 1024, unsigned int);
spp = hv_fetch((HV*)SvRV(cbv->self_sv), "NoExpand", 8, 0);
if (spp && SvTRUE(*spp))
cbv->no_expand = 1;
spp = hv_fetch((HV*)SvRV(cbv->self_sv), "Context", 7, 0);
if (! spp || ! *spp || !SvROK(*spp))
croak("XML::Parser instance missing Context");
cbv->context = (AV*) SvRV(*spp);
cbv->ns = (unsigned) namespaces;
if (namespaces)
{
spp = hv_fetch((HV*)SvRV(cbv->self_sv), "New_Prefixes", 12, 0);
if (! spp || ! *spp || !SvROK(*spp))
croak("XML::Parser instance missing New_Prefixes");
cbv->new_prefix_list = (AV *) SvRV(*spp);
spp = hv_fetch((HV*)SvRV(cbv->self_sv), "Namespace_Table",
15, FALSE);
if (! spp || ! *spp || !SvROK(*spp))
croak("XML::Parser instance missing Namespace_Table");
cbv->nstab = (HV *) SvRV(*spp);
spp = hv_fetch((HV*)SvRV(cbv->self_sv), "Namespace_List",
14, FALSE);
if (! spp || ! *spp || !SvROK(*spp))
croak("XML::Parser instance missing Namespace_List");
cbv->nslst = (AV *) SvRV(*spp);
RETVAL = XML_ParserCreate_MM(enc, &ms, nsdelim);
XML_SetNamespaceDeclHandler(RETVAL,nsStart, nsEnd);
}
else
{
RETVAL = XML_ParserCreate_MM(enc, &ms, NULL);
}
cbv->p = RETVAL;
XML_SetUserData(RETVAL, (void *) cbv);
XML_SetElementHandler(RETVAL, startElement, endElement);
XML_SetUnknownEncodingHandler(RETVAL, unknownEncoding, 0);
spp = hv_fetch((HV*)SvRV(cbv->self_sv), "ParseParamEnt",
13, FALSE);
if (spp && SvTRUE(*spp)) {
pep = XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE;
cbv->parseparam = 1;
}
XML_SetParamEntityParsing(RETVAL, pep);
}
OUTPUT:
RETVAL
void
XML_ParserRelease(parser)
XML_Parser parser
CODE:
{
CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
SvREFCNT_dec(cbv->self_sv);
}
void
XML_ParserFree(parser)
XML_Parser parser
CODE:
{
CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
Safefree(cbv->st_serial_stack);
/* Clean up any SVs that we have */
/* (Note that self_sv must already be taken care of
or we couldn't be here */
if (cbv->recstring)
SvREFCNT_dec(cbv->recstring);
if (cbv->start_sv)
SvREFCNT_dec(cbv->start_sv);
if (cbv->end_sv)
SvREFCNT_dec(cbv->end_sv);
if (cbv->char_sv)
SvREFCNT_dec(cbv->char_sv);
if (cbv->proc_sv)
SvREFCNT_dec(cbv->proc_sv);
if (cbv->cmnt_sv)
SvREFCNT_dec(cbv->cmnt_sv);
if (cbv->dflt_sv)
SvREFCNT_dec(cbv->dflt_sv);
if (cbv->entdcl_sv)
SvREFCNT_dec(cbv->entdcl_sv);
if (cbv->eledcl_sv)
SvREFCNT_dec(cbv->eledcl_sv);
if (cbv->attdcl_sv)
SvREFCNT_dec(cbv->attdcl_sv);
if (cbv->doctyp_sv)
SvREFCNT_dec(cbv->doctyp_sv);
if (cbv->doctypfin_sv)
SvREFCNT_dec(cbv->doctypfin_sv);
if (cbv->xmldec_sv)
SvREFCNT_dec(cbv->xmldec_sv);
if (cbv->unprsd_sv)
SvREFCNT_dec(cbv->unprsd_sv);
if (cbv->notation_sv)
SvREFCNT_dec(cbv->notation_sv);
if (cbv->extent_sv)
SvREFCNT_dec(cbv->extent_sv);
if (cbv->extfin_sv)
SvREFCNT_dec(cbv->extfin_sv);
if (cbv->startcd_sv)
SvREFCNT_dec(cbv->startcd_sv);
if (cbv->endcd_sv)
SvREFCNT_dec(cbv->endcd_sv);
/* ================ */
Safefree(cbv);
XML_ParserFree(parser);
}
int
XML_ParseString(parser, sv)
XML_Parser parser
SV * sv
CODE:
{
CallbackVector * cbv;
STRLEN len;
char *s = SvPV(sv, len);
cbv = (CallbackVector *) XML_GetUserData(parser);
RETVAL = XML_Parse(parser, s, len, 1);
SPAGAIN; /* XML_Parse might have changed stack pointer */
if (! RETVAL)
append_error(parser, NULL);
}
OUTPUT:
RETVAL
int
XML_ParseStream(parser, ioref, delim)
XML_Parser parser
SV * ioref
SV * delim
CODE:
{
SV **delimsv;
CallbackVector * cbv;
cbv = (CallbackVector *) XML_GetUserData(parser);
if (SvOK(delim)) {
cbv->delim = SvPV(delim, cbv->delimlen);
}
else {
cbv->delim = (char *) 0;
}
RETVAL = parse_stream(parser, ioref);
SPAGAIN; /* parse_stream might have changed stack pointer */
}
OUTPUT:
RETVAL
int
XML_ParsePartial(parser, sv)
XML_Parser parser
SV * sv
CODE:
{
STRLEN len;
char *s = SvPV(sv, len);
CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
RETVAL = XML_Parse(parser, s, len, 0);
if (! RETVAL)
append_error(parser, NULL);
}
OUTPUT:
RETVAL
int
XML_ParseDone(parser)
XML_Parser parser
CODE:
{
RETVAL = XML_Parse(parser, "", 0, 1);
if (! RETVAL)
append_error(parser, NULL);
}
OUTPUT:
RETVAL
SV *
XML_SetStartElementHandler(parser, start_sv)
XML_Parser parser
SV * start_sv
CODE:
{
CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);
XMLP_UPD(start_sv);
PUSHRET;
}
SV *
XML_SetEndElementHandler(parser, end_sv)
XML_Parser parser
SV * end_sv
CODE:
{
CallbackVector *cbv = (CallbackVector*) XML_GetUserData(parser);
XMLP_UPD(end_sv);
PUSHRET;
}
SV *
XML_SetCharacterDataHandler(parser, char_sv)
XML_Parser parser
SV * char_sv
CODE:
{
XML_CharacterDataHandler charhndl = (XML_CharacterDataHandler) 0;
CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);
XMLP_UPD(char_sv);
if (SvTRUE(char_sv))
charhndl = characterData;
XML_SetCharacterDataHandler(parser, charhndl);
PUSHRET;
}
SV *
XML_SetProcessingInstructionHandler(parser, proc_sv)
XML_Parser parser
SV * proc_sv
CODE:
{
XML_ProcessingInstructionHandler prochndl =
(XML_ProcessingInstructionHandler) 0;
CallbackVector* cbv = (CallbackVector*) XML_GetUserData(parser);
XMLP_UPD(proc_sv);
if (SvTRUE(proc_sv))
prochndl = processingInstruction;
XML_SetProcessingInstructionHandler(parser, prochndl);
PUSHRET;
}
SV *
XML_SetCommentHandler(parser, cmnt_sv)
XML_Parser parser
SV * cmnt_sv
CODE:
{
XML_CommentHandler cmnthndl = (XML_CommentHandler) 0;
CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);
XMLP_UPD(cmnt_sv);
if (SvTRUE(cmnt_sv))
cmnthndl = commenthandle;
XML_SetCommentHandler(parser, cmnthndl);
PUSHRET;
}
SV *
XML_SetDefaultHandler(parser, dflt_sv)
XML_Parser parser
SV * dflt_sv
CODE:
{
XML_DefaultHandler dflthndl = (XML_DefaultHandler) 0;
CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);
XMLP_UPD(dflt_sv);
if (SvTRUE(dflt_sv))
dflthndl = defaulthandle;
if (cbv->no_expand)
XML_SetDefaultHandler(parser, dflthndl);
else
XML_SetDefaultHandlerExpand(parser, dflthndl);
PUSHRET;
}
SV *
XML_SetUnparsedEntityDeclHandler(parser, unprsd_sv)
XML_Parser parser
SV * unprsd_sv
CODE:
{
XML_UnparsedEntityDeclHandler unprsdhndl =
(XML_UnparsedEntityDeclHandler) 0;
CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);
XMLP_UPD(unprsd_sv);
if (SvTRUE(unprsd_sv))
unprsdhndl = unparsedEntityDecl;
XML_SetUnparsedEntityDeclHandler(parser, unprsdhndl);
PUSHRET;
}
SV *
XML_SetNotationDeclHandler(parser, notation_sv)
XML_Parser parser
SV * notation_sv
CODE:
{
XML_NotationDeclHandler nothndlr = (XML_NotationDeclHandler) 0;
CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);
XMLP_UPD(notation_sv);
if (SvTRUE(notation_sv))
nothndlr = notationDecl;
XML_SetNotationDeclHandler(parser, nothndlr);
PUSHRET;
}
SV *
XML_SetExternalEntityRefHandler(parser, extent_sv)
XML_Parser parser
SV * extent_sv
CODE:
{
XML_ExternalEntityRefHandler exthndlr =
(XML_ExternalEntityRefHandler) 0;
CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);
XMLP_UPD(extent_sv);
if (SvTRUE(extent_sv))
exthndlr = externalEntityRef;
XML_SetExternalEntityRefHandler(parser, exthndlr);
PUSHRET;
}
SV *
XML_SetExtEntFinishHandler(parser, extfin_sv)
XML_Parser parser
SV * extfin_sv
CODE:
{
CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
/* There is no corresponding handler for this in expat. This is
called from the externalEntityRef function above after parsing
the external entity. */
XMLP_UPD(extfin_sv);
PUSHRET;
}
SV *
XML_SetEntityDeclHandler(parser, entdcl_sv)
XML_Parser parser
SV * entdcl_sv
CODE:
{
XML_EntityDeclHandler enthndlr =
(XML_EntityDeclHandler) 0;
CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);
XMLP_UPD(entdcl_sv);
if (SvTRUE(entdcl_sv))
enthndlr = entityDecl;
XML_SetEntityDeclHandler(parser, enthndlr);
PUSHRET;
}
SV *
XML_SetElementDeclHandler(parser, eledcl_sv)
XML_Parser parser
SV * eledcl_sv
CODE:
{
XML_ElementDeclHandler eldeclhndlr =
(XML_ElementDeclHandler) 0;
CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);
XMLP_UPD(eledcl_sv);
if (SvTRUE(eledcl_sv))
eldeclhndlr = elementDecl;
XML_SetElementDeclHandler(parser, eldeclhndlr);
PUSHRET;
}
SV *
XML_SetAttListDeclHandler(parser, attdcl_sv)
XML_Parser parser
SV * attdcl_sv
CODE:
{
XML_AttlistDeclHandler attdeclhndlr =
(XML_AttlistDeclHandler) 0;
CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);
XMLP_UPD(attdcl_sv);
if (SvTRUE(attdcl_sv))
attdeclhndlr = attributeDecl;
XML_SetAttlistDeclHandler(parser, attdeclhndlr);
PUSHRET;
}
SV *
XML_SetDoctypeHandler(parser, doctyp_sv)
XML_Parser parser
SV * doctyp_sv
CODE:
{
XML_StartDoctypeDeclHandler dtsthndlr =
(XML_StartDoctypeDeclHandler) 0;
CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);
int set = 0;
XMLP_UPD(doctyp_sv);
if (SvTRUE(doctyp_sv))
dtsthndlr = doctypeStart;
XML_SetStartDoctypeDeclHandler(parser, dtsthndlr);
PUSHRET;
}
SV *
XML_SetEndDoctypeHandler(parser, doctypfin_sv)
XML_Parser parser
SV * doctypfin_sv
CODE:
{
XML_EndDoctypeDeclHandler dtendhndlr =
(XML_EndDoctypeDeclHandler) 0;
CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);
XMLP_UPD(doctypfin_sv);
if (SvTRUE(doctypfin_sv))
dtendhndlr = doctypeEnd;
XML_SetEndDoctypeDeclHandler(parser, dtendhndlr);
PUSHRET;
}
SV *
XML_SetXMLDeclHandler(parser, xmldec_sv)
XML_Parser parser
SV * xmldec_sv
CODE:
{
XML_XmlDeclHandler xmldechndlr =
(XML_XmlDeclHandler) 0;
CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
XMLP_UPD(xmldec_sv);
if (SvTRUE(xmldec_sv))
xmldechndlr = xmlDecl;
XML_SetXmlDeclHandler(parser, xmldechndlr);
PUSHRET;
}
void
XML_SetBase(parser, base)
XML_Parser parser
SV * base
CODE:
{
char * b;
if (! SvOK(base)) {
b = (char *) 0;
}
else {
b = SvPV_nolen(base);
}
XML_SetBase(parser, b);
}
SV *
XML_GetBase(parser)
XML_Parser parser
CODE:
{
const char *ret = XML_GetBase(parser);
if (ret) {
ST(0) = sv_newmortal();
sv_setpv(ST(0), ret);
}
else {
ST(0) = &PL_sv_undef;
}
}
void
XML_PositionContext(parser, lines)
XML_Parser parser
int lines
PREINIT:
int parsepos;
int size;
const char *pos = XML_GetInputContext(parser, &parsepos, &size);
const char *markbeg;
const char *limit;
const char *markend;
int length, relpos;
int cnt;
PPCODE:
if (! pos)
return;
for (markbeg = &pos[parsepos], cnt = 0; markbeg >= pos; markbeg--)
{
if (*markbeg == '\n')
{
cnt++;
if (cnt > lines)
break;
}
}
markbeg++;
relpos = 0;
limit = &pos[size];
for (markend = &pos[parsepos + 1], cnt = 0;
markend < limit;
markend++)
{
if (*markend == '\n')
{
if (cnt == 0)
relpos = (markend - markbeg) + 1;
cnt++;
if (cnt > lines)
{
markend++;
break;
}
}
}
length = markend - markbeg;
if (relpos == 0)
relpos = length;
EXTEND(sp, 2);
PUSHs(sv_2mortal(newSVpvn((char *) markbeg, length)));
PUSHs(sv_2mortal(newSViv(relpos)));
SV *
GenerateNSName(name, xml_namespace, table, list)
SV * name
SV * xml_namespace
SV * table
SV * list
CODE:
{
STRLEN nmlen, nslen;
char * nmstr;
char * nsstr;
char * buff;
char * bp;
char * blim;
nmstr = SvPV(name, nmlen);
nsstr = SvPV(xml_namespace, nslen);
/* Form a namespace-name string that looks like expat's */
New(321, buff, nmlen + nslen + 2, char);
bp = buff;
blim = bp + nslen;
while (bp < blim)
*bp++ = *nsstr++;
*bp++ = NSDELIM;
blim = bp + nmlen;
while (bp < blim)
*bp++ = *nmstr++;
*bp = '\0';
RETVAL = gen_ns_name(buff, (HV *) SvRV(table), (AV *) SvRV(list));
Safefree(buff);
}
OUTPUT:
RETVAL
void
XML_DefaultCurrent(parser)
XML_Parser parser
CODE:
{
CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
XML_DefaultCurrent(parser);
}
SV *
XML_RecognizedString(parser)
XML_Parser parser
CODE:
{
XML_DefaultHandler dflthndl = (XML_DefaultHandler) 0;
CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
if (cbv->dflt_sv) {
dflthndl = defaulthandle;
}
if (cbv->recstring) {
sv_setpvn(cbv->recstring, "", 0);
}
if (cbv->no_expand)
XML_SetDefaultHandler(parser, recString);
else
XML_SetDefaultHandlerExpand(parser, recString);
XML_DefaultCurrent(parser);
if (cbv->no_expand)
XML_SetDefaultHandler(parser, dflthndl);
else
XML_SetDefaultHandlerExpand(parser, dflthndl);
RETVAL = newSVsv(cbv->recstring);
}
OUTPUT:
RETVAL
int
XML_GetErrorCode(parser)
XML_Parser parser
int
XML_GetCurrentLineNumber(parser)
XML_Parser parser
int
XML_GetCurrentColumnNumber(parser)
XML_Parser parser
long
XML_GetCurrentByteIndex(parser)
XML_Parser parser
int
XML_GetSpecifiedAttributeCount(parser)
XML_Parser parser
char *
XML_ErrorString(code)
int code
CODE:
const char *ret = XML_ErrorString(code);
ST(0) = sv_newmortal();
sv_setpv((SV*)ST(0), ret);
SV *
XML_LoadEncoding(data, size)
char * data
int size
CODE:
{
Encmap_Header *emh = (Encmap_Header *) data;
unsigned pfxsize, bmsize;
if (size < sizeof(Encmap_Header)
|| ntohl(emh->magic) != ENCMAP_MAGIC) {
RETVAL = &PL_sv_undef;
}
else {
Encinfo *entry;
SV *sv;
PrefixMap *pfx;
unsigned short *bm;
int namelen;
int i;
pfxsize = ntohs(emh->pfsize);
bmsize = ntohs(emh->bmsize);
if (size != (sizeof(Encmap_Header)
+ pfxsize * sizeof(PrefixMap)
+ bmsize * sizeof(unsigned short))) {
RETVAL = &PL_sv_undef;
}
else {
/* Convert to uppercase and get name length */
for (i = 0; i < sizeof(emh->name); i++) {
char c = emh->name[i];
if (c == (char) 0)
break;
if (c >= 'a' && c <= 'z')
emh->name[i] -= 'a' - 'A';
}
namelen = i;
RETVAL = newSVpvn(emh->name, namelen);
New(322, entry, 1, Encinfo);
entry->prefixes_size = pfxsize;
entry->bytemap_size = bmsize;
for (i = 0; i < 256; i++) {
entry->firstmap[i] = ntohl(emh->map[i]);
}
pfx = (PrefixMap *) &data[sizeof(Encmap_Header)];
bm = (unsigned short *) (((char *) pfx)
+ sizeof(PrefixMap) * pfxsize);
New(323, entry->prefixes, pfxsize, PrefixMap);
New(324, entry->bytemap, bmsize, unsigned short);
for (i = 0; i < pfxsize; i++, pfx++) {
PrefixMap *dest = &entry->prefixes[i];
dest->min = pfx->min;
dest->len = pfx->len;
dest->bmap_start = ntohs(pfx->bmap_start);
Copy(pfx->ispfx, dest->ispfx,
sizeof(pfx->ispfx) + sizeof(pfx->ischar), unsigned char);
}
for (i = 0; i < bmsize; i++)
entry->bytemap[i] = ntohs(bm[i]);
sv = newSViv(0);
sv_setref_pv(sv, "XML::Parser::Encinfo", (void *) entry);
if (! EncodingTable) {
EncodingTable
= perl_get_hv("XML::Parser::Expat::Encoding_Table",
FALSE);
if (! EncodingTable)
croak("Can't find XML::Parser::Expat::Encoding_Table");
}
hv_store(EncodingTable, emh->name, namelen, sv, 0);
}
}
}
OUTPUT:
RETVAL
void
XML_FreeEncoding(enc)
Encinfo * enc
CODE:
Safefree(enc->bytemap);
Safefree(enc->prefixes);
Safefree(enc);
SV *
XML_OriginalString(parser)
XML_Parser parser
CODE:
{
int parsepos, size;
const char *buff = XML_GetInputContext(parser, &parsepos, &size);
if (buff) {
RETVAL = newSVpvn((char *) &buff[parsepos],
XML_GetCurrentByteCount(parser));
}
else {
RETVAL = newSVpv("", 0);
}
}
OUTPUT:
RETVAL
SV *
XML_SetStartCdataHandler(parser, startcd_sv)
XML_Parser parser
SV * startcd_sv
CODE:
{
CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
XML_StartCdataSectionHandler scdhndl =
(XML_StartCdataSectionHandler) 0;
XMLP_UPD(startcd_sv);
if (SvTRUE(startcd_sv))
scdhndl = startCdata;
XML_SetStartCdataSectionHandler(parser, scdhndl);
PUSHRET;
}
SV *
XML_SetEndCdataHandler(parser, endcd_sv)
XML_Parser parser
SV * endcd_sv
CODE:
{
CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
XML_EndCdataSectionHandler ecdhndl =
(XML_EndCdataSectionHandler) 0;
XMLP_UPD(endcd_sv);
if (SvTRUE(endcd_sv))
ecdhndl = endCdata;
XML_SetEndCdataSectionHandler(parser, ecdhndl);
PUSHRET;
}
void
XML_UnsetAllHandlers(parser)
XML_Parser parser
CODE:
{
CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
suspend_callbacks(cbv);
if (cbv->ns) {
XML_SetNamespaceDeclHandler(cbv->p,
(XML_StartNamespaceDeclHandler) 0,
(XML_EndNamespaceDeclHandler) 0);
}
XML_SetElementHandler(parser,
(XML_StartElementHandler) 0,
(XML_EndElementHandler) 0);
XML_SetUnknownEncodingHandler(parser,
(XML_UnknownEncodingHandler) 0,
(void *) 0);
}
int
XML_ElementIndex(parser)
XML_Parser parser
CODE:
{
CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
RETVAL = cbv->st_serial_stack[cbv->st_serial_stackptr];
}
OUTPUT:
RETVAL
void
XML_SkipUntil(parser, index)
XML_Parser parser
unsigned int index
CODE:
{
CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
if (index <= cbv->st_serial)
return;
cbv->skip_until = index;
suspend_callbacks(cbv);
}
int
XML_Do_External_Parse(parser, result)
XML_Parser parser
SV * result
CODE:
{
int type;
CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
if (SvROK(result) && SvOBJECT(SvRV(result))) {
RETVAL = parse_stream(parser, result);
}
else if (isGV(result)) {
RETVAL = parse_stream(parser,
sv_2mortal(newRV((SV*) GvIOp(result))));
}
else if (SvPOK(result)) {
STRLEN eslen;
int pret;
char *entstr = SvPV(result, eslen);
RETVAL = XML_Parse(parser, entstr, eslen, 1);
}
}
OUTPUT:
RETVAL
use ExtUtils::MakeMaker;
use Config;
use English;
my $libs = "-lexpat";
my @extras = ();
push(@extras, INC => "-I$expat_incpath") if $expat_incpath;
$libs = "-L$expat_libpath $libs" if $expat_libpath;
push(@extras, CAPI => 'TRUE')
if (($PERL_VERSION >= 5.005) and ($OSNAME eq 'MSWin32')
and ($Config{archname} =~ /-object\b/i));
push(@extras,
ABSTRACT => "Lowlevel access to James Clark's expat XML parser",
AUTHOR => 'Matt Sergeant (matt@sergeant.org)')
if ($ExtUtils::MakeMaker::VERSION >= 5.4301);
WriteMakefile(
NAME => 'XML::Parser::Expat',
C => ['Expat.c'],
LIBS => $libs,
XSPROTOARG => '-noprototypes',
VERSION_FROM => 'Expat.pm',
@extras
);
/*****************************************************************
** encoding.h
**
** Copyright 1998 Clark Cooper
** All rights reserved.
**
** This program is free software; you can redistribute it and/or
** modify it under the same terms as Perl itself.
*/
#ifndef ENCODING_H
#define ENCODING_H 1
#define ENCMAP_MAGIC 0xfeebface
typedef struct prefixmap {
unsigned char min;
unsigned char len; /* 0 => 256 */
unsigned short bmap_start;
unsigned char ispfx[32];
unsigned char ischar[32];
} PrefixMap;
typedef struct encinf
{
unsigned short prefixes_size;
unsigned short bytemap_size;
int firstmap[256];
PrefixMap *prefixes;
unsigned short *bytemap;
} Encinfo;
typedef struct encmaphdr
{
unsigned int magic;
char name[40];
unsigned short pfsize;
unsigned short bmsize;
int map[256];
} Encmap_Header;
/*================================================================
** Structure of Encoding map binary encoding
**
** Note that all shorts and ints are in network order,
** so when packing or unpacking with perl, use 'n' and 'N' respectively.
** In C, use the htonl family of functions.
**
** The basic structure is:
**
** _______________________
** |Header (including map expat needs for 1st byte)
** |PrefixMap * pfsize
** | This section isn't included for single-byte encodings.
** | For multiple byte encodings, when a byte represents a prefix
** | then it indexes into this vector instead of mapping to a
** | Unicode character. The PrefixMap type is declared above. The
** | ispfx and ischar fields are bitvectors indicating whether
** | the byte being mapped is a prefix or character respectively.
** | If neither is set, then the character is not mapped to Unicode.
** |
** | The min field is the 1st byte mapped for this prefix; the
** | len field is the number of bytes mapped; and bmap_start is
** | the starting index of the map for this prefix in the overall
** | map (next section).
** |unsigned short * bmsize
** | This section also is omitted for single-byte encodings.
** | Each short is either a Unicode scalar or an index into the
** | PrefixMap vector.
**
** The header for these files is declared above as the Encmap_Header type.
** The magic field is a magic number which should match the ENCMAP_MAGIC
** macro above. The next 40 bytes stores IANA registered name for the
** encoding. The pfsize field holds the number of PrefixMaps, which should
** be zero for single byte encodings. The bmsize field holds the number of
** shorts used for the overall map.
**
** The map field contains either the Unicode scalar encoded by the 1st byte
** or -n where n is the number of bytes that such a 1st byte implies (Expat
** requires that the number of bytes to encode a character is indicated by
** the 1st byte) or -1 if the byte doesn't map to any Unicode character.
**
** If the encoding is a multiple byte encoding, then there will be PrefixMap
** and character map sections. The 1st PrefixMap (index 0), covers a range
** of bytes that includes all 1st byte prefixes.
**
** Look at convert_to_unicode in Expat.xs to see how this data structure
** is used.
*/
#endif /* ndef ENCODING_H */
#
##### XML::Parser::Expat typemap
#
XML_Parser T_PTR
Encinfo * T_ENCOBJ
################################################################
INPUT
T_ENCOBJ
if (sv_derived_from($arg, \"XML::Parser::Encinfo\")) {
IV tmp = SvIV((SV*)SvRV($arg));
$var = ($type) tmp;
}
else
croak(\"$var is not of type XML::Parser::Encinfo\")
################################################################
OUTPUT
T_ENCOBJ
if ($var) {
sv_setref_pv($arg, \"XML::Parser::Encinfo\", (void*)$var);
}
else
$arg = &PL_sv_undef;
|