summaryrefslogtreecommitdiff
path: root/src/raptor_general.c
blob: b84c5d07d4606ed3b3fb9888ccb0aba07bcce52e (plain)
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
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
/* -*- Mode: c; c-basic-offset: 2 -*-
 *
 * raptor_parse.c - Redland Parser for RDF (RAPTOR)
 *
 * $Id$
 *
 * Copyright (C) 2000-2001 David Beckett - http://purl.org/net/dajobe/
 * Institute for Learning and Research Technology - http://www.ilrt.org/
 * University of Bristol - http://www.bristol.ac.uk/
 * 
 * This package is Free Software or Open Source available under the
 * following licenses (these are alternatives):
 *   1. GNU Lesser General Public License (LGPL)
 *   2. GNU General Public License (GPL)
 *   3. Mozilla Public License (MPL)
 * 
 * See LICENSE.html or LICENSE.txt at the top of this package for the
 * full license terms.
 * 
 */


#ifdef HAVE_CONFIG_H
#ifdef LIBRDF_INTERNAL
#include <rdf_config.h>
#else
#include <config.h>
#endif
#endif

#ifdef WIN32
#define WIN32_LEAD_AND_MEAN 1
#define HAVE_STDLIB_H 1
#define HAVE_STDARG_H 1

/* use expat on win32 */
#define NEED_EXPAT 1
#define HAVE_XMLPARSE_H 1

#include <windows.h>

#define strcasecmp(X,Y) strcmpi(X,Y)

/* dll entry point */
BOOL APIENTRY
DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
  return TRUE;
}
#endif


#include <stdio.h>
#include <string.h>
#include <ctype.h>
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif

#ifndef WIN32
extern int errno;
#endif

#ifdef LIBRDF_INTERNAL
/* if inside Redland */

#ifdef LIBDF_DEBUG
#define RAPTOR_DEBUG 1
#endif

#include <librdf.h>

#include <rdf_parser.h>
#include <rdf_node.h>
#include <rdf_stream.h>
#include <rdf_statement.h>
#include <rdf_uri.h>

#else
/* else standalone */

#define LIBRDF_MALLOC(type, size) malloc(size)
#define LIBRDF_CALLOC(type, size, count) calloc(size, count)
#define LIBRDF_FREE(type, ptr)   free((void*)ptr)

#ifdef RAPTOR_DEBUG
/* Debugging messages */
#define LIBRDF_DEBUG1(function, msg) do {fprintf(stderr, "%s:%d:%s: " msg, __FILE__, __LINE__, #function); } while(0)
#define LIBRDF_DEBUG2(function, msg, arg1) do {fprintf(stderr, "%s:%d:%s: " msg, __FILE__, __LINE__, #function, arg1);} while(0)
#define LIBRDF_DEBUG3(function, msg, arg1, arg2) do {fprintf(stderr, "%s:%d:%s: " msg, __FILE__, __LINE__, #function, arg1, arg2);} while(0)
#define LIBRDF_DEBUG4(function, msg, arg1, arg2, arg3) do {fprintf(stderr, "%s:%d:%s: " msg, __FILE__, __LINE__, #function, arg1, arg2, arg3);} while(0)
#define LIBRDF_DEBUG4(function, msg, arg1, arg2, arg3) do {fprintf(stderr, "%s:%d:%s: " msg, __FILE__, __LINE__, #function, arg1, arg2, arg3);} while(0)

#else
/* DEBUGGING TURNED OFF */

/* No debugging messages */
#define LIBRDF_DEBUG1(function, msg)
#define LIBRDF_DEBUG2(function, msg, arg1)
#define LIBRDF_DEBUG3(function, msg, arg1, arg2)
#define LIBRDF_DEBUG4(function, msg, arg1, arg2, arg3)

#endif

/* Fatal errors - always happen */
#define LIBRDF_FATAL1(function, msg) do {fprintf(stderr, "%s:%d:%s: fatal error: " msg, __FILE__, __LINE__ , #function); abort();} while(0)
#define LIBRDF_FATAL2(function, msg,arg) do {fprintf(stderr, "%s:%d:%s: fatal error: " msg, __FILE__, __LINE__ , #function, arg); abort();} while(0)



#endif


/* for the memory allocation functions */
#if defined(HAVE_DMALLOC_H) && defined(RAPTOR_MEMORY_DEBUG_DMALLOC)
#include <dmalloc.h>
#undef HAVE_STDLIB_H
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#undef HAVE_STDLIB_H
#endif


/* XML parser includes */
#ifdef NEED_EXPAT
#ifdef HAVE_EXPAT_H
#include <expat.h>
#endif
#ifdef HAVE_XMLPARSE_H
#include <xmlparse.h>
#endif
#endif

#ifdef NEED_LIBXML

#ifdef HAVE_PARSER_H
#include <parser.h>
#else
#ifdef HAVE_GNOME_XML_PARSER_H
#include <gnome-xml/parser.h>
#else
DIE
#endif

#endif

/* translate names from expat to libxml */
#define XML_Char xmlChar
#endif


/* Raptor includes */
#include "raptor.h"

/* Raptor structures */
/* namespace stack node */
typedef struct raptor_ns_map_s raptor_ns_map;

typedef enum {
  RAPTOR_STATE_INVALID = 0,

  /* Skipping current tree of elements - used to recover finding
   * illegal content, when parsling permissively.
   */
  RAPTOR_STATE_SKIPPING = 1,

  /* Not in RDF grammar yet - searching for a start element.
   * This can be <rdf:RDF> (goto 6.1) but since it is optional,
   * the start element can also be <Description> (goto 6.3), 
   * <rdf:Seq> (goto 6.25) <rdf:Bag> (goto 6.26) or <rdf:Alt> (goto 6.27)
   * OR from 6.3 can have ANY other element matching
   * typedNode (6.13) - goto 6.3
   * CHOICE: Search for <rdf:RDF> node before starting match
   * OR assume RDF content, hence go straight to production
   */
  RAPTOR_STATE_UNKNOWN = 1000,

  /* No need for 6.1 - go straight to 6.2 */

  /* Met production 6.1 (RDF) <rdf:RDF> element or 6.17 (value) and expecting
   *   description (6.3) | container (6.4)
   * = description (6.3) | sequence (6.25) | bag (6.26) | alternative (6.27)
   */
  RAPTOR_STATE_OBJ   = 6020,

  /* Met production 6.3 (description) <rdf:Description> element
   * OR 6.13 (typedNode)
   */
  RAPTOR_STATE_DESCRIPTION = 6030,

  /* production 6.4 (container) - not used (pick one of sequence, bag,
   * alternative immediately in state 6.2
   */
  
  /* productions 6.5-6.11 are for attributes - not used */

  /* met production 6.12 (propertyElt)
   */
  RAPTOR_STATE_PROPERTYELT = 6120,

  /* met production 6.13 (typedNode)
   */
  RAPTOR_STATE_TYPED_NODE = 6130,


  /* productions 6.14-6.16 are for attributes - not used */

  /* production 6.17 (value) - not used */

  /* productions 6.18-6.24 are for attributes / values - not used */


  /* Met production 6.25 (sequence) <rdf:Seq> element seen. Goto 6.28  */
  /* RAPTOR_STATE_SEQ = 6250, */

  /* Met production 6.26 (bag) <rdf:Bag> element seen. Goto 6.28  */
  /* RAPTOR_STATE_BAG = 6260, */

  /* Met production 6.27 (alternative) <rdf:Alt> element seen. Goto 6.28 */
  /* RAPTOR_STATE_ALT = 6270, */

  /* Met production 6.28 (member) 
   * Now expect <rdf:li> element and if it empty, with resource attribute
   * goto 6.29 otherwise goto 6.30
   * CHOICE: Match rdf:resource/resource
   */
  RAPTOR_STATE_MEMBER = 6280,

  /* met production 6.29 (referencedItem) after 
   * Found a container item with reference - <rdf:li (rdf:)resource=".."/> */
  RAPTOR_STATE_REFERENCEDITEM = 6290,

  /* met production 6.30 (inlineItem) part 1 - plain container item */
  RAPTOR_STATE_INLINEITEM = 6300,


  /* productions 6.31-6.33 are for attributes - not used */


  /* met production 6.30 (inlineItem) part 2 - container item with
   * rdf:parseType="literal" */
  RAPTOR_STATE_PARSETYPE_LITERAL = 6400,

  /* met production 6.30 (inlineItem) part 3 - container item with 
   * rdf:parseType="literal" */
  RAPTOR_STATE_PARSETYPE_RESOURCE = 6410,




  /* ******************************************************************* */
  /* Additional non-M&S states */

  /* met production 6.30 (inlineItem) - container item 
   * with other rdf:parseType value */
  RAPTOR_STATE_PARSETYPE_OTHER = 6420,

  /* met production 6.30 (inlineItem) - container item 
   * with rdf:parseType value "daml:collection" */
  RAPTOR_STATE_PARSETYPE_DAML_COLLECTION = 6430,

} raptor_state;


static const char * const raptor_state_names[]={
  NULL, /* No 6.0 */
  NULL, /* 6.1 not used */
  "object (6.2)",
  "description (6.3)",
  NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, /* 6.4 - 6.11 not used */
  "propertyElt (6.12)",
  "typed_node (6.13)",
  NULL,NULL,NULL, NULL, NULL,NULL,NULL,NULL,NULL,NULL,NULL, /* 6.14 - 6.24 not used */
  NULL, /* was sequence (6.25) */
  NULL, /* was bag (6.26) */
  NULL, /* was alternative (6.27) */
  "member (6.28)",
  "referencedItem (6.29)",
  "inlineItem (6.30 part 1)",
  NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  "parseTypeLiteral (6.30 part 2)",
  "parseTypeResource (6.30 part 3)",
  "parseTypeOther (not M&S)", /* 6.43 */
  "parseTypeDamlCollection (not M&S)", /* 6.44 */
};


static const char * raptor_state_as_string(raptor_state state) 
{
  int offset=(state - 6000)/10;
  if(state == RAPTOR_STATE_UNKNOWN)
    return "UNKNOWN";
  if(state == RAPTOR_STATE_SKIPPING)
    return "SKIPPING";
  if(offset<0 || offset > 43)
    return "INVALID";
  if(!raptor_state_names[offset])
    return "NOT-USED";
  return raptor_state_names[offset];
}


/* Forms:
 * 1) prefix=NULL uri=<URI>      - default namespace defined
 * 2) prefix=NULL, uri=NULL      - no default namespace
 * 3) prefix=<prefix>, uri=<URI> - regular pair defined <prefix>:<URI>
 */
struct raptor_ns_map_s {
  /* next down the stack, NULL at bottom */
  struct raptor_ns_map_s* next;
  /* NULL means is the default namespace */
  const char *prefix;
  /* needed to safely compare prefixed-names */
  int prefix_length;
  /* URI of namespace or NULL for default */
  raptor_uri *uri;
#ifdef LIBRDF_INTERNAL
#else
  /* When implementing URIs as char*, need this for efficiency */
  int uri_length;
#endif
  /* parsing depth that this ns was added.  It will
   * be deleted when the parser leaves this depth 
   */
  int depth;
  /* Non 0 if is xml: prefixed name */
  int is_xml;
  /* Non 0 if is RDF M&S Namespace */
  int is_rdf_ms;
  /* Non 0 if is RDF Schema Namespace */
  int is_rdf_schema;
};


/* 
 * Raptor XML-namespaced name, for elements or attributes 
 */

/* There are three forms
 * namespace=NULL                                - un-namespaced name
 * namespace=defined, namespace->prefix=NULL     - (default ns) name
 * namespace=defined, namespace->prefix=defined  - ns:name
 */
typedef struct {
  /* Name - always present */
  const char *local_name;
  int local_name_length;
  /* Namespace or NULL if not in a namespace */
  const raptor_ns_map *nspace;
  /* URI of namespace+local_name or NULL if not defined */
  raptor_uri *uri;
  /* optional value - used when name is an attribute */
  const char *value;
  int value_length;
} raptor_ns_name;


/* These are used in the RDF/XML syntax as attributes, not
 * elements and are mostly not concepts in the RDF model (except for
 * the type and value attributes which are properties too).
 */
typedef enum {
  RDF_ATTR_about           = 0, /* value of rdf:about attribute */
  RDF_ATTR_aboutEach       = 1, /* " rdf:aboutEach */
  RDF_ATTR_aboutEachPrefix = 2, /* " rdf:aboutEachPrefix */
  RDF_ATTR_ID              = 3, /* " rdf:ID */
  RDF_ATTR_bagID           = 4, /* " rdf:bagID */
  RDF_ATTR_resource        = 5, /* " rdf:resource */
  RDF_ATTR_parseType       = 6, /* " rdf:parseType */
  RDF_ATTR_type            = 7, /* " rdf:type -- a property in RDF Model */
  RDF_ATTR_value           = 8, /* " rdf:value -- a property in RDF model */

  RDF_ATTR_LAST            = RDF_ATTR_value
} rdf_attr;


/* Information about rdf attributes
 * type: Set when the attribute is a property rather than just syntax
 *   NOTE: raptor_process_property_attributes() expects only 
 *     RAPTOR_IDENTIFIER_TYPE_NONE,
 *     RAPTOR_IDENTIFIER_TYPE_LITERAL or RAPTOR_IDENTIFIER_TYPE_RESOURCE
 *      
 * name: name of property
 */

static const struct { 
  const char * const name;            /* attribute name */
  const raptor_identifier_type type;  /* statement value */
} rdf_attr_info[]={
  { "about",           RAPTOR_IDENTIFIER_TYPE_UNKNOWN  },
  { "aboutEach",       RAPTOR_IDENTIFIER_TYPE_UNKNOWN  },
  { "aboutEachPrefix", RAPTOR_IDENTIFIER_TYPE_UNKNOWN  },
  { "ID",              RAPTOR_IDENTIFIER_TYPE_UNKNOWN  },
  { "bagID",           RAPTOR_IDENTIFIER_TYPE_UNKNOWN  },
  { "resource",        RAPTOR_IDENTIFIER_TYPE_UNKNOWN  },
  { "parseType",       RAPTOR_IDENTIFIER_TYPE_UNKNOWN  },
  { "type",            RAPTOR_IDENTIFIER_TYPE_RESOURCE },
  { "value",           RAPTOR_IDENTIFIER_TYPE_LITERAL  }
};


/* For a typedNode production, what is the real production of the
 * block?  This can be typedNode (the default), sequence, bag, alternative
 * or (invented) container for other container types.
 */
typedef enum {
  RAPTOR_TYPED_NODE_BLOCK_TYPE_TYPED_NODE, /* default */
  RAPTOR_TYPED_NODE_BLOCK_TYPE_SEQ,
  RAPTOR_TYPED_NODE_BLOCK_TYPE_BAG,
  RAPTOR_TYPED_NODE_BLOCK_TYPE_ALT,
  RAPTOR_TYPED_NODE_BLOCK_TYPE_CONTAINER
} raptor_typed_node_block_type;


typedef enum {
  /* undetermined yet - whitespace is stored */
  RAPTOR_ELEMENT_CONTENT_TYPE_UNKNOWN,
  /* empty - no elements or cdata - whitespace is ignored */
  RAPTOR_ELEMENT_CONTENT_TYPE_EMPTY,
  /* literal content (cdata) - whitespace is significant */
  RAPTOR_ELEMENT_CONTENT_TYPE_LITERAL,
  /* properties (0+ elements) - whitespace is ignored,
   * any non-whitespace is error */
  RAPTOR_ELEMENT_CONTENT_TYPE_PROPERTIES,
  /* resource (0 or 1 element) - whitespace is ignored,
   * any non-whitespace is error */
  RAPTOR_ELEMENT_CONTENT_TYPE_RESOURCE,
  /* all content is preserved */
  RAPTOR_ELEMENT_CONTENT_TYPE_PRESERVED,
  /* a daml:collection */
  RAPTOR_ELEMENT_CONTENT_TYPE_DAML_COLLECTION
} raptor_element_content_type;


/*
 * Raptor Element/attributes on stack 
 */
struct raptor_element_s {
  /* NULL at bottom of stack */
  struct raptor_element_s *parent;
  raptor_ns_name *name;
  raptor_ns_name **attributes;
  int attribute_count;
  /* attributes declared in M&S */
  const char * rdf_attr[RDF_ATTR_LAST+1];
  /* how many of above seen */
  int rdf_attr_count;

  /* state that this production matches */
  raptor_state state;

  /* starting state for children of this element */
  raptor_state child_state;

  /* XML elements in RDF can contain EITHER cdata OR just sub-elements
   * Mixed content is not allowed 
   */

  raptor_element_content_type content_type;

  /* CDATA content of element and checks for mixed content */
  char *content_cdata;
  unsigned int content_cdata_length;
  /* how many cdata blocks seen */
  unsigned int content_cdata_seen;
  /* all cdata so far is whitespace */
  unsigned int content_cdata_all_whitespace;
  /* how many contained elements seen */
  unsigned int content_element_seen;

  /* STATIC Bag identifier */
  raptor_identifier bag;


  /* STATIC Subject identifier (URI/anon ID), type, source
   *
   * When the XML element represents a node, this is the identifier 
   */
  raptor_identifier subject;
  
