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
|
/* This code implements a set of tDOM C Handlers that can be
dynamically loaded into a tclsh with already loaded tDOM
package. This parser extension does some tests according to the DTD
against the data within an XML document.
Copyright (c) 2001-2003 Rolf Ade */
#include <tdom.h>
#include <string.h>
#include <stdlib.h>
#ifndef TCL_THREADS
# define TDomThreaded(x)
#else
# define TDomThreaded(x) x
#endif
/* The initial stack sizes must be at least 1 */
#define TNC_INITCONTENTSTACKSIZE 512
/*----------------------------------------------------------------------------
| local globals
|
\---------------------------------------------------------------------------*/
/* Counter to generate unique validateCmd names */
static int uniqueCounter = 0;
TDomThreaded(static Tcl_Mutex counterMutex;) /* Protect the counter */
/* To enable some debugging output at stdout use this.
But beware: this debugging output isn't systematic
and only understandable, if you know the internals
of tnc. */
/* #define TNC_DEBUG */
/* The elements of TNC_Content carry exactly the same information
as expats XML_Content. But the element is identified by his
Tcl_HashEntry entry within the "tagNames" Hashtable (see TNC_Data)
and not the element name. This should be much more efficient. */
typedef struct TNC_cp TNC_Content;
typedef struct TNC_elemAttInfo TNC_ElemAttInfo;
struct TNC_cp
{
enum XML_Content_Type type;
enum XML_Content_Quant quant;
Tcl_HashEntry *nameId;
unsigned int numchildren;
TNC_Content *children;
TNC_ElemAttInfo *attInfo;
};
typedef struct TNC_contentStack
{
TNC_Content *model;
int activeChild;
int deep;
int alreadymatched;
} TNC_ContentStack;
typedef struct TNC_data
{
char *doctypeName; /* From DOCTYPE declaration */
int skipWhiteCDATAs; /* Flag: white space allowed in
current content model? */
int ignorePCDATA; /* Flag: currently mixed content
model? */
Tcl_HashTable *tagNames; /* Hash table of all ELEMENT
declarations of the DTD.
Element name is the key.
While parsing, entry points
to the XML_Content of that
Element, after finishing of
DTD parsing, entry holds a
pointer to the TNC_Content
of that element. */
TNC_ElemAttInfo *elemAttInfo; /* TncElementStartCommand stores
the elemAttInfo pointer of
the current element here for
DOM validation, to avoid two
element name lookups. */
int elemContentsRewriten; /* Signals, if the tagNames
entries point to
TNC_Contents */
int dtdstatus; /* While used with expat obj:
1 after successful parsed
DTD, 0 otherwise.
For validateCmd used for
error report during
validation: 0 OK, 1 validation
error. */
int idCheck; /* Flag: check IDREF resolution*/
Tcl_HashTable *attDefsTables; /* Used to store ATTLIST
declarations while parsing.
Keys are the element names. */
Tcl_HashTable *entityDecls; /* Used to store ENTITY
declarations. */
Tcl_HashTable *notationDecls; /* Used to store NOTATION
declarations. */
Tcl_HashTable *ids; /* Used to track IDs */
Tcl_Interp *interp;
Tcl_Obj *expatObj; /* If != NULL, points to the
parserCmd structure. NULL
for ValidateCmds. Used, to
distinguish between SAX
and DOM validation. */
int contentStackSize; /* Current size of the content
stack */
int contentStackPtr; /* Points to the currently active
content model on the stack */
TNC_ContentStack *contentStack; /* Stack for the currently
nested open content models. */
} TNC_Data;
typedef enum TNC_attType {
TNC_ATTTYPE_CDATA,
TNC_ATTTYPE_ID,
TNC_ATTTYPE_IDREF,
TNC_ATTTYPE_IDREFS,
TNC_ATTTYPE_ENTITY,
TNC_ATTTYPE_ENTITIES,
TNC_ATTTYPE_NMTOKEN,
TNC_ATTTYPE_NMTOKENS,
TNC_ATTTYPE_NOTATION,
TNC_ATTTYPE_ENUMERATION,
} TNC_AttType;
struct TNC_elemAttInfo
{
Tcl_HashTable *attributes;
int nrOfreq;
int nrOfIdAtts;
};
typedef struct TNC_attDecl
{
TNC_AttType att_type;
char *dflt;
int isrequired;
Tcl_HashTable *lookupTable; /* either NotationTypes or enum values */
} TNC_AttDecl;
typedef struct TNC_entityInfo
{
int is_notation;
char *notationName;
} TNC_EntityInfo;
typedef Tcl_HashEntry TNC_NameId;
static char tnc_usage[] =
"Usage tnc <expat parser obj> <subCommand>, where subCommand can be: \n"
" enable \n"
" remove \n"
" getValidateCmd ?cmdName?\n"
;
static char validateCmd_usage[] =
"Usage validateCmd <method> <args>, where method can be: \n"
" validateDocument <domDocument> \n"
" validateTree <domNode> \n"
" validateAttributes <domNode> \n"
" delete \n"
;
enum TNC_Error {
TNC_ERROR_NONE,
TNC_ERROR_DUPLICATE_ELEMENT_DECL,
TNC_ERROR_DUPLICATE_MIXED_ELEMENT,
TNC_ERROR_UNKNOWN_ELEMENT,
TNC_ERROR_EMPTY_ELEMENT,
TNC_ERROR_DISALLOWED_PCDATA,
TNC_ERROR_DISALLOWED_CDATA,
TNC_ERROR_NO_DOCTYPE_DECL,
TNC_ERROR_WRONG_ROOT_ELEMENT,
TNC_ERROR_NO_ATTRIBUTES,
TNC_ERROR_UNKNOWN_ATTRIBUTE,
TNC_ERROR_WRONG_FIXED_ATTVALUE,
TNC_ERROR_MISSING_REQUIRED_ATTRIBUTE,
TNC_ERROR_MORE_THAN_ONE_ID_ATT,
TNC_ERROR_ID_ATT_DEFAULT,
TNC_ERROR_DUPLICATE_ID_VALUE,
TNC_ERROR_UNKNOWN_ID_REFERRED,
TNC_ERROR_ENTITY_ATTRIBUTE,
TNC_ERROR_ENTITIES_ATTRIBUTE,
TNC_ERROR_ATT_ENTITY_DEFAULT_MUST_BE_DECLARED,
TNC_ERROR_NOTATION_REQUIRED,
TNC_ERROR_NOTATION_MUST_BE_DECLARED,
TNC_ERROR_IMPOSSIBLE_DEFAULT,
TNC_ERROR_ENUM_ATT_WRONG_VALUE,
TNC_ERROR_NMTOKEN_REQUIRED,
TNC_ERROR_NAME_REQUIRED,
TNC_ERROR_NAMES_REQUIRED,
TNC_ERROR_ELEMENT_NOT_ALLOWED_HERE,
TNC_ERROR_ELEMENT_CAN_NOT_END_HERE,
TNC_ERROR_ONLY_THREE_BYTE_UTF8,
TNC_ERROR_UNKNOWN_NODE_TYPE
};
const char *
TNC_ErrorString (int code)
{
static const char *message[] = {
"No error.",
"Element declared more than once.",
"The same name must not appear more than once in \n\tone mixed-content declaration.",
"No declaration for this element.",
"Element is declared to be empty, but isn't.",
"PCDATA not allowed here.",
"CDATA section not allowed here.",
"No DOCTYPE declaration.",
"Root element doesn't match DOCTYPE name.",
"No attributes defined for this element.",
"Unknown attribute for this element.",
"Attribute value must match the FIXED default.",
"Required attribute missing.",
"Only one attribute with type ID allowed.",
"No default value allowed for attribute type ID.",
"ID attribute values must be unique within the document.",
"Unknown ID referred.",
"Attribute value has to be a unparsed entity.",
"Attribute value has to be a sequence of unparsed entities.",
"The defaults of attributes with type ENTITY or ENTITIES\nhas to be unparsed entities.",
"Attribute value has to be one of the allowed notations.",
"Every used NOTATION must be declared.",
"Attribute default is not one of the allowed values",
"Attribute hasn't one of the allowed values.",
"Attribute value has to be a NMTOKEN.",
"Attribute value has to be a Name.",
"Attribute value has to match production Names.",
"Element is not allowed here.",
"Element can not end here (required element(s) missing).",
"Can only handle UTF8 chars up to 3 bytes length."
"Unknown or unexpected dom node type."
};
/* if (code > 0 && code < sizeof(message)/sizeof(message[0])) */
return message[code];
return 0;
}
#define CHECK_UTF_CHARLEN(d) if (!(d)) { \
signalNotValid (userData, TNC_ERROR_ONLY_THREE_BYTE_UTF8);\
return;\
}
#define CHECK_UTF_CHARLENR(d) if (!(d)) { \
signalNotValid (userData, TNC_ERROR_ONLY_THREE_BYTE_UTF8);\
return 0;\
}
#define CHECK_UTF_CHARLEN_COPY(d) if (!(d)) { \
signalNotValid (userData, TNC_ERROR_ONLY_THREE_BYTE_UTF8);\
FREE (copy);\
return;\
}
#define SetResult(str) Tcl_ResetResult(interp); \
Tcl_SetStringObj(Tcl_GetObjResult(interp), (str), -1)
#define SetBooleanResult(i) Tcl_ResetResult(interp); \
Tcl_SetBooleanObj(Tcl_GetObjResult(interp), (i))
extern char *Tdom_InitStubs (Tcl_Interp *interp, char *version, int exact);
static void
signalNotValid (
void *userData,
int code
)
{
TNC_Data *tncdata = (TNC_Data *) userData;
TclGenExpatInfo *expat;
char s[1000];
if (tncdata->expatObj) {
expat = GetExpatInfo (tncdata->interp, tncdata->expatObj);
sprintf (s, "Validation error at line %ld, character %ld: %s",
XML_GetCurrentLineNumber (expat->parser),
XML_GetCurrentColumnNumber (expat->parser),
TNC_ErrorString (code));
expat->status = TCL_ERROR;
expat->result = Tcl_NewStringObj (s, -1);
Tcl_IncrRefCount (expat->result);
} else {
tncdata->dtdstatus = 1;
Tcl_SetResult (tncdata->interp, (char *)TNC_ErrorString (code),
TCL_VOLATILE);
}
}
/*
*----------------------------------------------------------------------------
*
* FindUniqueCmdName --
*
* Generate new command name. Used for getValidateCmd.
*
* Results:
* Returns newly allocated Tcl object containing name.
*
* Side effects:
* Allocates Tcl object.
*
*----------------------------------------------------------------------------
*/
static void
FindUniqueCmdName(
Tcl_Interp *interp,
char *s
)
{
Tcl_CmdInfo info;
TDomThreaded(Tcl_MutexLock(&counterMutex);)
do {
sprintf(s, "DTDvalidator%d", uniqueCounter++);
} while (Tcl_GetCommandInfo(interp, s, &info));
TDomThreaded(Tcl_MutexUnlock(&counterMutex);)
}
/*
*----------------------------------------------------------------------------
*
* TncStartDoctypeDeclHandler --
*
* This procedure is called for the start of the DOCTYPE
* declaration.
*
* Results:
* None.
*
* Side effects:
* Stores the doctype Name in the TNC_data.
*
*----------------------------------------------------------------------------
*/
void
TncStartDoctypeDeclHandler (
void *userData,
const char *doctypeName,
const char *sysid,
const char *pubid,
int has_internal_subset
)
{
TNC_Data *tncdata = (TNC_Data *) userData;
#ifdef TNC_DEBUG
printf ("TncStartDoctypeDeclHandler start\n");
#endif
tncdata->doctypeName = tdomstrdup (doctypeName);
}
/*
*----------------------------------------------------------------------------
*
* TncFreeTncModel --
*
* This helper procedure frees recursively TNC_Contents.
*
* Results:
* None.
*
* Side effects:
* Frees memory.
*
*----------------------------------------------------------------------------
*/
static void
TncFreeTncModel (
TNC_Content *tmodel
)
{
unsigned int i;
if (tmodel->children) {
for (i = 0; i < tmodel->numchildren; i++) {
TncFreeTncModel (&tmodel->children[i]);
}
FREE ((char *) tmodel->children);
}
}
/*
*----------------------------------------------------------------------------
*
* TncRewriteModel --
*
* This helper procedure creates recursively a TNC_Content from
* an XML_Content.
*
* Results:
* None.
*
* Side effects:
* Allocates memory for the TNC_Content models.
*
*----------------------------------------------------------------------------
*/
static void
TncRewriteModel (
XML_Content *emodel,
TNC_Content *tmodel,
Tcl_HashTable *tagNames
)
{
Tcl_HashEntry *entryPtr;
unsigned int i;
tmodel->type = emodel->type;
tmodel->quant = emodel->quant;
tmodel->numchildren = emodel->numchildren;
tmodel->children = NULL;
tmodel->nameId = NULL;
switch (emodel->type) {
case XML_CTYPE_MIXED:
if (emodel->quant == XML_CQUANT_REP) {
tmodel->children = (TNC_Content *)
MALLOC (sizeof (TNC_Content) * emodel->numchildren);
for (i = 0; i < emodel->numchildren; i++) {
TncRewriteModel (&emodel->children[i], &tmodel->children[i],
tagNames);
}
}
break;
case XML_CTYPE_ANY:
case XML_CTYPE_EMPTY:
/* do nothing */
break;
case XML_CTYPE_SEQ:
case XML_CTYPE_CHOICE:
tmodel->children = (TNC_Content *)
MALLOC (sizeof (TNC_Content) * emodel->numchildren);
for (i = 0; i < emodel->numchildren; i++) {
TncRewriteModel (&emodel->children[i], &tmodel->children[i],
tagNames);
}
break;
case XML_CTYPE_NAME:
entryPtr = Tcl_FindHashEntry (tagNames, emodel->name);
/* Notice, that it is possible for entryPtr to be NULL.
This means, a content model uses a not declared element.
This is legal even in valid documents. (Of course, if the
undeclared element actually shows up in the document
that would make the document invalid.) See rec 3.2
QUESTION: Should there be a flag to enable a warning,
when a declaration contains an element type for which
no declaration is provided, as rec 3.2 metioned?
This would be the appropriated place to omit the
warning. */
tmodel->nameId = entryPtr;
}
}
/*
*----------------------------------------------------------------------------
*
* TncEndDoctypeDeclHandler --
*
* This procedure is called at the end of the DOCTYPE
* declaration, after processing any external subset.
* It rewrites the XML_Content models to TNC_Content
* models and frees the XML_Content models.
*
* Results:
* None.
*
* Side effects:
* Rewrites the XML_Content models to TNC_Content
* models.
*
*----------------------------------------------------------------------------
*/
void
TncEndDoctypeDeclHandler (
void *userData
)
{
TNC_Data *tncdata = (TNC_Data *) userData;
Tcl_HashEntry *entryPtr, *ePtr1;
Tcl_HashSearch search;
XML_Content *emodel;
TNC_Content *tmodel = NULL;
char *elementName;
entryPtr = Tcl_FirstHashEntry (tncdata->tagNames, &search);
while (entryPtr != NULL) {
#ifdef TNC_DEBUG
printf ("name: %-20s nameId: %p\n",
Tcl_GetHashKey (tncdata->tagNames, entryPtr),
entryPtr);
#endif
emodel = (XML_Content*) Tcl_GetHashValue (entryPtr);
tmodel = (TNC_Content*) MALLOC (sizeof (TNC_Content));
TncRewriteModel (emodel, tmodel, tncdata->tagNames);
elementName = Tcl_GetHashKey (tncdata->tagNames, entryPtr);
ePtr1 = Tcl_FindHashEntry (tncdata->attDefsTables, elementName);
if (ePtr1) {
tmodel->attInfo = (TNC_ElemAttInfo *) Tcl_GetHashValue (ePtr1);
} else {
tmodel->attInfo = NULL;
}
Tcl_SetHashValue (entryPtr, tmodel);
entryPtr = Tcl_NextHashEntry (&search);
}
tncdata->elemContentsRewriten = 1;
/* Checks, if every used notation name is in deed declared */
entryPtr = Tcl_FirstHashEntry (tncdata->notationDecls, &search);
while (entryPtr != NULL) {
#ifdef TNC_DEBUG
printf ("check notation name %s\n",
Tcl_GetHashKey (tncdata->notationDecls, entryPtr));
printf ("value %p\n", Tcl_GetHashValue (entryPtr));
#endif
if (!Tcl_GetHashValue (entryPtr)) {
signalNotValid (userData, TNC_ERROR_NOTATION_MUST_BE_DECLARED);
return;
}
entryPtr = Tcl_NextHashEntry (&search);
}
/* Checks, if every used entity name is indeed declared */
entryPtr = Tcl_FirstHashEntry (tncdata->entityDecls, &search);
while (entryPtr != NULL) {
if (!Tcl_GetHashValue (entryPtr)) {
signalNotValid (userData,
TNC_ERROR_ATT_ENTITY_DEFAULT_MUST_BE_DECLARED);
return;
}
entryPtr = Tcl_NextHashEntry (&search);
}
tncdata->dtdstatus = 1;
}
/*
*----------------------------------------------------------------------------
*
* TncEntityDeclHandler --
*
* This procedure is called for every entity declaration.
*
* Results:
* None.
*
* Side effects:
* Stores either the name of the entity and
* type information in a lookup table.
*
*----------------------------------------------------------------------------
*/
void
TncEntityDeclHandler (
void *userData,
const char *entityName,
int is_parameter_entity,
const char *value,
int value_length,
const char *base,
const char *systemId,
const char *publicId,
const char *notationName
)
{
TNC_Data *tncdata = (TNC_Data *) userData;
Tcl_HashEntry *entryPtr;
int newPtr;
TNC_EntityInfo *entityInfo;
/* expat collects entity definitions internaly by itself. So this is
maybe superfluous, if it possible to access the expat internal
representation. To study this is left to the reader. */
if (is_parameter_entity) return;
entryPtr = Tcl_CreateHashEntry (tncdata->entityDecls, entityName, &newPtr);
/* multiple declaration of the same entity are allowed; first
definition wins (rec. 4.2) */
if (!newPtr) {
/* Eventually, an attribute declaration with type ENTITY or ENTITIES
has used this (up to the attribute declaration undeclared) ENTITY
within his default value. In this case, the hash value has to
be NULL and the entity must be a unparsed entity. */
if (!Tcl_GetHashValue (entryPtr)) {
if (notationName == NULL) {
signalNotValid (userData,
TNC_ERROR_ATT_ENTITY_DEFAULT_MUST_BE_DECLARED);
return;
}
newPtr = 1;
}
}
if (newPtr) {
entityInfo = (TNC_EntityInfo *) MALLOC (sizeof (TNC_EntityInfo));
if (notationName != NULL) {
entityInfo->is_notation = 1;
Tcl_CreateHashEntry (tncdata->notationDecls,
notationName, &newPtr);
entityInfo->notationName = tdomstrdup (notationName);
}
else {
entityInfo->is_notation = 0;
}
Tcl_SetHashValue (entryPtr, entityInfo);
}
}
/*
*----------------------------------------------------------------------------
*
* TncNotationDeclHandler --
*
* This procedure is called for every notation declaration.
*
* Results:
* None.
*
* Side effects:
* Stores the notationName in the notationDecls table with value
* one.
*
*----------------------------------------------------------------------------
*/
void
TncNotationDeclHandler (
void *userData,
const char *notationName,
const char *base,
const char *systemId,
const char *publicId
)
{
TNC_Data *tncdata = (TNC_Data *) userData;
Tcl_HashEntry *entryPtr;
int newPtr;
entryPtr = Tcl_CreateHashEntry (tncdata->notationDecls,
notationName,
&newPtr);
#ifdef TNC_DEBUG
printf ("Notation %s declared\n", notationName);
#endif
Tcl_SetHashValue (entryPtr, (char *) 1);
}
/*
*----------------------------------------------------------------------------
*
* TncElementDeclCommand --
*
* This procedure is called for every element declaration.
*
* Results:
* None.
*
* Side effects:
* Stores the tag name of the element in a lookup table.
*
*----------------------------------------------------------------------------
*/
void
TncElementDeclCommand (
void *userData,
const char *name,
XML_Content *model
)
{
TNC_Data *tncdata = (TNC_Data *) userData;
Tcl_HashEntry *entryPtr;
int newPtr;
unsigned int i, j;
entryPtr = Tcl_CreateHashEntry (tncdata->tagNames, name, &newPtr);
/* "No element type may be declared more than once." (rec. 3.2) */
if (!newPtr) {
signalNotValid (userData, TNC_ERROR_DUPLICATE_ELEMENT_DECL);
return;
}
/* "The same name must not appear more than once in a
single mixed-content declaration." (rec. 3.2.2)
NOTE: OK, OK, doing it this way may not be optimal or even fast
in some cases. Please step in with a more fancy solution, if you
feel the need. */
if (model->type == XML_CTYPE_MIXED && model->quant == XML_CQUANT_REP) {
for (i = 0; i < model->numchildren; i++) {
for (j = i + 1; j < model->numchildren; j++) {
if (strcmp ((&model->children[i])->name,
(&model->children[j])->name) == 0) {
signalNotValid (userData,
TNC_ERROR_DUPLICATE_MIXED_ELEMENT);
return;
}
}
}
}
Tcl_SetHashValue (entryPtr, model);
return;
}
/*
*----------------------------------------------------------------------------
*
* TncAttDeclCommand --
*
* This procedure is called for *each* attribute in an XML
* ATTLIST declaration. It stores the attribute definition in
* an element specific hash table.
*
* Results:
* None.
*
* Side effects:
* Stores the tag name of the element in a lookup table.
*
*----------------------------------------------------------------------------
*/
void
TncAttDeclCommand (
void *userData,
const char *elname,
const char *attname,
const char *att_type,
const char *dflt,
int isrequired
)
{
TNC_Data *tncdata = (TNC_Data *) userData;
Tcl_HashEntry *entryPtr, *entryPtr1;
Tcl_HashTable *elemAtts;
TNC_ElemAttInfo *elemAttInfo;
TNC_AttDecl *attDecl;
TNC_EntityInfo *entityInfo;
int newPtr, start, i, clen;
char *copy;
entryPtr = Tcl_CreateHashEntry (tncdata->attDefsTables, elname, &newPtr);
if (newPtr) {
elemAttInfo = (TNC_ElemAttInfo *) MALLOC (sizeof (TNC_ElemAttInfo));
elemAtts = (Tcl_HashTable *) MALLOC (sizeof (Tcl_HashTable));
Tcl_InitHashTable (elemAtts, TCL_STRING_KEYS);
elemAttInfo->attributes = elemAtts;
elemAttInfo->nrOfreq = 0;
elemAttInfo->nrOfIdAtts = 0;
Tcl_SetHashValue (entryPtr, elemAttInfo);
} else {
elemAttInfo = (TNC_ElemAttInfo *) Tcl_GetHashValue (entryPtr);
elemAtts = elemAttInfo->attributes;
}
entryPtr = Tcl_CreateHashEntry (elemAtts, attname, &newPtr);
/* Multiple Attribute declarations are allowed, but later declarations
are ignored. See rec 3.3. */
if (newPtr) {
attDecl = (TNC_AttDecl *) MALLOC (sizeof (TNC_AttDecl));
if (strcmp (att_type, "CDATA") == 0) {
attDecl->att_type = TNC_ATTTYPE_CDATA;
}
else if (strcmp (att_type, "ID") == 0) {
if (elemAttInfo->nrOfIdAtts) {
signalNotValid (userData, TNC_ERROR_MORE_THAN_ONE_ID_ATT);
return;
}
elemAttInfo->nrOfIdAtts++;
if (dflt != NULL) {
signalNotValid (userData, TNC_ERROR_ID_ATT_DEFAULT);
return;
}
attDecl->att_type = TNC_ATTTYPE_ID;
}
else if (strcmp (att_type, "IDREF") == 0) {
attDecl->att_type = TNC_ATTTYPE_IDREF;
}
else if (strcmp (att_type, "IDREFS") == 0) {
attDecl->att_type = TNC_ATTTYPE_IDREFS;
}
else if (strcmp (att_type, "ENTITY") == 0) {
attDecl->att_type = TNC_ATTTYPE_ENTITY;
}
else if (strcmp (att_type, "ENTITIES") == 0) {
attDecl->att_type = TNC_ATTTYPE_ENTITIES;
}
else if (strcmp (att_type, "NMTOKEN") == 0) {
attDecl->att_type = TNC_ATTTYPE_NMTOKEN;
}
else if (strcmp (att_type, "NMTOKENS") == 0) {
attDecl->att_type = TNC_ATTTYPE_NMTOKENS;
}
else if (strncmp (att_type, "NOTATION(", 9) == 0) {
/* This is a bit puzzling. expat returns something like
<!NOTATION gif PUBLIC "gif">
<!ATTLIST c type NOTATION (gif) #IMPLIED>
as att_type "NOTATION(gif)". */
attDecl->att_type = TNC_ATTTYPE_NOTATION;
attDecl->lookupTable =
(Tcl_HashTable *) MALLOC (sizeof (Tcl_HashTable));
Tcl_InitHashTable (attDecl->lookupTable, TCL_STRING_KEYS);
copy = tdomstrdup (att_type);
start = i = 9;
while (i) {
if (copy[i] == ')') {
copy[i] = '\0';
#ifdef TNC_DEBUG
printf ("att type NOTATION: notation %s allowed\n",
©[start]);
#endif
Tcl_CreateHashEntry (attDecl->lookupTable,
©[start], &newPtr);
entryPtr1 = Tcl_CreateHashEntry (tncdata->notationDecls,
©[start], &newPtr);
#ifdef TNC_DEBUG
if (newPtr) {
printf ("up to now unknown NOTATION\n");
} else {
printf ("NOTATION already known\n");
}
#endif
FREE (copy);
break;
}
if (copy[i] == '|') {
copy[i] = '\0';
#ifdef TNC_DEBUG
printf ("att type NOTATION: notation %s allowed\n",
©[start]);
#endif
Tcl_CreateHashEntry (attDecl->lookupTable,
©[start], &newPtr);
entryPtr1 = Tcl_CreateHashEntry (tncdata->notationDecls,
©[start], &newPtr);
#ifdef TNC_DEBUG
if (newPtr) {
printf ("up to now unknown NOTATION\n");
} else {
printf ("NOTATION already known\n");
}
#endif
start = ++i;
continue;
}
clen = UTF8_CHAR_LEN (copy[i]);
CHECK_UTF_CHARLEN_COPY (clen);
if (!UTF8_GET_NAMING_NMTOKEN (©[i], clen)) {
signalNotValid (userData, TNC_ERROR_NMTOKEN_REQUIRED);
FREE (copy);
return;
}
i += clen;
}
}
else {
/* expat returns something like
<!ATTLIST a type ( numbered
|bullets ) #IMPLIED>
as att_type "(numbered|bullets)", e.g. in some
"non-official" normalized way.
Makes things easier for us. */
attDecl->att_type = TNC_ATTTYPE_ENUMERATION;
attDecl->lookupTable =
(Tcl_HashTable *) MALLOC (sizeof (Tcl_HashTable));
Tcl_InitHashTable (attDecl->lookupTable, TCL_STRING_KEYS);
copy = tdomstrdup (att_type);
start = i = 1;
while (1) {
if (copy[i] == ')') {
copy[i] = '\0';
Tcl_CreateHashEntry (attDecl->lookupTable,
©[start], &newPtr);
FREE (copy);
break;
}
if (copy[i] == '|') {
copy[i] = '\0';
Tcl_CreateHashEntry (attDecl->lookupTable,
©[start], &newPtr);
start = ++i;
continue;
}
clen = UTF8_CHAR_LEN (copy[i]);
CHECK_UTF_CHARLEN_COPY (clen);
if (!UTF8_GET_NAMING_NMTOKEN (©[i], clen)) {
signalNotValid (userData, TNC_ERROR_NMTOKEN_REQUIRED);
FREE (copy);
return;
}
i += clen;
}
}
if (dflt != NULL) {
switch (attDecl->att_type) {
case TNC_ATTTYPE_ENTITY:
case TNC_ATTTYPE_IDREF:
clen = UTF8_CHAR_LEN (*dflt);
CHECK_UTF_CHARLEN (clen);
if (!UTF8_GET_NAME_START (dflt, clen)) {
signalNotValid (userData, TNC_ERROR_NAME_REQUIRED);
return;
}
i = clen;
while (1) {
if (dflt[i] == '\0') {
break;
}
clen = UTF8_CHAR_LEN (dflt[i]);
CHECK_UTF_CHARLEN (clen);
if (!UTF8_GET_NAMING_NMTOKEN (&dflt[i], clen)) {
signalNotValid (userData, TNC_ERROR_NAME_REQUIRED);
return;
}
i += clen;
}
if (attDecl->att_type == TNC_ATTTYPE_ENTITY) {
entryPtr1 = Tcl_CreateHashEntry (tncdata->entityDecls,
dflt, &newPtr);
if (!newPtr) {
entityInfo =
(TNC_EntityInfo *) Tcl_GetHashValue (entryPtr1);
if (!entityInfo->is_notation) {
signalNotValid (userData,TNC_ERROR_ATT_ENTITY_DEFAULT_MUST_BE_DECLARED);
}
}
}
break;
case TNC_ATTTYPE_IDREFS:
start = i = 0;
while (1) {
if (dflt[i] == '\0') {
break;
}
if (dflt[i] == ' ') {
start = ++i;
}
if (start == i) {
clen = UTF8_CHAR_LEN (dflt[i]);
CHECK_UTF_CHARLEN (clen);
if (!UTF8_GET_NAME_START (&dflt[i], clen)) {
signalNotValid (userData, TNC_ERROR_NAME_REQUIRED);
return;
}
i += clen;
}
else {
clen = UTF8_CHAR_LEN (dflt[i]);
CHECK_UTF_CHARLEN (clen);
if (!UTF8_GET_NAMING_NMTOKEN (&dflt[i], clen)) {
signalNotValid (userData, TNC_ERROR_NAME_REQUIRED);
return;
}
i += clen;
}
}
break;
case TNC_ATTTYPE_ENTITIES:
copy = tdomstrdup (dflt);
start = i = 0;
while (1) {
if (copy[i] == '\0') {
FREE (copy);
break;
}
if (copy[i] == ' ') {
copy[i] = '\0';
entryPtr1 = Tcl_CreateHashEntry (tncdata->entityDecls,
©[start],
&newPtr);
if (!newPtr) {
entityInfo =
(TNC_EntityInfo *) Tcl_GetHashValue (entryPtr1);
if (!entityInfo->is_notation) {
signalNotValid (userData,TNC_ERROR_ATT_ENTITY_DEFAULT_MUST_BE_DECLARED);
}
}
start = ++i;
}
if (start == i) {
clen = UTF8_CHAR_LEN (copy[i]);
CHECK_UTF_CHARLEN_COPY (clen);
if (!UTF8_GET_NAME_START (©[i], clen)) {
signalNotValid (userData, TNC_ERROR_NAME_REQUIRED);
FREE (copy);
return;
}
i += clen;
}
else {
clen = UTF8_CHAR_LEN (copy[i]);
CHECK_UTF_CHARLEN_COPY (clen);
if (!UTF8_GET_NAMING_NMTOKEN (©[i], clen)) {
signalNotValid (userData, TNC_ERROR_NAME_REQUIRED);
FREE (copy);
return;
}
i += clen;
}
}
break;
case TNC_ATTTYPE_NMTOKEN:
i = 0;
while (1) {
if (dflt[i] == '\0') {
break;
}
clen = UTF8_CHAR_LEN (dflt[i]);
CHECK_UTF_CHARLEN (clen);
if (!UTF8_GET_NAMING_NMTOKEN (&dflt[i], clen)) {
signalNotValid (userData, TNC_ERROR_NMTOKEN_REQUIRED);
return;
}
i += clen;
}
if (!i) signalNotValid (userData, TNC_ERROR_NMTOKEN_REQUIRED);
break;
case TNC_ATTTYPE_NMTOKENS:
i = 0;
while (1) {
if (dflt[i] == '\0') {
break;
}
if (dflt[i] == ' ') {
i++;
}
clen = UTF8_CHAR_LEN (dflt[i]);
CHECK_UTF_CHARLEN (clen);
if (!UTF8_GET_NAMING_NMTOKEN (&dflt[i], clen)) {
signalNotValid (userData, TNC_ERROR_NMTOKEN_REQUIRED);
return;
}
i += clen;
}
if (!i) signalNotValid (userData, TNC_ERROR_NMTOKEN_REQUIRED);
break;
case TNC_ATTTYPE_NOTATION:
if (!Tcl_FindHashEntry (attDecl->lookupTable, dflt)) {
signalNotValid (userData, TNC_ERROR_IMPOSSIBLE_DEFAULT);
return;
}
case TNC_ATTTYPE_ENUMERATION:
if (!Tcl_FindHashEntry (attDecl->lookupTable, dflt)) {
signalNotValid (userData, TNC_ERROR_IMPOSSIBLE_DEFAULT);
return;
}
case TNC_ATTTYPE_CDATA:
case TNC_ATTTYPE_ID:
/* This both cases are only there, to pacify -Wall.
CDATA may have any allowed characters (and
everything else is detected by extpat). ID's not
allowed to have defaults (handled above). */
;
}
attDecl->dflt = tdomstrdup (dflt);
}
else {
attDecl->dflt = NULL;
}
if (isrequired) {
elemAttInfo->nrOfreq++;
}
attDecl->isrequired = isrequired;
Tcl_SetHashValue (entryPtr, attDecl);
}
}
#ifdef TNC_DEBUG
void
printNameIDs (TNC_Data *tncdata)
{
Tcl_HashEntry *entryPtr;
Tcl_HashSearch search;
for (entryPtr = Tcl_FirstHashEntry (tncdata->tagNames, &search);
entryPtr != NULL;
entryPtr = Tcl_NextHashEntry (&search)) {
printf ("name: %-20s nameId: %p\n",
Tcl_GetHashKey (tncdata->tagNames, entryPtr),
entryPtr);
}
}
void
printStackElm (TNC_ContentStack *stackelm)
{
if (stackelm->model->type == XML_CTYPE_NAME) {
printf ("\tmodel %p\tNAME: %p\n\tactiveChild %d\n\tdeep %d\n\talreadymatched %d\n",
stackelm->model, stackelm->model->nameId,
stackelm->activeChild, stackelm->deep, stackelm->alreadymatched);
}
else {
printf ("\tmodel %p\n\tactiveChild %d\n\tdeep %d\n\talreadymatched %d\n",
stackelm->model, stackelm->activeChild, stackelm->deep,
stackelm->alreadymatched);
}
}
void
printTNC_Content (TNC_Content *model)
{
printf ("TNC_Content..\n\ttype %d\n\tquant %d\n\tnameId %p\n\tnumchildren %d\n\tchildren %p\n", model->type, model->quant, model->nameId,
model->numchildren, model->children);
}
void
printContentStack (TNC_Data *tncdata)
{
TNC_ContentStack stackelm;
int i;
printf ("Current contentStack state (used stack slots %d):\n",
tncdata->contentStackPtr);
for (i = 0; i < tncdata->contentStackPtr; i++) {
stackelm = tncdata->contentStack[i];
printf ("%3d:\n", i);
printStackElm (&stackelm);
}
}
#endif /* TNC_DEBUG */
/*
*----------------------------------------------------------------------------
*
* TncProbeElement --
*
* This function checks, if the element match the
* topmost content model on the content stack.
*
* Results:
* 1 if the element match,
* 0 if not.
* -1 if not, but this isn't a validation error
*
* Side effects:
* Eventually pushes data to the contentStack (even in
* recurive calls).
*
*----------------------------------------------------------------------------
*/
static int
TncProbeElement (
TNC_NameId *nameId,
TNC_Data *tncdata
)
{
TNC_ContentStack *stackelm;
TNC_Content *activeModel;
int myStackPtr, zeroMatchPossible, result;
unsigned int i, seqstartindex;
#ifdef TNC_DEBUG
printf ("TncProbeElement start\n");
printContentStack (tncdata);
#endif
myStackPtr = tncdata->contentStackPtr - 1;
stackelm = &(tncdata->contentStack)[myStackPtr];
switch (stackelm->model->type) {
case XML_CTYPE_MIXED:
#ifdef TNC_DEBUG
printf ("TncProbeElement XML_CTYPE_MIXED\n");
#endif
for (i = 0; i < stackelm->model->numchildren; i++) {
if ((&stackelm->model->children[i])->nameId == nameId) {
return 1;
}
}
return 0;
case XML_CTYPE_ANY:
#ifdef TNC_DEBUG
printf ("TncProbeElement XML_CTYPE_ANY\n");
#endif
return 1;
case XML_CTYPE_EMPTY:
#ifdef TNC_DEBUG
printf ("TncProbeElement XML_CTYPE_EMPTY\n");
#endif
return 0;
case XML_CTYPE_CHOICE:
#ifdef TNC_DEBUG
printf ("TncProbeElement XML_CTYPE_CHOICE\n");
#endif
if (stackelm->alreadymatched) {
activeModel = &stackelm->model->children[stackelm->activeChild];
if (activeModel->type == XML_CTYPE_NAME) {
/* so this stackelement must be the topmost */
if (activeModel->quant == XML_CQUANT_REP
|| activeModel->quant == XML_CQUANT_PLUS) {
/* the last matched element is multiple, maybe it
matches again */
if (nameId == activeModel->nameId) {
#ifdef TNC_DEBUG
printf ("-->matched! child Nr. %d\n",
stackelm->activeChild);
#endif
/* stack and activeChild nr. are already OK, just
report success. */
return 1;
}
}
}
/* The active child is a SEQ or CHOICE. */
if (stackelm->model->quant == XML_CQUANT_NONE ||
stackelm->model->quant == XML_CQUANT_OPT) {
/*The child cp's type SEQ or CHOICE keep track by
themselve about if they are repeated. Because we are
here, they don't. Since the current cp has already
matched and isn't multiple, the current cp as a whole
is done. But no contradiction detected, so return
"search further" */
return -1;
}
}
/* If one of the alternatives within the CHOICE cp is quant
REP or OPT, it isn't a contradition to the document structure,
if the cp doesn't match, even if it is quant
NONE or PLUS, because of the "zero time" match of this one
alternative. We use zeroMatchPossible, to know about this.*/
zeroMatchPossible = 0;
for (i = 0; i < stackelm->model->numchildren; i++) {
if ((&stackelm->model->children[i])->type == XML_CTYPE_NAME) {
#ifdef TNC_DEBUG
printf ("child is type NAME\n");
#endif
if ((&stackelm->model->children[i])->nameId == nameId) {
#ifdef TNC_DEBUG
printf ("-->matched! child Nr. %d\n",i);
#endif
(&tncdata->contentStack[myStackPtr])->activeChild = i;
(&tncdata->contentStack[myStackPtr])->alreadymatched = 1;
return 1;
}
else {
/* If the name child is optional, we have a
candidat for "zero match". */
if ((&stackelm->model->children[i])->quant
== XML_CQUANT_OPT ||
(&stackelm->model->children[i])->quant
== XML_CQUANT_REP) {
#ifdef TNC_DEBUG
printf ("zero match possible\n");
#endif
zeroMatchPossible = 1;
}
}
}
else {
#ifdef TNC_DEBUG
printf ("complex child type\n");
#endif
if (tncdata->contentStackPtr == tncdata->contentStackSize) {
tncdata->contentStack = (TNC_ContentStack *)
Tcl_Realloc ((char *)tncdata->contentStack,
sizeof (TNC_Content *) * 2 *
tncdata->contentStackSize);
tncdata->contentStackSize *= 2;
}
(&tncdata->contentStack[tncdata->contentStackPtr])->model
= &stackelm->model->children[i];
tncdata->contentStack[tncdata->contentStackPtr].activeChild
= 0;
tncdata->contentStack[tncdata->contentStackPtr].deep
= stackelm->deep + 1;
tncdata->contentStack[tncdata->contentStackPtr].alreadymatched
= 0;
tncdata->contentStackPtr++;
result = TncProbeElement (nameId, tncdata);
if (result == 1) {
#ifdef TNC_DEBUG
printf ("-->matched! child nr. %d\n",i);
#endif
(&tncdata->contentStack[myStackPtr])->activeChild = i;
(&tncdata->contentStack[myStackPtr])->alreadymatched = 1;
return 1;
}
/* The child cp says, it doesn't has matched, but says
also, it's perfectly OK, if it doesn't at all. So we
have a candidat for "zero match". */
if (result == -1) {
zeroMatchPossible = 1;
}
tncdata->contentStackPtr--;
}
}
/* OK, nobody has claimed a match. Question is: try further or is
this a document structure error. */
if (zeroMatchPossible ||
stackelm->alreadymatched ||
stackelm->model->quant == XML_CQUANT_REP ||
stackelm->model->quant == XML_CQUANT_OPT) {
return -1;
}
#ifdef TNC_DEBUG
printf ("validation error\n");
#endif
return 0;
case XML_CTYPE_SEQ:
#ifdef TNC_DEBUG
printf ("TncProbeElement XML_CTYPE_SEQ\n");
#endif
if (stackelm->alreadymatched) {
activeModel = &stackelm->model->children[stackelm->activeChild];
if (activeModel->type == XML_CTYPE_NAME) {
/* so this stackelement must be the topmost */
if (activeModel->quant == XML_CQUANT_REP
|| activeModel->quant == XML_CQUANT_PLUS) {
/* the last matched element is multiple, maybe it
matches again */
if (nameId == activeModel->nameId) {
#ifdef TNC_DEBUG
printf ("-->matched! child Nr. %d\n",
stackelm->activeChild);
#endif
/* stack and activeChild nr. are already OK, just
report success. */
return 1;
}
}
}
}
if (stackelm->alreadymatched) {
seqstartindex = stackelm->activeChild + 1;
}
else {
seqstartindex = 0;
}
/* This time zeroMatchPossible flags, if every of the remaining
children - that may every child, if !alreadymatched - doesn't
must occur. We assume, the (outstanding children of, in case
of alreadymatched) current stackelement model has only
optional children, and set to false, if we find any
non-optional child */
zeroMatchPossible = 1;
for (i = seqstartindex; i < stackelm->model->numchildren; i++) {
if ((&stackelm->model->children[i])->type == XML_CTYPE_NAME) {
if ((&stackelm->model->children[i])->nameId == nameId) {
#ifdef TNC_DEBUG
printf ("-->matched! child Nr. %d\n",i);
#endif
(&tncdata->contentStack[myStackPtr])->activeChild = i;
(&tncdata->contentStack[myStackPtr])->alreadymatched = 1;
return 1;
} else if ((&stackelm->model->children[i])->quant
== XML_CQUANT_NONE
|| (&stackelm->model->children[i])->quant
== XML_CQUANT_PLUS) {
zeroMatchPossible = 0;
break;
}
} else {
if (tncdata->contentStackPtr == tncdata->contentStackSize) {
tncdata->contentStack = (TNC_ContentStack *)
Tcl_Realloc ((char *)tncdata->contentStack,
sizeof (TNC_Content *) * 2 *
tncdata->contentStackSize);
tncdata->contentStackSize *= 2;
}
(&tncdata->contentStack[tncdata->contentStackPtr])->model =
&stackelm->model->children[i];
tncdata->contentStack[tncdata->contentStackPtr].activeChild
= 0;
tncdata->contentStack[tncdata->contentStackPtr].deep
= stackelm->deep + 1;
tncdata->contentStack[tncdata->contentStackPtr].alreadymatched
= 0;
tncdata->contentStackPtr++;
result = TncProbeElement (nameId, tncdata);
if (result == 1) {
(&tncdata->contentStack[myStackPtr])->activeChild = i;
(&tncdata->contentStack[myStackPtr])->alreadymatched = 1;
return 1;
}
tncdata->contentStackPtr--;
if (result == 0) {
zeroMatchPossible = 0;
break;
}
}
}
if (!stackelm->alreadymatched) {
if (zeroMatchPossible) {
/* The stackelm hasn't matched, but don't have to
after all. Return try further */
return -1;
} else {
/* No previous match, but at least one child is
necessary. Return depends of the quant of the
entire seq */
if (stackelm->model->quant == XML_CQUANT_NONE ||
stackelm->model->quant == XML_CQUANT_PLUS) {
/* DTD claims, the seq as to be there, but isn't */
return 0;
} else {
/* The seq is optional */
return -1;
}
}
}
if (stackelm->alreadymatched) {
if (!zeroMatchPossible) {
/* Some child at the start of the seq has matched in
the past, but since zeroMatchPossible has changed
to zero, there must be a non-matching non-optional
child later. Error in document structure. */
return 0;
} else {
/* OK, SEQ has matched before. But after the last match, there
where no required (quant NONE or PLUS) children. */
if (stackelm->model->quant == XML_CQUANT_NONE ||
stackelm->model->quant == XML_CQUANT_OPT) {
/* The entire seq isn't multiple. Just look further. */
return -1;
}
}
}
/* The last untreated case is alreadymatched true,
zeroMatchPossible (of the rest of the seq children after the
last match) true and the entire seq may be
multiple. Therefore, start again with activeChild = 0, to
see, if the current nameId starts a repeated match of the
seq. By the way: zeroMatchPossible still has initial value
1, therefore, no second initialiation is needed */
for (i = 0; i < seqstartindex; i++) {
if ((&stackelm->model->children[i])->type == XML_CTYPE_NAME) {
if ((&stackelm->model->children[i])->nameId == nameId) {
#ifdef TNC_DEBUG
printf ("-->matched! child Nr. %d\n",i);
#endif
(&tncdata->contentStack[myStackPtr])->activeChild = i;
(&tncdata->contentStack[myStackPtr])->alreadymatched = 1;
return 1;
} else if ((&stackelm->model->children[i])->quant
== XML_CQUANT_NONE
|| (&stackelm->model->children[i])->quant
== XML_CQUANT_PLUS) {
zeroMatchPossible = 0;
break;
}
} else {
if (tncdata->contentStackPtr == tncdata->contentStackSize) {
tncdata->contentStack = (TNC_ContentStack *)
Tcl_Realloc ((char *)tncdata->contentStack,
sizeof (TNC_Content *) * 2 *
tncdata->contentStackSize);
tncdata->contentStackSize *= 2;
}
(&tncdata->contentStack[tncdata->contentStackPtr])->model =
&stackelm->model->children[i];
tncdata->contentStack[tncdata->contentStackPtr].activeChild
= 0;
tncdata->contentStack[tncdata->contentStackPtr].deep
= stackelm->deep + 1;
tncdata->contentStack[tncdata->contentStackPtr].alreadymatched
= 0;
tncdata->contentStackPtr++;
result = TncProbeElement (nameId, tncdata);
if (result) {
(&tncdata->contentStack[myStackPtr])->activeChild = i;
/* alreadymatched is already 1 */
return 1;
}
tncdata->contentStackPtr--;
if (result == 0) {
/* OK, the seq doesn't match again. But since it have
already matched, this isn't return 0 but.. */
return -1;
}
}
}
/* seq doesn't match again and every seq child from the very first
up to (not including) the last match aren't required. This last
fact may be nice to know, but after all since the entire seq have
matched already ... */
return -1;
case XML_CTYPE_NAME:
/* NAME type doesn't occur at top level of a content model and is
handled in some "shotcut" way directly in the CHOICE and SEQ cases.
It's only here to pacify gcc -Wall. */
printf ("error!!! - in TncProbeElement: XML_CTYPE_NAME shouldn't reached in any case.\n");
default:
printf ("error!!! - in TncProbeElement: unknown content type: %d\n",
stackelm->model->type);
}
/* not reached */
printf ("error!!! - in TncProbeElement: end of function reached.\n");
return 0;
}
/*
*----------------------------------------------------------------------------
*
* TncProbeAttribute --
*
* This function checks, if the given attribute
* and it's value are allowed for this element.
*
* Results:
* 1 if the attribute name/value is OK,
* 0 if not.
*
* Side effects:
* Eventually increments the required attributes counter.
*
*----------------------------------------------------------------------------
*/
static int
TncProbeAttribute (
void *userData,
Tcl_HashTable *elemAtts,
char *attrName,
char *attrValue,
int *nrOfreq
)
{
TNC_Data *tncdata = (TNC_Data *) userData;
Tcl_HashEntry *entryPtr;
TNC_AttDecl *attDecl;
char *pc, *copy, save;
int clen, i, start, hnew;
TNC_EntityInfo *entityInfo;
entryPtr = Tcl_FindHashEntry (elemAtts, attrName);
if (!entryPtr) {
signalNotValid (userData, TNC_ERROR_UNKNOWN_ATTRIBUTE);
return 0;
}
/* NOTE: attribute uniqueness per element is a wellformed
constrain and therefore done by expat. */
attDecl = (TNC_AttDecl *) Tcl_GetHashValue (entryPtr);
switch (attDecl->att_type) {
case TNC_ATTTYPE_CDATA:
if (attDecl->isrequired && attDecl->dflt) {
if (strcmp (attDecl->dflt, attrValue) != 0) {
signalNotValid (userData,
TNC_ERROR_WRONG_FIXED_ATTVALUE);
return 0;
}
}
break;
case TNC_ATTTYPE_ID:
pc = (char*)attrValue;
clen = UTF8_CHAR_LEN (*pc);
CHECK_UTF_CHARLENR (clen);
if (!UTF8_GET_NAME_START (pc, clen)) {
signalNotValid (userData, TNC_ERROR_NAME_REQUIRED);
}
pc += clen;
while (1) {
if (*pc == '\0') {
break;
}
clen = UTF8_CHAR_LEN (*pc);
CHECK_UTF_CHARLENR (clen);
if (!UTF8_GET_NAMING_NMTOKEN (pc, clen)) {
signalNotValid (userData, TNC_ERROR_NAME_REQUIRED);
return 0;
}
pc += clen;
}
entryPtr = Tcl_CreateHashEntry (tncdata->ids, attrValue, &hnew);
if (!hnew) {
if (Tcl_GetHashValue (entryPtr)) {
signalNotValid (userData,
TNC_ERROR_DUPLICATE_ID_VALUE);
return 0;
}
}
Tcl_SetHashValue (entryPtr, (char *) 1);
break;
case TNC_ATTTYPE_IDREF:
/* Name type constraint "implicit" checked. If the
referenced ID exists, the type must be OK, because the
type of the ID's within the document are checked.
If there isn't such an ID, it's an error anyway. */
if (attrValue[0] == '\0') {
signalNotValid (userData, TNC_ERROR_NAME_REQUIRED);
return 0;
}
entryPtr = Tcl_CreateHashEntry (tncdata->ids, attrValue, &hnew);
break;
case TNC_ATTTYPE_IDREFS:
if (attrValue[0] == '\0') {
signalNotValid (userData, TNC_ERROR_NAMES_REQUIRED);
return 0;
}
/* Due to attribute value normalization (xml rec 3.3.3) this
is a simple list "ref ref ref ..." without leading or
trailing spaces and exact one space between the refs. */
start = i = 0;
while (attrValue[i]) {
if (attrValue[i] == ' ') {
save = attrValue[i];
attrValue[i] = '\0';
entryPtr = Tcl_CreateHashEntry (tncdata->ids,
&attrValue[start], &hnew);
attrValue[i] = save;
start = ++i;
continue;
}
i++;
}
entryPtr = Tcl_CreateHashEntry (tncdata->ids, &attrValue[start],
&hnew);
break;
case TNC_ATTTYPE_ENTITY:
/* There is a validity constraint requesting entity attributes
values to be type Name. But if there would be an entity
declaration that doesn't fit this constraint, expat would
have already complained about the definition. So we go the
easy way and just look up the att value. If it's declared,
type must be OK, if not, it's an error anyway. */
entryPtr = Tcl_FindHashEntry (tncdata->entityDecls, attrValue);
if (!entryPtr) {
signalNotValid (userData, TNC_ERROR_ENTITY_ATTRIBUTE);
return 0;
}
entityInfo = (TNC_EntityInfo *) Tcl_GetHashValue (entryPtr);
if (!entityInfo->is_notation) {
signalNotValid (userData, TNC_ERROR_ENTITY_ATTRIBUTE);
return 0;
}
break;
case TNC_ATTTYPE_ENTITIES:
/* Normalized by exapt; for type see comment to
TNC_ATTTYPE_ENTITY */
copy = tdomstrdup (attrValue);
start = i = 0;
while (1) {
if (copy[i] == '\0') {
entryPtr = Tcl_FindHashEntry (tncdata->entityDecls,
©[start]);
if (!entryPtr) {
signalNotValid (userData, TNC_ERROR_ENTITIES_ATTRIBUTE);
FREE (copy);
return 0;
}
entityInfo = (TNC_EntityInfo *) Tcl_GetHashValue (entryPtr);
if (!entityInfo->is_notation) {
signalNotValid (userData, TNC_ERROR_ENTITIES_ATTRIBUTE);
FREE (copy);
return 0;
}
FREE (copy);
break;
}
if (copy[i] == ' ') {
copy[i] = '\0';
entryPtr = Tcl_FindHashEntry (tncdata->entityDecls,
©[start]);
if (!entryPtr) {
signalNotValid (userData, TNC_ERROR_ENTITIES_ATTRIBUTE);
FREE (copy);
return 0;
}
entityInfo = (TNC_EntityInfo *) Tcl_GetHashValue (entryPtr);
if (!entityInfo->is_notation) {
signalNotValid (userData, TNC_ERROR_ENTITIES_ATTRIBUTE);
FREE (copy);
return 0;
}
start = ++i;
continue;
}
i++;
}
break;
case TNC_ATTTYPE_NMTOKEN:
/* We assume, that the UTF-8 representation of the value is
valid (no partial chars, minimum encoding). This makes
things a little more easy and faster. I guess (but
haven't deeply checked - QUESTION -), expat would have
already complained otherwise. */
pc = (char*)attrValue;
clen = 0;
while (1) {
if (*pc == '\0') {
break;
}
clen = UTF8_CHAR_LEN (*pc);
CHECK_UTF_CHARLENR (clen);
if (!UTF8_GET_NAMING_NMTOKEN (pc, clen)) {
signalNotValid (userData, TNC_ERROR_NMTOKEN_REQUIRED);
return 0;
}
pc += clen;
}
if (!clen)
signalNotValid (userData, TNC_ERROR_NMTOKEN_REQUIRED);
break;
case TNC_ATTTYPE_NMTOKENS:
pc = (char*)attrValue;
clen = 0;
while (1) {
if (*pc == '\0') {
break;
}
/* NMTOKENS are normalized by expat, so this should
be secure. */
if (*pc == ' ') {
pc++;
}
clen = UTF8_CHAR_LEN (*pc);
CHECK_UTF_CHARLENR (clen);
if (!UTF8_GET_NAMING_NMTOKEN (pc, clen)) {
signalNotValid (userData, TNC_ERROR_NMTOKEN_REQUIRED);
return 0;
}
pc += clen;
}
if (!clen)
signalNotValid (userData, TNC_ERROR_NMTOKEN_REQUIRED);
break;
case TNC_ATTTYPE_NOTATION:
entryPtr = Tcl_FindHashEntry (attDecl->lookupTable, attrValue);
if (!entryPtr) {
signalNotValid (userData, TNC_ERROR_NOTATION_REQUIRED);
return 0;
}
break;
case TNC_ATTTYPE_ENUMERATION:
if (!Tcl_FindHashEntry (attDecl->lookupTable, attrValue)) {
signalNotValid (userData, TNC_ERROR_ENUM_ATT_WRONG_VALUE);
return 0;
}
break;
}
if (attDecl->isrequired) {
(*nrOfreq)++;
}
return 1;
}
/*
*----------------------------------------------------------------------------
*
* TncElementStartCommand --
*
* This procedure is called for every element start event
* while parsing XML Data with a "tnc" enabled tclexpat
* parser. Checks, if the element can occur here and if it
* has an acceptable set of attributes.
*
* Results:
* None.
*
* Side effects:
* Eventually signals application error.
*
*----------------------------------------------------------------------------
*/
void
TncElementStartCommand (
void *userData,
const char *name,
const char **atts
)
{
TNC_Data *tncdata = (TNC_Data *) userData;
Tcl_HashEntry *entryPtr;
Tcl_HashTable *elemAtts;
const char **atPtr;
TNC_ElemAttInfo *elemAttInfo;
TNC_Content *model;
int result, nrOfreq, acceptNoDoctype = 0;
#ifdef TNC_DEBUG
printf ("TncElementStartCommand name: %s\n", name);
#endif
/* If the document doesn't have a doctype declaration, but the
user have used the -useForeignDTD 1 feature, the collected
data out of the provided DTD isn't postprocessed by
TncElementStartCommand. We do this now.
NOTE: Since there wasn't a doctype declaration, there is no
information available which element is expected to be the
document element. Eventually it would be desirable, to set
this somehow. For now, this means, that every valid subtree
of the given DTD information is accepted. */
if (!tncdata->contentStackPtr && !tncdata->elemContentsRewriten) {
TncEndDoctypeDeclHandler (userData);
acceptNoDoctype = 1;
}
entryPtr = Tcl_FindHashEntry (tncdata->tagNames, name);
if (!entryPtr) {
signalNotValid (userData, TNC_ERROR_UNKNOWN_ELEMENT);
return;
}
model = (TNC_Content *) Tcl_GetHashValue (entryPtr);
switch (model->type) {
case XML_CTYPE_MIXED:
case XML_CTYPE_ANY:
tncdata->skipWhiteCDATAs = 1;
tncdata->ignorePCDATA = 1;
break;
case XML_CTYPE_EMPTY:
tncdata->skipWhiteCDATAs = 0;
break;
case XML_CTYPE_CHOICE:
case XML_CTYPE_SEQ:
tncdata->skipWhiteCDATAs = 1;
tncdata->ignorePCDATA = 0;
break;
case XML_CTYPE_NAME:
break;
}
if (tncdata->contentStackPtr) {
/* This is the normal case, within some content,
at least the root element content. */
while (1) {
result = TncProbeElement (entryPtr, tncdata);
if (result == -1) {
if (tncdata->contentStack[tncdata->contentStackPtr - 1].deep
== 0) {
signalNotValid (userData,
TNC_ERROR_ELEMENT_NOT_ALLOWED_HERE);
return;
}
tncdata->contentStackPtr--;
continue;
}
if (result) {
break;
}
if (!result) {
signalNotValid (userData, TNC_ERROR_ELEMENT_NOT_ALLOWED_HERE);
return;
}
}
if (tncdata->contentStackPtr == tncdata->contentStackSize) {
tncdata->contentStackSize *= 2;
tncdata->contentStack = (TNC_ContentStack *)
Tcl_Realloc ((char *)tncdata->contentStack,
sizeof (TNC_Content *)*tncdata->contentStackSize);
}
(&tncdata->contentStack[tncdata->contentStackPtr])->model = model;
(&tncdata->contentStack[tncdata->contentStackPtr])->activeChild = 0;
(&tncdata->contentStack[tncdata->contentStackPtr])->deep = 0;
(&tncdata->contentStack[tncdata->contentStackPtr])->alreadymatched = 0;
tncdata->contentStackPtr++;
} else {
/* This is only in case of the root element */
if (atts) {
if (!tncdata->doctypeName) {
if (!acceptNoDoctype) {
signalNotValid (userData, TNC_ERROR_NO_DOCTYPE_DECL);
return;
}
} else {
if (strcmp (tncdata->doctypeName, name) != 0) {
signalNotValid (userData, TNC_ERROR_WRONG_ROOT_ELEMENT);
return;
}
}
}
(&(tncdata->contentStack)[0])->model = model;
(&(tncdata->contentStack)[0])->activeChild = 0;
(&(tncdata->contentStack)[0])->deep = 0;
(&(tncdata->contentStack)[0])->alreadymatched = 0;
tncdata->contentStackPtr++;
}
if (atts) {
elemAttInfo = model->attInfo;
if (!elemAttInfo) {
if (atts[0] != NULL) {
signalNotValid (userData, TNC_ERROR_NO_ATTRIBUTES);
return;
}
} else {
elemAtts = elemAttInfo->attributes;
nrOfreq = 0;
for (atPtr = atts; atPtr[0]; atPtr += 2) {
if (!TncProbeAttribute (userData, elemAtts, (char *) atPtr[0],
(char *) atPtr[1], &nrOfreq))
return;
}
if (nrOfreq != elemAttInfo->nrOfreq) {
signalNotValid (userData,
TNC_ERROR_MISSING_REQUIRED_ATTRIBUTE);
return;
}
}
} else {
tncdata->elemAttInfo = model->attInfo;
}
#ifdef TNC_DEBUG
printf ("TncElementStartCommand end\n");
#endif
}
/*
*----------------------------------------------------------------------------
*
* TncProbeElementEnd --
*
* This procedure checks, if the current content allows the
* the element to end here.
*
* Results:
* 1 if element end is OK,
* 0 if not.
*
* Side effects:
* Let the contentStackPtr point to the last current content
* model before the element had started.
*
*----------------------------------------------------------------------------
*/
static int
TncProbeElementEnd (
TNC_Data *tncdata
)
{
TNC_ContentStack stackelm;
unsigned int i;
int zeroMatchPossible, seqstartindex;
stackelm = tncdata->contentStack[tncdata->contentStackPtr - 1];
switch (stackelm.model->type) {
case XML_CTYPE_MIXED:
case XML_CTYPE_ANY:
case XML_CTYPE_EMPTY:
return 1;
case XML_CTYPE_CHOICE:
if (stackelm.alreadymatched) {
return 1;
}
if (stackelm.model->quant == XML_CQUANT_REP ||
stackelm.model->quant == XML_CQUANT_OPT) {
return 1;
}
zeroMatchPossible = 0;
for (i = 0; i < stackelm.model->numchildren; i++) {
if ((&stackelm.model->children[i])->type == XML_CTYPE_NAME) {
if ((&stackelm.model->children[i])->quant == XML_CQUANT_OPT ||
(&stackelm.model->children[i])->quant == XML_CQUANT_REP) {
zeroMatchPossible = 1;
break;
}
}
else {
if (tncdata->contentStackPtr == tncdata->contentStackSize) {
tncdata->contentStack = (TNC_ContentStack *)
Tcl_Realloc ((char *)tncdata->contentStack,
sizeof (TNC_Content *) * 2 *
tncdata->contentStackSize);
tncdata->contentStackSize *= 2;
}
(&tncdata->contentStack[tncdata->contentStackPtr])->model
= &stackelm.model->children[i];
tncdata->contentStack[tncdata->contentStackPtr].activeChild
= 0;
tncdata->contentStack[tncdata->contentStackPtr].deep
= stackelm.deep + 1;
tncdata->contentStack[tncdata->contentStackPtr].alreadymatched
= 0;
tncdata->contentStackPtr++;
if (TncProbeElementEnd (tncdata)) {
zeroMatchPossible = 1;
tncdata->contentStackPtr--;
break;
}
tncdata->contentStackPtr--;
}
}
if (zeroMatchPossible) {
return 1;
} else {
return 0;
}
case XML_CTYPE_SEQ:
if (!stackelm.alreadymatched) {
if (stackelm.model->quant == XML_CQUANT_REP ||
stackelm.model->quant == XML_CQUANT_OPT) {
return 1;
}
}
if (!stackelm.alreadymatched) {
seqstartindex = 0;
}
else {
seqstartindex = stackelm.activeChild + 1;
}
for (i = seqstartindex; i < stackelm.model->numchildren; i++) {
if ((&stackelm.model->children[i])->type == XML_CTYPE_NAME) {
if ((&stackelm.model->children[i])->quant == XML_CQUANT_OPT ||
(&stackelm.model->children[i])->quant == XML_CQUANT_REP) {
continue;
} else {
return 0;
}
} else {
if (tncdata->contentStackPtr == tncdata->contentStackSize) {
tncdata->contentStack = (TNC_ContentStack *)
Tcl_Realloc ((char *)tncdata->contentStack,
sizeof (TNC_Content *) * 2 *
tncdata->contentStackSize);
tncdata->contentStackSize *= 2;
}
(&tncdata->contentStack[tncdata->contentStackPtr])->model
= &stackelm.model->children[i];
tncdata->contentStack[tncdata->contentStackPtr].activeChild
= 0;
tncdata->contentStack[tncdata->contentStackPtr].deep
= stackelm.deep + 1;
tncdata->contentStack[tncdata->contentStackPtr].alreadymatched
= 0;
tncdata->contentStackPtr++;
if (TncProbeElementEnd (tncdata)) {
tncdata->contentStackPtr--;
continue;
}
else {
tncdata->contentStackPtr--;
return 0;
}
}
}
return 1;
case XML_CTYPE_NAME:
/* NAME type doesn't occur at top level of a content model and is
handled in some "shotcut" way directly in the CHOICE and SEQ cases.
It's only here to pacify gcc -Wall. */
fprintf (stderr, "error!!! - in TncProbeElementEnd: XML_CTYPE_NAME "
"shouldn't be reached in any case.\n");
default:
fprintf (stderr, "error!!! - in TncProbeElementEnd: unknown content "
"type: %d\n", stackelm.model->type);
return 1;
}
}
/*
*----------------------------------------------------------------------------
*
* TncElementEndCommand --
*
* This procedure is called for every element end event
* while parsing XML Data with a "tnc" enabled tclexpat
* parser. Checks, if the content model allows the element
* to end at this point.
*
* Results:
* None.
*
* Side effects:
* Eventually signals application error.
*
*----------------------------------------------------------------------------
*/
void
TncElementEndCommand (
void *userData,
const char *name
)
{
TNC_Data *tncdata = (TNC_Data *) userData;
Tcl_HashEntry *entryPtr;
Tcl_HashSearch search;
#ifdef TNC_DEBUG
printf ("TncElementEndCommand start\n");
printContentStack (tncdata);
#endif
while (1) {
if (!TncProbeElementEnd (tncdata)) {
signalNotValid (userData, TNC_ERROR_ELEMENT_CAN_NOT_END_HERE);
return;
}
if (tncdata->contentStack[tncdata->contentStackPtr - 1].deep == 0) {
break;
}
tncdata->contentStackPtr--;
}
/* Remove the content model of the closed element from the stack */
tncdata->contentStackPtr--;
#ifdef TNC_DEBUG
printf ("after removing ended element from the stack\n");
printContentStack (tncdata);
#endif
if (tncdata->contentStackPtr) {
switch ((&tncdata->contentStack[tncdata->contentStackPtr - 1])->model->type) {
case XML_CTYPE_MIXED:
case XML_CTYPE_ANY:
tncdata->skipWhiteCDATAs = 1;
tncdata->ignorePCDATA = 1;
break;
case XML_CTYPE_EMPTY:
tncdata->skipWhiteCDATAs = 0;
break;
case XML_CTYPE_CHOICE:
case XML_CTYPE_SEQ:
case XML_CTYPE_NAME:
tncdata->skipWhiteCDATAs = 1;
tncdata->ignorePCDATA = 0;
break;
}
} else {
/* This means, the root element is closed,
therefore, the place to check, if every IDREF points
to an ID. */
if (tncdata->idCheck) {
for (entryPtr = Tcl_FirstHashEntry (tncdata->ids, &search);
entryPtr != NULL;
entryPtr = Tcl_NextHashEntry (&search)) {
#ifdef TNC_DEBUG
printf ("check id value %s\n",
Tcl_GetHashKey (tncdata->ids, entryPtr));
printf ("value %p\n", Tcl_GetHashValue (entryPtr));
#endif
if (!Tcl_GetHashValue (entryPtr)) {
signalNotValid (userData, TNC_ERROR_UNKNOWN_ID_REFERRED);
return;
}
}
}
}
}
/*
*----------------------------------------------------------------------------
*
* TncCharacterdataCommand --
*
* This procedure is called with a piece of CDATA found in
* document.
*
* Results:
* None.
*
* Side effects:
* Eventually signals application error.
*
*----------------------------------------------------------------------------
*/
void
TncCharacterdataCommand (
void *userData,
const char *data,
domLength len
)
{
TNC_Data *tncdata = (TNC_Data *) userData;
int i;
char *pc;
if (!tncdata->skipWhiteCDATAs && len > 0) {
signalNotValid (userData, TNC_ERROR_EMPTY_ELEMENT);
return;
}
if (!tncdata->ignorePCDATA) {
for (i = 0, pc = (char*)data; i < len; i++, pc++) {
if ( (*pc == ' ') ||
(*pc == '\n') ||
(*pc == '\r') ||
(*pc == '\t') ) {
continue;
}
signalNotValid (userData, TNC_ERROR_DISALLOWED_PCDATA);
return;
}
}
}
/*
*----------------------------------------------------------------------------
*
* TncStartCdataSectionHandler --
*
* This procedure is called at the start of a CDATA section
* within the document.
*
* Results:
* None.
*
* Side effects:
* Eventually signals application error.
*
*----------------------------------------------------------------------------
*/
void
TncStartCdataSectionHandler (
void *userData
)
{
TNC_Data *tncdata = (TNC_Data *) userData;
if (!tncdata->ignorePCDATA) {
signalNotValid (userData, TNC_ERROR_DISALLOWED_CDATA);
}
}
/*
*----------------------------------------------------------------------------
*
* validateNodeAttributes --
*
* Validates the attributes of the given domNode. The domNode must be
* an ELEMENT_NODE.
*
* Results:
* 1 if OK, 0 for validation error.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
static int
validateNodeAttributes (
TNC_Data *tncdata,
TNC_ElemAttInfo *elemAttInfo,
domNode *node
)
{
int nrOfreq;
domAttrNode *attr;
if (!elemAttInfo) {
if (node->firstAttr) {
signalNotValid (tncdata, TNC_ERROR_NO_ATTRIBUTES);
return 0;
}
} else {
attr = node->firstAttr;
nrOfreq = 0;
while (attr) {
if (!TncProbeAttribute (tncdata,
elemAttInfo->attributes,
attr->nodeName,
attr->nodeValue,
&nrOfreq))
return 0;
attr = attr->nextSibling;
}
if (nrOfreq != elemAttInfo->nrOfreq) {
signalNotValid (tncdata,
TNC_ERROR_MISSING_REQUIRED_ATTRIBUTE);
return 0;
}
}
return 1;
}
/*
*----------------------------------------------------------------------------
*
* validateTree --
*
* Validates a complete DOM (sub-)tree against the DTD information in
* the given tncdata structure. The node argument acts as root of the
* (sub-)tree.
*
* Results:
* 1 if OK, 0 for validation error.
*
* Side effects:
* May alter the context state part of the tnc clientData (and even
* mallocs additional memory for them).
*
*----------------------------------------------------------------------------
*/
static int validateTree (
TNC_Data *tncdata,
domNode *node
)
{
domNode *child;
switch (node->nodeType) {
case ELEMENT_NODE:
TncElementStartCommand (tncdata, node->nodeName, NULL);
if (tncdata->dtdstatus) return 0;
if (!validateNodeAttributes (tncdata, tncdata->elemAttInfo, node))
return 0;
if (node->firstChild) {
child = node->firstChild;
while (child) {
if (!validateTree (tncdata, child)) return 0;
child = child->nextSibling;
}
}
TncElementEndCommand (tncdata, node->nodeName);
if (tncdata->dtdstatus) return 0;
break;
case TEXT_NODE:
case CDATA_SECTION_NODE:
TncCharacterdataCommand (tncdata, ((domTextNode*)node)->nodeValue,
((domTextNode*)node)->valueLength);
if (tncdata->dtdstatus) return 0;
break;
case COMMENT_NODE:
case PROCESSING_INSTRUCTION_NODE:
break;
default:
signalNotValid (tncdata, TNC_ERROR_UNKNOWN_NODE_TYPE);
return 0;
}
return 1;
}
/*
*----------------------------------------------------------------------------
*
* tnc_ValidateObjCmd
*
* Implements the validateObjCmds. See the user documentation
* for details.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* May alter some parts of the tnc_ValidateObjCmd clientData
* structure.
*
*----------------------------------------------------------------------------
*/
static int
tnc_ValidateObjCmd (
ClientData clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *const objv[]
)
{
TNC_Data *tncdata = (TNC_Data*) clientData;
int methodIndex, result = 1;
domNode *node;
char *errMsg = NULL;
Tcl_HashEntry *entryPtr;
TNC_Content *model;
static const char *validateMethods[] = {
"validateTree", "validateDocument", "validateAttributes",
"delete",
NULL
};
enum validateMethod {
m_validateTree, m_validateDocument, m_validateAttributes,
m_delete
};
if (objc < 2 || objc > 4) {
SetResult (validateCmd_usage);
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj (interp, objv[1], validateMethods, "method", 0,
&methodIndex) != TCL_OK) {
return TCL_ERROR;
}
switch ((enum validateMethod) methodIndex) {
case m_validateTree:
if (objc < 3 || objc > 4) {
SetResult (validateCmd_usage);
return TCL_ERROR;
}
node = tcldom_getNodeFromName (
interp, Tcl_GetStringFromObj(objv[2], NULL), &errMsg
);
if (!node || (node->nodeType != ELEMENT_NODE)) {
SetResult ("The validateTree method needs a domNode as argument.");
return TCL_ERROR;
}
tncdata->dtdstatus = 0;
tncdata->idCheck = 0;
if (tncdata->ids->numEntries) {
Tcl_DeleteHashTable (tncdata->ids);
Tcl_InitHashTable (tncdata->ids, TCL_STRING_KEYS);
}
tncdata->contentStackPtr = 0;
Tcl_ResetResult (interp);
result = validateTree (tncdata, node);
if (objc == 4) {
if (Tcl_ObjSetVar2(interp, objv[3], NULL,
Tcl_GetObjResult(interp), 0) == NULL) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"couldn't save msg in variable", -1);
return TCL_ERROR;
}
}
if (result) {
SetBooleanResult (1);
} else {
SetBooleanResult (0);
}
break;
case m_validateDocument:
if (objc < 3 || objc > 4) {
SetResult (validateCmd_usage);
return TCL_ERROR;
}
node = (domNode *) tcldom_getDocumentFromName (
interp, Tcl_GetStringFromObj (objv[2], NULL), &errMsg
);
if (!node) {
SetResult ("The validateDocument method needs a domDocument as argument.");
return TCL_ERROR;
}
node = ((domDocument *) node)->documentElement;
if (!tncdata->doctypeName) {
signalNotValid (tncdata, TNC_ERROR_NO_DOCTYPE_DECL);
if (objc == 4) {
if (Tcl_ObjSetVar2(interp, objv[3], NULL,
Tcl_GetObjResult(interp), 0) == NULL) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"couldn't save msg in variable", -1);
return TCL_ERROR;
}
}
SetBooleanResult (0);
return TCL_OK;
}
if (strcmp (tncdata->doctypeName, node->nodeName) != 0) {
signalNotValid (tncdata, TNC_ERROR_WRONG_ROOT_ELEMENT);
if (objc == 4) {
if (Tcl_ObjSetVar2(interp, objv[3], NULL,
Tcl_GetObjResult(interp), 0) == NULL) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"couldn't save msg in variable", -1);
return TCL_ERROR;
}
}
SetBooleanResult (0);
return TCL_OK;
}
tncdata->dtdstatus = 0;
tncdata->idCheck = 1;
if (tncdata->ids->numEntries) {
Tcl_DeleteHashTable (tncdata->ids);
Tcl_InitHashTable (tncdata->ids, TCL_STRING_KEYS);
}
tncdata->contentStackPtr = 0;
Tcl_ResetResult (interp);
result = validateTree (tncdata, node);
if (objc == 4) {
if (Tcl_ObjSetVar2(interp, objv[3], NULL,
Tcl_GetObjResult(interp), 0) == NULL) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"couldn't save msg in variable", -1);
return TCL_ERROR;
}
}
if (result) {
SetBooleanResult (1);
} else {
SetBooleanResult (0);
}
break;
case m_validateAttributes:
if (objc < 3 || objc > 4) {
SetResult (validateCmd_usage);
return TCL_ERROR;
}
node = tcldom_getNodeFromName (
interp, Tcl_GetStringFromObj(objv[2], NULL), &errMsg
);
if (!node || (node->nodeType != ELEMENT_NODE)) {
SetResult ("The validateAttributes method needs a domNode as argument.");
return TCL_ERROR;
}
entryPtr = Tcl_FindHashEntry (tncdata->tagNames, node->nodeName);
if (!entryPtr) {
signalNotValid (tncdata, TNC_ERROR_UNKNOWN_ELEMENT);
if (objc == 4) {
if (Tcl_ObjSetVar2(interp, objv[3], NULL,
Tcl_GetObjResult(interp), 0) == NULL) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"couldn't save msg in variable", -1);
return TCL_ERROR;
}
}
SetBooleanResult (0);
return TCL_OK;
}
model = (TNC_Content *) Tcl_GetHashValue (entryPtr);
tncdata->dtdstatus = 0;
tncdata->idCheck = 0;
if (tncdata->ids->numEntries) {
Tcl_DeleteHashTable (tncdata->ids);
Tcl_InitHashTable (tncdata->ids, TCL_STRING_KEYS);
}
Tcl_ResetResult (interp);
result = validateNodeAttributes (tncdata, model->attInfo, node);
if (objc == 4) {
if (Tcl_ObjSetVar2(interp, objv[3], NULL,
Tcl_GetObjResult(interp), 0) == NULL) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"couldn't save msg in variable", -1);
return TCL_ERROR;
}
}
if (result) {
SetBooleanResult (1);
} else {
SetBooleanResult (0);
}
break;
case m_delete:
if (objc != 2) {
SetResult (validateCmd_usage);
return TCL_ERROR;
}
Tcl_DeleteCommand (interp, Tcl_GetStringFromObj (objv[0], NULL));
SetResult ("");
break;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------------
*
* FreeTncData
*
* Helper proc, used from TncResetProc and TncFreeProc. Frees all
* collected DTD data and the id table.
*
* Results:
* None.
*
* Side effects:
* Frees memory.
*
*---------------------------------------------------------------------------- */
static void
FreeTncData (
TNC_Data *tncdata
)
{
Tcl_HashEntry *entryPtr, *attentryPtr;
Tcl_HashSearch search, attsearch;
TNC_Content *model;
TNC_ElemAttInfo *elemAttInfo;
TNC_EntityInfo *entityInfo;
TNC_AttDecl *attDecl;
if (tncdata->elemContentsRewriten) {
entryPtr = Tcl_FirstHashEntry (tncdata->tagNames, &search);
while (entryPtr) {
model = Tcl_GetHashValue (entryPtr);
if (model) {
TncFreeTncModel (model);
FREE ((char *) model);
}
entryPtr = Tcl_NextHashEntry (&search);
}
}
Tcl_DeleteHashTable (tncdata->tagNames);
entryPtr = Tcl_FirstHashEntry (tncdata->attDefsTables, &search);
while (entryPtr) {
elemAttInfo = Tcl_GetHashValue (entryPtr);
if (!elemAttInfo) {
entryPtr = Tcl_NextHashEntry (&search);
continue;
}
attentryPtr = Tcl_FirstHashEntry (elemAttInfo->attributes, &attsearch);
while (attentryPtr) {
attDecl = Tcl_GetHashValue (attentryPtr);
if (attDecl) {
if (attDecl->att_type == TNC_ATTTYPE_NOTATION ||
attDecl->att_type == TNC_ATTTYPE_ENUMERATION) {
Tcl_DeleteHashTable (attDecl->lookupTable);
FREE ((char *) attDecl->lookupTable);
}
if (attDecl->dflt) {
FREE (attDecl->dflt);
}
FREE ((char *) attDecl);
}
attentryPtr = Tcl_NextHashEntry (&attsearch);
}
Tcl_DeleteHashTable (elemAttInfo->attributes);
FREE ((char *) elemAttInfo->attributes);
FREE ((char *) elemAttInfo);
entryPtr = Tcl_NextHashEntry (&search);
}
Tcl_DeleteHashTable (tncdata->attDefsTables);
entryPtr = Tcl_FirstHashEntry (tncdata->entityDecls, &search);
while (entryPtr) {
entityInfo = Tcl_GetHashValue (entryPtr);
if (entityInfo) {
if (entityInfo->is_notation) {
FREE (entityInfo->notationName);
}
FREE ((char *) entityInfo);
}
entryPtr = Tcl_NextHashEntry (&search);
}
Tcl_DeleteHashTable (tncdata->entityDecls);
Tcl_DeleteHashTable (tncdata->notationDecls);
Tcl_DeleteHashTable (tncdata->ids);
if (tncdata->doctypeName) {
FREE (tncdata->doctypeName);
}
}
/*
*----------------------------------------------------------------------------
*
* TncResetProc
*
* Called for C handler set specific reset actions in case of
* parser reset.
*
* Results:
* None.
*
* Side effects:
* Resets the "userData" of the C handler set parser extension.
*
*----------------------------------------------------------------------------
*/
void
TncResetProc (
Tcl_Interp *interp,
void *userData
)
{
TNC_Data *tncdata = (TNC_Data *) userData;
FreeTncData (tncdata);
Tcl_InitHashTable (tncdata->tagNames, TCL_STRING_KEYS);
tncdata->elemContentsRewriten = 0;
tncdata->dtdstatus = 0;
tncdata->idCheck = 1;
Tcl_InitHashTable (tncdata->attDefsTables, TCL_STRING_KEYS);
Tcl_InitHashTable (tncdata->entityDecls, TCL_STRING_KEYS);
Tcl_InitHashTable (tncdata->notationDecls, TCL_STRING_KEYS);
Tcl_InitHashTable (tncdata->ids, TCL_STRING_KEYS);
tncdata->doctypeName = NULL;
tncdata->skipWhiteCDATAs = 1;
tncdata->ignorePCDATA = 0;
tncdata->contentStackPtr = 0;
}
/*
*----------------------------------------------------------------------------
*
* createTncData --
*
* Helper proc. Allocates a TNC_Data structure and initializes it.
*
* Results:
* None.
*
* Side effects:
* Memory allocation and initialization.
*
*----------------------------------------------------------------------------
*/
static TNC_Data *
createTncData (
Tcl_Interp *interp,
Tcl_Obj *expatObj
)
{
TNC_Data *tncdata;
tncdata = (TNC_Data *) MALLOC (sizeof (TNC_Data));
tncdata->tagNames = (Tcl_HashTable *) MALLOC (sizeof (Tcl_HashTable));
Tcl_InitHashTable (tncdata->tagNames, TCL_STRING_KEYS);
tncdata->elemContentsRewriten = 0;
tncdata->dtdstatus = 0;
tncdata->idCheck = 1;
tncdata->attDefsTables =
(Tcl_HashTable *) MALLOC (sizeof (Tcl_HashTable));
Tcl_InitHashTable (tncdata->attDefsTables, TCL_STRING_KEYS);
tncdata->entityDecls =
(Tcl_HashTable *) MALLOC (sizeof (Tcl_HashTable));
Tcl_InitHashTable (tncdata->entityDecls, TCL_STRING_KEYS);
tncdata->notationDecls =
(Tcl_HashTable *) MALLOC (sizeof (Tcl_HashTable));
Tcl_InitHashTable (tncdata->notationDecls, TCL_STRING_KEYS);
tncdata->ids = (Tcl_HashTable *) MALLOC (sizeof (Tcl_HashTable));
Tcl_InitHashTable (tncdata->ids, TCL_STRING_KEYS);
tncdata->doctypeName = NULL;
tncdata->interp = interp;
tncdata->expatObj = expatObj;
tncdata->skipWhiteCDATAs = 1;
tncdata->ignorePCDATA = 0;
tncdata->contentStack = (TNC_ContentStack *)
MALLOC (sizeof (TNC_ContentStack) * TNC_INITCONTENTSTACKSIZE);
tncdata->contentStackSize = TNC_INITCONTENTSTACKSIZE;
tncdata->contentStackPtr = 0;
return tncdata;
}
/*
*----------------------------------------------------------------------------
*
* TncFreeProc
*
* Called for C handler set specific cleanup in case of parser
* delete.
*
* Results:
* None.
*
* Side effects:
* C handler set specific userData gets free'd.
*
*----------------------------------------------------------------------------
*/
void
TncFreeProc (
Tcl_Interp *interp,
void *userData
)
{
TNC_Data *tncdata = (TNC_Data *) userData;
FreeTncData (tncdata);
FREE ((char *) tncdata->tagNames);
FREE ((char *) tncdata->attDefsTables);
FREE ((char *) tncdata->entityDecls);
FREE ((char *) tncdata->notationDecls);
FREE ((char *) tncdata->ids);
FREE ((char *) tncdata->contentStack);
FREE ((char *) tncdata);
}
/*
*----------------------------------------------------------------------------
*
* tnc_ValidateObjDeleteCmd
*
* Called when a validateObjCmd is deleted. It's infact nothing
* but a wrapper for TncFreeProc.
*
* Results:
* None.
*
* Side effects:
* The clientData structure will be freed, during cleanup routine calls.
*
*----------------------------------------------------------------------------
*/
static void
tnc_ValidateObjDeleteCmd (
ClientData clientData
)
{
TNC_Data *tncdata = (TNC_Data*) clientData;
TncFreeProc (tncdata->interp, tncdata);
}
/*
*----------------------------------------------------------------------------
*
* TclTncObjCmd --
*
* This procedure is invoked to process the "tnc" command.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* The expat parser object provided as argument is enhanced by
* by the "tnc" handler set.
*
*----------------------------------------------------------------------------
*/
int
TclTncObjCmd(dummy, interp, objc, objv)
ClientData dummy;
Tcl_Interp *interp;
int objc;
Tcl_Obj *const objv[];
{
char *cmdName, s[20];
CHandlerSet *handlerSet;
int methodIndex, result;
TNC_Data *tncdata;
static const char *tncMethods[] = {
"enable", "remove", "getValidateCmd",
NULL
};
enum tncMethod {
m_enable, m_remove, m_getValidateCmd
};
if (!CheckExpatParserObj (interp, objv[1])) {
SetResult ("First argument has to be a expat parser object");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj (interp, objv[2], tncMethods, "method", 0,
&methodIndex) != TCL_OK)
{
return TCL_ERROR;
}
switch ((enum tncMethod) methodIndex) {
case m_enable:
if (objc != 3) {
Tcl_WrongNumArgs (interp, 1, objv, tnc_usage);
return TCL_ERROR;
}
handlerSet = CHandlerSetCreate ("tnc");
handlerSet->userData = createTncData (interp, objv[1]);
handlerSet->ignoreWhiteCDATAs = 0;
handlerSet->resetProc = TncResetProc;
handlerSet->freeProc = TncFreeProc;
handlerSet->elementDeclCommand = TncElementDeclCommand;
handlerSet->attlistDeclCommand = TncAttDeclCommand;
handlerSet->entityDeclCommand = TncEntityDeclHandler;
handlerSet->notationcommand = TncNotationDeclHandler;
handlerSet->elementstartcommand = TncElementStartCommand;
handlerSet->elementendcommand = TncElementEndCommand;
handlerSet->datacommand = TncCharacterdataCommand;
handlerSet->startCdataSectionCommand = TncStartCdataSectionHandler;
handlerSet->startDoctypeDeclCommand = TncStartDoctypeDeclHandler;
handlerSet->endDoctypeDeclCommand = TncEndDoctypeDeclHandler;
result = CHandlerSetInstall (interp, objv[1], handlerSet);
if (result != 0) {
SetResult ("already have tnc C handler set");
TncFreeProc (interp, handlerSet->userData);
FREE (handlerSet->name);
FREE ((char *) handlerSet);
return TCL_ERROR;
}
return TCL_OK;
case m_remove:
if (objc != 3) {
Tcl_WrongNumArgs (interp, 1, objv, tnc_usage);
return TCL_ERROR;
}
result = CHandlerSetRemove (interp, objv[1], "tnc");
if (result == 1) {
/* This should not happen if CheckExpatParserObj() is used. */
SetResult ("argument has to be a expat parser object");
return TCL_ERROR;
}
if (result == 2) {
SetResult("expat parser obj hasn't a C handler set named \"tnc\"");
return TCL_ERROR;
}
return TCL_OK;
case m_getValidateCmd:
if (objc != 3 && objc != 4) {
Tcl_WrongNumArgs (interp, 1, objv, tnc_usage);
return TCL_ERROR;
}
handlerSet = CHandlerSetGet (interp, objv[1], "tnc");
if (!handlerSet) {
SetResult("expat parser obj hasn't a C handler set named \"tnc\"");
return TCL_ERROR;
}
tncdata = (TNC_Data *) handlerSet->userData;
if (!tncdata->dtdstatus) {
SetResult ("No complete and error free DTD data available.");
return TCL_ERROR;
}
/* After we finished, the validator structure is its own command,
there isn't a parser cmd anymore. */
tncdata->expatObj = NULL;
tncdata->dtdstatus = 0;
handlerSet->userData = createTncData (interp, objv[1]);
if (objc == 4) {
cmdName = Tcl_GetStringFromObj (objv[3], NULL);
} else {
FindUniqueCmdName (interp, s);
cmdName = s;
}
Tcl_CreateObjCommand (interp, cmdName, tnc_ValidateObjCmd, tncdata,
tnc_ValidateObjDeleteCmd);
Tcl_SetResult (interp, cmdName, TCL_VOLATILE);
return TCL_OK;
default:
Tcl_SetResult (interp, "unknown method", NULL);
return TCL_ERROR;
}
}
#ifdef BUILD_tnc
# undef TCL_STORAGE_CLASS
# define TCL_STORAGE_CLASS DLLEXPORT
#endif
/*
*----------------------------------------------------------------------------
*
* Tnc_Init --
*
* Initialization routine for loadable module
*
* Results:
* None.
*
* Side effects:
* Defines "tnc" enhancement command for expat parser obj
*
*----------------------------------------------------------------------------
*/
#if defined(_MSC_VER) || defined(__MINGW32__)
# undef TCL_STORAGE_CLASS
# define TCL_STORAGE_CLASS DLLEXPORT
#endif
#if TCL_MAJOR_VERSION == 8
# define STUB_VERSION "8.5"
#else
# define STUB_VERSION "9.0"
#endif
EXTERN int
Tnc_Init (interp)
Tcl_Interp *interp;
{
#ifdef USE_TCL_STUBS
if (Tcl_InitStubs(interp, STUB_VERSION, 0) == NULL) {
return TCL_ERROR;
}
#endif
#ifdef USE_TDOM_STUBS
if (Tdom_InitStubs(interp, "0.8", 0) == NULL) {
return TCL_ERROR;
}
#endif
Tcl_PkgRequire (interp, "tdom", NULL, 0);
Tcl_CreateObjCommand (interp, "tnc", TclTncObjCmd, NULL, NULL );
Tcl_PkgProvide (interp, "tnc", PACKAGE_VERSION);
return TCL_OK;
}
|