File: schema-schema_readers.adb

package info (click to toggle)
libxmlada 25.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,212 kB
  • sloc: ada: 78,068; sh: 3,310; makefile: 394; xml: 111; python: 39
file content (4073 lines) | stat: -rw-r--r-- 144,746 bytes parent folder | download | duplicates (2)
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
------------------------------------------------------------------------------
--                     XML/Ada - An XML suite for Ada95                     --
--                                                                          --
--                     Copyright (C) 2004-2021, AdaCore                     --
--                                                                          --
-- This library is free software;  you can redistribute it and/or modify it --
-- under terms of the  GNU General Public License  as published by the Free --
-- Software  Foundation;  either version 3,  or (at your  option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE.                            --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
------------------------------------------------------------------------------

pragma Ada_05;

with Ada.Exceptions;    use Ada.Exceptions;
with GNAT.Task_Lock;    use GNAT.Task_Lock;
with Unicode;           use Unicode;
with Unicode.CES;       use Unicode.CES;
with Sax.Encodings;     use Sax.Encodings;
with Sax.Exceptions;    use Sax.Exceptions;
with Sax.Locators;      use Sax.Locators;
with Sax.Symbols;       use Sax.Symbols;
with Sax.Utils;         use Sax.Utils;
with Schema.Simple_Types; use Schema.Simple_Types;
with Schema.Readers;      use Schema.Readers;
with Ada.Unchecked_Deallocation;
with Ada.IO_Exceptions;
with Input_Sources.File;  use Input_Sources.File;

package body Schema.Schema_Readers is
   use Schema_State_Machines;
   use Type_Tables, Element_HTables, Group_HTables;
   use AttrGroup_HTables, Reference_HTables, Attribute_HTables;

   Default_Contexts : constant := 30;
   --  Default number of nested levels in a schema.
   --  If the actual schema uses more, we will simply reallocate some memory.

   Max_Max_Occurs : constant := 9_999;
   --  Maximum value for maxOccurs.
   --  Higher values result in an explosion in the number of states in the NFA,
   --  so should not be used for now.

   Max_Occurs_Warn : constant := 300;
   --  Warn when maxOccurs is greater than this value, since this might
   --  potentially result in a very large state machine. This is not a fatal
   --  error though.

   procedure Push_Context
     (Handler : access Schema_Reader'Class; Ctx : Context);
   --  Add a new context to the list

   procedure Unchecked_Free is new Ada.Unchecked_Deallocation
     (Attr_Array, Attr_Array_Access);
   procedure Unchecked_Free is new Ada.Unchecked_Deallocation
     (Context_Array, Context_Array_Access);

   procedure Free (Shared : in out XSD_Data_Access);
   --  Free [Shared] and its htables

   procedure Free (Self : in out Type_Details_Access);
   --  Free [Self], [Self.Next] and so on

   procedure Create_NFA (Parser : access Schema_Reader);
   --  Create the state machine from the registered elements and types
   --  Return the start state for the current grammar (we do not use the
   --  NFA's default start state, since each grammar has its own list of valid
   --  toplevel elements.

   function To_String (Final  : Final_Status) return String;

   function In_Redefine_Context
     (Handler : Schema_Reader'Class) return Boolean;
   --  Whether we are currently processing a <redefine> tag

   function Resolve_QName
     (Handler     : access Schema_Reader'Class;
      QName       : Sax.Symbols.Symbol;
      NS_If_Empty : Sax.Symbols.Symbol := Empty_String;
      Loc         : Location) return Qualified_Name;
   --  Resolve namespaces for QName.
   --  [NS_If_Empty] is used if no namespace was found for the element. This
   --  will often be the target namespace of the schema.

   procedure Internal_Parse
     (Parser            : in out Schema_Reader;
      Input             : in out Input_Sources.Input_Source'Class;
      Default_Namespace : Symbol;
      Do_Create_NFA     : Boolean;
      Do_Initialize_Shared : Boolean);
   --  Internal version of [Parse], which allows reuse of the shared data.
   --  This is useful while parsing a <include> XSD

   procedure Insert_In_Type
     (Handler : access Schema_Reader'Class;
      Element : in out Type_Details_Access);
   --  Insert Element in the type definition in [Handler.Contexts].
   --  If there is an error inserting the element, an exception is raised and
   --  [Element] is freed.

   procedure Prepare_Type
     (Handler   : access Schema_Reader'Class;
      Atts      : Sax_Attribute_List;
      Is_Simple : Boolean);
   --  Prepare a type context (simpleType or complexType)

   procedure Add_Type_Member
     (Handler : access Schema_Reader'Class;
      List    : in out Type_Member_Array;
      Member  : Type_Member;
      Loc     : Location);
   --  Add a new item in [List], and raise an exception if [List] is full.

   procedure Compute_Blocks
     (Atts    : Sax_Attribute_List;
      Handler : access Schema_Reader'Class;
      Blocks  : out Block_Status;
      Is_Set  : out Boolean;
      Index   : Integer);
   --  Compute the list of blocked elements from the attribute "block".

   function Compute_Final
     (Atts    : Sax_Attribute_List;
      Handler : access Schema_Reader'Class;
      Index   : Integer) return Final_Status;
   --  Compute the list of final attributes from value. Value is a list similar
   --  to what is used for the "final" attribute of elements in a schema

   function Compute_Form
     (Atts      : Sax_Attribute_List;
      Handler   : access Schema_Reader'Class;
      Index     : Integer) return Form_Type;
   --  Parse the given attribute

   procedure Append (List : in out Attr_Array_Access; Attr : Attr_Descr);
   --  Add an attribute to the list

   procedure Insert_Attribute
     (Handler        : access Schema_Reader'Class;
      In_Context     : Natural;
      Attribute      : Attr_Descr);
   --  Insert attribute at the right location in In_Context.

   function Process_Contents_From_Atts
     (Handler : access Schema_Reader'Class;
      Atts    : Sax_Attribute_List;
      Index   : Integer) return Process_Contents_Type;
   --  Get the value of processContents from the attributes

   procedure Create_Element
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Notation
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Complex_Type
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Simple_Type
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Restriction
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_All
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Sequence
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Attribute
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Schema
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Extension
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_List
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Union
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Choice
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Redefine
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Include
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Group
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Attribute_Group
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Any
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Import
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   procedure Create_Any_Attribute
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List);
   --  Create a new context for a specific tag:
   --  resp. <element>, <complexType>, <restriction>, <all>, <sequence>,
   --  <attribute>, <schema>, <extension>, <list>, <union>, <choice>,
   --  <redefine>, <group>, <attributeGroup>, <any>, <import>, <anyAttribute>

   procedure Finish_Element      (Handler : access Schema_Reader'Class);
   procedure Finish_Complex_Type (Handler : access Schema_Reader'Class);
   procedure Finish_Simple_Type  (Handler : access Schema_Reader'Class);
   procedure Finish_Restriction  (Handler : access Schema_Reader'Class);
   procedure Finish_Extension    (Handler : access Schema_Reader'Class);
   procedure Finish_Attribute    (Handler : access Schema_Reader'Class);
   procedure Finish_Union        (Handler : access Schema_Reader'Class);
   procedure Finish_List         (Handler : access Schema_Reader'Class);
   procedure Finish_Group        (Handler : access Schema_Reader'Class);
   procedure Finish_Attribute_Group (Handler : access Schema_Reader'Class);
   --  Finish the handling of various tags:
   --  resp. <element>, <complexType>, <restriction>, <all>, <sequence>,
   --  <extension>, <union>, <list>, <choice>, <group>

   procedure Get_Occurs
     (Handler : access Schema_Reader'Class;
      Atts    : Sax_Attribute_List;
      Min_Occurs, Max_Occurs : out Occurrences);
   --  Get the "minOccurs" and "maxOccurs" attributes

   ---------------
   -- To_String --
   ---------------

   function To_String (Final : Final_Status) return String is
   begin
      return "restr=" & Final (Final_Restriction)'Img
        & " ext=" & Final (Final_Extension)'Img
        & " union=" & Final (Final_Union)'Img
        & " list=" & Final (Final_List)'Img;
   end To_String;

   -------------------------
   -- In_Redefine_Context --
   -------------------------

   function In_Redefine_Context
     (Handler : Schema_Reader'Class) return Boolean is
   begin
      for J in 1 .. Handler.Contexts_Last loop
         if Handler.Contexts (J).Typ = Context_Redefine then
            return True;
         end if;
      end loop;
      return False;
   end In_Redefine_Context;

   ----------------
   -- Create_NFA --
   ----------------

   procedure Create_NFA (Parser : access Schema_Reader) is
      NFA : constant Schema_NFA_Access := Get_NFA (Get_Grammar (Parser.all));
      Ref : constant Reference_HTable :=
        Get_References (Get_Grammar (Parser.all));

      Any_Simple_Type_Index : constant Type_Index :=
        Get (Ref.all,
          ((Local => Parser.Any_Simple_Type,
            NS    => Parser.XML_Schema_URI), Ref_Type)).Typ;

      Shared : XSD_Data_Access renames Parser.Shared;

      package Type_HTables is new GNAT.Dynamic_HTables.Simple_HTable
        (Header_Num => Header_Num,
         Element    => Internal_Type_Index,
         No_Element => No_Internal_Type_Index,
         Key        => Qualified_Name,
         Hash       => Hash,
         Equal      => "=");
      use Type_HTables;
      Types : Type_HTables.Instance;

      procedure Process_Global_Element
        (Info : in out Element_Descr; Start_At : State);
      procedure Process_Type (Info : in out Internal_Type_Descr);
      procedure Process_Details
        (In_Type    : access Type_Descr;
         Details    : Type_Details_Access;
         Start, From : State;
         Nested_End : out State;
         Mask        : out Visited_All_Children);
      --  [Start] is the start of the current nested.
      --  [Mask] is the mask to apply to a closing transition out of a <all>
      --  node. It is only set if Details.Kind = Type_All.

      procedure Create_Element_State
        (Info : in out Element_Descr;
         Start, From : State;
         Global : Boolean;
         S1, S2 : out State;
         Trans_Kind : Transition_Kind;
         All_Child_Index : Integer := 0);
      --  Create (and decorate) the nodes [S1]..[S2] corresponding to an
      --  <element>. A link is created [From]->[S1].
      --  [Start] is the first element of the current nested machine.
      --  Trans_Kind is the kind of the first transition, from From to the
      --  first created state. This transition is associated with the name of
      --  the element.
      --  All_Child_Index indicates the index of the created transition in all
      --  the children of an <all> node. This is used to memory efficiently
      --  which transitions have already been visited for this <all>.

      type Type_Descr_Access is access all Type_Descr;
      procedure Create_Simple_Type
        (J     : Internal_Type_Index;
         Descr : out Type_Descr_Access;
         Index : out Type_Index);
      --  Create the new simple type info at index [J]

      procedure Get_Type_Descr
        (Name          : Qualified_Name;
         Loc           : Location;
         NFA_Type      : out Type_Index;
         Internal_Type : out Internal_Type_Index);
      --  Lookup the type information in the grammar. This type information
      --  could be in several places:
      --    - Either defined in the current XSD and its <input>: in that case,
      --      [Internal_Type] will be set.
      --    - Or in a previously loaded XSD. In that case, it is set to
      --      [No_Internal_Type_Index]

      procedure Lookup_Simple_Type
        (Name  : Qualified_Name;
         Loc   : Location;
         Descr : out Type_Descr_Access;
         Index : out Type_Index);
      --  Search for the simpleType [Name]

      procedure Add_Attributes
        (List             : in out Attributes_List;
         Attrs            : Attr_Array_Access;
         Processed_Groups : in out AttrGroup_HTables.Instance;
         As_Restriction   : Boolean;
         Had_Any          : in out Boolean);
      --  Create List from the list of attributes or attribute groups in
      --  [Attrs]. [Had_Any] is set to true if a <anyAttribute> was
      --  encountered. The caller should first set it to False.

      procedure Check_Unique_Particle_Attribution
        (Details : Type_Details_Access);
      --  Check that all elements in the content model are unique (we must not
      --  have two elements that compete with each other).

      procedure Create_Global_Attributes (Attr : Internal_Attribute_Descr);
      --  Register a global attribute.

      procedure Resolve_Attribute_Type
        (Attr : in out Internal_Attribute_Descr;
         Loc  : Sax.Locators.Location);
      --  Set [Attr.Descr.Simple_Type] to the appropriate value, after looking
      --  up the type

      --------------------------
      -- Create_Element_State --
      --------------------------

      procedure Create_Element_State
        (Info : in out Element_Descr;
         Start, From : State;
         Global : Boolean;
         S1, S2 : out State;
         Trans_Kind : Transition_Kind;
         All_Child_Index : Integer := 0)
      is
         pragma Unreferenced (Start);
         Real      : Element_Descr;
         Trans     : Transition_Descr;
         TRef      : Global_Reference := No_Global_Reference;
         S         : State := No_State;

         function Build_Trans
           (N : Qualified_Name; F : Form_Type) return Transition_Descr;
         function Build_Trans
           (N : Qualified_Name; F : Form_Type) return Transition_Descr is
         begin
            case Trans_Kind is
               when Transition_Symbol =>
                  return (Transition_Symbol, N, F, All_Child_Index => 0);
               when Transition_Symbol_From_All =>
                  return (Transition_Symbol_From_All, N, F,
                          All_Child_Index => All_Child_Index);
               when others =>
                  raise Program_Error with "Invalid transition type";
            end case;
         end Build_Trans;

      begin
         if Info.Is_Abstract then
            --  ??? Should use the substitutionGroup elements, instead
            S1 := No_State;
            S2 := No_State;

            Validation_Error
              (Parser,
               "Abstract elements not handled yet",
               Except => XML_Not_Implemented'Identity);
            return;
         end if;

         S1 := NFA.Add_State;
         Info.S := S1;

         if Debug then
            Debug_Output ("Create_Element_State S1 for element "
                          & To_QName (Info.Name) & To_QName (Info.Ref)
                          & S1'Img);
         end if;

         --  Resolve element references

         if Info.Name = No_Qualified_Name then
            Real := Get (Shared.Global_Elements, Info.Ref);
            if Real = No_Element_Descr then
               TRef := Get (Ref.all, (Info.Ref, Ref_Element));
               if TRef = No_Global_Reference then
                  Validation_Error
                    (Parser, "Unknown refed element " & To_QName (Info.Ref),
                     Info.Loc);
               end if;

               S := TRef.Element;
               Trans := Build_Trans (Info.Ref, Info.Form);

            else
               Trans := Build_Trans (Real.Name, Info.Form);
               S := Real.S;
            end if;

            if S /= No_State then
               if Debug then
                  Debug_Output ("copying data from" & S'Img & " to" & S1'Img);
               end if;
               NFA.Get_Data (S1).all := NFA.Get_Data (S).all;
               NFA.Set_Nested (S1, NFA.Get_Nested (S));
            end if;

         else
            declare
               NFA_Type      : Type_Index;
               Internal_Type : Internal_Type_Index;
               Data          : access State_Data;
            begin
               Real := Info;
               Trans := Build_Trans (Real.Name, Info.Form);

               --  Create nested NFA for the type, if needed

               if Real.Typ /= No_Qualified_Name then
                  Get_Type_Descr (Real.Typ, Info.Loc, NFA_Type, Internal_Type);

               elsif Real.Local_Type /= No_Internal_Type_Index then
                  Internal_Type := Real.Local_Type;
                  NFA_Type := Shared.Types.Table (Internal_Type).In_NFA;
               else
                  --  "<anyType>" (3.3.2.1 {type definition})
                  NFA_Type := No_Type_Index;
               end if;

               if Info.Substitution_Group /= No_Qualified_Name then
                  --  ??? Handling of substitutionGroup: the type of the
                  --  element is the same as the head unless overridden.
                  Validation_Error
                    (Parser,
                     "substitutionGroup not supported",
                     Except => XML_Not_Implemented'Identity);
               end if;

               Data := NFA.Get_Data (S1);

               if NFA_Type /= No_Type_Index then
                  Data.all := State_Data'(Simple   => NFA_Type,
                                          Fixed    => No_Symbol, --  See below
                                          Default  => Info.Default,
                                          Nillable => Info.Nillable,
                                          Block    => Info.Block);
                  NFA.Set_Nested
                    (S1, NFA.Create_Nested
                       (Get_Type_Descr (NFA, NFA_Type).Complex_Content));
               else
                  NFA.Set_Nested (S1, NFA.Ur_Type (Process_Lax));
                  Data.Default  := Info.Default;
                  Data.Block    := Info.Block;
                  Data.Nillable := Info.Nillable;
               end if;

               --  Check that the fixed value is valid

               if Info.Fixed /= No_Symbol
                 and then NFA.Get_Data (S1).Simple /= No_Type_Index
               then
                  Normalize_And_Validate
                    (Parser => Parser,
                     Simple => Get_Type_Descr
                       (NFA, NFA.Get_Data (S1).Simple).Simple_Content,
                     Fixed  => Info.Fixed,
                     Loc    => Info.Loc);
               end if;

               Data.Fixed := Info.Fixed;
            end;
         end if;

         --  Link with previous element

         NFA.Add_Transition (From, S1, Trans);

         S2 := NFA.Add_State;
         if Debug then
            Debug_Output ("Create_Element_State S2 for element "
                          & To_QName (Info.Name) & To_QName (Info.Ref)
                          & S2'Img);
         end if;

         if NFA.Get_Nested (S1) /= No_Nested then
            NFA.On_Empty_Nested_Exit (S1, S2);  --  a complexType
         else
            NFA.Add_Transition (S1, S2, (Kind => Transition_Close));
         end if;

         --  Save this element for later reuse in other namespaces

         if Global then
            if Debug then
               Debug_Output ("Global elem: " & To_QName (Real.Name));
            end if;
            Set (Ref.all,
                 (Kind => Ref_Element, Name => Real.Name, Element => S1));
         end if;
      end Create_Element_State;

      ----------------------------
      -- Process_Global_Element --
      ----------------------------

      procedure Process_Global_Element
        (Info : in out Element_Descr; Start_At : State)
      is
         S1, S2 : State;
      begin
         Create_Element_State
           (Info, Start_At, Start_At, True, S1, S2, Transition_Symbol);

         if S2 /= No_State then
            NFA.Add_Empty_Transition (S2, Final_State);
         end if;
      end Process_Global_Element;

      --------------------
      -- Get_Type_Descr --
      --------------------

      procedure Get_Type_Descr
        (Name          : Qualified_Name;
         Loc           : Location;
         NFA_Type      : out Type_Index;
         Internal_Type : out Internal_Type_Index)
      is
         TRef : Global_Reference;
      begin
         Internal_Type := Get (Types, Name);
         if Internal_Type = No_Internal_Type_Index then
            TRef := Get (Ref.all, (Name, Ref_Type));
            if TRef = No_Global_Reference then
               Validation_Error
                 (Parser, "Unknown type " & To_QName (Name), Loc);
            end if;

            NFA_Type := TRef.Typ;
         else
            NFA_Type := Shared.Types.Table (Internal_Type).In_NFA;
         end if;

         if Name = (NS => Parser.XML_Schema_URI, Local => Parser.IDREF)
           or else
             Name = (NS => Parser.XML_Schema_URI, Local => Parser.IDREFS)
         then
            Validation_Error
              (Parser,
               "Unsupported type IDREF and IDREFS",
               Loc => Loc,
               Except => XML_Not_Implemented'Identity);
         end if;
      end Get_Type_Descr;

      ------------------------
      -- Lookup_Simple_Type --
      ------------------------

      procedure Lookup_Simple_Type
        (Name  : Qualified_Name;
         Loc   : Location;
         Descr : out Type_Descr_Access;
         Index : out Type_Index)
      is
         TRef     : Global_Reference;
         Internal : Internal_Type_Index;
      begin
         TRef := Get (Ref.all, (Name, Ref_Type));
         if TRef = No_Global_Reference then
            Validation_Error
              (Parser, "Unknown type " & To_QName (Name), Loc);
         end if;

         Index := TRef.Typ;
         Descr := Type_Descr_Access (Get_Type_Descr (NFA, Index));

         if Descr.Simple_Content = No_Simple_Type_Index then
            if Debug then
               Debug_Output ("Lookup_Simple_Type: generate "
                             & To_QName (Name) & " early");
            end if;

            Internal := Get (Types, Name);
            if Internal = No_Internal_Type_Index then
               Validation_Error
                 (Parser,
                  "Type is not a simple type: " & To_QName (Name), Loc);
            end if;

            Create_Simple_Type (Internal, Descr, Index);
         end if;
      end Lookup_Simple_Type;

      ---------------------------------------
      -- Check_Unique_Particle_Attribution --
      ---------------------------------------

      procedure Check_Unique_Particle_Attribution
        (Details : Type_Details_Access)
      is
         Duplicates : Element_HTables.Instance;
         --  Used to check for duplicate elements within a sequence or choice.
         --  ??? Could use a htable of locations, so that we can point to the
         --  two duplicate declarations.

         T     : Type_Details_Access;
         Elem  : Element_Descr;

      begin
         case Details.Kind is
            when Type_Sequence =>
               T := Details.First_In_Seq;
            when Type_Choice =>
               T := Details.First_In_Choice;
            when Type_All =>
               T := Details.First_In_All;
            when others =>
               raise Program_Error with "Internal error";
         end case;

         while T /= null loop
            case T.Kind is
               when Type_Element =>
                  if T.Element.Name /= No_Qualified_Name then
                     Elem := Element_HTables.Get (Duplicates, T.Element.Name);

                     if Elem /= No_Element_Descr then
                        --  It is always invalid to have an element with the
                        --  same name but with different types in the same
                        --  model, even if there is no ambiguity (for instance
                        --  in a sequence).

                        if Elem.Typ /= No_Qualified_Name
                          and then Elem.Typ /= T.Element.Typ
                        then
                           Validation_Error
                             (Parser,
                              "Multiple elements with name '"
                              & To_QName (T.Element.Name)
                              & "', with different types, appear in the model"
                              & " group",
                              Details.Loc);
                        end if;

                        --  In a <choice> or <all>, we cannot have the same
                        --  element multiple times, since that would be
                        --  ambiguous.

                        if Details.Kind = Type_Choice
                          or else Details.Kind = Type_All
                        then
                           Validation_Error
                             (Parser,
                              "'" & To_QName (T.Element.Name) & "' and '"
                              & To_QName (Elem.Name) & "' violate the Unique"
                              & " Particle Attribution rule, creating an"
                              & " ambiguity for the validation",
                              Details.Loc);
                        end if;

                     else
                        Element_HTables.Set
                          (Duplicates, T.Element.Name, T.Element);
                     end if;
                  end if;

               when Type_Group =>
                  null;

               when others =>
                  --  ??? Should check, in particular for groups or nested
                  --  sequences (we can have a Sequence that contains choices)
                  null;
            end case;

            T := T.Next;
         end loop;

         Reset (Duplicates);

      exception
         when others =>
            Reset (Duplicates);
            raise;
      end Check_Unique_Particle_Attribution;

      ---------------------
      -- Process_Details --
      ---------------------

      procedure Process_Details
        (In_Type     : access Type_Descr;
         Details     : Type_Details_Access;
         Start, From : State;
         Nested_End  : out State;
         Mask        : out Visited_All_Children)
      is
         S, S1 : State;
         T     : Type_Details_Access;
         Gr    : Group_Descr;

      begin
         Nested_End := From;
         Mask := 0;

         if Details = null then
            return;
         end if;

         Details.In_Process := True;

         case Details.Kind is
            when Type_Empty =>
               null;

            when Type_Sequence =>
               Check_Unique_Particle_Attribution (Details);

               S := From;
               T := Details.First_In_Seq;
               while T /= null loop
                  Process_Details (In_Type, T, Start, S, Nested_End, Mask);
                  S1 := Nested_End;

                  --  We can have one element start exactly where the other
                  --  finishes. Otherwise we have issues with the following
                  --  sequence:
                  --  <sequence>
                  --    <element name="a" minOccurs="0" maxOccurs="unbounded"/>
                  --    <element name="b" minOccurs="0" maxOccurs="unbounded"/>
                  --  </sequence>
                  --  which would allow any number of a, any number of b, and
                  --  again any number of a (because of empty transition from
                  --  end of "b*" that goes back to the beginning of "b*",
                  --  which would be the same as end of "a*", which has empty
                  --  transition to "a*". The following empty transition
                  --  ensures that end of "a*" is not same as beginning of
                  --  "b*".
                  S := NFA.Add_State;
                  NFA.Add_Empty_Transition (S1, S);

                  T := T.Next;
               end loop;
               Nested_End := S;

            when Type_Choice =>
               Check_Unique_Particle_Attribution (Details);

               T := Details.First_In_Choice;
               Nested_End := NFA.Add_State;
               while T /= null loop
                  S1 := NFA.Add_State;
                  NFA.Add_Empty_Transition (From, S1);

                  Process_Details (In_Type, T, Start, S1, S, Mask);
                  NFA.Add_Empty_Transition (S, Nested_End);

                  T := T.Next;
               end loop;

            when Type_All =>
               Check_Unique_Particle_Attribution (Details);

               if Details.First_In_All /= null then
                  declare
                     Count : Natural := 0;
                  begin
                     Mask := 0;
                     T := Details.First_In_All;
                     while T /= null loop
                        pragma Assert
                          (T.Kind = Type_Element,
                           "Children of <all> must be simple elements");

                        --  If the element has maxOccurs=0, there is no need to
                        --  put it in the state machine.

                        if T.Max_Occurs.Value /= 0 then
                           Create_Element_State
                             (T.Element, Start, From, False,
                              S1 => S1,
                              S2 => S,
                              Trans_Kind => Transition_Symbol_From_All,
                              All_Child_Index => Count);

                           if T.Min_Occurs.Value = 1 then
                              --  The child is mandatory
                              Mask := Mask or (2 ** Count);
                           end if;

                           Count := Count + 1;

                           --  All elements, after being processed, come back
                           --  to <all> itself. The latter is in charge of
                           --  deciding, through the subprogram Match, which
                           --  transitions are valid in the current state.
                           NFA.Add_Empty_Transition (S, Start);
                        end if;

                        T := T.Next;
                     end loop;

                     --  The actual end of <all> is through a conditional empty
                     --  transition controlled by Match.

                     Nested_End := NFA.Add_State;
                     NFA.Add_Empty_Transition (From, Nested_End);
                  end;
               end if;

            when Type_Element =>
               Create_Element_State
                 (Details.Element, Start, From, False, S1, Nested_End,
                  Transition_Symbol);

            when Type_Group =>
               Gr := Get (Parser.Shared.Global_Groups, Details.Group.Ref);
               if Gr = No_Group_Descr then
                  Validation_Error
                    (Parser,
                     "No group """ & To_QName (Details.Group.Ref) & '"',
                     Details.Group.Loc);

               elsif Gr.Details.In_Process then
                  Validation_Error
                    (Parser,
                     "Circular group reference for "
                     & To_QName (Details.Group.Ref),
                     Details.Group.Loc);
               end if;

               Process_Details
                 (In_Type, Gr.Details, Start, From, Nested_End, Mask);

            when Type_Extension =>
               declare
                  NFA_Type      : Type_Index;  --  Attributes of the base type
                  Internal_Type : Internal_Type_Index;
                  Base_Descr    : access Type_Descr;
               begin
                  Get_Type_Descr
                    (Name          => Details.Extension.Base,
                     Loc           => Details.Extension.Loc,
                     NFA_Type      => NFA_Type,
                     Internal_Type => Internal_Type);

                  Base_Descr := Get_Type_Descr (NFA, NFA_Type);

                  if Base_Descr.Final (Final_Extension) then
                     Validation_Error
                       (Parser,
                        To_QName (Base_Descr.Name)
                        & " is final for extensions",
                        Details.Extension.Loc);
                  end if;

                  if Base_Descr.Simple_Content = No_Simple_Type_Index then
                     if Internal_Type /= No_Internal_Type_Index then
                        --  We have all the details, and just have to copy them
                        --  Details might be null, for instance for an
                        --  <extension> that just adds attributes

                        if Shared.Types.Table (Internal_Type).Details /= null
                          and then
                            Shared.Types.Table (Internal_Type)
                            .Details.In_Process
                        then
                           Validation_Error
                             (Parser,
                              "Circular inheritance of type "
                              & To_QName (Details.Extension.Base),
                              Details.Extension.Loc);
                        end if;

                        Process_Details
                          (In_Type,
                           Shared.Types.Table (Internal_Type).Details,
                           Start, From, S, Mask);

                     else
                        --  ??? Should copy the nested NFA for TyS
                        Validation_Error
                          (Parser,
                           "Extension's base in a different file "
                           & To_QName (Details.Extension.Base),
                           Details.Extension.Loc,
                           XML_Not_Implemented'Identity);
                     end if;

                     Process_Details
                       (In_Type,
                        Details.Extension.Details, Start, S, Nested_End,
                        Mask);
                  else
                     --  ??? Should handle simple types
                     Nested_End := Start;

                     --  The test is correct. However, it makes
                     --  msData/particles/particlesZ031.xsd fails, because the
                     --  test is incorrect. The test pretends that the XSD is
                     --  valid, but later checkins in the testsuite have proven
                     --  it incorrect. The following Ada test would make the
                     --  test pass, but then
                     --     MS-Additional2006-07-15/addB036
                     --  fails
                     --
                     --  and then
                     --    (Internal_Type = No_Internal_Type_Index
                     --   or else Shared.Types.Table (Internal_Type).Is_Simple)

                     if not Details.Simple_Content then
                        Validation_Error
                          (Parser,
                           "base type specified in complexContent definition"
                           & " must be a complex type",
                           Details.Extension.Loc);
                     end if;
                  end if;

                  In_Type.Extension_Of   := NFA_Type;
                  In_Type.Restriction_Of := No_Type_Index;
               end;

            when Type_Restriction =>
               declare
                  Internal : Internal_Type_Index;
                  NFA_Type : Type_Index;  --  Attributes of the base type
                  Base_Descr : access Type_Descr;
               begin
                  Get_Type_Descr
                    (Name          => Details.Restriction.Base,
                     Loc           => No_Location,
                     NFA_Type      => NFA_Type,
                     Internal_Type => Internal);

                  Base_Descr := Get_Type_Descr (NFA, NFA_Type);

                  if Base_Descr.Final (Final_Restriction) then
                     Validation_Error
                       (Parser,
                        To_QName (Base_Descr.Name)
                        & " is final for restrictions",
                        Details.Restriction.Loc);
                  end if;

                  if Base_Descr.Simple_Content = No_Simple_Type_Index then
                     if Internal /= No_Internal_Type_Index
                       and then Shared.Types.Table (Internal).Details /= null
                       and then
                         Shared.Types.Table (Internal).Details.In_Process
                     then
                        Validation_Error
                          (Parser,
                           "Circular inheritance of type "
                           & To_QName (Details.Restriction.Base),
                           Details.Restriction.Loc);
                     end if;

                     Process_Details
                       (In_Type,
                        Details.Restriction.Details, Start, From, Nested_End,
                        Mask);
                  else
                     --  ??? Should handle simple types
                     Nested_End := Start;

                     if not Details.Simple_Content_Restriction then
                        Validation_Error
                          (Parser,
                           "base type specified in complexContent definition"
                           & " must be a complex type",
                           Details.Restriction.Loc);
                     end if;
                  end if;

                  In_Type.Restriction_Of := NFA_Type;
                  In_Type.Extension_Of   := No_Type_Index;
               end;

            when Type_Any =>
               S := NFA.Add_State; --   ((Simple => 1, others => <>));
               NFA.Set_Nested (S, Ur_Type (NFA, Details.Any.Process_Contents));

               NFA.Add_Transition
                 (From, S, (Transition_Any,
                  Combine (Parser.Grammar, No_Any_Descr,
                           Local_Process  => Details.Any.Process_Contents,
                           Local          => Details.Any.Namespaces,
                           As_Restriction => False,
                           Target_NS      => Details.Any.Target_NS)));

               Nested_End := NFA.Add_State;
               NFA.On_Empty_Nested_Exit (S, Nested_End);
         end case;

         --  For <all> elements, we can only have maxOccurs=1 and minOccurs=0
         --  or 1. In the case of minOccurs=0, and since we use conditional
         --  links there, we cannot create a direct empty transition from the
         --  start state to the final state (since the
         --  transition_close_from_all is on exit of that final state, and
         --  thus would be *after* the new empty transition). So this case
         --  is handled specially in Process_Type.

         if Details.Kind /= Type_All then
            if Details.Max_Occurs.Unbounded then
               Nested_End := NFA.Repeat
                 (From, Nested_End, Details.Min_Occurs.Value,
                  Natural'Last);
            else
               Nested_End := NFA.Repeat
                 (From, Nested_End, Details.Min_Occurs.Value,
                  Details.Max_Occurs.Value);
            end if;
         end if;

         Details.In_Process := False;
      end Process_Details;

      ----------------------------
      -- Resolve_Attribute_Type --
      ----------------------------

      procedure Resolve_Attribute_Type
        (Attr : in out Internal_Attribute_Descr;
         Loc  : Sax.Locators.Location)
      is
         TRef     : Global_Reference;
         NFA_Type : Type_Index;  --  In NFA
      begin
         if Attr.Local_Type /= No_Internal_Type_Index then
            NFA_Type := Shared.Types.Table (Attr.Local_Type).In_NFA;
            Attr.Descr.Simple_Type :=
              Get_Type_Descr (NFA, NFA_Type).Simple_Content;

         elsif Attr.Typ = No_Qualified_Name then
               --  ??? Type should be ur-type (3.2.2)
               null;

         else
            TRef := Get (Ref.all, (Attr.Typ, Ref_Type));
            if TRef = No_Global_Reference then
               Validation_Error
                 (Parser,
                  "Unknown type: " & To_QName (Attr.Typ),
                  Loc);

            else
               Attr.Descr.Simple_Type :=
                 Get_Type_Descr (NFA, TRef.Typ).Simple_Content;
            end if;
         end if;
      end Resolve_Attribute_Type;

      --------------------
      -- Add_Attributes --
      --------------------

      procedure Add_Attributes
        (List             : in out Attributes_List;
         Attrs            : Attr_Array_Access;
         Processed_Groups : in out AttrGroup_HTables.Instance;
         As_Restriction   : Boolean;
         Had_Any          : in out Boolean)
      is
         Gr   : AttrGroup_Descr;
         TRef : Global_Reference;
      begin
         if Attrs /= null then
            for A in Attrs'Range loop
               case Attrs (A).Kind is
                  when Kind_Unset =>
                     null;
                  when Kind_Group =>
                     Gr := Get (Shared.Global_AttrGroups, Attrs (A).Group_Ref);
                     if Gr = No_AttrGroup_Descr then
                        Validation_Error
                          (Parser,
                           "Reference to undefined attributeGroup: "
                           & To_QName (Attrs (A).Group_Ref),
                           Attrs (A).Loc);

                     elsif Get (Processed_Groups, Gr.Name) /=
                       No_AttrGroup_Descr
                     then
                        Validation_Error
                          (Parser,
                           "attributeGroup """ & To_QName (Attrs (A).Group_Ref)
                           & """ has circular reference",
                           Attrs (A).Loc);
                     else
                        Set (Processed_Groups, Gr.Name, Gr);
                        Add_Attributes
                          (List, Gr.Attributes, Processed_Groups,
                           As_Restriction, Had_Any);
                     end if;

                  when Kind_Attribute =>
                     if Attrs (A).Attr.Ref /= No_Qualified_Name then
                        TRef := Get
                          (Ref.all, (Attrs (A).Attr.Ref, Ref_Attribute));

                        if TRef = No_Global_Reference then
                           Validation_Error
                             (Parser,
                              "Unknown referenced attribute: "
                              & To_QName (Attrs (A).Attr.Ref),
                              Attrs (A).Loc);
                        end if;

                        Add_Attribute
                          (Parser, List,
                           Attribute => Attrs (A).Attr.Descr,
                           Ref       => TRef.Attributes.Named,
                           Loc       => Attrs (A).Loc);

                     else
                        Resolve_Attribute_Type (Attrs (A).Attr, Attrs (A).Loc);

                        if Attrs (A).Attr.Any /= No_Internal_Any_Descr then
                           Had_Any := True;
                           Add_Any_Attribute
                             (Parser.Grammar, List, Attrs (A).Attr.Any,
                              As_Restriction);

                        else
                           Add_Attribute
                             (Parser, List, Attrs (A).Attr.Descr,
                              Loc => Attrs (A).Loc);
                        end if;
                     end if;
               end case;
            end loop;
         end if;
      end Add_Attributes;

      ------------------------
      -- Create_Simple_Type --
      ------------------------

      procedure Create_Simple_Type
        (J     : Internal_Type_Index;
         Descr : out Type_Descr_Access;
         Index : out Type_Index)
      is
         Info : Internal_Type_Descr renames Shared.Types.Table (J);
         Simple : Simple_Type_Descr;
         Index_In_Simple : Natural;
         Internal : Internal_Simple_Type_Descr;
         Result : Simple_Type_Index;
      begin
         Index := Info.In_NFA;
         Descr := Type_Descr_Access (NFA.Get_Type_Descr (Index));

         Result := Descr.Simple_Content;
         if Result /= No_Simple_Type_Index then
            if Debug then
               Debug_Output ("Create_Simple_Type: already done "
                             & To_QName (Info.Properties.Name));
            end if;
            return;
         end if;

         Internal := Info.Simple;
         if Internal.Kind = Simple_Type_None then
            --  Not a simple type, nothing to do
            Descr := null;
            return;
         end if;

         if Internal.In_Process then
            Validation_Error
              (Parser,
               "Circular inheritance of type "
               & To_QName (Info.Properties.Name),
               Info.Loc);
         end if;

         if Debug then
            Debug_Output
              ("Create_Simple_Type " & To_QName (Info.Properties.Name)
               & " " & Internal.Kind'Img);
         end if;

         Info.Simple.In_Process := True;

         case Internal.Kind is
            when Simple_Type_None =>
               Descr := null;
               return;

            when Simple_Type =>
               --  ??? Shouldn't we set Simple_Content as well ?

               Descr.Restriction_Of := Any_Simple_Type_Index;

            when Simple_Type_Union =>
               Simple := (Kind => Primitive_Union,
                          Union => (others => No_Simple_Type_Index),
                          others => <>);
               Index_In_Simple := Simple.Union'First;

               for U in Internal.Union_Items'Range loop
                  declare
                     Member : constant Type_Member := Internal.Union_Items (U);
                     Item   : Type_Descr_Access;
                     Index  : Type_Index;
                  begin
                     exit when Member = No_Type_Member;

                     if Member.Name /= No_Qualified_Name then
                        Lookup_Simple_Type
                          (Member.Name, Internal.Loc, Item, Index);
                        if Item.Final (Final_Union) then
                           Validation_Error
                             (Parser,
                              To_QName (Member.Name) & " is final for union",
                              Internal.Loc);
                        end if;

                     else
                        Create_Simple_Type (Member.Local, Item, Index);
                     end if;

                     Simple.Union (Index_In_Simple) := Item.Simple_Content;
                     Index_In_Simple := Index_In_Simple + 1;
                  end;
               end loop;

               Result := Create_Simple_Type (NFA, Simple);
               Descr.Simple_Content := Result;
               Descr.Restriction_Of := Any_Simple_Type_Index;

            when Simple_Type_List =>
               Simple := (Kind      => Primitive_List,
                          List_Item => No_Simple_Type_Index,
                          others    => <>);

               for U in Internal.List_Items'Range loop
                  declare
                     Member : constant Type_Member := Internal.List_Items (U);
                     Item   : Type_Descr_Access;
                     Index  : Type_Index;
                  begin
                     exit when Member = No_Type_Member;
                     if Member.Name /= No_Qualified_Name then
                        Lookup_Simple_Type
                          (Member.Name, Internal.Loc, Item, Index);
                        if Item.Final (Final_List) then
                           Validation_Error
                             (Parser,
                              To_QName (Member.Name) & " is final for list",
                              Internal.Loc);
                        end if;
                     else
                        Create_Simple_Type (Member.Local, Item, Index);
                     end if;

                     Simple.List_Item := Item.Simple_Content;
                  end;
               end loop;

               Result := Create_Simple_Type (NFA, Simple);
               Descr.Simple_Content := Result;
               Descr.Restriction_Of := Any_Simple_Type_Index;

            when Simple_Type_Restriction | Simple_Type_Extension =>
               declare
                  Base  : Simple_Type_Descr;
                  Error : Symbol;
                  Loc   : Location;
                  NFA_Simple : Type_Descr_Access;
                  NFA_Type   : Type_Index;
               begin
                  if Internal.Base.Name /= No_Qualified_Name then
                     Lookup_Simple_Type
                       (Internal.Base.Name, Internal.Loc,
                        NFA_Simple, NFA_Type);
                  else
                     Create_Simple_Type
                       (Internal.Base.Local, NFA_Simple, NFA_Type);
                  end if;

                  if Internal.Base = No_Type_Member then
                     Base     := Any_Simple_Type;
                     NFA_Type := Any_Simple_Type_Index;

                  elsif NFA_Simple = null
                    or else NFA_Simple.Simple_Content = No_Simple_Type_Index
                  then
                     Validation_Error
                       (Parser,
                        "base type specified in simpleContent definition must"
                        & " be a simple type",
                        Loc);

                  else
                     Base := Copy
                       (Get_Simple_Type (NFA, NFA_Simple.Simple_Content));
                     Override (Simple  => Base,
                               Facets  => Internal.Facets,
                               Symbols => Get_Symbol_Table (Parser.all),
                               As_Restriction => Internal.Kind =
                                 Simple_Type_Restriction,
                               Error   => Error,
                               Error_Loc => Loc);
                     if Error /= No_Symbol then
                        Validation_Error (Parser, Get (Error).all, Loc);
                     end if;
                  end if;

                  case Internal.Kind is
                     when Simple_Type_Restriction =>
                        Descr.Restriction_Of := NFA_Type;
                     when Simple_Type_Extension =>
                        Descr.Extension_Of := NFA_Type;
                     when others =>
                        null;
                  end case;

                  Result := Create_Simple_Type (NFA, Base);
                  Descr.Simple_Content := Result;
               end;
         end case;

         Info.Simple.In_Process := False;
      end Create_Simple_Type;

      ------------------------------
      -- Create_Global_Attributes --
      ------------------------------

      procedure Create_Global_Attributes (Attr : Internal_Attribute_Descr) is
         Attr2 : Internal_Attribute_Descr;
      begin
         pragma Assert (Attr.Ref = No_Qualified_Name,
                        "A global attribute cannot define ref");
         pragma Assert (Attr.Descr.Name /= No_Qualified_Name,
                        "A global attribute must have a name");
         pragma Assert (Attr.Any = No_Internal_Any_Descr,
                        "A global attribute is not <anyAttribute>");
         pragma Assert (Attr.Descr.Next = Empty_Named_Attribute_List,
                        "Global attributes cannot be in a list");
         pragma Assert (Attr.Descr.Simple_Type = No_Simple_Type_Index,
                        "Type of global attributes should be undefined here");

         Attr2 := Attr;
         Resolve_Attribute_Type (Attr2, No_Location);

         Create_Global_Attribute (Parser, Attr2.Descr, No_Location);
      end Create_Global_Attributes;

      ------------------
      -- Process_Type --
      ------------------

      procedure Process_Type (Info : in out Internal_Type_Descr) is
         S1    : State;
         List  : Attributes_List := No_Attributes;

         Processed_Groups : AttrGroup_HTables.Instance;
         --  ??? If this table is here, we can't have an <extension> with the
         --  same attributeGroup as its base type. Maybe this should be local
         --  to Recursive_Add_Attributes instead

         procedure Recursive_Add_Attributes (Info : Internal_Type_Descr);
         procedure Recursive_Add_Attributes (Info : Internal_Type_Descr) is
            Ty    : Global_Reference;
            Index : Internal_Type_Index;
            Had_Any : Boolean := False;
            Base    : Qualified_Name;
            As_Restriction : Boolean;
         begin
            if Info.Is_Simple then
               --  A simpleType has no attribute
               return;
            elsif Info.Details = null then
               Add_Attributes (List, Info.Attributes, Processed_Groups,
                               As_Restriction => True, Had_Any => Had_Any);
               Reset (Processed_Groups);
               return;
            end if;

            if Info.Details.Kind = Type_Extension then
               Base           := Info.Details.Extension.Base;
               As_Restriction := False;
            elsif Info.Details.Kind = Type_Restriction then
               Base           := Info.Details.Restriction.Base;
               As_Restriction := True;
            else
               Base           := No_Qualified_Name;
            end if;

            if Base = No_Qualified_Name then
               --  No character data is allowed, but we might have attributes
               Add_Attributes (List, Info.Attributes, Processed_Groups,
                               As_Restriction => True, Had_Any => Had_Any);

            elsif not As_Restriction then
               Ty := Get (Ref.all, (Base, Ref_Type));
               if Ty = No_Global_Reference then
                  Validation_Error
                    (Parser, "No type """ & To_QName (Base) & """", Info.Loc);
               end if;

               --  If the base type is in the current package, we might not
               --  have computed all its attributes. Otherwise, get the list of
               --  attributes already computed in the grammar, since it is
               --  complete.

               Index := Get (Types, Base);
               if Index /= No_Internal_Type_Index then
                  Recursive_Add_Attributes (Shared.Types.Table (Index));
               else
                  Add_Attributes
                    (Parser, List,
                     Get_Type_Descr (NFA, Ty.Typ).Attributes,
                     As_Restriction => False, Loc => Info.Loc);
               end if;

               Add_Attributes (List, Info.Attributes,
                               Processed_Groups, As_Restriction => False,
                               Had_Any => Had_Any);

            else
               Ty := Get (Ref.all, (Base, Ref_Type));
               if Ty = No_Global_Reference then
                  Validation_Error
                    (Parser, "No type """ & To_QName (Base) & """", Info.Loc);
               end if;

               Index := Get (Types, Base);
               if Index /= No_Internal_Type_Index then
                  Recursive_Add_Attributes (Shared.Types.Table (Index));
               else
                  Add_Attributes
                    (Parser, List,
                     Get_Type_Descr (NFA, Ty.Typ).Attributes,
                     As_Restriction => True, Loc => Info.Loc);
               end if;

               Had_Any := False;
               Add_Attributes (List, Info.Attributes,
                               Processed_Groups, As_Restriction => True,
                               Had_Any => Had_Any);

               --  Always add <anyAttribute>, even if none was given in the
               --  restriction (in which case none should exist for the type
               --  either);

               if not Had_Any then
                  List.Any := No_Any_Descr;  --  Nothing matches
               end if;
            end if;
         end Recursive_Add_Attributes;

      begin
         if Info.Is_Simple then
            null; --  Already done in Process_Type
         else
            if Debug then
               Debug_Output ("Process complexType "
                             & To_QName (Info.Properties.Name));
            end if;

            declare
               Descr : constant access Type_Descr :=
                 Get_Type_Descr (NFA, Info.In_NFA);
               Mask : Visited_All_Children;
               Start : State := Descr.Complex_Content;
               Is_All : constant Boolean :=
                 Info.Details /= null and then Info.Details.Kind = Type_All;
            begin
               pragma Assert (Descr.Complex_Content /= No_State);

               if Is_All and then Info.Details.Min_Occurs.Value = 0 then
                  --  See comment in Process_Details as to why this is
                  --  handled here.
                  --  We should not make the transition directly from the
                  --  start node (Descr.Complex_Content), because it would
                  --  mean a user can start a <all minOccurs="0"> and stop
                  --  in the middle with no error

                  Start := NFA.Add_State;
                  NFA.Add_Empty_Transition (Descr.Complex_Content, Start);
               end if;

               Process_Details
                 (In_Type    => Descr,
                  Details    => Info.Details,
                  Start      => Start, --  Descr.Complex_Content,
                  From       => Start,
                  Nested_End => S1,
                  Mask       => Mask);

               --  Add the attributes only after we did the details, so that we
               --  know there is no infinite recursion between the base types
               --  of extensions and restrictions

               if Debug then
                  Debug_Output ("Process attributes for complexType "
                                & To_QName (Info.Properties.Name) & " State="
                                & Descr.Complex_Content'Img
                                & " type_index=" & Info.In_NFA'Img);
               end if;

               Recursive_Add_Attributes (Info);
               Descr.Attributes := List;

               if Is_All then
                  NFA.Add_Transition
                    (S1, Final_State,
                     (Kind => Transition_Close_From_All,
                      Mask => Mask));

                  if Info.Details.Min_Occurs.Value = 0 then
                     NFA.Add_Transition
                       (Descr.Complex_Content, Final_State,
                        (Kind => Transition_Close));
                  end if;

               else
                  NFA.Add_Transition
                    (S1, Final_State, (Kind => Transition_Close));
               end if;
            end;
         end if;

         Reset (Processed_Groups);

      exception
         when others =>
            Reset (Processed_Groups);
            raise;
      end Process_Type;

      Element_Info : Element_Descr;
      Attr : Internal_Attribute_Descr;
      S : State;

      Ignored       : Type_Descr_Access;
      Ignored_Index : Type_Index;

   begin
      if Debug then
         Debug_Output ("Create_NFA");
      end if;

      --  Prepare the entries for the types. These are empty to start with, but
      --  they are needed to be able to create the more complex types, and the
      --  global element.

      for J in Type_Tables.First .. Last (Shared.Types) loop
         --  Create the empty nested NFA if needed

         S := No_State;

         if not Shared.Types.Table (J).Is_Simple then
            S := NFA.Add_State;
            Shared.Types.Table (J).Properties.Complex_Content := S;
            if Debug then
               Debug_Output
                 ("Created state for complexContent "
                  & To_QName (Shared.Types.Table (J).Properties.Name)
                  & " type=" & J'Img & " state=" & S'Img);
            end if;
         end if;

         Shared.Types.Table (J).In_NFA :=
           Create_Type (NFA, Shared.Types.Table (J).Properties);

         if S /= No_State then
            --  At least .Complex_Content has changed, so we need to reset data
            NFA.Set_Data
              (S,
               State_Data'
                 (Simple   => Shared.Types.Table (J).In_NFA,
                  Block    => No_Block,
                  Nillable => False,
                  Default  => No_Symbol,
                  Fixed    => No_Symbol));
         end if;

         if Shared.Types.Table (J).Properties.Name /= No_Qualified_Name then
            Set (Types, Shared.Types.Table (J).Properties.Name, J);
         end if;
      end loop;

      --  Process the simple types (must be in a separate loop, since a
      --  restriction or a union needs to know about its base type)

      for J in Type_Tables.First .. Last (Shared.Types) loop
         Create_Simple_Type (J, Ignored, Ignored_Index);
      end loop;

      --  Prepare the entries for the global attributes

      Attr := Get_First (Shared.Global_Attributes);
      while Attr /= No_Internal_Attribute loop
         Create_Global_Attributes (Attr);
         Attr := Get_Next (Shared.Global_Attributes);
      end loop;

      --  Prepare schema for global elements

      if Debug then
         Debug_Output ("Create_NFA: adding global elements");
      end if;

      Element_Info := Get_First (Shared.Global_Elements);
      while Element_Info /= No_Element_Descr loop
         Process_Global_Element (Element_Info, Start_State);

         --  Save the state
         Set (Shared.Global_Elements, Element_Info.Name, Element_Info);

         Element_Info := Get_Next (Shared.Global_Elements);
      end loop;

      if Debug then
         Debug_Output ("Create_NFA: complete type definition");
      end if;

      --  Finally, complete the definition of complexTypes

      for J in Type_Tables.First .. Last (Shared.Types) loop
         Process_Type (Shared.Types.Table (J));
      end loop;

      if Debug then
         Output_Action ("NFA: " & Dump_Dot_NFA (Get_Grammar (Parser.all)));
      end if;

      Reset (Types);

   exception
      when others =>
         Reset (Types);
         raise;
   end Create_NFA;

   ----------
   -- Free --
   ----------

   procedure Free (Shared : in out XSD_Data_Access) is
      procedure Unchecked_Free is new Ada.Unchecked_Deallocation
        (XSD_Data, XSD_Data_Access);
      Attr         : AttrGroup_Descr;
      Gr           : Group_Descr;
   begin
      if Shared /= null then

         --  Free all data structures, no longer needed

         Reset (Shared.Global_Elements);

         Gr := Get_First (Shared.Global_Groups);
         while Gr /= No_Group_Descr loop
            Free (Gr.Details);
            Gr := Get_Next (Shared.Global_Groups);
         end loop;
         Reset (Shared.Global_Groups);

         Attr := Get_First (Shared.Global_AttrGroups);
         while Attr /= No_AttrGroup_Descr loop
            Unchecked_Free (Attr.Attributes);
            Attr := Get_Next (Shared.Global_AttrGroups);
         end loop;
         Reset (Shared.Global_AttrGroups);

         for T in Type_Tables.First .. Last (Shared.Types) loop
            if Shared.Types.Table (T).Is_Simple then
               null;
            else
               Unchecked_Free (Shared.Types.Table (T).Attributes);
               Free (Shared.Types.Table (T).Details);
            end if;
         end loop;
         Free (Shared.Types);

         Reset (Shared.Global_Attributes);

         Unchecked_Free (Shared);
      end if;
   end Free;

   -----------------------------
   -- Parse_Grammar_If_Needed --
   -----------------------------

   procedure Parse_Grammar_If_Needed
     (Handler  : not null access Validating_Reader'Class;
      URI      : Symbol;
      Xsd_File : Symbol;
      Do_Create_NFA : Boolean)
   is
      File     : File_Input;
      Schema   : Schema_Reader;
      S_File_Full : constant Symbol := To_Absolute_URI (Handler.all, Xsd_File);
      Need_To_Initialize : Boolean := True;
   begin
      GNAT.Task_Lock.Lock;
      Set_XML_Version (Schema, Get_XML_Version (Handler.all));
      if URI_Was_Parsed (Get_Grammar (Handler.all), S_File_Full) then
         if Debug then
            Debug_Output ("Parse_Grammar " & Get (S_File_Full).all
                          & " already parsed");
         end if;
         GNAT.Task_Lock.Unlock;
         return;
      end if;

      if Debug then
         Debug_Output ("Parse_Grammar NS={" & Get (URI).all
                       & "} XSD={" & Get (Xsd_File).all & "} "
                       & Get (S_File_Full).all);
      end if;

      if Get_XSD_Version (Handler.Grammar) = XSD_1_0 then
         --  Must check that no element of the same namespace was seen
         --  already (as per 4.3.2 (4) in the XSD 1.0 norm, which was
         --  changed in XSD 1.1).

         declare
            NS : XML_NS;
         begin
            Find_NS_From_URI
              (Handler.all,
               URI     => URI,
               NS      => NS);

            if NS /= No_XML_NS
              and then Element_Count (NS) > 0
              and then S_File_Full /= Get_System_Id (NS)
              and then Get_Feature
                (Handler.all, Sax.Readers.Schema_Validation_Feature)
            then
               Validation_Error
                 (Handler,
                  "schemaLocation for """
                  & Get (URI).all
                  & """ cannot occur after the first"
                  & " element of that namespace in XSD 1.0");
            end if;
         end;
      end if;

      if Debug then
         Output_Seen ("Parsing grammar: " & Get (S_File_Full).all);
      end if;

      Open (Get (S_File_Full).all, File);
      Set_Public_Id (File, Get (S_File_Full).all);
      Set_System_Id (File, Get (S_File_Full).all);

      --  Add_To will likely already contain the grammar for the
      --  schema-for-schema, and we won't have to recreate it in most cases.

      Set_Symbol_Table (Schema, Get_Symbol_Table (Handler.all));
      Set_Grammar (Schema, Handler.Grammar);
      Use_Basename_In_Error_Messages
        (Schema, Use_Basename_In_Error_Messages (Handler.all));

      Set_Feature
        (Schema,
         Sax.Readers.Schema_Validation_Feature,
         Get_Feature (Handler.all, Sax.Readers.Schema_Validation_Feature));

      if Handler.all in Schema_Reader'Class then
         Schema.Shared := Schema_Reader (Handler.all).Shared;
         Need_To_Initialize := False;
      end if;

      begin
         Internal_Parse
           (Schema, File,
            Default_Namespace    => URI,
            Do_Initialize_Shared => Need_To_Initialize,
            Do_Create_NFA        => Need_To_Initialize and Do_Create_NFA);
      exception
         when XML_Not_Implemented | XML_Validation_Error =>
            --  Copy the error message and location from Schema to Handler
            Close (File);
            Handler.Error_Msg := Schema.Error_Msg;
            Handler.Error_Location := Schema.Error_Location;
            raise;
      end;

      Free (Schema);
      Close (File);

      if Debug then
         Output_Seen ("Done parsing new grammar: " & Get (Xsd_File).all);
      end if;

      GNAT.Task_Lock.Unlock;

   exception
      when Ada.IO_Exceptions.Name_Error =>
         Free (Schema);
         Close (File);
         GNAT.Task_Lock.Unlock;

         if Debug then
            Debug_Output
              (ASCII.LF
               & "!!!! Could not open file " & Get (S_File_Full).all
               & ASCII.LF);
         end if;

         --  According to XML Schema Primer 0, section 5.6, this is not an
         --  error when we do not find the schema, since this attribute is only
         --  a hint.
         Warning
           (Handler.all,
            Create (Message => "Could not open file " & Get (S_File_Full).all,
                    Loc     => Handler.Current_Location));

      when others =>
         GNAT.Task_Lock.Unlock;
         Close (File);
         raise;
   end Parse_Grammar_If_Needed;

   --------------------
   -- Internal_Parse --
   --------------------

   procedure Internal_Parse
     (Parser            : in out Schema_Reader;
      Input             : in out Input_Sources.Input_Source'Class;
      Default_Namespace : Symbol;
      Do_Create_NFA     : Boolean;
      Do_Initialize_Shared : Boolean)
   is
      Grammar : constant XML_Grammar := Get_Grammar (Parser);
      URI     : Symbol;
   begin
      if Debug then
         Output_Action
           ("Parsing schema " & Input_Sources.Get_System_Id (Input));
      end if;

      Initialize_Symbols (Parser);

      URI := Find_Symbol (Parser, Input_Sources.Get_System_Id (Input));

      if not URI_Was_Parsed (Grammar, URI) then

         if Do_Initialize_Shared then
            Parser.Shared := new XSD_Data;
            Init (Parser.Shared.Types);
         end if;

         Initialize_Grammar (Parser);

         Parser.Target_NS := Default_Namespace;

         Set_Grammar (Parser, Grammar); --  In case it was not initialized yet
         Set_Parsed_URI (Parser, URI);

         Schema.Readers.Parse (Validating_Reader (Parser), Input);

         if Do_Create_NFA then
            Create_NFA (Parser'Access);
         end if;

         if Do_Initialize_Shared then
            Free (Parser.Shared);
         end if;

         Unchecked_Free (Parser.Contexts);
      end if;

   exception
      when others =>
         Unchecked_Free (Parser.Contexts);

         if Do_Initialize_Shared then
            Free (Parser.Shared);
         end if;

         raise;
   end Internal_Parse;

   -----------
   -- Parse --
   -----------

   procedure Parse
     (Parser : in out Schema_Reader;
      Input  : in out Input_Sources.Input_Source'Class) is
   begin
      Internal_Parse
        (Parser,
         Input,
         Default_Namespace => Empty_String,
         Do_Create_NFA     => True,
         Do_Initialize_Shared => True);
   end Parse;

   -------------------
   -- Resolve_QName --
   -------------------

   function Resolve_QName
     (Handler     : access Schema_Reader'Class;
      QName       : Sax.Symbols.Symbol;
      NS_If_Empty : Sax.Symbols.Symbol := Empty_String;
      Loc         : Location) return Qualified_Name
   is
      Val       : Cst_Byte_Sequence_Access;
      Separator : Integer;
      NS        : XML_NS;
      Prefix    : Symbol;
   begin
      if QName = No_Symbol then
         return No_Qualified_Name;
      else
         Val       := Get (QName);
         Separator := Split_Qname (Val.all);

         Prefix :=
           Find_Symbol (Handler.all, Val (Val'First .. Separator - 1));
         Get_Namespace_From_Prefix
           (Handler  => Handler.all,
            Prefix   => Prefix,
            NS       => NS);

         if NS = No_XML_NS then
            if Prefix /= Empty_String then
               Validation_Error
                 (Handler,
                  "Cannot resolve namespace prefix "
                  & Val (Val'First .. Separator - 1),
                  Loc);
               return No_Qualified_Name;
            else
               return
                 (NS    => NS_If_Empty,
                  Local =>
                   Find_Symbol (Handler.all, Val (Separator + 1 .. Val'Last)));
            end if;

         else
            return
              (NS    => Get_URI (NS),
               Local =>
                 Find_Symbol (Handler.all, Val (Separator + 1 .. Val'Last)));
         end if;
      end if;
   end Resolve_QName;

   ----------------
   -- Get_Occurs --
   ----------------

   procedure Get_Occurs
     (Handler : access Schema_Reader'Class;
      Atts    : Sax_Attribute_List;
      Min_Occurs, Max_Occurs : out Occurrences)
   is
      Min_Occurs_Index : constant Integer :=
        Get_Index (Atts, URI => Empty_String, Local_Name => Handler.MinOccurs);
      Max_Occurs_Index : constant Integer :=
        Get_Index (Atts, URI => Empty_String, Local_Name => Handler.MaxOccurs);

      function Occurs_From_Value (Index  : Integer) return Occurrences;
      --  Return the value of maxOccurs from the attributes'value. This
      --  properly takes into account the "unbounded" case

      function Occurs_From_Value (Index  : Integer) return Occurrences is
         Value : constant Symbol := Get_Value (Atts, Index);
      begin
         if Value = Handler.Unbounded then
            return (Unbounded => True);
         else
            declare
               Val : constant Cst_Byte_Sequence_Access := Get (Value);
               Pos : Integer;
               C   : Unicode_Char;
            begin
               return (Unbounded => False, Value => Natural'Value (Val.all));
            exception
               when Constraint_Error =>
                  --  Either we have an integer too big to fit in Integer, or
                  --  we do not have an integer at all
                  Pos := Val'First;
                  while Pos <= Val'Last loop
                     Encoding.Read (Val.all, Pos, C);
                     if not Is_Digit (C) then
                        Validation_Error
                          (Handler, "Value for ""maxOccurs"" must"
                           & " be an integer or ""unbounded""");
                     end if;
                  end loop;

                  return (Unbounded => False, Value => Natural'Last);
            end;
         end if;
      end Occurs_From_Value;

   begin
      Min_Occurs := (False, 1);
      Max_Occurs := (False, 1);

      if Min_Occurs_Index /= -1 then
         Min_Occurs := Occurs_From_Value (Min_Occurs_Index);
         if Min_Occurs.Unbounded then
            Validation_Error
              (Handler, "minOccurs cannot be ""unbounded""");
         end if;
      end if;

      if Max_Occurs_Index /= -1 then
         Max_Occurs := Occurs_From_Value (Max_Occurs_Index);
      end if;

      if not Max_Occurs.Unbounded then
         if Max_Occurs.Value > Max_Max_Occurs then
            Validation_Error
              (Handler,
               "maxOccurs is too big (XML/Ada supports up to"
                  & Max_Max_Occurs'Img & "), consider using ""unbounded""",
               Except => XML_Limitation'Identity);

         elsif Max_Occurs.Value > Max_Occurs_Warn then
            Handler.Warning
              (Create
                 (Message =>
                    "maxOccurs is big and could result in a very large state"
                    & " machine. Consider using ""unbounded"" instead",
                  Loc     => Handler.Current_Location));
         end if;
      end if;
   end Get_Occurs;

   ------------------
   -- Push_Context --
   ------------------

   procedure Push_Context
     (Handler : access Schema_Reader'Class; Ctx : Context)
   is
      Tmp : Context_Array_Access;
   begin
      if Handler.Contexts_Last = 0 then
         Handler.Contexts := new Context_Array (1 .. Default_Contexts);
      elsif Handler.Contexts_Last = Handler.Contexts'Last then
         Tmp := new Context_Array (1 .. Handler.Contexts'Last + 30);
         Tmp (Handler.Contexts'Range) := Handler.Contexts.all;
         Unchecked_Free (Handler.Contexts);
         Handler.Contexts := Tmp;
      end if;

      Handler.Contexts_Last := Handler.Contexts_Last + 1;
      Handler.Contexts (Handler.Contexts_Last) := Ctx;
   end Push_Context;

   ------------------
   -- Create_Group --
   ------------------

   procedure Create_Group
     (Handler : access Schema_Reader'Class;
      Atts    : Sax_Attribute_List)
   is
      Min_Occurs, Max_Occurs : Occurrences := (False, 1);
      Group   : Group_Descr;
      Name    : Qualified_Name;
      Details : Type_Details_Access;
   begin
      Group.Loc := Handler.Current_Location;

      for J in 1 .. Get_Length (Atts) loop
         Name := Get_Name (Atts, J);
         if Name.NS = Empty_String then
            if Name.Local = Handler.Name then
               Group.Name := (NS    => Handler.Target_NS,
                              Local => Get_Value (Atts, J));
            elsif Name.Local = Handler.Ref then
               Group.Ref := Resolve_QName
                 (Handler, Get_Value (Atts, J),
                  Loc => Get_Location (Atts, J));
            end if;
         end if;
      end loop;

      case Handler.Contexts (Handler.Contexts_Last).Typ is
         when Context_Schema | Context_Redefine =>
            null;

         when Context_Sequence | Context_Choice | Context_Extension
            | Context_Restriction =>
            Get_Occurs (Handler, Atts, Min_Occurs, Max_Occurs);
            Details := new Type_Details'
              (Kind       => Type_Group,
               Min_Occurs => Min_Occurs,
               Max_Occurs => Max_Occurs,
               Loc        => Handler.Current_Location,
               In_Process => False,
               Next       => null,
               Group      => Group);
            Insert_In_Type (Handler, Details);

         when others =>
            Validation_Error
              (Handler,
               "Unsupported ""group"" in this context",
              Except => XML_Not_Implemented'Identity);
      end case;

      Push_Context (Handler,
                    (Typ   => Context_Group,
                     Group => Group));

   end Create_Group;

   ------------------
   -- Finish_Group --
   ------------------

   procedure Finish_Group (Handler : access Schema_Reader'Class) is
      Ctx : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last)'Access;
      Next : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last - 1)'Access;
   begin
      case Next.Typ is
         when Context_Schema | Context_Redefine =>
            Set (Handler.Shared.Global_Groups, Ctx.Group.Name, Ctx.Group);
         when others =>
            null;
      end case;
   end Finish_Group;

   ----------------------------
   -- Create_Attribute_Group --
   ----------------------------

   procedure Create_Attribute_Group
     (Handler : access Schema_Reader'Class;
      Atts    : Sax_Attribute_List)
   is
      Group : AttrGroup_Descr;
      Name  : Qualified_Name;
   begin
      for J in 1 .. Get_Length (Atts) loop
         Name := Get_Name (Atts, J);
         if Name.NS = Empty_String then
            if Name.Local = Handler.Name then
               Group.Name := (NS    => Handler.Target_NS,
                              Local => Get_Value (Atts, J));
            elsif Name.Local = Handler.Ref then
               Group.Ref := Resolve_QName
                 (Handler, Get_Value (Atts, J),
                  Loc => Get_Location (Atts, J));
            end if;
         end if;
      end loop;

      Push_Context
        (Handler, (Typ        => Context_Attribute_Group,
                   Attr_Group => Group));
   end Create_Attribute_Group;

   ----------------------------
   -- Finish_Attribute_Group --
   ----------------------------

   procedure Finish_Attribute_Group (Handler : access Schema_Reader'Class) is
      Ctx : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last)'Access;
      Next : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last - 1)'Access;
      Ctx2 : Context_Access;
      Index : Natural;
   begin
      case Next.Typ is
         when Context_Schema | Context_Redefine =>
            Set
              (Handler.Shared.Global_AttrGroups,
               Ctx.Attr_Group.Name, Ctx.Attr_Group);
         when Context_Type_Def =>
            pragma Assert (Ctx.Attr_Group.Attributes = null);
            Append
              (Handler.Shared.Types.Table (Next.Type_Info).Attributes,
               (Kind      => Kind_Group,
                Loc       => Handler.Current_Location,
                Group_Ref => Ctx.Attr_Group.Ref));
         when Context_Extension | Context_Simple_Extension =>
            pragma Assert (Ctx.Attr_Group.Attributes = null);
            Index := Handler.Contexts_Last - 1;
            while Index >= Handler.Contexts'First loop
               Ctx2 := Handler.Contexts (Index)'Access;
               if Ctx2.Typ = Context_Type_Def then
                  Append
                    (Handler.Shared.Types.Table (Ctx2.Type_Info).Attributes,
                     (Kind      => Kind_Group,
                      Loc       => Handler.Current_Location,
                      Group_Ref => Ctx.Attr_Group.Ref));
                  exit;
               end if;

               Index := Index - 1;
            end loop;
         when Context_Attribute_Group =>
            pragma Assert (Ctx.Attr_Group.Attributes = null);
            Append
              (Next.Attr_Group.Attributes,
               (Kind      => Kind_Group,
                Loc       => Handler.Current_Location,
                Group_Ref => Ctx.Attr_Group.Ref));

         when others =>
            Unchecked_Free (Ctx.Attr_Group.Attributes);
            Validation_Error
              (Handler,
               "Invalid context for attributeGroup: " & Next.Typ'Img,
               Except => XML_Not_Implemented'Identity);
      end case;
   end Finish_Attribute_Group;

   ------------
   -- Append --
   ------------

   procedure Append (List : in out Attr_Array_Access; Attr : Attr_Descr) is
      Tmp : Attr_Array_Access;
   begin
      if List = null then
         List := new Attr_Array'(1 => Attr,
                                 2 .. 10 => (Kind => Kind_Unset,
                                             Loc  => No_Location));

      elsif List (List'Last).Kind /= Kind_Unset then
         Tmp := new Attr_Array (1 .. List'Last + 10);
         Tmp (List'Range) := List.all;
         Tmp (List'Last + 1) := Attr;
         Tmp (List'Last + 2 .. Tmp'Last) :=
           (others => Attr_Descr'(Kind => Kind_Unset, Loc => No_Location));
         Unchecked_Free (List);
         List := Tmp;

      else
         for L in List'Range loop
            if List (L).Kind = Kind_Unset then
               List (L) := Attr;
               return;
            end if;
         end loop;
      end if;
   end Append;

   --------------------
   -- Create_Include --
   --------------------

   procedure Create_Include
     (Handler : access Schema_Reader'Class;
      Atts    : Sax_Attribute_List)
   is
      Schema_Location_Index : constant Integer :=
        Get_Index (Atts, Empty_String, Handler.Schema_Location);
   begin
      Parse_Grammar
        (Handler,
         URI      => Handler.Target_NS,
         Xsd_File => Get_Value (Atts, Schema_Location_Index),
         Do_Create_NFA => False);  --  Will be performed later
   end Create_Include;

   ---------------------
   -- Create_Redefine --
   ---------------------

   procedure Create_Redefine
     (Handler : access Schema_Reader'Class;
      Atts    : Sax_Attribute_List)
   is
      Location_Index : constant Integer :=
        Get_Index (Atts, Empty_String, Handler.Schema_Location);
   begin
      --  Disable for now.
      --  On the test./testschema -xsd boeingData/ipo4/ipo.xsd
      --    -xsd boeingData/ipo4/address.xsd
      --    -xsd boeingData/ipo4/itematt.xsd
      --    boeingData/ipo4/ipo_1.xml
      --  we redefine an extension whose base type comes from the redefined
      --  grammar, and whose name is the same. As a result, the extension and
      --  its base type end up being the same XML_Type, and thus we get
      --  infinite loops. We should really merge the models when the grammar is
      --  parsed.

      Validation_Error
        (Handler,
         "<redefine> not supported",
         Except => XML_Not_Implemented'Identity);
      Parse_Grammar
        (Handler,
         URI      => Handler.Target_NS,
         Do_Create_NFA => True,
         Xsd_File => Get_Value (Atts, Location_Index));

      Push_Context (Handler, (Typ => Context_Redefine));
   end Create_Redefine;

   -------------------
   -- Create_Import --
   -------------------

   procedure Create_Import
     (Handler : access Schema_Reader'Class;
      Atts    : Sax_Attribute_List)
   is
      Location_Index : constant Integer :=
        Get_Index (Atts, Empty_String, Handler.Schema_Location);
      --  Namespace_Index : constant Integer :=
      --    Get_Index (Atts, Empty_String, Handler.Namespace);
   begin
      if Location_Index = -1 then
         --  if Namespace_Index = -1 then
         --     --  See 4.2.6.1: If that attribute is absent, then the import
         --     --  allows unqualified reference to components with no target
         --     --  namespace
         --     null;
         --  end if;

         Validation_Error
           (Handler,
            "Import with no schemaLocation is unsupported",
            Except => XML_Not_Implemented'Identity);

      else
         declare
            Location : constant Symbol := Get_Value (Atts, Location_Index);
         begin
            if Debug then
               Debug_Output ("Import: " & Get (Location).all);
               Debug_Output ("Adding new grammar to Handler.Created_Grammar");
            end if;

            --  The namespace attribute indicates that the XSD may contain
            --  qualified references to schema components in that namespace.
            --  (4.2.6.1). It does not give the default targetNamespace

            Parse_Grammar
              (Handler,
               URI      => Empty_String,
               Do_Create_NFA => True,
               Xsd_File => Location);
         end;
      end if;
   end Create_Import;

   --------------------------
   -- Create_Any_Attribute --
   --------------------------

   procedure Create_Any_Attribute
     (Handler : access Schema_Reader'Class;
      Atts    : Sax_Attribute_List)
   is
      Name  : Qualified_Name;
      Att   : Attr_Descr (Kind => Kind_Attribute);
   begin
      Att.Loc := Handler.Current_Location;
      Att.Attr.Any := (Namespaces       => Handler.Any_Namespace,
                       Target_NS        => Handler.Target_NS,
                       Process_Contents => Process_Strict);

      for J in 1 .. Get_Length (Atts) loop
         Name := Get_Name (Atts, J);
         if Name.NS = Empty_String then
            if Name.Local = Handler.Namespace then
               Att.Attr.Any.Namespaces := Get_Value (Atts, J);
            elsif Name.Local = Handler.Process_Contents then
               Att.Attr.Any.Process_Contents :=
                 Process_Contents_From_Atts (Handler, Atts, J);
            end if;
         end if;
      end loop;

      Insert_Attribute (Handler, Handler.Contexts_Last, Att);
   end Create_Any_Attribute;

   ---------------------
   -- Create_Notation --
   ---------------------

   procedure Create_Notation
     (Handler : access Schema_Reader'Class; Atts : Sax_Attribute_List)
   is
      type Notation_Descr is record
         Name      : Symbol;
         System_Id : Symbol := Empty_String;
         Public_Id : Symbol := Empty_String;
      end record;

      Name     : Qualified_Name;
      Notation : Notation_Descr;

   begin
      for J in 1 .. Get_Length (Atts) loop
         Name := Get_Name (Atts, J);
         if Name.NS = Empty_String then
            if Name.Local = Handler.Name then
               Notation.Name := Get_Value (Atts, J);
            elsif Name.Local = Handler.Public then
               Notation.Public_Id := Get_Value (Atts, J);
            elsif Name.Local = Handler.System then
               Notation.System_Id := Get_Value (Atts, J);
            end if;
         end if;
      end loop;

      Add_Notation (Get_NFA (Handler.Grammar), Notation.Name);
      Notation_Decl
        (Sax_Reader'Class (Handler.all),
         Name      => Get (Notation.Name).all,
         System_Id => Get (Notation.System_Id).all,
         Public_Id => Get (Notation.Public_Id).all);
   end Create_Notation;

   --------------------
   -- Create_Element --
   --------------------

   procedure Create_Element
     (Handler : access Schema_Reader'Class;
      Atts    : Sax_Attribute_List)
   is
      Min_Occurs, Max_Occurs : Occurrences := (False, 1);
      Info    : Element_Descr;
      Name    : Qualified_Name;
      Details : Type_Details_Access;

   begin
      Info.Loc   := Handler.Current_Location;
      Info.Form  := Handler.Element_Form_Default;
      Info.Block := Handler.Target_Block_Default;

      for J in 1 .. Get_Length (Atts) loop
         Name := Get_Name (Atts, J);
         if Name.NS = Empty_String then
            if Name.Local = Handler.Typ then
               Info.Typ := Resolve_QName
                 (Handler, Get_Value (Atts, J),
                  NS_If_Empty => Handler.Target_NS,
                  Loc         => Get_Location (Atts, J));
            elsif Name.Local = Handler.Name then
               Info.Name := (NS    => Handler.Target_NS,
                             Local => Get_Value (Atts, J));
            elsif Name.Local = Handler.Ref then
               Info.Ref := Resolve_QName
                 (Handler, Get_Value (Atts, J),
                  Loc => Get_Location (Atts, J));
            elsif Name.Local = Handler.Substitution_Group then
               Info.Substitution_Group :=
                 Resolve_QName
                   (Handler, Get_Value (Atts, J),
                    Loc => Get_Location (Atts, J));
            elsif Name.Local = Handler.Default then
               Info.Default := Get_Value (Atts, J);
            elsif Name.Local = Handler.Fixed then
               Info.Fixed := Get_Value (Atts, J);
            elsif Name.Local = Handler.S_Abstract then
               Info.Is_Abstract := Get_Value_As_Boolean (Atts, J, False);
            elsif Name.Local = Handler.Nillable then
               Info.Nillable := Get_Value_As_Boolean (Atts, J, False);
            elsif Name.Local = Handler.Form then
               Info.Form := Compute_Form (Atts, Handler, J);
            elsif Name.Local = Handler.Final then
               Info.Final := Compute_Final (Atts, Handler, J);
            elsif Name.Local = Handler.Block then
               Compute_Blocks (Atts, Handler, Info.Block, Info.Has_Block, J);
            end if;
         end if;
      end loop;

      if Info.Name /= No_Qualified_Name then
         if Info.Ref /= No_Qualified_Name
           and then Info.Ref.NS = No_Symbol
           and then Info.Name = Info.Ref
           and then not In_Redefine_Context (Handler.all)
         then
            Validation_Error
              (Handler, """ref"" attribute cannot be self-referencing");

         elsif Info.Ref /= No_Qualified_Name then
            Validation_Error
              (Handler, "Name and Ref cannot be both specified");
         end if;

      elsif Info.Ref = No_Qualified_Name then
         Validation_Error
           (Handler, "Either ""name"" or ""ref"" attribute must be present");

      else
         --  Section 3.3.2, validity constraints 3.3.3
         if Info.Typ /= No_Qualified_Name then
            Validation_Error
              (Handler,
               """type"" attribute cannot be specified along with ""ref""");
         end if;
      end if;

      if Info.Default /= No_Symbol and then Info.Fixed /= No_Symbol then
         Validation_Error
           (Handler, "Default and Fixed cannot be both specified");
      end if;

      if Info.Ref /= No_Qualified_Name then
         Info.Form := Qualified;
      end if;

      if Handler.Contexts (Handler.Contexts_Last).Typ /= Context_Schema then
         Get_Occurs (Handler, Atts, Min_Occurs, Max_Occurs);
         Details := new Type_Details'
           (Kind         => Type_Element,
            Min_Occurs   => Min_Occurs,
            Max_Occurs   => Max_Occurs,
            Loc          => Handler.Current_Location,
            In_Process   => False,
            Next         => null,
            Element      => Info);
         Insert_In_Type (Handler, Details);
      end if;

      Push_Context
        (Handler,
         (Typ          => Context_Element,
          Elem_Details => Details,
          Element      => Info));
   end Create_Element;

   --------------------
   -- Finish_Element --
   --------------------

   procedure Finish_Element (Handler : access Schema_Reader'Class) is
      Ctx : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last)'Access;
      Next : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last - 1)'Access;
      Info : constant Element_Descr := Ctx.Element;
   begin
      case Next.Typ is
         when Context_Schema | Context_Redefine =>
            Set (Handler.Shared.Global_Elements, Info.Name, Info);
         when others =>
            --  We might have added the type definition
            Ctx.Elem_Details.Element := Ctx.Element;
      end case;
   end Finish_Element;

   ------------------------
   -- Create_Simple_Type --
   ------------------------

   procedure Create_Simple_Type
     (Handler : access Schema_Reader'Class;
      Atts    : Sax_Attribute_List)
   is
      Ctx : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last)'Access;
   begin
      Prepare_Type (Handler, Atts, Is_Simple => True);

      if Ctx.Typ = Context_Simple_Restriction then
         Ctx.Simple.Base :=
           (Name  => No_Qualified_Name,
            Local => Handler.Contexts (Handler.Contexts_Last).Type_Info);
      end if;
   end Create_Simple_Type;

   ---------------------
   -- Add_Type_Member --
   ---------------------

   procedure Add_Type_Member
     (Handler : access Schema_Reader'Class;
      List    : in out Type_Member_Array;
      Member  : Type_Member;
      Loc     : Location)
   is
   begin
      for A in List'Range loop
         if List (A) = No_Type_Member then
            List (A) := Member;
            return;
         end if;
      end loop;

      Validation_Error
        (Handler,
         "Too many types in the union", Loc,
         XML_Not_Implemented'Identity);
   end Add_Type_Member;

   ------------------------
   -- Finish_Simple_Type --
   ------------------------

   procedure Finish_Simple_Type (Handler : access Schema_Reader'Class) is
      Ctx : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last)'Access;
      Next : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last - 1)'Access;
   begin
      case Next.Typ is
         when Context_Schema | Context_Redefine => null;
         when Context_Element   => Next.Element.Local_Type   := Ctx.Type_Info;
         when Context_Attribute =>
            Next.Attribute.Attr.Local_Type := Ctx.Type_Info;
         when Context_List    =>
            Add_Type_Member (Handler, Next.List.List_Items,
              (Name => No_Qualified_Name,
               Local => Ctx.Type_Info),
              No_Location);
         when Context_Union   =>
            Add_Type_Member (Handler, Next.Union.Union_Items,
              (Name => No_Qualified_Name,
               Local => Ctx.Type_Info),
              No_Location);
         when Context_Restriction =>
            Next.Restriction.Restriction.Details :=
              Handler.Shared.Types.Table (Ctx.Type_Info).Details;
         when Context_Simple_Restriction =>
            --  The simpleType is in fact the base type of the restriction. The
            --  following was already done in Create_Simple_Type:
            --     Next.Simple.Base.Local := Ctx.Type_Info;
            null;
         when others          =>
            Validation_Error
              (Handler,
               "Unsupported: ""simpleType"" in this context",
               Except => XML_Not_Implemented'Identity);
      end case;
   end Finish_Simple_Type;

   --------------------
   -- Compute_Blocks --
   --------------------

   procedure Compute_Blocks
     (Atts    : Sax_Attribute_List;
      Handler : access Schema_Reader'Class;
      Blocks  : out Block_Status;
      Is_Set  : out Boolean;
      Index   : Integer)
   is
      procedure On_Item (Str : Byte_Sequence);
      procedure On_Item (Str : Byte_Sequence) is
      begin
         if Str = "restriction" then
            Blocks (Block_Restriction) := True;
         elsif Str = "extension" then
            Blocks (Block_Extension) := True;
         elsif Str = "substitution" then
            Blocks (Block_Substitution) := True;
         elsif Str = "#all" then
            Blocks := (others => True);
         else
            Validation_Error
              (Handler, "Invalid value for block: """ & Str & """");
         end if;
      end On_Item;

      procedure For_Each is new For_Each_Item (On_Item);
   begin
      Is_Set := Index /= -1;
      Blocks := No_Block;

      if Index /= -1 then
         For_Each (Get (Get_Value (Atts, Index)).all);

         if Debug then
            Output_Action ("Set_Block (" & To_String (Blocks) & ")");
         end if;
      end if;
   end Compute_Blocks;

   ------------------
   -- Compute_Form --
   ------------------

   function Compute_Form
     (Atts      : Sax_Attribute_List;
      Handler   : access Schema_Reader'Class;
      Index     : Integer) return Form_Type is
   begin
      if Get_Value (Atts, Index) = Handler.Qualified then
         return Qualified;
      else
         return Unqualified;
      end if;
   end Compute_Form;

   -------------------
   -- Compute_Final --
   -------------------

   function Compute_Final
     (Atts    : Sax_Attribute_List;
      Handler : access Schema_Reader'Class;
      Index   : Integer) return Final_Status
   is
      Final : Final_Status;

      procedure On_Item (Str : Byte_Sequence);
      procedure On_Item (Str : Byte_Sequence) is
      begin
         if Str = "restriction" then
            Final (Final_Restriction) := True;
         elsif Str = "extension" then
            Final (Final_Extension)   := True;
         elsif Str = "#all" then
            Final := (others => True);
         elsif Str = "union" then
            Final (Final_Union)       := True;
         elsif Str = "list" then
            Final (Final_List)        := True;
         else
            Validation_Error
              (Handler, "Invalid value for final: """ & Str & """");
         end if;
      end On_Item;

      procedure For_Each is new For_Each_Item (On_Item);
   begin
      Final := (others => False);

      if Index /= -1 then
         For_Each (Get (Get_Value (Atts, Index)).all);

         if Debug then
            Output_Action ("Set_Final (" & To_String (Final) & ")");
         end if;
      end if;

      return Final;
   end Compute_Final;

   ------------------
   -- Prepare_Type --
   ------------------

   procedure Prepare_Type
     (Handler   : access Schema_Reader'Class;
      Atts      : Sax_Attribute_List;
      Is_Simple : Boolean)
   is
      Info   : Internal_Type_Descr (Is_Simple => Is_Simple);
      Is_Set : Boolean;
      Name   : Qualified_Name;
      Props  : Type_Descr;

   begin
      Info.Loc := Handler.Current_Location;
      Props.Block := Handler.Target_Block_Default;

      for J in 1 .. Get_Length (Atts) loop
         Name := Get_Name (Atts, J);
         if Name.NS = Empty_String then
            if Name.Local = Handler.Mixed then
               Props.Mixed := Get_Value_As_Boolean (Atts, J, False);
            elsif Name.Local = Handler.Name then
               Props.Name :=
                 (NS    => Handler.Target_NS,
                  Local => Get_Value (Atts, J));
            elsif Name.Local = Handler.Block then
               Compute_Blocks (Atts, Handler, Props.Block, Is_Set, J);
            elsif Name.Local = Handler.Final then
               Props.Final := Compute_Final (Atts, Handler, J);
            elsif Name.Local = Handler.S_Abstract then
               Props.Is_Abstract := Get_Value_As_Boolean (Atts, J, False);
            end if;
         end if;
      end loop;

      --  block="substitution" does not apply to types, only to elements
      Props.Block (Block_Substitution) := False;

      Info.Properties := Props;

      Append (Handler.Shared.Types, Info);

      Push_Context
        (Handler,
         (Typ       => Context_Type_Def,
          Type_Info => Last (Handler.Shared.Types)));
   end Prepare_Type;

   -------------------------
   -- Create_Complex_Type --
   -------------------------

   procedure Create_Complex_Type
     (Handler  : access Schema_Reader'Class;
      Atts     : Sax_Attribute_List) is
   begin
      Prepare_Type (Handler, Atts, Is_Simple => False);
   end Create_Complex_Type;

   -------------------------
   -- Finish_Complex_Type --
   -------------------------

   procedure Finish_Complex_Type (Handler  : access Schema_Reader'Class) is
      Ctx : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last)'Access;
      Next : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last - 1)'Access;
   begin
      case Next.Typ is
         when Context_Element => Next.Element.Local_Type := Ctx.Type_Info;
         when others          => null;
      end case;
   end Finish_Complex_Type;

   ------------------------
   -- Create_Restriction --
   ------------------------

   procedure Create_Restriction
     (Handler  : access Schema_Reader'Class;
      Atts     : Sax_Attribute_List)
   is
      Ctx : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last)'Access;
      Restr      : Restriction_Descr;
      Details    : Type_Details_Access;
      Name       : Qualified_Name;
      In_Type    : constant Internal_Type_Index := Ctx.Type_Info;
   begin
      Restr.Loc := Handler.Current_Location;
      Restr.Base := (NS    => Handler.XML_Schema_URI,
                     Local => Handler.Any_Simple_Type);

      for J in 1 .. Get_Length (Atts) loop
         Name := Get_Name (Atts, J);
         if Name.NS = Empty_String then
            if Name.Local = Handler.Base then
               Restr.Base := Resolve_QName
                 (Handler, Get_Value (Atts, J), Handler.Target_NS,
                  Get_Location (Atts, J));
            end if;
         end if;
      end loop;

      if Handler.Shared.Types.Table (In_Type).Is_Simple
        or else Handler.Shared.Types.Table (In_Type).Simple.Kind /=
        Simple_Type_None
      then
         if Restr.Base = (NS => Handler.XML_Schema_URI, Local => Handler.IDREF)
           or else Restr.Base =
             (NS => Handler.XML_Schema_URI, Local => Handler.IDREFS)
         then
            Validation_Error
              (Handler,
               "Unsupported type IDREF and IDREFS",
               Except => XML_Not_Implemented'Identity);
         end if;

         if not Handler.Shared.Types.Table (In_Type).Is_Simple then
            Details := new Type_Details'
              (Kind        => Type_Restriction,
               Min_Occurs  => (False, 1),
               Max_Occurs  => (False, 1),
               Loc         => Handler.Current_Location,
               In_Process  => False,
               Next        => null,
               Simple_Content_Restriction => True,
               Restriction => Restr);
            Insert_In_Type (Handler, Details);
         end if;

         Push_Context
           (Handler,
            (Typ         => Context_Simple_Restriction,
             Simple      => (Kind             => Simple_Type_Restriction,
                             In_Process       => False,
                             Facets           => No_Facets,
                             Base =>
                               (Name  => Restr.Base,
                                Local => No_Internal_Type_Index),
                             Loc         => Handler.Current_Location)));

      else
         Details := new Type_Details'
           (Kind        => Type_Restriction,
            Min_Occurs  => (False, 1),
            Max_Occurs  => (False, 1),
            Loc         => Handler.Current_Location,
            In_Process  => False,
            Next        => null,
            Simple_Content_Restriction => False,
            Restriction => Restr);
         Insert_In_Type (Handler, Details);
         Push_Context
           (Handler,
            (Typ         => Context_Restriction,
             Restriction => Details));
      end if;
   end Create_Restriction;

   ------------------------
   -- Finish_Restriction --
   ------------------------

   procedure Finish_Restriction (Handler : access Schema_Reader'Class) is
      Ctx : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last)'Access;
      Next : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last - 1)'Access;
   begin
      if Ctx.Typ = Context_Simple_Restriction then
         pragma Assert (Next.Typ = Context_Type_Def);
         Handler.Shared.Types.Table (Next.Type_Info).Simple := Ctx.Simple;
      end if;
   end Finish_Restriction;

   ------------------
   -- Create_Union --
   ------------------

   procedure Create_Union
     (Handler  : access Schema_Reader'Class;
      Atts     : Sax_Attribute_List)
   is
      Name : Qualified_Name;

      procedure Add_Union (Str : Byte_Sequence);
      --  Add a unioned type to [Simple]

      procedure Add_Union (Str : Byte_Sequence) is
         Sym  : constant Symbol := Find_Symbol (Handler.all, Str);
         Ctx : constant Context_Access :=
           Handler.Contexts (Handler.Contexts_Last)'Access;
         Name : constant Qualified_Name :=
           Resolve_QName (Handler, Sym, Handler.Target_NS,
                          Loc => Ctx.Union.Loc);
      begin
         Add_Type_Member
           (Handler,
            Ctx.Union.Union_Items,
            (Name => Name, Local => No_Internal_Type_Index),
            Ctx.Union.Loc);
      end Add_Union;

      procedure For_Each_Union is new For_Each_Item (Add_Union);

   begin
      Push_Context
        (Handler,
         (Typ   => Context_Union,
          Union => (Kind             => Simple_Type_Union,
                    In_Process       => False,
                    Loc              => Handler.Current_Location,
                    Union_Items      => (others => No_Type_Member))));

      for J in 1 .. Get_Length (Atts) loop
         Name := Get_Name (Atts, J);
         if Name.NS = Empty_String then
            if Name.Local = Handler.Member_Types then
               For_Each_Union (Get (Get_Value (Atts, J)).all);
            end if;
         end if;
      end loop;
   end Create_Union;

   ------------------
   -- Finish_Union --
   ------------------

   procedure Finish_Union (Handler : access Schema_Reader'Class) is
      Ctx : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last)'Access;
      Next : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last - 1)'Access;
   begin
      case Next.Typ is
         when Context_Type_Def =>
            Handler.Shared.Types.Table (Next.Type_Info).Simple := Ctx.Union;
         when others =>
            Validation_Error
              (Handler,
               "Unsupported: ""union"" in this context",
               Except => XML_Not_Implemented'Identity);
      end case;
   end Finish_Union;

   ----------------------
   -- Create_Extension --
   ----------------------

   procedure Create_Extension
     (Handler  : access Schema_Reader'Class;
      Atts     : Sax_Attribute_List)
   is
      Ctx : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last)'Access;
      Ext   : Extension_Descr;
      Name  : Qualified_Name;
      Details : Type_Details_Access;
      In_Type : constant Internal_Type_Index := Ctx.Type_Info;
   begin
      Ext.Loc := Handler.Current_Location;

      for J in 1 .. Get_Length (Atts) loop
         Name := Get_Name (Atts, J);
         if Name.NS = Empty_String then
            if Name.Local = Handler.Base then
               Ext.Base := Resolve_QName
                 (Handler, Get_Value (Atts, J), Handler.Target_NS,
                  Loc => Get_Location (Atts, J));
            end if;
         end if;
      end loop;

      if Ext.Base = No_Qualified_Name then
         Validation_Error
           (Handler, "Attribute ""base"" required for <extensionType>");
      end if;

      if Handler.Shared.Types.Table (In_Type).Is_Simple
        or else Handler.Shared.Types.Table (In_Type).Simple.Kind /=
           Simple_Type_None
      then
         if Debug then
            Debug_Output ("Create extension: in simpleContent or simpleType");
         end if;

         if not Handler.Shared.Types.Table (In_Type).Is_Simple then
            Details := new Type_Details'
              (Kind       => Type_Extension,
               Min_Occurs => (False, 1),
               Max_Occurs => (False, 1),
               Loc        => Handler.Current_Location,
               In_Process => False,
               Next       => null,
               Simple_Content => True,
               Extension  => Ext);
            Insert_In_Type (Handler, Details);
         end if;

         Push_Context
           (Handler,
            (Typ    => Context_Simple_Extension,
             Simple => (Kind             => Simple_Type_Extension,
                        In_Process       => False,
                        Base             => (Name  => Ext.Base,
                                             Local => No_Internal_Type_Index),
                        Facets           => No_Facets,
                        Loc              => Handler.Current_Location)));
      else
         Details := new Type_Details'
           (Kind       => Type_Extension,
            Min_Occurs => (False, 1),
            Max_Occurs => (False, 1),
            Loc        => Handler.Current_Location,
            In_Process => False,
            Next       => null,
            Simple_Content => False,
            Extension  => Ext);
         Insert_In_Type (Handler, Details);
         Push_Context
           (Handler,
            (Typ       => Context_Extension,
             Extension => Details));
      end if;
   end Create_Extension;

   ----------------------
   -- Finish_Extension --
   ----------------------

   procedure Finish_Extension (Handler : access Schema_Reader'Class) is
      Ctx : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last)'Access;
      Next : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last - 1)'Access;
   begin
      if Ctx.Typ = Context_Simple_Extension then
         pragma Assert (Next.Typ = Context_Type_Def);  --  a simple type
         Handler.Shared.Types.Table (Next.Type_Info).Simple := Ctx.Simple;
      end if;
   end Finish_Extension;

   -----------------
   -- Create_List --
   -----------------

   procedure Create_List
     (Handler : access Schema_Reader'Class;
      Atts    : Sax_Attribute_List)
   is
      Name  : Qualified_Name;
   begin
      Push_Context
        (Handler,
         (Typ   => Context_List,
          List  => (Kind       => Simple_Type_List,
                    In_Process => False,
                    Loc        => Handler.Current_Location,
                    List_Items => (others => No_Type_Member))));

      for J in 1 .. Get_Length (Atts) loop
         Name := Get_Name (Atts, J);
         if Name.NS = Empty_String then
            if Name.Local = Handler.Item_Type then
               Name := Resolve_QName
                 (Handler, Get_Value (Atts, J), Handler.Target_NS,
                  Loc => Get_Location (Atts, J));
               Add_Type_Member
                 (Handler,
                  Handler.Contexts (Handler.Contexts_Last).List.List_Items,
                  (Name => Name, Local => No_Internal_Type_Index),
                  Handler.Current_Location);
            end if;
         end if;
      end loop;
   end Create_List;

   -----------------
   -- Finish_List --
   -----------------

   procedure Finish_List (Handler : access Schema_Reader'Class) is
      Ctx : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last)'Access;
      Next : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last - 1)'Access;
      Next_Next : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last - 2)'Access;
   begin
      case Next.Typ is
         when Context_Type_Def =>
            if Next.Type_Info = No_Internal_Type_Index then
               --  within a <simpleType><restriction><simpleType><list>
               pragma Assert (Next_Next.Typ = Context_Simple_Restriction);
               Next_Next.Simple := Ctx.List;

            else
               --  within a <simpleType><list>
               pragma Assert (Next.Type_Info /= No_Internal_Type_Index);

               Handler.Shared.Types.Table (Next.Type_Info).Simple := Ctx.List;
            end if;
         when others =>
            Validation_Error
              (Handler,
               "Unsupported: ""list"" in this context",
               Except => XML_Not_Implemented'Identity);
      end case;
   end Finish_List;

   --------------------
   -- Insert_In_Type --
   --------------------

   procedure Insert_In_Type
     (Handler : access Schema_Reader'Class;
      Element : in out Type_Details_Access)
   is
      procedure Append (List : in out Type_Details_Access;
                        Elem : Type_Details_Access);
      procedure Append (List : in out Type_Details_Access;
                        Elem : Type_Details_Access)
      is
         Tmp  : Type_Details_Access;
      begin
         if List = null then
            List := Elem;
         else
            Tmp := List;
            while Tmp.Next /= null loop
               Tmp := Tmp.Next;
            end loop;

            Tmp.Next := Elem;
         end if;
      end Append;

      Ctx : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last)'Access;

   begin
      case Ctx.Typ is
         when Context_Type_Def =>
            if Handler.Shared.Types.Table (Ctx.Type_Info).Is_Simple then
               Free (Element);
               Validation_Error
                 (Handler, "Invalid element in simple type");
            end if;

            if Debug and then
              Handler.Shared.Types.Table (Ctx.Type_Info).Details /= null
            then
               Debug_Output ("Insert_In_Type, type already has details "
                             & " when inserting " & Element.Kind'Img);
            end if;

            pragma Assert
              (Handler.Shared.Types.Table (Ctx.Type_Info).Details = null);

            Handler.Shared.Types.Table (Ctx.Type_Info).Details := Element;

         when Context_Sequence =>
            Append (Ctx.Seq.First_In_Seq, Element);

         when Context_Choice =>
            Append (Ctx.Choice.First_In_Choice, Element);

         when Context_All =>
            Append (Ctx.All_Detail.First_In_All, Element);

         when Context_Group =>
            if Ctx.Group.Details /= null then
               Free (Element);
               Validation_Error
                 (Handler, "Invalid element in non group");
            end if;

            Ctx.Group.Details := Element;

         when Context_Extension =>
            if Ctx.Extension.Extension.Details /= null then
               Free (Element);
               Validation_Error
                 (Handler, "Invalid element in non-empty extension");
            end if;

            Ctx.Extension.Extension.Details := Element;

         when Context_Restriction =>
            if Ctx.Restriction.Restriction.Details /= null then
               Free (Element);
               Validation_Error
                 (Handler, "Invalid element in non-empty restriction");
            end if;
            Ctx.Restriction.Restriction.Details := Element;

         when Context_Simple_Restriction | Context_Simple_Extension =>
            Free (Element);

         when Context_Schema | Context_Attribute | Context_Element
            | Context_Union
            | Context_List | Context_Redefine | Context_Attribute_Group =>

            Free (Element);
            Validation_Error
              (Handler,
               "Unsupported: """ & Element.Kind'Img & """ in context "
               & Ctx.Typ'Img,
               Except => XML_Not_Implemented'Identity);
      end case;
   end Insert_In_Type;

   -------------------
   -- Create_Choice --
   -------------------

   procedure Create_Choice
     (Handler : access Schema_Reader'Class;
      Atts : Sax_Attribute_List)
   is
      Min_Occurs, Max_Occurs : Occurrences := (False, 1);
      Choice  : Type_Details_Access;
   begin
      Get_Occurs (Handler, Atts, Min_Occurs, Max_Occurs);
      Choice := new Type_Details'
        (Kind            => Type_Choice,
         Min_Occurs      => Min_Occurs,
         Max_Occurs      => Max_Occurs,
         Loc             => Handler.Current_Location,
         In_Process      => False,
         Next            => null,
         First_In_Choice => null);
      Insert_In_Type (Handler, Choice);
      Push_Context
        (Handler,
         (Typ    => Context_Choice,
          Choice => Choice));
   end Create_Choice;

   ---------------------
   -- Create_Sequence --
   ---------------------

   procedure Create_Sequence
     (Handler  : access Schema_Reader'Class;
      Atts     : Sax_Attribute_List)
   is
      Min_Occurs, Max_Occurs : Occurrences := (False, 1);
      Seq  : Type_Details_Access;
   begin
      Get_Occurs (Handler, Atts, Min_Occurs, Max_Occurs);
      Seq := new Type_Details'
        (Kind         => Type_Sequence,
         Min_Occurs   => Min_Occurs,
         Max_Occurs   => Max_Occurs,
         Loc          => Handler.Current_Location,
         In_Process   => False,
         Next         => null,
         First_In_Seq => null);
      Insert_In_Type (Handler, Seq);
      Push_Context
        (Handler,
         (Typ  => Context_Sequence,
          Seq  => Seq));
   end Create_Sequence;

   ----------------------
   -- Create_Attribute --
   ----------------------

   procedure Create_Attribute
     (Handler  : access Schema_Reader'Class;
      Atts     : Sax_Attribute_List)
   is
      Name  : Qualified_Name;
      Att   : Attr_Descr (Kind => Kind_Attribute);
      Ctx : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last)'Access;
      Has_Form : Boolean := False;

   begin
      Att.Attr.Descr.Form := Handler.Attribute_Form_Default;
      Att.Loc := Handler.Current_Location;

      for J in 1 .. Get_Length (Atts) loop
         Name := Get_Name (Atts, J);
         if Name.NS = Empty_String then
            if Name.Local = Handler.Name then
               Att.Attr.Descr.Name := (NS    => Handler.Target_NS,
                                       Local => Get_Value (Atts, J));
            elsif Name.Local = Handler.Typ then
               Att.Attr.Typ  := Resolve_QName
                 (Handler, Get_Value (Atts, J),
                  Loc => Get_Location (Atts, J));

               if Att.Attr.Typ =
                 (NS => Handler.XML_Schema_URI, Local => Handler.IDREF)
                 or else Att.Attr.Typ =
                   (NS => Handler.XML_Schema_URI, Local => Handler.IDREFS)
               then
                  Validation_Error
                    (Handler,
                     "Unsupported type IDREF and IDREFS",
                     Get_Location (Atts, J),
                     Except => XML_Not_Implemented'Identity);
               end if;

            elsif Name.Local = Handler.S_Use then
               if Get_Value (Atts, J) = Handler.Required then
                  Att.Attr.Descr.Use_Type := Required;
               elsif Get_Value (Atts, J) = Handler.Prohibited then
                  Att.Attr.Descr.Use_Type := Prohibited;
               else
                  Att.Attr.Descr.Use_Type := Optional;
               end if;

            elsif Name.Local = Handler.Fixed then
               Att.Attr.Descr.Fixed := Get_Value (Atts, J);
            elsif Name.Local = Handler.Ref then
               Att.Attr.Ref := Resolve_QName
                 (Handler, Get_Value (Atts, J), Handler.Target_NS,
                  Loc => Get_Location (Atts, J));
            elsif Name.Local = Handler.Form then
               Att.Attr.Descr.Form :=
                 Form_Type'Value (Get (Get_Value (Atts, J)).all);
               Has_Form := True;
            elsif Name.Local = Handler.Default then
               Att.Attr.Descr.Default := Get_Value (Atts, J);
            elsif Name.Local = Handler.Namespace_Target then
               Att.Attr.Descr.Target_NS := Get_Value (Atts, J);
            end if;
         end if;
      end loop;

      --  See section 3.2.3 for valid attributes combination

      if Att.Attr.Descr.Target_NS /= No_Symbol then
         if Att.Attr.Descr.Name /= No_Qualified_Name then
            Validation_Error
              (Handler,
               "name must be specified when targetNamespace is specified");
         end if;

         if Has_Form then
            Validation_Error
              (Handler,
               "Cannot specify ""form"" when targetNamespace is given");
         end if;

         Validation_Error
           (Handler,
            "targetNamespace not supported in attribute declaration",
            Except => XML_Not_Implemented'Identity);
      end if;

      if Has_Form and then Att.Attr.Ref /= No_Qualified_Name then
         Validation_Error
           (Handler,
            "Attributes ""form"" and ""ref"" cannot be both specified");
      end if;

      if Att.Attr.Typ /= No_Qualified_Name then
         if Att.Attr.Ref /= No_Qualified_Name then
            Validation_Error
              (Handler,
               "Attributes ""type"" and ""ref"" cannot be both specified");
         end if;
      end if;

      if Att.Attr.Descr.Fixed /= No_Symbol
        and then Att.Attr.Descr.Default /= No_Symbol
      then
         Validation_Error
           (Handler,
            "Attributes ""fixed"" and ""default"" cannot be both specified");
      end if;

      if Att.Attr.Descr.Default /= No_Symbol
        and then Att.Attr.Descr.Use_Type /= Optional
      then
         Validation_Error
           (Handler,
            "Use must be ""optional"" when a default value is specified");
      end if;

      if Get_XSD_Version (Handler.Grammar) = XSD_1_1
        and then Att.Attr.Descr.Fixed /= No_Symbol
        and then Att.Attr.Descr.Use_Type = Prohibited
      then
         Validation_Error
           (Handler,
            """prohibited"" is forbidden when"
            & " a fixed value is specified");
      end if;

      if Att.Attr.Descr.Name /= No_Qualified_Name then
         case Ctx.Typ is
            when Context_Attribute_Group | Context_Type_Def =>
               null;

            when others =>
               if Handler.Target_NS = Handler.XML_Instance_URI then
                  Validation_Error
                    (Handler,
                     "Invalid target namespace for attribute declaration: """
                     & Get (Handler.Target_NS).all & """");
               end if;
         end case;
      end if;

      Att.Attr.Descr.Is_Local := Att.Attr.Ref = No_Qualified_Name;

      Push_Context
        (Handler,
         (Typ       => Context_Attribute,
          Attribute => Att));
   end Create_Attribute;

   ----------------------
   -- Insert_Attribute --
   ----------------------

   procedure Insert_Attribute
     (Handler        : access Schema_Reader'Class;
      In_Context     : Natural;
      Attribute      : Attr_Descr)
   is
      Ctx   : Context renames Handler.Contexts (In_Context);
      Index : Natural;
      Ctx2  : Context_Access;
   begin
      case Ctx.Typ is
         when Context_Type_Def =>
            Append
              (Handler.Shared.Types.Table (Ctx.Type_Info).Attributes,
               Attribute);

         when Context_Schema | Context_Redefine =>
            Set
              (Handler.Shared.Global_Attributes,
               Attribute.Attr.Descr.Name, Attribute.Attr);

         when Context_Extension | Context_Restriction =>
            Index := Handler.Contexts_Last;
            while Index >= Handler.Contexts'First loop
               Ctx2 := Handler.Contexts (Index)'Access;
               if Ctx2.Typ = Context_Type_Def then
                  Append
                    (Handler.Shared.Types.Table (Ctx2.Type_Info).Attributes,
                     Attribute);
                  exit;
               end if;

               Index := Index - 1;
            end loop;

         when Context_Simple_Extension | Context_Simple_Restriction =>
            pragma Assert
              (Handler.Contexts (In_Context - 1).Typ = Context_Type_Def);
            pragma Assert  --  a <simpleType><extension> cannot have attributes
              (not Handler.Shared.Types.Table
                 (Handler.Contexts (In_Context - 1).Type_Info).Is_Simple);

            Append
              (Handler.Shared.Types.Table
                 (Handler.Contexts (In_Context - 1).Type_Info).Attributes,
               Attribute);

         when Context_Attribute_Group =>
            Append (Ctx.Attr_Group.Attributes, Attribute);

         when Context_Element | Context_Sequence | Context_Choice
            | Context_Attribute | Context_All
            | Context_Union | Context_List | Context_Group =>
            Validation_Error
              (Handler,
               "Unsupported: ""attribute"" in this context",
               Except => XML_Not_Implemented'Identity);
      end case;
   end Insert_Attribute;

   ----------------------
   -- Finish_Attribute --
   ----------------------

   procedure Finish_Attribute (Handler : access Schema_Reader'Class) is
      Ctx : constant Context_Access :=
        Handler.Contexts (Handler.Contexts_Last)'Access;
   begin
      Insert_Attribute (Handler, Handler.Contexts_Last - 1, Ctx.Attribute);
   end Finish_Attribute;

   -------------------
   -- Create_Schema --
   -------------------

   procedure Create_Schema
     (Handler  : access Schema_Reader'Class;
      Atts     : Sax_Attribute_List)
   is
      Info   : Schema_Descr;
      Is_Set : Boolean := False;
      Name   : Qualified_Name;
   begin
      Info.Element_Form_Default   := Unqualified;
      Info.Attribute_Form_Default := Unqualified;

      for J in 1 .. Get_Length (Atts) loop
         Name := Get_Name (Atts, J);
         if Name.NS = Empty_String then
            if Name.Local = Handler.S_Element_Form_Default then
               Info.Element_Form_Default := Compute_Form (Atts, Handler, J);
            elsif Name.Local = Handler.S_Attribute_Form_Default then
               Info.Attribute_Form_Default := Compute_Form (Atts, Handler, J);
            elsif Name.Local = Handler.Block_Default then
               Compute_Blocks (Atts, Handler, Info.Block, Is_Set, J);
            elsif Name.Local = Handler.Namespace_Target then
               Info.Target_NS := Get_Value (Atts, J);
            end if;

         elsif Name.NS = Handler.XML_Instance_URI then
            if Name.Local = Handler.No_Namespace_Schema_Location then
               --  Already handled through Hook_Start_Element when validating
               --  the grammar itself, but needed if we do not validate the
               --  grammar
               Parse_Grammar
                 (Handler,
                  URI           => Empty_String,
                  Xsd_File      => Get_Value (Atts, J),
                  Do_Create_NFA => False);

            elsif Name.Local = Handler.Schema_Location then
               --  Already handled through Hook_Start_Element when validating
               --  the grammar itself
               Parse_Grammars
                 (Handler, Get_Value (Atts, J), Do_Create_NFA => False);
            end if;
         end if;
      end loop;

      if Info.Target_NS /= No_Symbol then
         if Debug then
            Output_Action ("Get_NS (Handler.Created_Grammar, """
                    & Get (Info.Target_NS).all & """, Handler.Target_NS)");
         end if;

         Handler.Target_NS := Info.Target_NS;
      end if;

      Handler.Element_Form_Default   := Info.Element_Form_Default;
      Handler.Attribute_Form_Default := Info.Attribute_Form_Default;

      if Is_Set then
         Handler.Target_Block_Default := Info.Block;
      end if;

      Push_Context (Handler, (Typ => Context_Schema));
   end Create_Schema;

   --------------------------------
   -- Process_Contents_From_Atts --
   --------------------------------

   function Process_Contents_From_Atts
     (Handler : access Schema_Reader'Class;
      Atts    : Sax_Attribute_List;
      Index   : Integer) return Process_Contents_Type is
   begin
      if Get_Value (Atts, Index) = Handler.Lax then
         return Process_Lax;
      elsif Get_Value (Atts, Index) = Handler.Strict then
         return Process_Strict;
      else
         return Process_Skip;
      end if;
   end Process_Contents_From_Atts;

   ----------------
   -- Create_Any --
   ----------------

   procedure Create_Any
     (Handler : access Schema_Reader'Class;
      Atts    : Sax_Attribute_List)
   is
      Details : Type_Details_Access;
      Any     : Internal_Any_Descr;
      Name    : Qualified_Name;
      Min_Occurs, Max_Occurs : Occurrences := (False, 1);

   begin
      Any.Target_NS  := Handler.Target_NS;
      Any.Namespaces := Handler.Any_Namespace;

      for J in 1 .. Get_Length (Atts) loop
         Name := Get_Name (Atts, J);
         if Name.NS = Empty_String then
            if Name.Local = Handler.Namespace then
               Any.Namespaces := Get_Value (Atts, J);
            elsif Name.Local = Handler.Process_Contents then
               Any.Process_Contents :=
                 Process_Contents_From_Atts (Handler, Atts, J);
            end if;
         end if;
      end loop;

      Get_Occurs (Handler, Atts, Min_Occurs, Max_Occurs);
      Details := new Type_Details'
        (Kind       => Type_Any,
         Min_Occurs => Min_Occurs,
         Max_Occurs => Max_Occurs,
         Loc        => Handler.Current_Location,
         In_Process => False,
         Next       => null,
         Any        => Any);
      Insert_In_Type (Handler, Details);
   end Create_Any;

   ----------------
   -- Create_All --
   ----------------

   procedure Create_All
     (Handler  : access Schema_Reader'Class;
      Atts     : Sax_Attribute_List)
   is
      Min_Occurs, Max_Occurs : Occurrences := (False, 1);
      Details : Type_Details_Access;
   begin
      Get_Occurs (Handler, Atts, Min_Occurs, Max_Occurs);
      Details := new Type_Details'
        (Kind         => Type_All,
         Min_Occurs   => Min_Occurs,
         Max_Occurs   => Max_Occurs,
         Loc          => Handler.Current_Location,
         In_Process   => False,
         Next         => null,
         First_In_All => null);
      Insert_In_Type (Handler, Details);
      Push_Context
        (Handler,
         (Typ        => Context_All,
          All_Detail => Details));
   end Create_All;

   -------------------
   -- Start_Element --
   -------------------

   overriding procedure Start_Element
     (Handler       : in out Schema_Reader;
      NS            : Sax.Utils.XML_NS;
      Local_Name    : Sax.Symbols.Symbol;
      Atts          : Sax.Readers.Sax_Attribute_List)
   is
      H   : constant Schema_Reader_Access := Handler'Unchecked_Access;
      Ctx : Context_Access;
   begin
      if Debug then
         Output_Seen ("Start " & Get (Local_Name).all
                      & " at " & To_String (Handler.Current_Location));
      end if;

      --  Check the grammar
      Start_Element (Validating_Reader (Handler), NS, Local_Name, Atts);

      --  Process the element

      if Handler.Contexts = null then
         if Local_Name /= Handler.S_Schema then
            Validation_Error (H, "Root element must be <schema>");
         end if;

         Create_Schema (H, Atts);

      elsif Local_Name = Handler.Annotation then
         Handler.In_Annotation := True;

      elsif Local_Name = Handler.Notation then
         Create_Notation (H, Atts);

      elsif Local_Name = Handler.Element then
         Create_Element (H, Atts);

      elsif Local_Name = Handler.Complex_Type then
         Create_Complex_Type (H, Atts);

      elsif Local_Name = Handler.Simple_Type then
         Create_Simple_Type (H, Atts);

      elsif Local_Name = Handler.Restriction then
         Create_Restriction (H, Atts);

      elsif Local_Name = Handler.Extension then
         Create_Extension (H, Atts);

      elsif Local_Name = Handler.Any_Attribute then
         Create_Any_Attribute (H, Atts);

      elsif Local_Name = Handler.Pattern then
         Ctx := Handler.Contexts (Handler.Contexts_Last)'Access;
         pragma Assert (Ctx.Typ = Context_Simple_Restriction);
         pragma Assert (Ctx.Simple.Kind = Simple_Type_Restriction);
         --  Use the non-normalized value for <pattern>
         Add_Facet
           (Grammar    => Handler.Grammar,
            Facets     => Ctx.Simple.Facets,
            Facet_Name => Local_Name,
            Value      => Get_Non_Normalized_Value
              (Atts, Get_Index (Atts, Empty_String, Handler.Value)),
            Loc        => Handler.Current_Location);

      elsif Local_Name = Handler.Maxlength
        or else Local_Name = Handler.Minlength
        or else Local_Name = Handler.Length
        or else Local_Name = Handler.Enumeration
        or else Local_Name = Handler.Whitespace
        or else Local_Name = Handler.Total_Digits
        or else Local_Name = Handler.Fraction_Digits
        or else Local_Name = Handler.MaxInclusive
        or else Local_Name = Handler.MaxExclusive
        or else Local_Name = Handler.MinInclusive
        or else Local_Name = Handler.MinExclusive
      then
         Ctx := Handler.Contexts (Handler.Contexts_Last)'Access;
         pragma Assert (Ctx.Typ = Context_Simple_Restriction);
         pragma Assert (Ctx.Simple.Kind = Simple_Type_Restriction);
         Add_Facet
           (Grammar    => Handler.Grammar,
            Facets     => Ctx.Simple.Facets,
            Facet_Name => Local_Name,
            Value      => Get_Value
              (Atts, Get_Index (Atts, Empty_String, Handler.Value)),
            Loc        => Handler.Current_Location);

      elsif Local_Name = Handler.S_All then
         Create_All (H, Atts);

      elsif Local_Name = Handler.Sequence then
         Create_Sequence (H, Atts);

      elsif Local_Name = Handler.Choice then
         Create_Choice (H, Atts);

      elsif Local_Name = Handler.List then
         Create_List (H, Atts);

      elsif Local_Name = Handler.Union then
         Create_Union (H, Atts);

      elsif Local_Name = Handler.Attribute then
         Create_Attribute (H, Atts);

      elsif Local_Name = Handler.Group then
         Create_Group (H, Atts);

      elsif Local_Name = Handler.Simple_Content then
         Ctx := Handler.Contexts (Handler.Contexts_Last)'Access;
         pragma Assert (Ctx.Typ = Context_Type_Def);
         Handler.Shared.Types.Table (Ctx.Type_Info).Simple :=
           (Kind       => Simple_Type,
            In_Process => False,
            Loc        => Handler.Current_Location);
         Handler.Shared.Types.Table (Ctx.Type_Info).Properties.Mixed := True;

      elsif Local_Name = Handler.Complex_Content then
         Ctx := Handler.Contexts (Handler.Contexts_Last)'Access;
         pragma Assert (Ctx.Typ = Context_Type_Def);

         --  Do not reset Properties.Mixed here, since it might have been set
         --  to "true" on the parent <complexType> node.

      elsif Local_Name = Handler.Attribute_Group then
         Create_Attribute_Group (H, Atts);

      elsif Local_Name = Handler.Any then
         Create_Any (H, Atts);

      elsif Local_Name = Handler.Redefine then
         Validation_Error
           (Handler'Access,
            "Unsupported <redefined>",
            Except => XML_Not_Implemented'Identity);
         Create_Redefine (H, Atts);

      elsif Local_Name = Handler.Include then
         Create_Include (H, Atts);

      elsif Local_Name = Handler.Import then
         Create_Import (H, Atts);

      elsif Handler.In_Annotation then
         null;   --  ignore all tags

      elsif Handler.Feature_Ignore_Unsupported_XSD_Elements
        and then
          (Local_Name = Handler.Keyref
           or else Local_Name = Handler.Key
           or else Local_Name = Handler.Selector
           or else Local_Name = Handler.Unique
           or else Local_Name = Handler.Field)
      then
         Warning
           (Handler,
            Create (Message => "Unsupported element in the schema: "
                    & Get (Local_Name).all,
                    Loc     => Handler.Current_Location));

      else
         Validation_Error
           (Handler'Access,
            "Unsupported element in the schema: " & Get (Local_Name).all,
            Except => XML_Not_Implemented'Identity);
      end if;
   end Start_Element;

   -----------------
   -- End_Element --
   -----------------

   overriding procedure End_Element
     (Handler       : in out Schema_Reader;
      NS            : Sax.Utils.XML_NS;
      Local_Name    : Sax.Symbols.Symbol)
   is
      H       : constant Schema_Reader_Access := Handler'Unchecked_Access;
      Handled : Boolean := True;
   begin
      --  Check the grammar
      End_Element (Validating_Reader (Handler), NS, Local_Name);

      --  Process the tag
      if Local_Name = Handler.Element then
         Finish_Element (H);

      elsif Local_Name = Handler.S_Schema then
         null;

      elsif Local_Name = Handler.Complex_Type then
         Finish_Complex_Type (H);

      elsif Local_Name = Handler.Simple_Type then
         Finish_Simple_Type (H);

      elsif Local_Name = Handler.S_All then
         null;

      elsif Local_Name = Handler.Sequence then
         null;

      elsif Local_Name = Handler.Any_Attribute then
         Handled := False;

      elsif Local_Name = Handler.Choice then
         null;

      elsif Local_Name = Handler.Restriction then
         Finish_Restriction (H);

      elsif Local_Name = Handler.Extension then
         Finish_Extension (H);

      elsif Local_Name = Handler.Attribute then
         Finish_Attribute (H);

      elsif Local_Name = Handler.Union then
         Finish_Union (H);

      elsif Local_Name = Handler.List then
         Finish_List (H);

      elsif Local_Name = Handler.Maxlength
        or else Local_Name = Handler.Pattern
        or else Local_Name = Handler.Minlength
        or else Local_Name = Handler.Enumeration
        or else Local_Name = Handler.Whitespace
        or else Local_Name = Handler.Total_Digits
        or else Local_Name = Handler.Fraction_Digits
        or else Local_Name = Handler.MaxInclusive
        or else Local_Name = Handler.MaxExclusive
        or else Local_Name = Handler.MinInclusive
        or else Local_Name = Handler.MinExclusive
      then
         Handled := False;

      elsif Local_Name = Handler.Attribute_Group then
         Finish_Attribute_Group (H);

      elsif Local_Name = Handler.Redefine then
         null;

      elsif Local_Name = Handler.Group then
         Finish_Group (H);

      elsif Local_Name = Handler.Any
        or else Local_Name = Handler.Include
        or else Local_Name = Handler.Import
        or else Local_Name = Handler.Simple_Content
        or else Local_Name = Handler.Complex_Content
      then
         Handled := False;

      elsif Local_Name = Handler.Annotation then
         Handler.In_Annotation := False;
         Handled := False;

      else
         if Debug then
            Output_Action
              ("Close tag not handled yet: " & Get (Local_Name).all);
         end if;
         Handled := False;
      end if;

      --  Release the context
      if Handled then
         Handler.Contexts_Last := Handler.Contexts_Last - 1;
      end if;
   end End_Element;

   ----------------
   -- Characters --
   ----------------

   procedure Characters
     (Handler : in out Schema_Reader; Ch : Unicode.CES.Byte_Sequence)
   is
   begin
      Characters (Validating_Reader (Handler), Ch);
   end Characters;

   ----------
   -- Free --
   ----------

   procedure Free (Self : in out Type_Details_Access) is
      procedure Unchecked_Free is new Ada.Unchecked_Deallocation
        (Type_Details, Type_Details_Access);
      Next : Type_Details_Access;
   begin
      while Self /= null loop
         Next := Self.Next;

         case Self.Kind is
            when Type_Empty | Type_Element | Type_Any => null;
            when Type_Sequence    => Free (Self.First_In_Seq);
            when Type_Choice      => Free (Self.First_In_Choice);
            when Type_All         => Free (Self.First_In_All);
            when Type_Group       => Free (Self.Group.Details);
            when Type_Extension   =>
               Free (Self.Extension.Details);
            when Type_Restriction =>
               Free (Self.Restriction.Details);
         end case;

         Unchecked_Free (Self);
         Self := Next;
      end loop;
   end Free;

   -----------------
   -- Set_Feature --
   -----------------

   overriding procedure Set_Feature
     (Parser : in out Schema_Reader; Name : String; Value : Boolean) is
   begin
      if Name = Feature_Ignore_Unsupported_XSD_Elements then
         Parser.Feature_Ignore_Unsupported_XSD_Elements := Value;
      else
         Set_Feature (Validating_Reader (Parser), Name, Value);
      end if;
   end Set_Feature;

   -----------------
   -- Get_Feature --
   -----------------

   overriding function Get_Feature
     (Parser : Schema_Reader; Name : String) return Boolean is
   begin
      if Name = Feature_Ignore_Unsupported_XSD_Elements then
         return Parser.Feature_Ignore_Unsupported_XSD_Elements;
      else
         return Get_Feature (Validating_Reader (Parser), Name);
      end if;
   end Get_Feature;

end Schema.Schema_Readers;