  /* STATIC Predicate URI, source is either
   * RAPTOR_URI_SOURCE_ELEMENT or RAPTOR_URI_SOURCE_ATTRIBUTE
   *
   * When the XML element represents a node or predicate,
   * this is the identifier of the predicate 
   */
  raptor_identifier predicate;

  /* STATIC Object identifier (URI/anon ID), type, source
   *
   * When this XML element generates a statement that needs an object,
   * possibly from a child element, this is the identifier of the object 
   */
  raptor_identifier object;


  /* URI of type of container */
  raptor_uri *container_type;

  /* last ordinal used, so initialising to 0 works, emitting rdf:_1 first */
  int last_ordinal;

  /* If this element's parseType is a daml:collection 
   * this identifies the current tail of the collection(list). 
   */
  raptor_uri *tail_uri;
};

typedef struct raptor_element_s raptor_element;


/*
 * Raptor parser object
 */
struct raptor_parser_s {

#ifdef LIBRDF_INTERNAL
  librdf_world *world;

  /* DAML collection URIs */
  librdf_uri *raptor_daml_oil_uri;
  librdf_uri *raptor_daml_List_uri;
  librdf_uri *raptor_daml_first_uri;
  librdf_uri *raptor_daml_rest_uri;
  librdf_uri *raptor_daml_nil_uri;
#endif

  /* XML parser specific stuff */
#ifdef NEED_EXPAT
  XML_Parser xp;
#endif
#ifdef NEED_LIBXML
  /* structure holding sax event handlers */
  xmlSAXHandler sax;
  /* parser context */
  xmlParserCtxtPtr xc;
#endif  

  /* element depth */
  int depth;

  /* stack of namespaces, most recently added at top */
  raptor_ns_map *namespaces;

  /* can be filled with error location information */
  raptor_locator locator;

  /* stack of elements - elements add after current_element */
  raptor_element *root_element;
  raptor_element *current_element;

  /* non 0 if parser had fatal error and cannot continue */
  int failed;

  /* typedNode block type */
  raptor_typed_node_block_type typed_node_block_type;

  /* generated ID counter */
  int genid;

  /* base URI of RDF/XML */
  raptor_uri *base_uri;


  /* static statement for use in passing to user code */
  raptor_statement statement;


  /* FEATURE: 
   * non 0 if scanning for <rdf:RDF> element, else assume doc is RDF */
  int feature_scanning_for_rdf_RDF;

  /* FEATURE:
   * non 0 to allow non-namespaced resource, ID etc attributes
   * on RDF namespaced-elements
   */
  int feature_allow_non_ns_attributes;


  /* FEATURE:
   * non 0 to handle other rdf:parseType values that are not
   * literal or resource
   */
  int feature_allow_other_parseTypes;


  /* stuff for our user */
  void *user_data;

  void *fatal_error_user_data;
  void *error_user_data;
  void *warning_user_data;

  raptor_message_handler fatal_error_handler;
  raptor_message_handler error_handler;
  raptor_message_handler warning_handler;

  raptor_container_test_handler container_test_handler;

  /* parser callbacks */
  raptor_statement_handler statement_handler;
};




/* static variables */

#ifdef LIBRDF_INTERNAL
#define RAPTOR_RDF_type_URI LIBRDF_MS_type_URI
#define RAPTOR_RDF_value_URI LIBRDF_MS_value_URI
#define RAPTOR_RDF_subject_URI LIBRDF_MS_subject_URI
#define RAPTOR_RDF_predicate_URI LIBRDF_MS_predicate_URI
#define RAPTOR_RDF_object_URI LIBRDF_MS_object_URI
#define RAPTOR_RDF_Statement_URI LIBRDF_MS_Statement_URI

#define RAPTOR_RDF_Seq_URI LIBRDF_MS_Seq_URI
#define RAPTOR_RDF_Bag_URI LIBRDF_MS_Bag_URI
#define RAPTOR_RDF_Alt_URI LIBRDF_MS_Alt_URI

#define RAPTOR_DAML_LIST_URI(rdf_parser) rdf_parser->raptor_daml_List_uri
#define RAPTOR_DAML_FIRST_URI(rdf_parser) rdf_parser->raptor_daml_first_uri
#define RAPTOR_DAML_REST_URI(rdf_parser) rdf_parser->raptor_daml_rest_uri
#define RAPTOR_DAML_NIL_URI(rdf_parser) rdf_parser->raptor_daml_nil_uri

#else

static const char * const raptor_rdf_ms_uri=RAPTOR_RDF_MS_URI;
static const char * const raptor_rdf_schema_uri=RAPTOR_RDF_SCHEMA_URI;
static const char * const raptor_rdf_type_uri=RAPTOR_RDF_MS_URI "type";
static const char * const raptor_rdf_value_uri=RAPTOR_RDF_MS_URI "value";
static const char * const raptor_rdf_subject_uri=RAPTOR_RDF_MS_URI "subject";
static const char * const raptor_rdf_predicate_uri=RAPTOR_RDF_MS_URI "predicate";
static const char * const raptor_rdf_object_uri=RAPTOR_RDF_MS_URI "object";
static const char * const raptor_rdf_Statement_uri=RAPTOR_RDF_MS_URI "Statement";

static const char * const raptor_rdf_Seq_uri=RAPTOR_RDF_MS_URI "Seq";
static const char * const raptor_rdf_Bag_uri=RAPTOR_RDF_MS_URI "Bag";
static const char * const raptor_rdf_Alt_uri=RAPTOR_RDF_MS_URI "Alt";

static const char * const raptor_daml_List_uri=RAPTOR_DAML_OIL_URI "List";
static const char * const raptor_daml_first_uri=RAPTOR_DAML_OIL_URI "first";
static const char * const raptor_daml_rest_uri=RAPTOR_DAML_OIL_URI "rest";
static const char * const raptor_daml_nil_uri=RAPTOR_DAML_OIL_URI "nil";


#define RAPTOR_RDF_type_URI raptor_rdf_type_uri
#define RAPTOR_RDF_value_URI raptor_rdf_value_uri
#define RAPTOR_RDF_subject_URI raptor_rdf_subject_uri
#define RAPTOR_RDF_predicate_URI raptor_rdf_predicate_uri
#define RAPTOR_RDF_object_URI raptor_rdf_object_uri
#define RAPTOR_RDF_Statement_URI raptor_rdf_Statement_uri

#define RAPTOR_RDF_Seq_URI raptor_rdf_Seq_uri
#define RAPTOR_RDF_Bag_URI raptor_rdf_Bag_uri
#define RAPTOR_RDF_Alt_URI raptor_rdf_Alt_uri

#define RAPTOR_DAML_LIST_URI(rdf_parser) raptor_daml_List_uri
#define RAPTOR_DAML_FIRST_URI(rdf_parser) raptor_daml_first_uri
#define RAPTOR_DAML_REST_URI(rdf_parser) raptor_daml_rest_uri
#define RAPTOR_DAML_NIL_URI(rdf_parser) raptor_daml_nil_uri

#endif


static const char * const raptor_xml_uri="http://www.w3.org/XML/1998/namespace";



/* Prototypes for common expat/libxml parsing event-handling functions */
static void raptor_xml_start_element_handler(void *user_data, const XML_Char *name, const XML_Char **atts);
static void raptor_xml_end_element_handler(void *user_data, const XML_Char *name);
/* s is not 0 terminated. */
static void raptor_xml_cdata_handler(void *user_data, const XML_Char *s, int len);

#ifdef HAVE_XML_SetNamespaceDeclHandler
static void raptor_start_namespace_decl_handler(void *user_data, const XML_Char *prefix, const XML_Char *uri);
static void raptor_end_namespace_decl_handler(void *user_data, const XML_Char *prefix);
#endif


/* libxml-only prototypes */
#ifdef NEED_LIBXML
static void raptor_xml_warning(void *context, const char *msg, ...);
static void raptor_xml_error(void *context, const char *msg, ...);
static void raptor_xml_fatal_error(void *context, const char *msg, ...);
static void raptor_xml_validation_error(void *context, const char *msg, ...);
static void raptor_xml_validation_warning(void *context, const char *msg, ...);
static void raptor_xml_set_document_locator (void *ctx, xmlSAXLocatorPtr loc);
#endif


/* Prototypes for local functions */
#ifndef LIBRDF_INTERNAL
static char * raptor_file_uri_to_filename(const char *uri);
#endif
static void raptor_parser_fatal_error(raptor_parser* parser, const char *message, ...);
static void raptor_parser_error(raptor_parser* parser, const char *message, ...);
static void raptor_parser_warning(raptor_parser* parser, const char *message, ...);



/* prototypes for namespace and name/local_name functions */
static void raptor_init_namespaces(raptor_parser *rdf_parser);
static void raptor_start_namespace(raptor_parser *rdf_parser, const char *prefix, const char *nspace, int depth);
static void raptor_free_namespace(raptor_parser *rdf_parser,  raptor_ns_map *nspace);
static void raptor_end_namespace(raptor_parser *rdf_parser, const char *prefix, const char *nspace);
static void raptor_end_namespaces_for_depth(raptor_parser *rdf_parser);
static raptor_ns_name* raptor_make_namespaced_name(raptor_parser *rdf_parser, const char *name, const char *value, int is_element);
#ifdef RAPTOR_DEBUG
static void raptor_print_ns_name(FILE *stream, raptor_ns_name* name);
#endif
static void raptor_free_ns_name(raptor_ns_name* name);
static int raptor_ns_names_equal(raptor_ns_name *name1, raptor_ns_name *name2);


/* prototypes for element functions */
static raptor_element* raptor_element_pop(raptor_parser *rdf_parser);
static void raptor_element_push(raptor_parser *rdf_parser, raptor_element* element);
static void raptor_free_element(raptor_element *element);
#ifdef RAPTOR_DEBUG
static void raptor_print_element(raptor_element *element, FILE* stream);
#endif


/* prototypes for grammar functions */
static void raptor_start_element_grammar(raptor_parser *parser, raptor_element *element);
static void raptor_end_element_grammar(raptor_parser *parser, raptor_element *element);


/* prototype for statement related functions */
static void raptor_generate_statement(raptor_parser *rdf_parser, raptor_uri *subject_uri, const char *subject_id, const raptor_identifier_type subject_type, const raptor_uri_source subject_uri_source, raptor_uri *predicate_uri, const char *predicate_id, const raptor_identifier_type predicate_type, const raptor_uri_source predicate_uri_source, raptor_uri *object_uri, const char *object_id, const raptor_identifier_type object_type, const raptor_uri_source object_uri_source, raptor_uri *bag);
static void raptor_print_statement_detailed(const raptor_statement *statement, int detailed, FILE *stream);


/*
 * Namespaces in XML
 * http://www.w3.org/TR/1999/REC-xml-names-19990114/#nsc-NSDeclared
 * (section 4) says:
 *
 * --------------------------------------------------------------------
 *   The prefix xml is by definition bound to the namespace name 
 *   http://www.w3.org/XML/1998/namespace
 * --------------------------------------------------------------------
 *
 * Thus should define it in the table of namespaces before we start.
 *
 * We *can* also define others, but let's not.
 *
 */
static void
raptor_init_namespaces(raptor_parser *rdf_parser) {
  /* defined at level -1 since always 'present' when inside the XML world */
  raptor_start_namespace(rdf_parser, "xml", raptor_xml_uri, -1);
}


static void
raptor_start_namespace(raptor_parser *rdf_parser, 
                       const char *prefix, const char *nspace, int depth)
{
  int prefix_length=0;
#ifdef LIBRDF_INTERNAL
#else
  int uri_length=0;
#endif
  int len;
  raptor_ns_map *map;
  char *p;

  LIBRDF_DEBUG4(raptor_start_namespace,
                "namespace prefix %s uri %s depth %d\n",
                prefix ? prefix : "(default)", nspace, depth);

  /* Convert an empty namespace string "" to a NULL pointer */
  if(!*nspace)
    nspace=NULL;

  len=sizeof(raptor_ns_map);
#ifdef LIBRDF_INTERNAL
#else
  if(nspace) {
    uri_length=strlen(nspace);
    len+=uri_length+1;
  }
#endif
  if(prefix) {
    prefix_length=strlen(prefix);
    len+=prefix_length+1;
  }

  /* Just one malloc for map structure + namespace (maybe) + prefix (maybe)*/
  map=(raptor_ns_map*)LIBRDF_CALLOC(raptor_ns_map, len, 1);
  if(!map) {
    raptor_parser_fatal_error(rdf_parser, "Out of memory");
    return;
  }

  p=(char*)map+sizeof(raptor_ns_map);
#ifdef LIBRDF_INTERNAL
  map->uri=librdf_new_uri(rdf_parser->world, nspace);
  if(!map->uri) {
    raptor_parser_fatal_error(rdf_parser, "Out of memory");
    LIBRDF_FREE(raptor_ns_map, map);
    return;
  }
#else
  if(nspace) {
    map->uri=strcpy((char*)p, nspace);
    map->uri_length=uri_length;
    p+= uri_length+1;
  }
#endif
  if(prefix) {
    map->prefix=strcpy((char*)p, prefix);
    map->prefix_length=prefix_length;

    if(!strcmp(map->prefix, "xml"))
      map->is_xml=1;
  }
  map->depth=depth;

  /* set convienience flags when there is a defined namespace URI */
  if(nspace) {
#ifdef LIBRDF_INTERNAL
    if(librdf_uri_equals(map->uri, librdf_concept_ms_namespace_uri))
      map->is_rdf_ms=1;
    else if(librdf_uri_equals(map->uri, librdf_concept_schema_namespace_uri))
      map->is_rdf_schema=1;
#else
    if(!strcmp(nspace, raptor_rdf_ms_uri))
      map->is_rdf_ms=1;
    else if(!strcmp(nspace, raptor_rdf_schema_uri))
      map->is_rdf_schema=1;
#endif
  }

  if(rdf_parser->namespaces)
    map->next=rdf_parser->namespaces;
  rdf_parser->namespaces=map;
}


static void 
raptor_free_namespace(raptor_parser *rdf_parser,  raptor_ns_map* nspace)
{
#ifdef LIBRDF_INTERNAL
  if(nspace->uri)
    librdf_free_uri(nspace->uri);
#endif
  LIBRDF_FREE(raptor_ns_map, nspace);
}


static void 
raptor_end_namespace(raptor_parser *rdf_parser, 
                     const char *prefix, const char *nspace)
{
  LIBRDF_DEBUG3(raptor_end_namespace, "prefix %s uri \"%s\"\n", 
                prefix ? prefix : "(default)", nspace);
}


static void 
raptor_end_namespaces_for_depth(raptor_parser *rdf_parser) 
{
  while(rdf_parser->namespaces &&
        rdf_parser->namespaces->depth == rdf_parser->depth) {
    raptor_ns_map* ns=rdf_parser->namespaces;
    raptor_ns_map* next=ns->next;

#ifdef LIBRDF_INTERNAL
    raptor_end_namespace(rdf_parser, ns->prefix, 
                         librdf_uri_as_string(ns->uri));
#else  
    raptor_end_namespace(rdf_parser, ns->prefix, ns->uri);
#endif
    raptor_free_namespace(rdf_parser, ns);

    rdf_parser->namespaces=next;
  }

}



/*
 * Namespaces in XML
 * http://www.w3.org/TR/1999/REC-xml-names-19990114/#defaulting
 * says:
 *
 * --------------------------------------------------------------------
 *  5.2 Namespace Defaulting
 *
 *  A default namespace is considered to apply to the element where it
 *  is declared (if that element has no namespace prefix), and to all
 *  elements with no prefix within the content of that element. 
 *
 *  If the URI reference in a default namespace declaration is empty,
 *  then unprefixed elements in the scope of the declaration are not
 *  considered to be in any namespace.
 *
 *  Note that default namespaces do not apply directly to attributes.
 *
 * [...]
 *
 *  5.3 Uniqueness of Attributes
 *
 *  In XML documents conforming to this specification, no tag may
 *  contain two attributes which:
 *
 *    1. have identical names, or 
 *
 *    2. have qualified names with the same local part and with
 *    prefixes which have been bound to namespace names that are
 *    identical.
 * --------------------------------------------------------------------
 */

static raptor_ns_name*
raptor_make_namespaced_name(raptor_parser *rdf_parser, const char *name,
                            const char *value, int is_element) 
{
  raptor_ns_name* ns_name;
  const char *p;
  raptor_ns_map* ns;
  char* new_name;
  int prefix_length;
  int local_name_length=0;

#if RAPTOR_DEBUG > 1
  LIBRDF_DEBUG2(raptor_make_namespaced_name,
                "name %s\n", name);
#endif  

  ns_name=(raptor_ns_name*)LIBRDF_CALLOC(raptor_ns_name, sizeof(raptor_ns_name), 1);
  if(!ns_name) {
    raptor_parser_fatal_error(rdf_parser, "Out of memory");
    return NULL;
  }

  if(value) {
    int value_length=strlen(value);
    char* new_value=(char*)LIBRDF_MALLOC(cstring, value_length+1);

    if(!new_value) {
      raptor_parser_fatal_error(rdf_parser, "Out of memory");
      LIBRDF_FREE(raptor_ns_name, ns_name);
      return NULL;
    } 
    strcpy(new_value, value);
    ns_name->value=new_value;
    ns_name->value_length=value_length;
  }

  /* Find : */
  for(p=name; *p && *p != ':'; p++)
    ;

  if(!*p) {
    local_name_length=p-name;

    /* No : - pick up default namespace, if there is one */
    new_name=(char*)LIBRDF_MALLOC(cstring, local_name_length+1);
    if(!new_name) {
      raptor_parser_fatal_error(rdf_parser, "Out of memory");
      raptor_free_ns_name(ns_name);
      return NULL;
    }
    strcpy(new_name, name);
    ns_name->local_name=new_name;
    ns_name->local_name_length=local_name_length;
    
    /* Find a default namespace */
    for(ns=rdf_parser->namespaces; ns && ns->prefix; ns=ns->next)
      ;

    if(ns) {
      ns_name->nspace=ns;
#if RAPTOR_DEBUG > 1
    LIBRDF_DEBUG2(raptor_make_namespaced_name,
                  "Found default namespace %s\n", ns->uri);
#endif
    } else {
      /* failed to find namespace - now what? FIXME */
      /* raptor_parser_warning(rdf_parser, "No default namespace defined - cannot expand %s", name); */
#if RAPTOR_DEBUG > 1
      LIBRDF_DEBUG1(raptor_make_namespaced_name,
                    "No default namespace defined\n");
#endif
    }

  } else {
    /* There is a namespace prefix */

    prefix_length=p-name;
    p++; 

    /* p now is at start of local_name */
    local_name_length=strlen(p);
    new_name=(char*)LIBRDF_MALLOC(cstring, local_name_length+1);
    if(!new_name) {
      raptor_parser_fatal_error(rdf_parser, "Out of memory");
      raptor_free_ns_name(ns_name);
      return NULL;
    }
    strcpy(new_name, p);
    ns_name->local_name=new_name;
    ns_name->local_name_length=local_name_length;

    /* Find the namespace */
    for(ns=rdf_parser->namespaces; ns ; ns=ns->next)
      if(ns->prefix && prefix_length == ns->prefix_length && 
         !strncmp(name, ns->prefix, prefix_length))
        break;

    if(!ns) {
      /* failed to find namespace - now what? */
      raptor_parser_error(rdf_parser, "Failed to find namespace in %s", name);
      raptor_free_ns_name(ns_name);
      return NULL;
    }

#if RAPTOR_DEBUG > 1
    LIBRDF_DEBUG3(raptor_make_namespaced_name,
                  "Found namespace prefix %s URI %s\n", ns->prefix, ns->uri);
#endif
    ns_name->nspace=ns;
  }



  /* If namespace has a URI and a local_name is defined, create the URI
   * for this element 
   */
  if(ns_name->nspace && ns_name->nspace->uri && local_name_length) {
#ifdef LIBRDF_INTERNAL
    librdf_uri* uri=librdf_new_uri_from_uri_local_name(ns_name->nspace->uri,
                                                       new_name);
    if(!uri) {
      raptor_free_ns_name(ns_name);
      return NULL;
    }
    ns_name->uri=uri;
#else
    char *uri_string=(char*)LIBRDF_MALLOC(cstring, 
                                          ns_name->nspace->uri_length + 
                                          local_name_length + 1);
    if(!uri_string) {
      raptor_free_ns_name(ns_name);
      return NULL;
    }
    strcpy(uri_string, ns_name->nspace->uri);
    strcpy(uri_string + ns_name->nspace->uri_length, new_name);
    ns_name->uri=uri_string;
#endif
  }


  return ns_name;
}


#ifdef RAPTOR_DEBUG
static void
raptor_print_ns_name(FILE *stream, raptor_ns_name* name) 
{
  if(name->nspace) {
    if(name->nspace->prefix)
      fprintf(stream, "%s:%s", name->nspace->prefix, name->local_name);
    else
      fprintf(stream, "(default):%s", name->local_name);
  } else
    fputs(name->local_name, stream);
}
#endif

static void
raptor_free_ns_name(raptor_ns_name* name) 
{
  if(name->local_name)
    LIBRDF_FREE(cstring, (void*)name->local_name);

  if(name->uri)
    RAPTOR_FREE_URI(name->uri);

  if(name->value)
    LIBRDF_FREE(cstring, (void*)name->value);
  LIBRDF_FREE(raptor_ns_name, name);
}


static int
raptor_ns_names_equal(raptor_ns_name *name1, raptor_ns_name *name2)
{
#ifdef LIBRDF_INTERNAL
  if(name1->uri && name2->uri)
    return librdf_uri_equals(name1->uri, name2->uri);
#else
  if(name1->nspace != name2->nspace)
    return 0;
#endif
  if(name1->local_name_length != name2->local_name_length)
    return 0;
  if(strcmp(name1->local_name, name2->local_name))
    return 0;
  return 1;
}


static raptor_element*
raptor_element_pop(raptor_parser *rdf_parser) 
{
  raptor_element *element=rdf_parser->current_element;

  if(!element)
    return NULL;

  rdf_parser->current_element=element->parent;
  if(rdf_parser->root_element == element) /* just deleted root */
    rdf_parser->root_element=NULL;

  return element;
}


static void
raptor_element_push(raptor_parser *rdf_parser, raptor_element* element) 
{
  element->parent=rdf_parser->current_element;
  rdf_parser->current_element=element;
  if(!rdf_parser->root_element)
    rdf_parser->root_element=element;
}


static void
raptor_free_element(raptor_element *element)
{
  unsigned int i;

  for (i=0; i < element->attribute_count; i++)
    if(element->attributes[i])
      raptor_free_ns_name(element->attributes[i]);

  if(element->attributes)
    LIBRDF_FREE(raptor_ns_name_array, element->attributes);

  /* Free special RDF M&S attributes */
  for(i=0; i<= RDF_ATTR_LAST; i++) 
    if(element->rdf_attr[i])
      LIBRDF_FREE(cstring, (void*)element->rdf_attr[i]);

  if(element->content_cdata_length)
    LIBRDF_FREE(raptor_ns_name_array, element->content_cdata);

  raptor_free_identifier(&element->subject);
  raptor_free_identifier(&element->predicate);
  raptor_free_identifier(&element->object);
  raptor_free_identifier(&element->bag);

  if(element->tail_uri)
    RAPTOR_FREE_URI(element->tail_uri);

  raptor_free_ns_name(element->name);
  LIBRDF_FREE(raptor_element, element);
}



#ifdef RAPTOR_DEBUG
static void
raptor_print_element(raptor_element *element, FILE* stream)
{
  raptor_print_ns_name(stream, element->name);
  fputc('\n', stream);

  if(element->attribute_count) {
    unsigned int i;

    fputs(" attributes: ", stream);
    for (i = 0; i < element->attribute_count; i++) {
      if(i)
        fputc(' ', stream);
      raptor_print_ns_name(stream, element->attributes[i]);
      fprintf(stream, "='%s'", element->attributes[i]->value);
    }
    fputc('\n', stream);
  }
}
#endif


static char *
raptor_format_element(raptor_element *element, int *length_p, int is_end)
{
  int length;
  char *buffer;
  char *ptr;
  unsigned int i;

  /* get length of element name (and namespace-prefix: if there is one) */
  length=element->name->local_name_length + 1; /* < */
  if(element->name->nspace &&
     element->name->nspace->prefix_length > 0)
    length += element->name->nspace->prefix_length + 1; /* : */

  if(is_end)
    length++; /* / */

  if (!is_end && element->attributes) {
    for(i=0; i < element->attribute_count; i++) {
      length++; /* ' ' between attributes and after element name */

      /* qname */
      length += element->attributes[i]->local_name_length;
      if(element->attributes[i]->nspace &&
         element->attributes[i]->nspace->prefix_length > 0)
         /* prefix: */
        length += element->attributes[i]->nspace->prefix_length + 1;

      /* ="value" */
      length += element->attributes[i]->value_length + 3;
    }
  }
  
  length++; /* > */
  
  if(length_p)
    *length_p=length;

  /* +1 here is for \0 at end */
  buffer=(char*)LIBRDF_MALLOC(cstring, length + 1);
  if(!buffer)
    return NULL;

  ptr=buffer;

  *ptr++ = '<';
  if(is_end)
    *ptr++ = '/';
  if(element->name->nspace && element->name->nspace->prefix_length > 0) {
    strncpy(ptr, element->name->nspace->prefix,
            element->name->nspace->prefix_length);
    ptr+= element->name->nspace->prefix_length;
    *ptr++=':';
  }
  strcpy(ptr, element->name->local_name);
  ptr += element->name->local_name_length;

  if(!is_end && element->attributes) {
    for(i=0; i < element->attribute_count; i++) {
      *ptr++ =' ';
      
      if(element->attributes[i]->nspace && 
         element->attributes[i]->nspace->prefix_length > 0) {
        strncpy(ptr, element->attributes[i]->nspace->prefix,
                element->attributes[i]->nspace->prefix_length);
        ptr+= element->attributes[i]->nspace->prefix_length;
        *ptr++=':';
      }
    
      strcpy(ptr, element->attributes[i]->local_name);
      ptr += element->attributes[i]->local_name_length;
      
      *ptr++ ='=';
      *ptr++ ='"';
      
      strcpy(ptr, element->attributes[i]->value);
      ptr += element->attributes[i]->value_length;
      *ptr++ ='"';
    }
  }
  
  *ptr++ = '>';
  *ptr='\0';

  return buffer;
}



static void
raptor_xml_start_element_handler(void *user_data,
                                 const XML_Char *name, const XML_Char **atts)
{
  raptor_parser* rdf_parser=(raptor_parser*)user_data;
  int all_atts_count=0;
  int ns_attributes_count=0;
  raptor_ns_name** named_attrs=NULL;
  int i;
  raptor_ns_name* element_name;
  raptor_element* element=NULL;
#ifdef NEED_EXPAT
  /* for storing error info */
  raptor_locator *locator=&rdf_parser->locator; 
#endif
  int non_nspaced_count=0;
  
#ifdef RAPTOR_DEBUG
  fputc('\n', stderr);
#endif

#ifdef NEED_EXPAT
  locator->line=XML_GetCurrentLineNumber(rdf_parser->xp);
  locator->column=XML_GetCurrentColumnNumber(rdf_parser->xp);
  locator->byte=XML_GetCurrentByteIndex(rdf_parser->xp);
#endif

  rdf_parser->depth++;

  if (atts) {
    /* Round 1 - find special attributes, at present just namespaces */
    for (i = 0; atts[i]; i+=2) {
      all_atts_count++;

      /* synthesise the XML NS events */
      if(!strncmp(atts[i], "xmlns", 5)) {
        /* there is more i.e. xmlns:foo */
        const char *prefix=atts[i][5] ? &atts[i][6] : NULL;

        raptor_start_namespace(rdf_parser, prefix, atts[i+1],
                               rdf_parser->depth);
        /* Is it ok to zap XML parser array things? */
        atts[i]=NULL; 
        continue;
      }

      ns_attributes_count++;
    }
  }


  /* Now can recode element name with a namespace */

  element_name=raptor_make_namespaced_name(rdf_parser, name, NULL, 1);
  if(!element_name) {
    raptor_parser_fatal_error(rdf_parser, "Out of memory");
    return;
  }


  /* Create new element structure */
  element=(raptor_element*)LIBRDF_CALLOC(raptor_element, 
                                         sizeof(raptor_element), 1);
  if(!element) {
    raptor_parser_fatal_error(rdf_parser, "Out of memory");
    raptor_free_ns_name(element_name);
    return;
  } 


  element->name=element_name;

  /* Prepare for possible element content */
  element->content_element_seen=0;
  element->content_cdata_seen=0;
  element->content_cdata_length=0;


  if(!element_name->nspace)
    non_nspaced_count++;


  if(ns_attributes_count) {
    int offset = 0;

    /* Round 2 - turn attributes into namespaced-attributes */

    /* Allocate new array to hold namespaced-attributes */
    named_attrs=(raptor_ns_name**)LIBRDF_CALLOC(raptor_ns_name-array, sizeof(raptor_ns_name*), ns_attributes_count);
    if(!named_attrs) {
      raptor_parser_fatal_error(rdf_parser, "Out of memory");
      LIBRDF_FREE(raptor_element, element);
      raptor_free_ns_name(element_name);
      return;
    }

    for (i = 0; i < all_atts_count; i++) {
      raptor_ns_name* attribute;

      /* Skip previously processed attributes */
      if(!atts[i<<1])
        continue;

      /* namespace-name[i] stored in named_attrs[i] */
      attribute=raptor_make_namespaced_name(rdf_parser, atts[i<<1],
                                            atts[(i<<1)+1], 0);
      if(!attribute) { /* failed - tidy up and return */
        int j;

        for (j=0; j < i; j++)
          LIBRDF_FREE(raptor_ns_name, named_attrs[j]);
        LIBRDF_FREE(raptor_ns_name_array, named_attrs);
        LIBRDF_FREE(raptor_element, element);
        raptor_free_ns_name(element_name);
        return;
      }

      
      /* leave literal XML alone */
      if (rdf_parser->current_element &&
          rdf_parser->current_element->content_type != RAPTOR_ELEMENT_CONTENT_TYPE_LITERAL) {

        /* Save pointers to some RDF M&S attributes */

        /* If RDF M&S namespace-prefixed attributes */
        if(attribute->nspace && attribute->nspace->is_rdf_ms) {
          const char *attr_name=attribute->local_name;
          int j;

          for(j=0; j<= RDF_ATTR_LAST; j++)
            if(!strcmp(attr_name, rdf_attr_info[j].name)) {
              element->rdf_attr[j]=attribute->value;
              element->rdf_attr_count++;
              /* Delete it if it was stored elsewhere */
#if RAPTOR_DEBUG
              LIBRDF_DEBUG3(raptor_xml_start_element_handler,
                            "Found RDF M&S attribute %s URI %s\n",
                            attr_name, attribute->value);
#endif
              /* make sure value isn't deleted from ns_name structure */
              attribute->value=NULL;
              raptor_free_ns_name(attribute);
              attribute=NULL;
            }
        } /* end if RDF M&S namespaced-prefixed attributes */

        if(!attribute)
          continue;

        /* If non namespace-prefixed RDF M&S attributes found on an element */
        if(rdf_parser->feature_allow_non_ns_attributes) {
          const char *attr_name=attribute->local_name;
          int j;

          for(j=0; j<= RDF_ATTR_LAST; j++)
            if(!strcmp(attr_name, rdf_attr_info[j].name)) {
              element->rdf_attr[j]=attribute->value;
              element->rdf_attr_count++;
              /* Delete it if it was stored elsewhere */
#if RAPTOR_DEBUG
              LIBRDF_DEBUG3(raptor_xml_start_element_handler,
                            "Found non-namespaced RDF M&S attribute %s URI %s\n",
                            attr_name, attribute->value);
#endif
              /* make sure value isn't deleted from ns_name structure */
              attribute->value=NULL;
              raptor_free_ns_name(attribute);
              attribute=NULL;
            }
        } /* end if non-namespace prefixed RDF M&S attributes */

        if(!attribute)
          continue;

        if(!attribute->nspace)
          non_nspaced_count++;

      } /* end if leave literal XML alone */

      if(attribute)
        named_attrs[offset++]=attribute;
    }

    /* set actual count from attributes that haven't been skipped */
    ns_attributes_count=offset;
    if(!offset && named_attrs) {
      /* all attributes were RDF M&S or other specials and deleted
       * so delete array and don't store pointer */
      LIBRDF_FREE(raptor_ns_name_array, named_attrs);
      named_attrs=NULL;
    }

  } /* end if ns_attributes_count */

  element->attributes=named_attrs;
  element->attribute_count=ns_attributes_count;


  raptor_element_push(rdf_parser, element);


  /* start from unknown; if we have a parent, it may set this */
  element->state=RAPTOR_STATE_UNKNOWN;

  if(element->parent) {
    element->content_type=element->parent->content_type;

    element->parent->content_element_seen++;
    
    /* leave literal XML alone */
    if (rdf_parser->current_element->content_type != RAPTOR_ELEMENT_CONTENT_TYPE_LITERAL) {
      if(element->parent->content_element_seen == 1 &&
         element->parent->content_cdata_seen == 1) {
        /* Uh oh - mixed content, the parent element has cdata too */
        raptor_parser_warning(rdf_parser, "element %s has mixed content.", 
                              element->parent->name->local_name);
      }
    
      /* If there is some existing all-whitespace content cdata
       * (which is probably before the first element) delete it
       */
      if(element->parent->content_element_seen &&
         element->parent->content_cdata_all_whitespace &&
         element->parent->content_cdata) {
        LIBRDF_FREE(raptor_ns_name_array, element->parent->content_cdata);
        element->parent->content_cdata=NULL;
        element->parent->content_cdata_length=0;
      }

    } /* end if leave literal XML alone */

    if(element->parent->child_state)
      element->state=element->parent->child_state;
    else
      raptor_parser_fatal_error(rdf_parser, "raptor_xml_start_element_handler - no parent element child_state set\n");      
    
  } /* end if element->parent */


#ifdef RAPTOR_DEBUG
  fprintf(stderr, "raptor_xml_start_element_handler: Start ns-element: ");
  raptor_print_element(element, stderr);
#endif

  
  /* Check for non namespaced stuff */
  if(non_nspaced_count)
    element->state=RAPTOR_STATE_SKIPPING;

  /* Right, now ready to enter the grammar */
  raptor_start_element_grammar(rdf_parser, element);

}


static void
raptor_xml_end_element_handler(void *user_data, const XML_Char *name)
{
  raptor_parser* rdf_parser=(raptor_parser*)user_data;
  raptor_element* element;
  raptor_ns_name *element_name;
#ifdef NEED_EXPAT
  /* for storing error info */
  raptor_locator *locator=&rdf_parser->locator;
#endif

#ifdef NEED_EXPAT
  locator->line=XML_GetCurrentLineNumber(rdf_parser->xp);
  locator->column=XML_GetCurrentColumnNumber(rdf_parser->xp);
  locator->byte=XML_GetCurrentByteIndex(rdf_parser->xp);
#endif

  /* recode element name */

  element_name=raptor_make_namespaced_name(rdf_parser, name, NULL, 1);
  if(!element_name) {
    raptor_parser_fatal_error(rdf_parser, "Out of memory");
    return;
  }


#ifdef RAPTOR_DEBUG
  fprintf(stderr, "\nraptor_xml_end_element_handler: End ns-element: ");
  raptor_print_ns_name(stderr, element_name);
  fputc('\n', stderr);
#endif

  element=raptor_element_pop(rdf_parser);
  if(!raptor_ns_names_equal(element->name, element_name)) {
    /* Hmm, unexpected name - FIXME, should do something! */
    raptor_parser_warning(rdf_parser, 
                          "Element %s ended, expected end of element %s\n",
                          name, element->name->local_name);
    return;
  }

  raptor_end_element_grammar(rdf_parser, element);

  raptor_free_ns_name(element_name);

  raptor_end_namespaces_for_depth(rdf_parser);

  raptor_free_element(element);

  rdf_parser->depth--;

#ifdef RAPTOR_DEBUG
  fputc('\n', stderr);
#endif

}



/* cdata (and ignorable whitespace for libxml). 
 * s is not 0 terminated for expat, is for libxml - grrrr.
 */
static void
raptor_xml_cdata_handler(void *user_data, const XML_Char *s, int len)
{
  raptor_parser* rdf_parser=(raptor_parser*)user_data;
  raptor_element* element;
  raptor_state state;
  char *buffer;
  char *ptr;
  int all_whitespace=1;
  int i;

  for(i=0; i<len; i++)
    if(!isspace(s[i])) {
      all_whitespace=0;
      break;
    }

  element=rdf_parser->current_element;

  /* cdata never changes the parser state 
   * and the containing element state always determines what to do.
   * Use the child_state first if there is one, since that applies
   */
  state=element->child_state;
  LIBRDF_DEBUG3(raptor_xml_cdata_handler, "in state %d - %s\n", state,
                raptor_state_as_string(state));
  switch(state) {
    case RAPTOR_STATE_SKIPPING:
      return;

    case RAPTOR_STATE_UNKNOWN:
      /* Ignore all cdata if still looking for RDF */
      if(rdf_parser->feature_scanning_for_rdf_RDF)
        return;

      /* Ignore all whitespace cdata before first element */
      if(all_whitespace)
        return;

      /* This probably will never happen since that would make the
       * XML not be well-formed
       */
      raptor_parser_warning(rdf_parser, "Found cdata before RDF element.");
      break;

    case RAPTOR_STATE_PARSETYPE_OTHER:
      /* FIXME */

      /* FALLTHROUGH */

    case RAPTOR_STATE_PARSETYPE_LITERAL:
      element->content_type=RAPTOR_ELEMENT_CONTENT_TYPE_LITERAL;
      break;

    case RAPTOR_STATE_PARSETYPE_DAML_COLLECTION:
      element->content_type=RAPTOR_ELEMENT_CONTENT_TYPE_DAML_COLLECTION;
      break;

    default:
      break;
    } /* end switch */


  if(element->content_type != RAPTOR_ELEMENT_CONTENT_TYPE_LITERAL &&
     element->content_type != RAPTOR_ELEMENT_CONTENT_TYPE_PRESERVED) {

    /* Whitespace is ignored except for literal or preserved content types */
    if(all_whitespace) {
      LIBRDF_DEBUG2(raptor_xml_cdata_handler, "Ignoring whitespace cdata inside element %s\n", element->name->local_name);
      return;
    }

    if(++element->content_cdata_seen == 1 &&
       element->content_element_seen == 1) {
      /* Uh oh - mixed content, this element has elements too */
      raptor_parser_warning(rdf_parser, "element %s has mixed content.", 
                            element->name->local_name);
    }
  }


  buffer=(char*)LIBRDF_MALLOC(cstring, element->content_cdata_length + len + 1);
  if(!buffer) {
    raptor_parser_fatal_error(rdf_parser, "Out of memory");
    return;
  }

  if(element->content_cdata_length) {
    strncpy(buffer, element->content_cdata, element->content_cdata_length);
    LIBRDF_FREE(cstring, element->content_cdata);
    element->content_cdata_all_whitespace &= all_whitespace;
  } else
    element->content_cdata_all_whitespace = all_whitespace;

  element->content_cdata=buffer;

  /* move pointer to end of cdata buffer */
  ptr=buffer+element->content_cdata_length;

  /* adjust stored length */
  element->content_cdata_length += len;

  /* now write new stuff at end of cdata buffer */
  strncpy(ptr, s, len);
  ptr += len;
  *ptr = '\0';

  LIBRDF_DEBUG3(raptor_xml_cdata_handler, 
                "content cdata now: '%s' (%d bytes)\n", 
                buffer, element->content_cdata_length);

  LIBRDF_DEBUG3(raptor_xml_cdata_handler, 
                "ending in state %d - %s\n",
                state, raptor_state_as_string(state));

}


#ifdef HAVE_XML_SetNamespaceDeclHandler
static void
raptor_start_namespace_decl_handler(void *user_data,
                                    const XML_Char *prefix, const XML_Char *uri)
{
  raptor_parser* rdf_parser=(raptor_parser*)user_data;

#ifdef RAPTOR_DEBUG
  fprintf(stderr_parser->locator, "saw namespace %s URI %s\n", prefix, uri);
#endif
}


static void
raptor_end_namespace_decl_handler(void *user_data, const XML_Char *prefix)
{
  raptor_parser* rdf_parser=(raptor_parser*)user_data;

#ifdef RAPTOR_DEBUG
  fprintf(stderr_parser->locator, "saw end namespace prefix %s\n", prefix);
#endif
}
#endif


#ifdef NEED_EXPAT
/* This is called for a declaration of an unparsed (NDATA) entity */
static void
raptor_xml_unparsed_entity_decl_handler(void *user_data,
                                        const XML_Char *entityName,
                                        const XML_Char *base,
                                        const XML_Char *systemId,
                                        const XML_Char *publicId,
                                        const XML_Char *notationName) 
{
/*  raptor_parser* rdf_parser=(raptor_parser*)user_data; */
  fprintf(stderr,
          "raptor_xml_unparsed_entity_decl_handler: entityName %s base %s systemId %s publicId %s notationName %s\n",
          entityName, (base ? base : "(None)"), 
          systemId, (publicId ? publicId: "(None)"),
          notationName);
}


static int 
raptor_xml_external_entity_ref_handler(void *user_data,
                                       const XML_Char *context,
                                       const XML_Char *base,
                                       const XML_Char *systemId,
                                       const XML_Char *publicId)
{
/*  raptor_parser* rdf_parser=(raptor_parser*)user_data; */
  fprintf(stderr,
          "raptor_xml_external_entity_ref_handler: base %s systemId %s publicId %s\n",
          (base ? base : "(None)"), 
          systemId, (publicId ? publicId: "(None)"));

  /* "The handler should return 0 if processing should not continue
   * because of a fatal error in the handling of the external entity."
   */
  return 1;
}
#endif


#ifdef NEED_LIBXML
#include <stdarg.h>

static const char* xml_warning_prefix="XML parser warning - ";
static const char* xml_error_prefix="XML parser error - ";
static const char* xml_fatal_error_prefix="XML parser fatal error - ";
static const char* xml_validation_error_prefix="XML parser validation error - ";
static const char* xml_validation_warning_prefix="XML parser validation warning - ";

static void
raptor_xml_warning(void *ctx, const char *msg, ...) 
{
  va_list args;
  raptor_parser* rdf_parser=(raptor_parser*)ctx;
  int length;
  char *nmsg;

  va_start(args, msg);
  length=strlen(xml_warning_prefix)+strlen(msg)+1;
  nmsg=(char*)LIBRDF_MALLOC(cstring, length);
  if(!nmsg) {
    /* just pass on, might be out of memory error */
    raptor_parser_warning(rdf_parser, msg, args);
  } else {
    strcpy(nmsg, xml_warning_prefix);
    strcat(nmsg, msg);
    raptor_parser_warning(rdf_parser, nmsg, args);
    LIBRDF_FREE(cstring,nmsg);
  }
  va_end(args);
}


static void
raptor_xml_error(void *ctx, const char *msg, ...) 
{
  va_list args;
  raptor_parser* rdf_parser=(raptor_parser*)ctx;
  int length;
  char *nmsg;

  va_start(args, msg);
  length=strlen(xml_error_prefix)+strlen(msg)+1;
  nmsg=(char*)LIBRDF_MALLOC(cstring, length);
  if(!nmsg) {
    /* just pass on, might be out of memory error */
    raptor_parser_error(rdf_parser, nmsg, args);
  } else {
    strcpy(nmsg, xml_error_prefix);
    strcat(nmsg, msg);
    raptor_parser_error(rdf_parser, nmsg, args);
    LIBRDF_FREE(cstring,nmsg);
  }
  va_end(args);
}


static void
raptor_xml_fatal_error(void *ctx, const char *msg, ...) 
{
  va_list args;
  raptor_parser* rdf_parser=(raptor_parser*)ctx;
  int length;
  char *nmsg;

  va_start(args, msg);
  length=strlen(xml_fatal_error_prefix)+strlen(msg)+1;
  nmsg=(char*)LIBRDF_MALLOC(cstring, length);
  if(!nmsg) {
    /* just pass on, might be out of memory error */
    raptor_parser_fatal_error(rdf_parser, nmsg, args);
  } else {
    strcpy(nmsg, xml_error_prefix);
    strcat(nmsg, msg);
    raptor_parser_fatal_error(rdf_parser, nmsg, args);
    LIBRDF_FREE(cstring,nmsg);
  }
  va_end(args);
}


static void
raptor_xml_validation_error(void *ctx, const char *msg, ...) 
{
  va_list args;
  raptor_parser* rdf_parser=(raptor_parser*)ctx;
  int length;
  char *nmsg;

  va_start(args, msg);
  length=strlen(xml_validation_error_prefix)+strlen(msg)+1;
  nmsg=(char*)LIBRDF_MALLOC(cstring, length);
  if(!nmsg) {
    /* just pass on, might be out of memory error */
    raptor_parser_fatal_error(rdf_parser, nmsg, args);
  } else {
    strcpy(nmsg, xml_validation_error_prefix);
    strcat(nmsg, msg);
    raptor_parser_fatal_error(rdf_parser, nmsg, args);
    LIBRDF_FREE(cstring,nmsg);
  }
  va_end(args);
}


static void
raptor_xml_validation_warning(void *ctx, const char *msg, ...) 
{
  va_list args;
  raptor_parser* rdf_parser=(raptor_parser*)ctx;
  int length;
  char *nmsg;

  va_start(args, msg);
  length=strlen(xml_validation_warning_prefix)+strlen(msg)+1;
  nmsg=(char*)LIBRDF_MALLOC(cstring, length);
  if(!nmsg) {
    /* just pass on, might be out of memory error */
    raptor_parser_warning(rdf_parser, nmsg, args);
  } else {
    strcpy(nmsg, xml_validation_warning_prefix);
    strcat(nmsg, msg);
    raptor_parser_fatal_error(rdf_parser, nmsg, args);
    LIBRDF_FREE(cstring,nmsg);
  }
  va_end(args);
}


static void
raptor_xml_set_document_locator (void *ctx, xmlSAXLocatorPtr loc) 
{
  raptor_parser* rdf_parser=(raptor_parser*)ctx;
  /* for storing error info */
  raptor_locator *locator=&rdf_parser->locator;

  locator->line=loc->getLineNumber(rdf_parser->xc);
  locator->column=loc->getColumnNumber(rdf_parser->xc);
}
  

#endif



#ifndef LIBRDF_INTERNAL
/**
 * raptor_file_uri_to_filename - Convert a URI representing a file (starting file:) to a filename
 * @uri: URI of string
 * 
 * Return value: the filename or NULL on failure
 **/
static char *
raptor_file_uri_to_filename(const char *uri) 
{
  char *filename;
#ifndef WIN32
  int length;
#endif

#ifdef WIN32
  filename=LIBRDF_MALLOC(cstring, MAX_PATH);
  if (S_OK != URLDownloadToCacheFile(NULL, uri, filename, URLOSTRM_GETNEWESTVERSION, 0, NULL))
    return NULL;
#else
  if (strncmp(uri, "file:", 5))
    return NULL;

  /* FIXME: unix version of URI -> filename conversion */
  length=strlen(uri) -5 +1;
  filename=(char*)LIBRDF_MALLOC(cstring, length);
  if(!filename)
    return NULL;

  strcpy(filename, uri+5);
#endif

  return filename;
}
#endif


/*
 * raptor_parser_fatal_error - Error from a parser - Internal
 **/
static void
raptor_parser_fatal_error(raptor_parser* parser, const char *message, ...)
{
  va_list arguments;

  parser->failed=1;

  va_start(arguments, message);

  if(parser->fatal_error_handler) {
    parser->fatal_error_handler(parser->fatal_error_user_data, 
                                &parser->locator, message, arguments);
    abort();
  }

  raptor_print_locator(stderr, &parser->locator);
  fprintf(stderr, " raptor fatal error - ");
  vfprintf(stderr, message, arguments);
  fputc('\n', stderr);

  va_end(arguments);

  abort();
}


/*
 * raptor_parser_error - Error from a parser - Internal
 **/
static void
raptor_parser_error(raptor_parser* parser, const char *message, ...)
{
  va_list arguments;

  va_start(arguments, message);

  if(parser->error_handler) {
    parser->error_handler(parser->error_user_data, 
                          &parser->locator, message, arguments);
    return;
  }

  raptor_print_locator(stderr, &parser->locator);
  fprintf(stderr, " raptor error - ");
  vfprintf(stderr, message, arguments);
  fputc('\n', stderr);

  va_end(arguments);
}


/*
 * raptor_parser_warning - Warning from a parser - Internal
 **/
static void
raptor_parser_warning(raptor_parser* parser, const char *message, ...)
{
  va_list arguments;

  va_start(arguments, message);

  if(parser->warning_handler) {
    parser->warning_handler(parser->warning_user_data,
                            &parser->locator, message, arguments);
    return;
  }

  raptor_print_locator(stderr, &parser->locator);
  fprintf(stderr, " raptor warning - ");
  vfprintf(stderr, message, arguments);
  fputc('\n', stderr);

  va_end(arguments);
}



/* PUBLIC FUNCTIONS */

/**
 * raptor_new - Initialise the Raptor RDF parser
 *
 * Return value: non 0 on failure
 **/
raptor_parser*
raptor_new(
#ifdef LIBRDF_INTERNAL
  librdf_world *world
#else
  void
#endif
)
{
  raptor_parser* rdf_parser;
#ifdef NEED_EXPAT
  XML_Parser xp;
#endif

  rdf_parser=(raptor_parser*)LIBRDF_CALLOC(raptor_parser, 1, sizeof(raptor_parser));

  if(!rdf_parser)
    return NULL;

  /* Initialise default feature values */
  rdf_parser->feature_scanning_for_rdf_RDF=0;
  rdf_parser->feature_allow_non_ns_attributes=1;
  rdf_parser->feature_allow_other_parseTypes=1;

#ifdef NEED_EXPAT
  xp=XML_ParserCreate(NULL);

  /* create a new parser in the specified encoding */
  XML_SetUserData(xp, rdf_parser);

  /* XML_SetEncoding(xp, "..."); */

  XML_SetElementHandler(xp, raptor_xml_start_element_handler,
                        raptor_xml_end_element_handler);
  XML_SetCharacterDataHandler(xp, raptor_xml_cdata_handler);

  XML_SetUnparsedEntityDeclHandler(xp,
                                   raptor_xml_unparsed_entity_decl_handler);

  XML_SetExternalEntityRefHandler(xp,
                                  raptor_xml_external_entity_ref_handler);


#ifdef HAVE_XML_SetNamespaceDeclHandler
  XML_SetNamespaceDeclHandler(xp,
                              raptor_start_namespace_decl_handler,
                              raptor_end_namespace_decl_handler);
#endif
  rdf_parser->xp=xp;
#endif

#ifdef NEED_LIBXML
  xmlDefaultSAXHandlerInit();
  rdf_parser->sax.startElement=raptor_xml_start_element_handler;
  rdf_parser->sax.endElement=raptor_xml_end_element_handler;

  rdf_parser->sax.characters=raptor_xml_cdata_handler;
  rdf_parser->sax.ignorableWhitespace=raptor_xml_cdata_handler;

  rdf_parser->sax.warning=raptor_xml_warning;
  rdf_parser->sax.error=raptor_xml_error;
  rdf_parser->sax.fatalError=raptor_xml_fatal_error;

  rdf_parser->sax.setDocumentLocator=raptor_xml_set_document_locator;

  /* xmlInitParserCtxt(&rdf_parser->xc); */
#endif

#ifdef LIBRDF_INTERNAL
  rdf_parser->world=world;

  rdf_parser->raptor_daml_oil_uri=librdf_new_uri(world, "http://www.daml.org/2001/03/daml+oil#");
  rdf_parser->raptor_daml_List_uri=librdf_new_uri_from_uri_local_name(rdf_parser->raptor_daml_oil_uri, "List");
  rdf_parser->raptor_daml_first_uri=librdf_new_uri_from_uri_local_name(rdf_parser->raptor_daml_oil_uri, "first");
  rdf_parser->raptor_daml_rest_uri=librdf_new_uri_from_uri_local_name(rdf_parser->raptor_daml_oil_uri, "rest");
  rdf_parser->raptor_daml_nil_uri=librdf_new_uri_from_uri_local_name(rdf_parser->raptor_daml_oil_uri, "nil");

#endif

  raptor_init_namespaces(rdf_parser);

  return rdf_parser;
}




/**
 * raptor_free - Free the Raptor RDF parser
 * @rdf_parser: parser object
 * 
 **/
void
raptor_free(raptor_parser *rdf_parser) 
{
  raptor_element* element;
  raptor_ns_map* ns;

  ns=rdf_parser->namespaces;
  while(ns) {
    raptor_ns_map* next_ns=ns->next;

    raptor_free_namespace(rdf_parser, ns);
    ns=next_ns;
  }

  while((element=raptor_element_pop(rdf_parser))) {
    raptor_free_element(element);
  }

#ifdef LIBRDF_INTERNAL
  if(rdf_parser->raptor_daml_oil_uri)
    librdf_free_uri(rdf_parser->raptor_daml_oil_uri);
  if(rdf_parser->raptor_daml_List_uri)
    librdf_free_uri(rdf_parser->raptor_daml_List_uri);
  if(rdf_parser->raptor_daml_first_uri)
    librdf_free_uri(rdf_parser->raptor_daml_first_uri);
  if(rdf_parser->raptor_daml_rest_uri)
    librdf_free_uri(rdf_parser->raptor_daml_rest_uri);
  if(rdf_parser->raptor_daml_nil_uri)
    librdf_free_uri(rdf_parser->raptor_daml_nil_uri);
#endif

  LIBRDF_FREE(raptor_parser, rdf_parser);
}


/**
 * raptor_set_fatal_error_handler - Set the parser error handling function
 * @parser: the parser
 * @user_data: user data to pass to function
 * @handler: pointer to the function
 * 
 * The function will receive callbacks when the parser fails.
 * 
 **/
void
raptor_set_fatal_error_handler(raptor_parser* parser, void *user_data,
                               raptor_message_handler handler)
{
  parser->fatal_error_user_data=user_data;
  parser->fatal_error_handler=handler;
}


/**
 * raptor_set_error_handler - Set the parser error handling function
 * @parser: the parser
 * @user_data: user data to pass to function
 * @handler: pointer to the function
 * 
 * The function will receive callbacks when the parser fails.
 * 
 **/
void
raptor_set_error_handler(raptor_parser* parser, void *user_data,
                         raptor_message_handler handler)
{
  parser->error_user_data=user_data;
  parser->error_handler=handler;
}


/**
 * raptor_set_warning_handler - Set the parser warning handling function
 * @parser: the parser
 * @user_data: user data to pass to function
 * @handler: pointer to the function
 * 
 * The function will receive callbacks when the parser gives a warning.
 * 
 **/
void
raptor_set_warning_handler(raptor_parser* parser, void *user_data,
                           raptor_message_handler handler)
{
  parser->warning_user_data=user_data;
  parser->warning_handler=handler;
}


void
raptor_set_statement_handler(raptor_parser* parser,
                          void *user_data, 
                          raptor_statement_handler handler)
{
  parser->user_data=user_data;
  parser->statement_handler=handler;
}





/**
 * raptor_parse_file - Retrieve the RDF/XML content at URI
 * @rdf_parser: parser
 * @uri: URI of RDF content
 * @base_uri: the base URI to use (or NULL if the same)
 * 
 * Return value: non 0 on failure
 **/
int
raptor_parse_file(raptor_parser* rdf_parser,  raptor_uri *uri,
                  raptor_uri *base_uri) 
{
#ifdef NEED_EXPAT
  XML_Parser xp;
#endif
#ifdef NEED_LIBXML
  /* parser context */
  xmlParserCtxtPtr xc=NULL;
#endif
#define RBS 1024
  FILE *fh;
  char buffer[RBS];
  int rc=1;
  int len;
  const char *filename;
  /* for storing error info */
  raptor_locator *locator=&rdf_parser->locator;

  /* initialise fields */
  rdf_parser->depth=0;
  rdf_parser->root_element= rdf_parser->current_element=NULL;
  rdf_parser->failed=0;

  rdf_parser->base_uri=base_uri;


#ifdef NEED_EXPAT
  xp=rdf_parser->xp;

  XML_SetBase(xp, RAPTOR_URI_AS_STRING(base_uri));
#endif


#ifdef RAPTOR_URI_AS_FILENAME
  filename=RAPTOR_URI_AS_FILENAME(uri);
#else
  filename=RAPTOR_URI_TO_FILENAME(uri);
#endif
  if(!filename)
    return 1;

  locator->file=filename;
  locator->uri=base_uri;

  fh=fopen(filename, "r");
  if(!fh) {
    raptor_parser_error(rdf_parser, "file open failed - %s", strerror(errno));
#ifdef NEED_EXPAT
    XML_ParserFree(xp);
#endif /* EXPAT */
#ifdef RAPTOR_URI_TO_FILENAME
    LIBRDF_FREE(cstring, (void*)filename);
#endif
    return 1;
  }

#ifdef NEED_LIBXML
  /* libxml needs at least 4 bytes from the XML content to allow
   * content encoding detection to work */
  len=fread(buffer, 1, 4, fh);
  if(len>0) {
    xc = xmlCreatePushParserCtxt(&rdf_parser->sax, rdf_parser,
                                 buffer, len, filename);
    if(!xc) {
      fclose(fh);
#ifdef RAPTOR_URI_TO_FILENAME
      LIBRDF_FREE(cstring, (void*)filename);
#endif
      return 1;
    }
    xc->userData = rdf_parser;
    xc->vctxt.userData = rdf_parser;
    xc->vctxt.error=raptor_xml_validation_error;
    xc->vctxt.warning=raptor_xml_validation_warning;
    
    rdf_parser->xc = xc;
    
  } else {
    fclose(fh);
    fh=NULL;
  }

#endif

  while(fh && !feof(fh)) {
    len=fread(buffer, 1, RBS, fh);
    if(len <= 0) {
#ifdef NEED_EXPAT
      XML_Parse(xp, buffer, 0, 1);
#endif
#ifdef NEED_LIBXML
      xmlParseChunk(xc, buffer, 0, 1);
#endif
      break;
    }
#ifdef NEED_EXPAT
    rc=XML_Parse(xp, buffer, len, (len < RBS));
    if(len < RBS)
      break;
    if(!rc) /* expat: 0 is failure */
      break;
#endif
#ifdef NEED_LIBXML
    rc=xmlParseChunk(xc, buffer, len, 0);
    if(len < RBS)
      break;
    if(rc) /* libxml: non 0 is failure */
      break;
#endif
  }
  fclose(fh);


#ifdef NEED_EXPAT
  if(!rc) {
    int xe=XML_GetErrorCode(xp);

    locator->line=XML_GetCurrentLineNumber(xp);
    locator->column=XML_GetCurrentColumnNumber(xp);
    locator->byte=XML_GetCurrentByteIndex(xp);

    raptor_parser_error(rdf_parser, "XML Parsing failed - %s",
                        XML_ErrorString(xe));
    rc=1;
  } else
    rc=0;

  XML_ParserFree(xp);
#endif /* EXPAT */
#ifdef NEED_LIBXML
  if(rc)
    raptor_parser_error(rdf_parser, "XML Parsing failed");

  xmlFreeParserCtxt(xc);

#endif

#ifdef RAPTOR_URI_TO_FILENAME
  LIBRDF_FREE(cstring, (void*)filename);
#endif

  return (rc != 0);
}


void
raptor_print_locator(FILE *stream, raptor_locator* locator) 
{
  if(!locator)
    return;

  if(locator->uri)
    fprintf(stream, "URI %s", RAPTOR_URI_AS_STRING(locator->uri));
  else if (locator->file)
    fprintf(stream, "file %s", locator->file);
  else
    return;
  if(locator->line) {
    fprintf(stream, ":%d", locator->line);
    if(locator->column >= 0)
      fprintf(stream, " column %d", locator->column);
  }
}



void
raptor_set_feature(raptor_parser *parser, raptor_feature feature, int value) {
  switch(feature) {
    case RAPTOR_FEATURE_SCANNING:
      parser->feature_scanning_for_rdf_RDF=value;
      break;

    case RAPTOR_FEATURE_ALLOW_NON_NS_ATTRIBUTES:
      parser->feature_allow_non_ns_attributes=value;
      break;

    case RAPTOR_FEATURE_ALLOW_OTHER_PARSETYPES:
      parser->feature_allow_other_parseTypes=value;
      break;

    default:
      break;
  }
}


static void
raptor_generate_statement(raptor_parser *rdf_parser, 
                          raptor_uri *subject_uri,
                          const char *subject_id,
                          const raptor_identifier_type subject_type,
                          const raptor_uri_source subject_uri_source,
                          raptor_uri *predicate_uri,
                          const char *predicate_id,
                          const raptor_identifier_type predicate_type,
                          const raptor_uri_source predicate_uri_source,
                          raptor_uri *object_uri,
                          const char *object_id,
                          const raptor_identifier_type object_type,
                          const raptor_uri_source object_uri_source,
                          raptor_uri *bag)
{
  raptor_statement *statement=&rdf_parser->statement;

  statement->subject=subject_uri ? (void*)subject_uri : (void*)subject_id;
  statement->subject_type=subject_type;

  statement->predicate=predicate_uri ? (void*)predicate_uri : (void*)predicate_id;
  statement->predicate_type=predicate_type;

  statement->object=object_uri ? (void*)object_uri : (void*)object_id;
  statement->object_type=object_type;

#ifdef RAPTOR_DEBUG
  fprintf(stderr, "raptor_generate_statement: Generating statement: ");
  raptor_print_statement_detailed(statement, 1, stderr);
  fputc('\n', stderr);

  if(!(subject_uri||subject_id))
    LIBRDF_FATAL1(raptor_generate_statement, "Statement has no subject\n");
  
  if(!(predicate_uri||predicate_id))
    LIBRDF_FATAL1(raptor_generate_statement, "Statement has no predicate\n");
  
  if(!(object_uri||object_id))
    LIBRDF_FATAL1(raptor_generate_statement, "Statement has no object\n");
  
#endif

  if(!rdf_parser->statement_handler)
    return;

  /* Generate the statement; or is it fact? */
  (*rdf_parser->statement_handler)(rdf_parser->user_data, statement);

  if(!bag)
    return;

  /* generate reified statements */
  statement->subject_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
  statement->predicate_type=RAPTOR_IDENTIFIER_TYPE_PREDICATE;

  statement->subject=bag;
  statement->object_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;

  statement->predicate=RAPTOR_RDF_type_URI;
  statement->object=RAPTOR_RDF_Statement_URI;
  (*rdf_parser->statement_handler)(rdf_parser->user_data, statement);

  statement->predicate=RAPTOR_RDF_subject_URI;
  statement->object=subject_uri;
  (*rdf_parser->statement_handler)(rdf_parser->user_data, statement);

  statement->predicate=RAPTOR_RDF_predicate_URI;
  statement->object=predicate_uri;
  (*rdf_parser->statement_handler)(rdf_parser->user_data, statement);

  statement->predicate=RAPTOR_RDF_object_URI;
  statement->object=object_uri;
  statement->object_type=object_type;
  (*rdf_parser->statement_handler)(rdf_parser->user_data, statement);
}



static void
raptor_print_statement_detailed(const raptor_statement * statement,
                                int detailed, FILE *stream) 
{
  if(statement->subject_type == RAPTOR_IDENTIFIER_TYPE_ANONYMOUS)
    fprintf(stream, "[%s, ", (const char*)statement->subject);
  else {
#ifdef RAPTOR_DEBUG
    if(!statement->subject)
      LIBRDF_FATAL1(raptor_print_statement_detailed, "Statement has NULL subject URI\n");
#endif
    fprintf(stream, "[%s, ",
            RAPTOR_URI_AS_STRING((raptor_uri*)statement->subject));
  }

  if(statement->predicate_type == RAPTOR_IDENTIFIER_TYPE_ORDINAL)
    fprintf(stream, "[rdf:_%d]", *((int*)statement->predicate));
  else {
#ifdef RAPTOR_DEBUG
    if(!statement->predicate)
      LIBRDF_FATAL1(raptor_print_statement_detailed, "Statement has NULL predicate URI\n");
#endif
    fputs(RAPTOR_URI_AS_STRING((raptor_uri*)statement->predicate), stream);
  }

  if(statement->object_type == RAPTOR_IDENTIFIER_TYPE_LITERAL || 
     statement->object_type == RAPTOR_IDENTIFIER_TYPE_XML_LITERAL)
    fprintf(stream, ", \"%s\"]",  (const char*)statement->object);
  else if(statement->object_type == RAPTOR_IDENTIFIER_TYPE_ANONYMOUS)
    fprintf(stream, ", %s]", (const char*)statement->object);
  else {
#ifdef RAPTOR_DEBUG
    if(!statement->object)
      LIBRDF_FATAL1(raptor_generate_statement, "Statement has NULL object URI\n");
#endif
    fprintf(stream, ", %s]", 
            RAPTOR_URI_AS_STRING((raptor_uri*)statement->object));
  }
}


void
raptor_print_statement(const raptor_statement * const statement, FILE *stream) 
{
  raptor_print_statement_detailed(statement, 0, stream);
}


void
raptor_print_statement_as_ntriples(const raptor_statement * statement,
                                   FILE *stream) 
{
  if(statement->subject_type == RAPTOR_IDENTIFIER_TYPE_ANONYMOUS)
    fprintf(stream, "_:%s", (const char*)statement->subject);
  else
    fprintf(stream, "<%s>",
            RAPTOR_URI_AS_STRING((raptor_uri*)statement->subject));
  fputc(' ', stream);

  if(statement->predicate_type == RAPTOR_IDENTIFIER_TYPE_ORDINAL)
    fprintf(stream, "<http://www.w3.org/1999/02/22-rdf-syntax-ns#_%d>",
            *((int*)statement->predicate));
  else /* must be URI */
    fprintf(stream, "<%s>",
            RAPTOR_URI_AS_STRING((raptor_uri*)statement->predicate));
  fputc(' ', stream);

  if(statement->object_type == RAPTOR_IDENTIFIER_TYPE_LITERAL || 
     statement->object_type == RAPTOR_IDENTIFIER_TYPE_XML_LITERAL)
    fprintf(stream, "\"%s\"",  (const char*)statement->object);
  else if(statement->object_type == RAPTOR_IDENTIFIER_TYPE_ANONYMOUS)
    fprintf(stream, "_:%s", (const char*)statement->object);
  else /* must be URI */
    fprintf(stream, "<%s>", 
            RAPTOR_URI_AS_STRING((raptor_uri*)statement->object));

  fputs(" .", stream);
}




static const char *
raptor_generate_id(raptor_parser *rdf_parser, const int id_for_bag)
{
  char *buffer;
  /* "genid" + min length 1 + \0 */
  int length=7;
  int id=++rdf_parser->genid;
  int tmpid=id;

  while(tmpid/=10)
    length++;
  buffer=(char*)LIBRDF_MALLOC(cstring, length);
  if(!buffer)
    return NULL;
  sprintf(buffer, "genid%d", id);

  return buffer;
}


static raptor_uri*
raptor_make_uri_from_id(raptor_parser *rdf_parser, const char *id) 
{
#ifdef LIBRDF_INTERNAL
  librdf_uri *new_uri;
  char *local_name;
  int len;
#else
  char *new_uri;
  int len;
#endif

#if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1
  LIBRDF_DEBUG2(raptor_make_uri_from_id, "Using ID %s\n", id);
#endif

#ifdef LIBRDF_INTERNAL
  /* "#id\0" */
  len=1+strlen(id)+1;
  local_name=(char*)LIBRDF_MALLOC(cstring, len);
  if(!local_name)
    return NULL;
  *local_name='#';
  strcpy(local_name+1, id);
  new_uri=librdf_new_uri(rdf_parser->world, local_name);
  LIBRDF_FREE(cstring, local_name);
  return new_uri;
#else
  len=strlen(id)+1;
  new_uri=(char*)LIBRDF_MALLOC(cstring, len);
  if(!new_uri)
    return NULL;
  strncpy(new_uri, id, len);
  return new_uri;
#endif
}


raptor_uri*
raptor_make_uri(raptor_uri *base_uri, const char *uri_string) 
{
#ifdef LIBRDF_INTERNAL
#else
  char *new_uri;
  const char *p;
  int base_uri_len=strlen(base_uri);
#endif

#if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1
  LIBRDF_DEBUG3(raptor_make_uri, 
                "Using base URI %s and URI string '%s'\n", 
                base_uri, uri_string);
#endif

#ifdef LIBRDF_INTERNAL
  return librdf_new_uri_relative_to_base(base_uri, uri_string);
#else
  /* If URI string is empty, just copy base URI */
  if(!*uri_string) {
    new_uri=(char*)LIBRDF_MALLOC(cstring, base_uri_len+1);
    if(!new_uri)
      return NULL;
    strcpy(new_uri, base_uri);
    return new_uri;
  }

  /* If URI string is a fragment #foo, append to base URI */
  if(*uri_string == '#') {
    new_uri=(char*)LIBRDF_MALLOC(cstring, base_uri_len+1+strlen(uri_string)+1);
    if(!new_uri)
      return NULL;
    strncpy(new_uri, base_uri, base_uri_len);
    strcpy(new_uri+base_uri_len, uri_string);
    return new_uri;
  }

  /* If URI string is an absolute URI, just copy it */
  for(p=uri_string;*p; p++)
    if(!isalnum(*p))
       break;
  /* If first non-alphanumeric char is a ':' then probably a absolute URI
   * Need to check URI spec - FIXME
   */
  if(*p && *p == ':') {
    new_uri=(char*)LIBRDF_MALLOC(cstring, strlen(uri_string)+1);
    if(!new_uri)
      return NULL;
    strcpy(new_uri, uri_string);
    return new_uri;
  }

  /* Otherwise is a general URI relative to base URI */

  /* FIXME do this properly */

  /* Move p to the last /, : or # char in the base URI */
  for(p=base_uri+base_uri_len-1;
      p > base_uri && *p != '/' && *p != ':' && *p != '#' ;
      p--)
    ;

  new_uri=(char*)LIBRDF_MALLOC(cstring, (p-base_uri)+1+strlen(uri_string)+1);
  if(!new_uri)
    return NULL;
  strncpy(new_uri, base_uri, p-base_uri+1);
  strcpy(new_uri+(p-base_uri)+1, uri_string);
  return new_uri;
#endif

}


raptor_uri*
raptor_copy_uri(raptor_uri *uri) 
{
#ifdef LIBRDF_INTERNAL
  return librdf_new_uri_from_uri(uri);
#else
  char *new_uri;
  new_uri=(char*)LIBRDF_MALLOC(cstring, strlen(uri)+1);
  if(!new_uri)
    return NULL;
  strcpy(new_uri, uri);
  return new_uri;
#endif
}


/**
 * raptor_new_identifier - Constructor - create a raptor_identifier
 * @type: raptor_identifier_type of identifier
 * @uri: URI of identifier (if relevant)
 * @uri_source: raptor_uri_source of URI (if relevnant)
 * @id: string for ID or genid (if relevant)
 * 
 * Constructs a new identifier copying the URI, ID fields.
 * 
 * Return value: a new raptor_identifier object or NULL on failure
 **/
raptor_identifier*
raptor_new_identifier(raptor_identifier_type type,
                      raptor_uri *uri,
                      raptor_uri_source uri_source,
                      char *id)
{
  raptor_identifier *identifier;
  raptor_uri *new_uri=NULL;
  char *new_id=NULL;

  identifier=(raptor_identifier*)LIBRDF_CALLOC(raptor_identifier, 1,
                                                sizeof(raptor_identifier));
  if(!identifier)
    return NULL;

  if(uri) {
    new_uri=raptor_copy_uri(uri);
    if(!new_uri) {
      LIBRDF_FREE(raptor_identifier, identifier);
      return NULL;
    }
  }

  if(id) {
    int len=strlen(id);
    
    new_id=LIBRDF_MALLOC(cstring, len+1);
    if(!len) {
      if(new_uri)
        LIBRDF_FREE(cstring, new_uri);
      LIBRDF_FREE(raptor_identifier, identifier);
      return NULL;
    }
    strncpy(new_id, id, len+1);
  }
  

  identifier->is_malloced=1;
  raptor_init_identifier(identifier, type, new_uri, uri_source, new_id);
  return identifier;
}


/**
 * raptor_init_identifier - Initialise a pre-allocated raptor_identifier object
 * @identifier: Existing object
 * @type: raptor_identifier_type of identifier
 * @uri: URI of identifier (if relevant)
 * @uri_source: raptor_uri_source of URI (if relevnant)
 * @id: string for ID or genid (if relevant)
 * 
 * Fills in the fields of an existing allocated raptor_identifier object.
 * DOES NOT copy any of the option arguments.
 **/
void
raptor_init_identifier(raptor_identifier *identifier,
                       raptor_identifier_type type,
                       raptor_uri *uri,
                       raptor_uri_source uri_source,
                       char *id) 
{
  identifier->is_malloced=0;
  identifier->type=type;
  identifier->uri=uri;
  identifier->uri_source=uri_source;
  identifier->id=id;
}


/**
 * raptor_copy_identifier - Copy raptor_identifiers
 * @dest: destination &raptor_identifier (previously created)
 * @src:  source &raptor_identifier
 * 
 * Return value: Non 0 on failure
 **/
int
raptor_copy_identifier(raptor_identifier *dest, raptor_identifier *src)
{
  raptor_uri *new_uri=NULL;
  char *new_id=NULL;
  
  raptor_free_identifier(dest);
  raptor_init_identifier(dest, src->type, new_uri, src->uri_source, new_id);

  if(src->uri) {
    new_uri=raptor_copy_uri(src->uri);
    if(!new_uri)
      return 0;
  }

  if(src->id) {
    int len=strlen(src->id);
    
    new_id=LIBRDF_MALLOC(cstring, len+1);
    if(!len) {
      if(new_uri)
        LIBRDF_FREE(cstring, new_uri);
      return 0;
    }
    strncpy(new_id, src->id, len+1);
  }
  dest->uri=new_uri;
  dest->id=new_id;

  dest->type=src->type;
  dest->uri_source=src->uri_source;
  dest->ordinal=src->ordinal;

  return 0;
}


/**
 * raptor_free_identifier - Destructor - destroy a raptor_identifier object
 * @identifier: &raptor_identifier object
 * 
 * Does not free an object initialised by raptor_init_identifier()
 **/
void
raptor_free_identifier(raptor_identifier *identifier) 
{
  if(identifier->uri)
    RAPTOR_FREE_URI(identifier->uri);

  if(identifier->id)
    LIBRDF_FREE(cstring, (void*)identifier->id);

  if(identifier->is_malloced)
    LIBRDF_FREE(identifier, (void*)identifier);
}



/**
 * raptor_process_property_attributes: Process the property attributes for an element for a given resource
 * @rdf_parser: Raptor parser object
 * @attributes_element: element with the property attributes 
 * @resource_element: element that defines the resource URI 
 *                    subject_uri, subject_uri_source etc.
 * 
 **/
static void 
raptor_process_property_attributes(raptor_parser *rdf_parser, 
                                   raptor_element *attributes_element,
                                   raptor_element *resource_element)
{
  unsigned int i;

  /* Process attributes as propAttr* = * (propName="string")*
   */
  for(i=0; i<attributes_element->attribute_count; i++) {
    raptor_ns_name* attribute=attributes_element->attributes[i];
    const char *name=attribute->local_name;
    const char *value = attribute->value;
    
    /* Generate the property statement using one of these properties:
     * 1) rdf:li -> rdf:_n
     * 2) rdf:_n
     * 3) the URI from the attribute
     */
    if(attribute->nspace && attribute->nspace->is_rdf_ms) {
      /* is rdf: namespace */
      int ordinal=0;
        
      if(IS_RDF_MS_CONCEPT(attribute->local_name, attribute->uri, li)) {
        /* recognise rdf:li attribute */
        ordinal=++resource_element->last_ordinal;
      } else if(*name == '_') {
        /* recognise rdf:_ */
        name++;
        while(*name >= '0' && *name <= '9') {
          ordinal *= 10;
          ordinal += (*name++ - '0');
        }
        
        if(ordinal < 1) {
          raptor_parser_warning(rdf_parser, "Illegal ordinal value %d in attribute %s seen on container element %s.", ordinal, attribute->local_name, name);
        }
      }

      if(ordinal >= 1)
        /* Generate an ordinal property when there are no problems */
        raptor_generate_statement(rdf_parser, 
                                  resource_element->subject.uri,
                                  resource_element->subject.id,
                                  resource_element->subject.type,
                                  resource_element->subject.uri_source,
                                  
                                  (raptor_uri*)&ordinal,
                                  NULL,
                                  RAPTOR_IDENTIFIER_TYPE_ORDINAL,
                                  RAPTOR_URI_SOURCE_NOT_URI,
                                  
                                  (raptor_uri*)value,
                                  NULL,
                                  RAPTOR_IDENTIFIER_TYPE_LITERAL,
                                  RAPTOR_URI_SOURCE_NOT_URI,
                                  
                                  NULL);
    } else
      /* else not rdf: namespace - generate statement */
      raptor_generate_statement(rdf_parser, 
                                resource_element->subject.uri,
                                resource_element->subject.id,
                                resource_element->subject.type,
                                resource_element->subject.uri_source,

                                attribute->uri,
                                NULL,
                                RAPTOR_IDENTIFIER_TYPE_PREDICATE,
                                RAPTOR_URI_SOURCE_ATTRIBUTE,

                                (raptor_uri*)value,
                                NULL,
                                RAPTOR_IDENTIFIER_TYPE_LITERAL,
                                RAPTOR_URI_SOURCE_NOT_URI,

                                resource_element->bag.uri);

  } /* end for ... attributes */


  /* Handle rdf property attributes
   * (only rdf:type and rdf:value at present) 
   */
  for(i=0; i<= RDF_ATTR_LAST; i++) {
    const char *value=attributes_element->rdf_attr[i];
    int object_is_literal=(rdf_attr_info[i].type == RAPTOR_IDENTIFIER_TYPE_LITERAL);
    raptor_uri *property_uri, *object_uri;
    raptor_identifier_type object_type;
    
    if(!value || rdf_attr_info[i].type == RAPTOR_IDENTIFIER_TYPE_UNKNOWN)
      continue;

#ifdef LIBRDF_INTERNAL
    librdf_get_concept_by_name(rdf_parser->world, 1, rdf_attr_info[i].name,
                               &property_uri, NULL);
#else    
    property_uri=raptor_make_uri(RAPTOR_RDF_MS_URI, rdf_attr_info[i].name);
#endif
    
    object_uri=object_is_literal ? (raptor_uri*)value : raptor_make_uri(rdf_parser->base_uri, value);
    object_type=object_is_literal ? RAPTOR_IDENTIFIER_TYPE_LITERAL : RAPTOR_IDENTIFIER_TYPE_RESOURCE;
    
    raptor_generate_statement(rdf_parser, 
                              resource_element->subject.uri,
                              resource_element->subject.id,
                              resource_element->subject.type,
                              resource_element->subject.uri_source,
                              
                              property_uri,
                              NULL,
                              RAPTOR_IDENTIFIER_TYPE_PREDICATE,
                              RAPTOR_URI_SOURCE_ATTRIBUTE,
                              
                              object_uri,
                              NULL,
                              object_type,
                              RAPTOR_URI_SOURCE_NOT_URI,
                              
                              resource_element->bag.uri);
    if(!object_is_literal)
      RAPTOR_FREE_URI(object_uri);
#ifdef LIBRDF_INTERNAL
    /* noop */
#else    
    RAPTOR_FREE_URI(property_uri);
#endif
    
  } /* end for rdf:property values */

}


static void
raptor_start_element_grammar(raptor_parser *rdf_parser,
                             raptor_element *element) 
{
  int finished;
  raptor_state state;
  const char *el_name=element->name->local_name;
  int element_in_rdf_ns=(element->name->nspace && 
                         element->name->nspace->is_rdf_ms);


  state=element->state;
  LIBRDF_DEBUG3(raptor_start_element_grammar, "starting in state %d - %s\n",
                state, raptor_state_as_string(state));

  finished= 0;
  while(!finished) {
    switch(state) {
      case RAPTOR_STATE_SKIPPING:
        element->child_state=state;
        finished=1;
        break;
        
      case RAPTOR_STATE_UNKNOWN:
        /* found <rdf:RDF> ? */
        if(element_in_rdf_ns && 
          IS_RDF_MS_CONCEPT(el_name, element->name->uri, RDF)) {
          element->child_state=RAPTOR_STATE_OBJ;
          /* Yes - need more content before can continue,
           * so wait for another element
           */
          finished=1;
          break;
        }

        /* If scanning for element, can continue */
        if(rdf_parser->feature_scanning_for_rdf_RDF) {
          finished=1;
          break;
        }

        /* Otherwise the choice of the next state can be made
         * from the current element by the IN_RDF state
         */
        state=RAPTOR_STATE_OBJ;
        break;


      case RAPTOR_STATE_OBJ:
      case RAPTOR_STATE_INLINEITEM:
        /* Handling either 6.2 (obj) or 6.17 (value) (inside
         * rdf:RDF, propertyElt or rdf:li) and expecting
         * description (6.3) | sequence (6.25) | bag (6.26) | alternative (6.27)
         * [|other container (not M&S)]
         *
         * CHOICES:
         *   <rdf:Description> (goto 6.3)
         *   <rdf:Seq> (goto 6.25) (or typedNode CHOICE)
         *   <rdf:Bag> (goto 6.26) (or typedNode CHOICE)
         *   <rdf:Alt> (goto 6.27) (or typedNode CHOICE)
         * OR from 6.3 can have ANY other element matching
         * typedNode (6.13) - goto 6.3
         */

        if(element_in_rdf_ns) {
          if(IS_RDF_MS_CONCEPT(el_name, element->name->uri, Description)) {
            state=RAPTOR_STATE_DESCRIPTION;
            break;
          } 

          if(IS_RDF_MS_CONCEPT(el_name, element->name->uri, Seq)) {
            rdf_parser->typed_node_block_type=RAPTOR_TYPED_NODE_BLOCK_TYPE_SEQ;
            state=RAPTOR_STATE_TYPED_NODE;
            break;
          }

          if(IS_RDF_MS_CONCEPT(el_name, element->name->uri, Bag)) {
            rdf_parser->typed_node_block_type=RAPTOR_TYPED_NODE_BLOCK_TYPE_BAG;
            state=RAPTOR_STATE_TYPED_NODE;
            break;
          }

          if(IS_RDF_MS_CONCEPT(el_name, element->name->uri, Alt)) {
            rdf_parser->typed_node_block_type=RAPTOR_TYPED_NODE_BLOCK_TYPE_ALT;
            state=RAPTOR_STATE_TYPED_NODE;
            break;
          }

          if(rdf_parser->container_test_handler) {
            raptor_uri *container_type=rdf_parser->container_test_handler(element->name->uri);
            if(container_type) {
              rdf_parser->typed_node_block_type=RAPTOR_TYPED_NODE_BLOCK_TYPE_CONTAINER;
              state=RAPTOR_STATE_TYPED_NODE;
              break;
            }
          }

          /* Other rdf: elements at this layer are taken as matching
           * the typedNode production - just fallthrough
           */
        }

        /* Default to typedNode */
        state=RAPTOR_STATE_TYPED_NODE;
        break;


      /* No need for 6.2 - already chose 6.3, 6.25, 6.26 or 6.27 */


      case RAPTOR_STATE_DESCRIPTION:
      case RAPTOR_STATE_TYPED_NODE:
      case RAPTOR_STATE_PARSETYPE_RESOURCE:
      case RAPTOR_STATE_PARSETYPE_DAML_COLLECTION:
        /* Handling 6.3 (description), 6.13 (typedNode) or contents
         * of a property (propertyElt or member) with parseType="resource"
         *
         * 6.3 (description):
         * <rdf:Description idAboutAttr? bagIdAttr? propAttr* >
         *   Attributes: (ID|about|aboutEach|aboutEachPrefix)? bagID? propAttr*
         * (may have no content, that is tested in the end element code)
         *
         * 6.13 (typedNode):
         * haved fallen through and not chosen other productions -
         * 6.3, 6.25, 6.26, 6.27.
         * and handling rdf:Seq, rdf:Bag, rdf:Alt or other container
         * and this can be determined from the value of 
         * rdf_parser->typed_node_block_type
         *
         * Expect here from production 6.13 (typedNode)
         * <typeName idAboutAttr? bagIdAttr? propAttr* />
         *   Attributes: (ID|about|aboutEach|aboutEachPrefix)? bagID? propAttr*
         *
         * CHOICE (description): Create a bag here (always? even if
         * no bagId given?) FIXME - not decided / implemented yet.
         */


        /* lets add booleans - isn't C wonderful! */
        if((element->rdf_attr[RDF_ATTR_ID] != NULL) +
           (element->rdf_attr[RDF_ATTR_about] != NULL) +
           (element->rdf_attr[RDF_ATTR_aboutEach] != NULL) +
           (element->rdf_attr[RDF_ATTR_aboutEachPrefix] != NULL) > 1) {
          raptor_parser_warning(rdf_parser, "More than one of RDF ID, about, aboutEach or aboutEachPrefix attributes on element %s - from productions 6.5, 6.6, 6.7 and 6.8 expect at most one.", el_name);
        }

        if(element->rdf_attr[RDF_ATTR_ID]) {
          element->subject.id=(char*)element->rdf_attr[RDF_ATTR_ID];
          element->rdf_attr[RDF_ATTR_ID]=NULL;
          element->subject.uri=raptor_make_uri_from_id(rdf_parser, element->subject.id);
          element->subject.type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
          element->subject.uri_source=RAPTOR_URI_SOURCE_ID;
        } else if (element->rdf_attr[RDF_ATTR_about]) {
          element->subject.uri=raptor_make_uri(rdf_parser->base_uri, element->rdf_attr[RDF_ATTR_about]);
          element->subject.type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
          element->subject.uri_source=RAPTOR_URI_SOURCE_URI;
        } else if (element->rdf_attr[RDF_ATTR_aboutEach]) {
          element->subject.uri=raptor_make_uri(rdf_parser->base_uri, element->rdf_attr[RDF_ATTR_aboutEach]);
          element->subject.type=RAPTOR_IDENTIFIER_TYPE_RESOURCE_EACH;
          element->subject.uri_source=RAPTOR_URI_SOURCE_URI;
        } else if (element->rdf_attr[RDF_ATTR_aboutEachPrefix]) {
          element->subject.uri=raptor_make_uri(rdf_parser->base_uri, element->rdf_attr[RDF_ATTR_aboutEachPrefix]);
          element->subject.type=RAPTOR_IDENTIFIER_TYPE_RESOURCE_EACH_PREFIX;
          element->subject.uri_source=RAPTOR_URI_SOURCE_URI;
        } else if (element->parent && 
                   (element->parent->object.uri || element->parent->object.id)) {
          /* copy from parent, it has a URI for us */
          raptor_copy_identifier(&element->subject, &element->parent->object);
        } else {
          element->subject.id=raptor_generate_id(rdf_parser, 0);
          element->subject.type=RAPTOR_IDENTIFIER_TYPE_ANONYMOUS;
          element->subject.uri_source=RAPTOR_URI_SOURCE_GENERATED;
        }


        if(element->rdf_attr[RDF_ATTR_bagID]) {
          element->bag.id=(char*)element->rdf_attr[RDF_ATTR_bagID];
          element->rdf_attr[RDF_ATTR_bagID]=NULL;
          element->bag.uri=raptor_make_uri_from_id(rdf_parser,  element->bag.id);
          element->bag.type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
          element->bag.uri_source=RAPTOR_URI_SOURCE_GENERATED;
        }


        if(element->parent) {

          /* In a rdf:parseType daml:collection the resources are appended
           * to the list at element->parent->tail_uri 
           */
          if (state == RAPTOR_STATE_PARSETYPE_DAML_COLLECTION) {
            const char * idList = raptor_generate_id(rdf_parser, 0);
            raptor_uri* uriList = raptor_make_uri_from_id(rdf_parser, idList);
            LIBRDF_FREE(cstring, (char*)idList);
            
            /* <uriList> rdf:type daml:List */
            raptor_generate_statement(rdf_parser, 
                                      uriList,
                                      NULL,
                                      RAPTOR_IDENTIFIER_TYPE_RESOURCE,
                                      RAPTOR_URI_SOURCE_URI,

                                      RAPTOR_RDF_type_URI,
                                      NULL,
                                      RAPTOR_IDENTIFIER_TYPE_PREDICATE,
                                      RAPTOR_URI_SOURCE_URI,

                                      RAPTOR_DAML_LIST_URI(rdf_parser),
                                      NULL,
                                      RAPTOR_IDENTIFIER_TYPE_RESOURCE,
                                      RAPTOR_URI_SOURCE_URI,
                                      NULL);

            /* <uriList> daml:first <element->uri> */
            raptor_generate_statement(rdf_parser, 
                                      uriList,
                                      NULL,
                                      RAPTOR_IDENTIFIER_TYPE_RESOURCE,
                                      RAPTOR_URI_SOURCE_URI,

                                      RAPTOR_DAML_FIRST_URI(rdf_parser),
                                      NULL,
                                      RAPTOR_IDENTIFIER_TYPE_PREDICATE,
                                      RAPTOR_URI_SOURCE_URI,

                                      element->subject.uri,
                                      element->subject.id,
                                      element->subject.type,
                                      RAPTOR_URI_SOURCE_URI,
                                      NULL);
            
            /* If there is no daml:collection */
            if (!element->parent->tail_uri) {
              /* Free any existing object URI still around
               * I suspect this can never happen.
               */
              if(element->parent->object.uri) {
                abort();
                RAPTOR_FREE_URI(element->parent->object.uri);
              }
              
              element->parent->object.uri=raptor_copy_uri(uriList);
              element->parent->object.type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
              element->parent->object.uri_source=RAPTOR_URI_SOURCE_URI;
            } else {
              /* <tail_uri> daml:rest <uriListRest> */
              raptor_generate_statement(rdf_parser, 
                                        element->parent->tail_uri,
                                        NULL,
                                        RAPTOR_IDENTIFIER_TYPE_RESOURCE,
                                        RAPTOR_URI_SOURCE_URI,

                                        RAPTOR_DAML_REST_URI(rdf_parser),
                                        NULL,
                                        RAPTOR_IDENTIFIER_TYPE_PREDICATE,
                                        RAPTOR_URI_SOURCE_URI,

                                        uriList,
                                        NULL,
                                        RAPTOR_IDENTIFIER_TYPE_RESOURCE,
                                        RAPTOR_URI_SOURCE_URI,

                                        NULL);
            }

            /* update new tail */
            if(element->parent->tail_uri)
              RAPTOR_FREE_URI(element->parent->tail_uri);

            element->parent->tail_uri=uriList;
            
          } else if(!element->parent->object.uri &&
                    element->parent->state != RAPTOR_STATE_UNKNOWN) {
            /* If there is a parent element (property) containing this
             * element (node) and it has no object, set it from this subject
             */
            
            /* Store URI of this node in our parent as the property object */
            raptor_copy_identifier(&element->parent->object, &element->subject);
            element->parent->content_type = RAPTOR_ELEMENT_CONTENT_TYPE_RESOURCE;
          }
        }
        

        /* If this is a typed node, generate the rdf:type statement
         * from this node
         */
        if(state == RAPTOR_STATE_TYPED_NODE)
          raptor_generate_statement(rdf_parser, 
                                    element->subject.uri,
                                    element->subject.id,
                                    element->subject.type,
                                    element->subject.uri_source,

                                    RAPTOR_RDF_type_URI,
                                    NULL,
                                    RAPTOR_IDENTIFIER_TYPE_PREDICATE,
                                    RAPTOR_URI_SOURCE_URI,

                                    element->name->uri,  /* FIXME: element->container_type, ??? */
                                    NULL,
                                    RAPTOR_IDENTIFIER_TYPE_RESOURCE,
                                    element->object.uri_source,

                                    element->bag.uri);

        raptor_process_property_attributes(rdf_parser, element, element);

        /* for both productions now need some more content or
         * property elements before can do any more work.
         */

        element->child_state=RAPTOR_STATE_PROPERTYELT;
        element->content_type=RAPTOR_ELEMENT_CONTENT_TYPE_PROPERTIES;
        finished=1;
        break;


        /* Expect to meet the production member - 
         * referencedItem (6.29) | inlineItem (6.30) =
         *   '<rdf:li' resourceAttr '/>'  | 
         *   '<rdf:li' '>' value </rdf:li>' |
         *   '<rdf:li' parseLiteral '>' literal </rdf:li>' |
         *   '<rdf:li' parseResource '>' propertyElt* </rdf:li>' 
         * 
         * Taking just the start element work:
         *   1) <rdf:li rdf:resource="..."/>
         *   2) <rdf:li>
         *   3) <rdf:li rdf:parseType="literal">
         *   4) <rdf:li rdf:parseType="resource">
         *
         * 1) moves to RAPTOR_STATE_REFERENCEDITEM
         * 2) moves to RAPTOR_STATE_INLINEITEM to handle contents of rdf:li
         * 3) moves to RAPTOR_STATE_PARSETYPE_LITERAL
         * 4) moves to RAPTOR_STATE_PARSETYPE_RESOURCE
         * Other parseType content moves to RAPTOR_STATE_PARSETYPE_OTHER
         */
      case RAPTOR_STATE_MEMBER:
        if(element->rdf_attr[RDF_ATTR_resource]) {
          element->subject.uri=raptor_make_uri(rdf_parser->base_uri,
                                               element->rdf_attr[RDF_ATTR_resource]);
          element->subject.type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
          element->subject.uri_source=RAPTOR_URI_SOURCE_URI,
          state=RAPTOR_STATE_REFERENCEDITEM;
        } else if (element->rdf_attr[RDF_ATTR_parseType]) {
          const char *parse_type=element->rdf_attr[RDF_ATTR_parseType];
          
          if(!strcasecmp(parse_type, "literal")) {
            element->child_state=RAPTOR_STATE_PARSETYPE_LITERAL;
          } else if (!strcasecmp(parse_type, "resource")) {
            state=RAPTOR_STATE_PARSETYPE_RESOURCE;
            element->child_state=RAPTOR_STATE_PROPERTYELT;
            element->subject.id=raptor_generate_id(rdf_parser, 0);
            element->subject.type=RAPTOR_IDENTIFIER_TYPE_ANONYMOUS;
            element->subject.uri_source=RAPTOR_URI_SOURCE_GENERATED;
          } else {
            if(rdf_parser->feature_allow_other_parseTypes)
              element->child_state=RAPTOR_STATE_PARSETYPE_OTHER;
            else
              element->child_state=RAPTOR_STATE_PARSETYPE_LITERAL;
          }
        } else
          element->child_state=RAPTOR_STATE_INLINEITEM;

        finished=1;
        break;

      case RAPTOR_STATE_REFERENCEDITEM:
        /* FIXME - need to generate warning for unexpected content */
        finished=1;
        break;

      case RAPTOR_STATE_PARSETYPE_OTHER:
        /* FIXME */

        /* FALLTHROUGH */

      case RAPTOR_STATE_PARSETYPE_LITERAL:
        {
          char *fmt_buffer;
          int fmt_length;
          fmt_buffer=raptor_format_element(element, &fmt_length, 0);
          if(fmt_buffer && fmt_length) {
            /* Append cdata content content */
            if(element->content_cdata) {
              /* Append */
              char *new_cdata=(char*)LIBRDF_MALLOC(cstring, element->content_cdata_length + fmt_length + 1); /* DAML merge used 10 - why? */
              if(new_cdata) {
                strncpy(new_cdata, element->content_cdata,
                        element->content_cdata_length);
                strcpy(new_cdata+element->content_cdata_length, fmt_buffer);
                LIBRDF_FREE(cstring, element->content_cdata);
                element->content_cdata=new_cdata;
              }
              LIBRDF_FREE(cstring, fmt_buffer);
            } else {
              /* Copy - is empty */
              element->content_cdata       =fmt_buffer;
              element->content_cdata_length=fmt_length;
            }
          }

          element->child_state = RAPTOR_STATE_PARSETYPE_LITERAL;
        }
        
        finished=1;
        break;

        /* choices here from production 6.12 (propertyElt)
         *   <propName idAttr?> value </propName>
         *     Attributes: ID?
         *   <propName idAttr? parseLiteral> literal </propName>
         *     Attributes: ID? parseType="literal"
         *   <propName idAttr? parseResource> propertyElt* </propName>
         *     Attributes: ID? parseType="resource"
         *   <propName idRefAttr? bagIdAttr? propAttr* />
         *     Attributes: (ID|resource)? bagIdAttr? propAttr*
         *
         * The only one that must be handled here is the last one which
         * uses only attributes and no content.
         */
      case RAPTOR_STATE_PROPERTYELT:

        /* Handle rdf:li as a property in member state above */ 
        if(element_in_rdf_ns && 
           IS_RDF_MS_CONCEPT(el_name, element->name->uri, li)) {
          state=RAPTOR_STATE_MEMBER;
          break;
        }


        /* M&S says: "The value of the ID attribute, if specified, is
         * the identifier for the resource that represents the
         * reification of the statement." */

        /* Later on it implies something different
         * FIXME 1 - reference to this
         * FIXME 2 - pick one of these interpretations
         */

        element->bag.id=element->rdf_attr[RDF_ATTR_ID];
        element->rdf_attr[RDF_ATTR_ID]=NULL;

        if (element->rdf_attr[RDF_ATTR_parseType]) {
          const char *parse_type=element->rdf_attr[RDF_ATTR_parseType];

          if(!strcasecmp(parse_type, "literal")) {
            element->child_state=RAPTOR_STATE_PARSETYPE_LITERAL;
          } else if (!strcasecmp(parse_type, "resource")) {
            state=RAPTOR_STATE_PARSETYPE_RESOURCE;
            element->child_state=RAPTOR_STATE_PROPERTYELT;
            element->subject.id=raptor_generate_id(rdf_parser, 0);
            element->subject.type=RAPTOR_IDENTIFIER_TYPE_ANONYMOUS;
            element->subject.uri_source=RAPTOR_URI_SOURCE_GENERATED;
          } else {
            if(rdf_parser->feature_allow_other_parseTypes) {
              if(!strcasecmp(parse_type, "daml:collection")) {
                element->child_state=RAPTOR_STATE_PARSETYPE_DAML_COLLECTION;
              } else {
                element->child_state=RAPTOR_STATE_PARSETYPE_OTHER;
              }
            } else {
              element->child_state=RAPTOR_STATE_PARSETYPE_LITERAL;
            }
          }
        } else {

          /* Can only be last case */
          if(element->rdf_attr[RDF_ATTR_resource]) {
            element->subject.uri=raptor_make_uri(rdf_parser->base_uri,
                                                 element->rdf_attr[RDF_ATTR_resource]);
            element->subject.uri_source=RAPTOR_URI_SOURCE_URI;
            element->subject.type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
          }
          

          /* FIXME - what should be done here */
#if 0
          if(element->rdf_attr[RDF_ATTR_bagID]) {
            element->bag.id=element->rdf_attr[RDF_ATTR_bagID];
            element->rdf_attr[RDF_ATTR_bagID]=NULL;
            element->bag.uri=raptor_make_uri_from_id(rdf_parser,  element->bag.id);
            element->bag.type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
            element->bag.uri_source=RAPTOR_URI_SOURCE_GENERATED;
          }
#endif
          
          /* Assign the properties on this element to the resource
           * URI held in our parent element
           */
          raptor_process_property_attributes(rdf_parser, element, 
                                             element->parent);

          /*
           * Assign bag URI here so we don't reify property attributes using 
           * this bagID 
           */
          if(element->bag.id) {
            element->bag.uri=raptor_make_uri_from_id(rdf_parser,  element->bag.id);
            element->bag.type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
            element->bag.uri_source=RAPTOR_URI_SOURCE_GENERATED;
          }

          if(element->rdf_attr[RDF_ATTR_resource]) {
            /* Done - wait for end of this element to end in order to 
             * check the element was empty as expected */
            element->content_type = RAPTOR_ELEMENT_CONTENT_TYPE_EMPTY;
          } else {
            /* Otherwise process content in obj (value) state */
            element->child_state=RAPTOR_STATE_OBJ;
            element->content_type = RAPTOR_ELEMENT_CONTENT_TYPE_UNKNOWN;
          }
        }

        finished=1;

        break;


      default:
        raptor_parser_fatal_error(rdf_parser, "raptor_start_element_grammar - unexpected parser state %d - %s\n", state, raptor_state_as_string(state));
        finished=1;

    } /* end switch */

    if(state != element->state) {
      element->state=state;
      LIBRDF_DEBUG3(raptor_start_element_grammar, 
                    "moved to state %d - %s\n",
                    state, raptor_state_as_string(state));
    }

  } /* end while */

  LIBRDF_DEBUG3(raptor_start_element_grammar, 
                "ending in state %d - %s\n",
                state, raptor_state_as_string(state));
}


static void
raptor_end_element_grammar(raptor_parser *rdf_parser,
                           raptor_element *element) 
{
  raptor_state state;
  int finished;
  const char *el_name=element->name->local_name;
  int element_in_rdf_ns=(element->name->nspace && 
                         element->name->nspace->is_rdf_ms);


  state=element->state;
  LIBRDF_DEBUG3(raptor_end_element_grammar, "starting in state %d - %s\n",
                state, raptor_state_as_string(state));

  finished= 0;
  while(!finished) {
    switch(state) {
      case RAPTOR_STATE_SKIPPING:
        finished=1;
        break;

      case RAPTOR_STATE_UNKNOWN:
        finished=1;
        break;

      case RAPTOR_STATE_OBJ:
        if(element_in_rdf_ns && 
          IS_RDF_MS_CONCEPT(el_name, element->name->uri,RDF)) {
          /* end of RDF - boo hoo */
          state=RAPTOR_STATE_UNKNOWN;
          finished=1;
          break;
        }
        /* When scanning, another element ending is outside the RDF
         * world so this can happen without further work
         */
        if(rdf_parser->feature_scanning_for_rdf_RDF) {
          state=RAPTOR_STATE_UNKNOWN;
          finished=1;
          break;
        }
        /* otherwise found some junk after RDF content in an RDF-only 
         * document (probably never get here since this would be
         * a mismatched XML tag and cause an error earlier)
         */
        raptor_parser_warning(rdf_parser, "Element %s ended, expected end of RDF element\n", el_name);
        state=RAPTOR_STATE_UNKNOWN;
        finished=1;
        break;

      /* No need for 6.2 - already chose 6.3, 6.25, 6.26 or 6.27 */

      case RAPTOR_STATE_DESCRIPTION:
      case RAPTOR_STATE_TYPED_NODE:
      case RAPTOR_STATE_PARSETYPE_RESOURCE:

        /* If there is a parent element containing this element and
         * the parent isn't a description, has an identifier,
         * create the statement between this node using parent property
         * (Need to check for identifier so that top-level typed nodes
         * don't get connect to <rdf:RDF> parent element)
         */
        if(state != RAPTOR_STATE_DESCRIPTION && 
           element->parent &&
           (element->parent->subject.uri || element->parent->subject.id))
          raptor_generate_statement(rdf_parser, 
                                    element->parent->subject.uri,
                                    element->parent->subject.id,
                                    element->parent->subject.type,
                                    element->parent->subject.uri_source,

                                    element->parent->name->uri,
                                    NULL,
                                    RAPTOR_IDENTIFIER_TYPE_PREDICATE,
                                    RAPTOR_URI_SOURCE_ELEMENT,

                                    element->subject.uri,
                                    element->subject.id,
                                    element->subject.type,
                                    element->subject.uri_source,

                                    NULL);

        finished=1;
        break;

      case RAPTOR_STATE_PARSETYPE_DAML_COLLECTION:

        finished=1;
        break;

      case RAPTOR_STATE_REFERENCEDITEM:
        if((element->content_element_seen + element->content_cdata_seen))
          raptor_parser_warning(rdf_parser, "Unexpected content in %s with resource attribute - from production 6.29 expected an empty element.", el_name);

        element->parent->last_ordinal++;
        raptor_generate_statement(rdf_parser, 
                                  element->parent->subject.uri,
                                  element->parent->subject.id,
                                  element->parent->subject.type,
                                  element->parent->subject.uri_source,

                                  (raptor_uri*)&element->parent->last_ordinal,
                                  NULL,
                                  RAPTOR_IDENTIFIER_TYPE_ORDINAL,
                                  RAPTOR_URI_SOURCE_NOT_URI,
                                  
                                  element->subject.uri,
                                  element->subject.id,
                                  element->subject.type,
                                  element->subject.uri_source,

                                  NULL);

        finished=1;
        break;

      case RAPTOR_STATE_INLINEITEM:
        element->parent->last_ordinal++;
        raptor_generate_statement(rdf_parser, 
                                  element->parent->subject.uri,
                                  element->parent->subject.id,
                                  element->parent->subject.type,
                                  element->parent->subject.uri_source,

                                  (raptor_uri*)&element->parent->last_ordinal,
                                  NULL,
                                  RAPTOR_IDENTIFIER_TYPE_ORDINAL,
                                  RAPTOR_URI_SOURCE_NOT_URI,

                                  (raptor_uri*)element->content_cdata,
                                  NULL,
                                  RAPTOR_IDENTIFIER_TYPE_LITERAL,
                                  RAPTOR_URI_SOURCE_NOT_URI,

                                  NULL);
        finished=1;
        break;


      case RAPTOR_STATE_PARSETYPE_OTHER:
        /* FIXME */

        /* FALLTHROUGH */

      case RAPTOR_STATE_PARSETYPE_LITERAL:
        element->parent->content_type=RAPTOR_ELEMENT_CONTENT_TYPE_PRESERVED;

        /* Append closing element */
        {
          char *fmt_buffer;
          int fmt_length;
          fmt_buffer=raptor_format_element(element, &fmt_length,1);
          if(fmt_buffer && fmt_length) {
            /* Append cdata content content */
            char *new_cdata=(char*)LIBRDF_MALLOC(cstring, element->content_cdata_length + fmt_length + 1);
            if(new_cdata) {
              strncpy(new_cdata, element->content_cdata,
                      element->content_cdata_length); /* DAML merge had +1 - why? */
              strcpy(new_cdata+element->content_cdata_length, fmt_buffer);
              LIBRDF_FREE(cstring, element->content_cdata);
              element->content_cdata=new_cdata;
              element->content_cdata_length += fmt_length;
            }
            LIBRDF_FREE(cstring, fmt_buffer);
          }
        }

        /* Append this cdata content to parent element cdata content */
        if(element->parent->content_cdata) {
          /* Append */
          char *new_cdata=(char*)LIBRDF_MALLOC(cstring, element->parent->content_cdata_length + element->content_cdata_length + 1);
          if(new_cdata) {
            strncpy(new_cdata, element->parent->content_cdata,
                    element->parent->content_cdata_length);
            strncpy(new_cdata+element->parent->content_cdata_length, 
                    element->content_cdata, element->content_cdata_length+1);
            LIBRDF_FREE(cstring, element->parent->content_cdata);
            element->parent->content_cdata=new_cdata;
            element->parent->content_cdata_length += element->content_cdata_length;
            /* Done with our cdata - free it before pointer is zapped */
            LIBRDF_FREE(cstring, element->content_cdata);
          }
        } else {
          /* Copy - parent is empty */
          element->parent->content_cdata       =element->content_cdata;
          element->parent->content_cdata_length=element->content_cdata_length+1;
        }

        element->content_cdata=NULL;
        element->content_cdata_length=0;

        finished=1;
        break;


      case RAPTOR_STATE_PROPERTYELT:
      case RAPTOR_STATE_MEMBER:
        /* This is used inside these: productions
         *   6.12 (propertyElt) part 1 (element OR literal content),
         *                      part 4 (expect no content),
         *   6.30 (inlineItem) part 1 (element OR literal content)
         *
         * and similar parts of 6.28, 6.29 and 6.30 for rdf:li property
         *
         * The literal content part is handled here.
         * The element content is handled in the internal states
         * Empty content is checked here.
         */

        if(element->content_type == RAPTOR_ELEMENT_CONTENT_TYPE_UNKNOWN) {
          if(element->content_cdata_seen) 
            element->content_type= RAPTOR_ELEMENT_CONTENT_TYPE_LITERAL;
          else if (element->content_element_seen) 
            element->content_type= RAPTOR_ELEMENT_CONTENT_TYPE_PROPERTIES;
          else
            element->content_type= RAPTOR_ELEMENT_CONTENT_TYPE_EMPTY;
        }

        if(element->content_type == RAPTOR_ELEMENT_CONTENT_TYPE_EMPTY) {
          if(element->rdf_attr[RDF_ATTR_resource]) {
            element->object.uri=raptor_make_uri(rdf_parser->base_uri,
                                                 element->rdf_attr[RDF_ATTR_resource]);
            element->object.type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
            element->object.uri_source=RAPTOR_URI_SOURCE_URI;
            element->content_type = RAPTOR_ELEMENT_CONTENT_TYPE_RESOURCE;
          } else {
            element->object.id=raptor_generate_id(rdf_parser, 0);
            element->object.type=RAPTOR_IDENTIFIER_TYPE_ANONYMOUS;
            element->object.uri_source=RAPTOR_URI_SOURCE_GENERATED;
            element->content_type = RAPTOR_ELEMENT_CONTENT_TYPE_RESOURCE;
          }
        }


        /* If there is a parent resource node (Description,
         * typedNode or parseTypeResource)
         */
        if(element->parent->state == RAPTOR_STATE_DESCRIPTION ||
           element->parent->state == RAPTOR_STATE_TYPED_NODE ||
           element->parent->state == RAPTOR_STATE_PARSETYPE_RESOURCE) {
          int object_is_literal=(element->content_cdata != NULL); /* FIXME */
          raptor_identifier_type object_type=object_is_literal ? RAPTOR_IDENTIFIER_TYPE_LITERAL : element->object.type;
          raptor_uri *object_uri=object_is_literal ? (raptor_uri*)element->content_cdata : element->object.uri;

          /* then generate the statement:
           *   subject  : parent
           *   predicate: ordinal / this property
           *   object   : child object (node)
           */
          if(state == RAPTOR_STATE_MEMBER) {
            element->parent->last_ordinal++;
            raptor_generate_statement(rdf_parser, 
                                      element->parent->subject.uri,
                                      element->parent->subject.id,
                                      element->parent->subject.type,
                                      RAPTOR_URI_SOURCE_ELEMENT,

                                      (raptor_uri*)&element->parent->last_ordinal,
                                      NULL,
                                      RAPTOR_IDENTIFIER_TYPE_ORDINAL,
                                      RAPTOR_URI_SOURCE_NOT_URI,
                                      
                                      object_uri,
                                      element->object.id,
                                      object_type,
                                      element->object.uri_source,

                                      element->bag.uri);
          } else
            raptor_generate_statement(rdf_parser, 
                                      element->parent->subject.uri,
                                      element->parent->subject.id,
                                      element->parent->subject.type,
                                      RAPTOR_URI_SOURCE_ELEMENT,

                                      element->name->uri,
                                      NULL,
                                      RAPTOR_IDENTIFIER_TYPE_PREDICATE,
                                      RAPTOR_URI_SOURCE_ELEMENT,

                                      object_uri,
                                      element->object.id,
                                      object_type,
                                      element->object.uri_source,

                                      element->bag.uri);
          
        } else {
          int object_is_literal=(element->content_cdata != NULL); /* FIXME */
          raptor_uri *object_uri=object_is_literal ? (raptor_uri*)element->content_cdata : element->object.uri;
          raptor_identifier_type object_type=object_is_literal ? RAPTOR_IDENTIFIER_TYPE_LITERAL : element->object.type;

          /* Not child of Description, so process as child of propertyElt */
          switch(element->content_type) {
            case RAPTOR_ELEMENT_CONTENT_TYPE_LITERAL:
            case RAPTOR_ELEMENT_CONTENT_TYPE_RESOURCE:
              if(state == RAPTOR_STATE_MEMBER) {
                element->parent->last_ordinal++;
                raptor_generate_statement(rdf_parser, 
                                          element->parent->subject.uri,
                                          element->parent->subject.id,
                                          element->parent->subject.type,
                                          RAPTOR_URI_SOURCE_ELEMENT,

                                          (raptor_uri*)&element->parent->last_ordinal,
                                          NULL,
                                          RAPTOR_IDENTIFIER_TYPE_ORDINAL,
                                          RAPTOR_URI_SOURCE_NOT_URI,
                                          
                                          object_uri,
                                          element->object.id,
                                          object_type,
                                          element->object.uri_source,

                                          element->bag.uri);
              } else
                raptor_generate_statement(rdf_parser, 
                                          element->parent->subject.uri,
                                          element->parent->subject.id,
                                          element->parent->subject.type,
                                          RAPTOR_URI_SOURCE_ELEMENT,

                                          element->name->uri,
                                          NULL,
                                          RAPTOR_IDENTIFIER_TYPE_PREDICATE,
                                          RAPTOR_URI_SOURCE_NOT_URI,

                                          object_uri,
                                          element->object.id,
                                          object_type,
                                          element->object.uri_source,

                                          element->bag.uri);
              
              break;
              
            case RAPTOR_ELEMENT_CONTENT_TYPE_EMPTY:
              /* empty property of form
               * <property rdf:resource="object-URI"/> 
               */
              if(state == RAPTOR_STATE_MEMBER) {
                element->parent->last_ordinal++;
                raptor_generate_statement(rdf_parser, 
                                          element->parent->subject.uri,
                                          element->parent->subject.id,
                                          element->parent->subject.type,
                                          RAPTOR_URI_SOURCE_ELEMENT,

                                          (raptor_uri*)&element->parent->last_ordinal,
                                          NULL,
                                          RAPTOR_IDENTIFIER_TYPE_ORDINAL,
                                          RAPTOR_URI_SOURCE_NOT_URI,

                                          element->subject.uri,
                                          element->subject.id,
                                          element->subject.type,
                                          element->subject.uri_source,

                                          element->bag.uri);
              } else
                raptor_generate_statement(rdf_parser, 
                                          element->parent->subject.uri,
                                          element->parent->subject.id,
                                          element->parent->subject.type,
                                          RAPTOR_URI_SOURCE_ELEMENT,

                                          element->name->uri,
                                          NULL,
                                          RAPTOR_IDENTIFIER_TYPE_PREDICATE,
                                          RAPTOR_URI_SOURCE_ELEMENT,

                                          element->subject.uri,
                                          element->subject.id,
                                          element->subject.type,
                                          element->subject.uri_source,

                                          element->bag.uri);
              break;

          case RAPTOR_ELEMENT_CONTENT_TYPE_PRESERVED:
              if(state == RAPTOR_STATE_MEMBER) {
                element->parent->last_ordinal++;
                raptor_generate_statement(rdf_parser, 
                                          element->parent->subject.uri,
                                          element->parent->subject.id,
                                          element->parent->subject.type,
                                          element->parent->subject.uri_source,

                                          (raptor_uri*)&element->parent->last_ordinal,
                                          NULL,
                                          RAPTOR_IDENTIFIER_TYPE_ORDINAL,
                                          RAPTOR_URI_SOURCE_NOT_URI,
                                          
                                          (raptor_uri*)element->content_cdata,
                                          NULL,
                                          RAPTOR_IDENTIFIER_TYPE_XML_LITERAL,
                                          RAPTOR_URI_SOURCE_NOT_URI,

                                          element->bag.uri);
              } else {
                raptor_generate_statement(rdf_parser, 
                                          element->parent->subject.uri,
                                          element->parent->subject.id,
                                          element->parent->subject.type,
                                          element->parent->subject.uri_source,
                                          
                                          element->name->uri,
                                          NULL,
                                          RAPTOR_IDENTIFIER_TYPE_PREDICATE,
                                          RAPTOR_URI_SOURCE_ELEMENT,

                                          (raptor_uri*)element->content_cdata,
                                          NULL,
                                          RAPTOR_IDENTIFIER_TYPE_XML_LITERAL,
                                          RAPTOR_URI_SOURCE_NOT_URI,

                                          element->bag.uri);
              }

            break;
            
            case RAPTOR_ELEMENT_CONTENT_TYPE_DAML_COLLECTION:
              if (!element->tail_uri) {
                /* If No List: set parents object to daml:nil */
                raptor_generate_statement(rdf_parser, 
                                          element->parent->subject.uri,
                                          element->parent->subject.id,
                                          element->parent->subject.type,
                                          RAPTOR_URI_SOURCE_ELEMENT,

                                          element->subject.uri,
                                          element->subject.id,
                                          RAPTOR_IDENTIFIER_TYPE_PREDICATE,
                                          RAPTOR_URI_SOURCE_URI,

                                          RAPTOR_DAML_NIL_URI(rdf_parser),
                                          NULL,
                                          RAPTOR_IDENTIFIER_TYPE_RESOURCE,
                                          RAPTOR_URI_SOURCE_URI,

                                          NULL);
              } else {
                /* If List: set parents object to the list */
                raptor_generate_statement(rdf_parser, 
                                          element->parent->subject.uri,
                                          element->parent->subject.id,
                                          element->parent->subject.type,
                                          RAPTOR_URI_SOURCE_ELEMENT,

                                          element->name->uri,
                                          NULL,
                                          RAPTOR_IDENTIFIER_TYPE_PREDICATE,
                                          RAPTOR_URI_SOURCE_ELEMENT,

                                          element->object.uri,
                                          element->object.id,
                                          element->object.type,
                                          RAPTOR_URI_SOURCE_URI,

                                          NULL);
                /* and terminate the list */
                raptor_generate_statement(rdf_parser, 
                                          element->tail_uri,
                                          NULL,
                                          RAPTOR_IDENTIFIER_TYPE_RESOURCE,
                                          RAPTOR_URI_SOURCE_URI,

                                          RAPTOR_DAML_REST_URI(rdf_parser),
                                          NULL,
                                          RAPTOR_IDENTIFIER_TYPE_PREDICATE,
                                          RAPTOR_URI_SOURCE_URI,

                                          RAPTOR_DAML_NIL_URI(rdf_parser),
                                          NULL,
                                          RAPTOR_IDENTIFIER_TYPE_RESOURCE,
                                          RAPTOR_URI_SOURCE_URI,

                                          NULL);
              }
              break;
              
            default:
              raptor_parser_fatal_error(rdf_parser, "raptor_end_element_grammar state RAPTOR_STATE_PROPERTYELT - unexpected content type %d\n", element->content_type);
          } /* end switch */
          
        }

        finished=1;
        break;


      default:
        raptor_parser_fatal_error(rdf_parser, "raptor_end_element_grammar - unexpected parser state %d - %s\n", state, raptor_state_as_string(state));
        finished=1;

    } /* end switch */

    if(state != element->state) {
      element->state=state;
      LIBRDF_DEBUG3(raptor_end_element_grammar, "moved to state %d - %s\n",
                    state, raptor_state_as_string(state));
    }

  } /* end while */

  LIBRDF_DEBUG3(raptor_end_element_grammar, 
                "ending in state %d - %s\n",
                state, raptor_state_as_string(state));

}