1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150
|
################################################################################
#
# Perl module: XML::DOM
#
# By Enno Derksen (official maintainer), enno@att.com
# and Clark Cooper, coopercl@sch.ge.com
#
################################################################################
#
# To do:
#
# * BUG: setOwnerDocument - does not process default attr values correctly,
# they still point to the old doc.
# * change Exception mechanism
# * entity expansion
# * maybe: more checking of sysId etc.
# * NoExpand mode (don't know what else is useful)
# * various odds and ends: see comments starting with "??"
# * normalize(1) should also expand CDataSections and EntityReferences
# * parse a DocumentFragment?
# * encoding support
# * someone reported an error that an Entity or something contained a single
# quote and it printed ''' or something...
#
######################################################################
package Stat;
#?? Debugging class - remove later
sub cnt
{
$cnt{$_[0]}++;
}
sub print
{
for (keys %cnt)
{
print "$_: " . $cnt{$_} . "\n";
}
}
######################################################################
package XML::DOM;
######################################################################
use strict;
use vars qw( $VERSION @ISA @EXPORT
$IgnoreReadOnly $SafeMode $TagStyle
%DefaultEntities %DecodeDefaultEntity
$ChBaseChar $ChIdeographic
$ChLetter $ChDigit $ChExtender $ChCombiningChar $ChNameChar
$ReName $ReNmToken $ReEntityRef $ReCharRef $ReReference $ReAttValue
);
use Carp;
BEGIN
{
require XML::Parser;
$VERSION = '1.25';
my $needVersion = '2.23';
die "need at least XML::Parser version $needVersion (current=" .
$XML::Parser::VERSION . ")"
unless $XML::Parser::VERSION >= $needVersion;
@ISA = qw( Exporter );
@EXPORT = qw(
UNKNOWN_NODE
ELEMENT_NODE
ATTRIBUTE_NODE
TEXT_NODE
CDATA_SECTION_NODE
ENTITY_REFERENCE_NODE
ENTITY_NODE
PROCESSING_INSTRUCTION_NODE
COMMENT_NODE
DOCUMENT_NODE
DOCUMENT_TYPE_NODE
DOCUMENT_FRAGMENT_NODE
NOTATION_NODE
ELEMENT_DECL_NODE
ATT_DEF_NODE
XML_DECL_NODE
ATTLIST_DECL_NODE
);
}
#---- Constant definitions
# Node types
sub UNKNOWN_NODE () {0;} # not in the DOM Spec
sub ELEMENT_NODE () {1;}
sub ATTRIBUTE_NODE () {2;}
sub TEXT_NODE () {3;}
sub CDATA_SECTION_NODE () {4;}
sub ENTITY_REFERENCE_NODE () {5;}
sub ENTITY_NODE () {6;}
sub PROCESSING_INSTRUCTION_NODE () {7;}
sub COMMENT_NODE () {8;}
sub DOCUMENT_NODE () {9;}
sub DOCUMENT_TYPE_NODE () {10;}
sub DOCUMENT_FRAGMENT_NODE () {11;}
sub NOTATION_NODE () {12;}
sub ELEMENT_DECL_NODE () {13;} # not in the DOM Spec
sub ATT_DEF_NODE () {14;} # not in the DOM Spec
sub XML_DECL_NODE () {15;} # not in the DOM Spec
sub ATTLIST_DECL_NODE () {16;} # not in the DOM Spec
#
# Definitions of the character classes and regular expressions as defined in the
# XML Spec.
#
# NOTE: ChLetter maps to the 'Letter' definition in the XML Spec.
#
$ChBaseChar = '(?:[a-zA-Z]|\xC3[\x80-\x96\x98-\xB6\xB8-\xBF]|\xC4[\x80-\xB1\xB4-\xBE]|\xC5[\x81-\x88\x8A-\xBE]|\xC6[\x80-\xBF]|\xC7[\x80-\x83\x8D-\xB0\xB4\xB5\xBA-\xBF]|\xC8[\x80-\x97]|\xC9[\x90-\xBF]|\xCA[\x80-\xA8\xBB-\xBF]|\xCB[\x80\x81]|\xCE[\x86\x88-\x8A\x8C\x8E-\xA1\xA3-\xBF]|\xCF[\x80-\x8E\x90-\x96\x9A\x9C\x9E\xA0\xA2-\xB3]|\xD0[\x81-\x8C\x8E-\xBF]|\xD1[\x80-\x8F\x91-\x9C\x9E-\xBF]|\xD2[\x80\x81\x90-\xBF]|\xD3[\x80-\x84\x87\x88\x8B\x8C\x90-\xAB\xAE-\xB5\xB8\xB9]|\xD4[\xB1-\xBF]|\xD5[\x80-\x96\x99\xA1-\xBF]|\xD6[\x80-\x86]|\xD7[\x90-\xAA\xB0-\xB2]|\xD8[\xA1-\xBA]|\xD9[\x81-\x8A\xB1-\xBF]|\xDA[\x80-\xB7\xBA-\xBE]|\xDB[\x80-\x8E\x90-\x93\x95\xA5\xA6]|\xE0(?:\xA4[\x85-\xB9\xBD]|\xA5[\x98-\xA1]|\xA6[\x85-\x8C\x8F\x90\x93-\xA8\xAA-\xB0\xB2\xB6-\xB9]|\xA7[\x9C\x9D\x9F-\xA1\xB0\xB1]|\xA8[\x85-\x8A\x8F\x90\x93-\xA8\xAA-\xB0\xB2\xB3\xB5\xB6\xB8\xB9]|\xA9[\x99-\x9C\x9E\xB2-\xB4]|\xAA[\x85-\x8B\x8D\x8F-\x91\x93-\xA8\xAA-\xB0\xB2\xB3\xB5-\xB9\xBD]|\xAB\xA0|\xAC[\x85-\x8C\x8F\x90\x93-\xA8\xAA-\xB0\xB2\xB3\xB6-\xB9\xBD]|\xAD[\x9C\x9D\x9F-\xA1]|\xAE[\x85-\x8A\x8E-\x90\x92-\x95\x99\x9A\x9C\x9E\x9F\xA3\xA4\xA8-\xAA\xAE-\xB5\xB7-\xB9]|\xB0[\x85-\x8C\x8E-\x90\x92-\xA8\xAA-\xB3\xB5-\xB9]|\xB1[\xA0\xA1]|\xB2[\x85-\x8C\x8E-\x90\x92-\xA8\xAA-\xB3\xB5-\xB9]|\xB3[\x9E\xA0\xA1]|\xB4[\x85-\x8C\x8E-\x90\x92-\xA8\xAA-\xB9]|\xB5[\xA0\xA1]|\xB8[\x81-\xAE\xB0\xB2\xB3]|\xB9[\x80-\x85]|\xBA[\x81\x82\x84\x87\x88\x8A\x8D\x94-\x97\x99-\x9F\xA1-\xA3\xA5\xA7\xAA\xAB\xAD\xAE\xB0\xB2\xB3\xBD]|\xBB[\x80-\x84]|\xBD[\x80-\x87\x89-\xA9])|\xE1(?:\x82[\xA0-\xBF]|\x83[\x80-\x85\x90-\xB6]|\x84[\x80\x82\x83\x85-\x87\x89\x8B\x8C\x8E-\x92\xBC\xBE]|\x85[\x80\x8C\x8E\x90\x94\x95\x99\x9F-\xA1\xA3\xA5\xA7\xA9\xAD\xAE\xB2\xB3\xB5]|\x86[\x9E\xA8\xAB\xAE\xAF\xB7\xB8\xBA\xBC-\xBF]|\x87[\x80-\x82\xAB\xB0\xB9]|[\xB8\xB9][\x80-\xBF]|\xBA[\x80-\x9B\xA0-\xBF]|\xBB[\x80-\xB9]|\xBC[\x80-\x95\x98-\x9D\xA0-\xBF]|\xBD[\x80-\x85\x88-\x8D\x90-\x97\x99\x9B\x9D\x9F-\xBD]|\xBE[\x80-\xB4\xB6-\xBC\xBE]|\xBF[\x82-\x84\x86-\x8C\x90-\x93\x96-\x9B\xA0-\xAC\xB2-\xB4\xB6-\xBC])|\xE2(?:\x84[\xA6\xAA\xAB\xAE]|\x86[\x80-\x82])|\xE3(?:\x81[\x81-\xBF]|\x82[\x80-\x94\xA1-\xBF]|\x83[\x80-\xBA]|\x84[\x85-\xAC])|\xEA(?:[\xB0-\xBF][\x80-\xBF])|\xEB(?:[\x80-\xBF][\x80-\xBF])|\xEC(?:[\x80-\xBF][\x80-\xBF])|\xED(?:[\x80-\x9D][\x80-\xBF]|\x9E[\x80-\xA3]))';
$ChIdeographic = '(?:\xE3\x80[\x87\xA1-\xA9]|\xE4(?:[\xB8-\xBF][\x80-\xBF])|\xE5(?:[\x80-\xBF][\x80-\xBF])|\xE6(?:[\x80-\xBF][\x80-\xBF])|\xE7(?:[\x80-\xBF][\x80-\xBF])|\xE8(?:[\x80-\xBF][\x80-\xBF])|\xE9(?:[\x80-\xBD][\x80-\xBF]|\xBE[\x80-\xA5]))';
$ChDigit = '(?:[0-9]|\xD9[\xA0-\xA9]|\xDB[\xB0-\xB9]|\xE0(?:\xA5[\xA6-\xAF]|\xA7[\xA6-\xAF]|\xA9[\xA6-\xAF]|\xAB[\xA6-\xAF]|\xAD[\xA6-\xAF]|\xAF[\xA7-\xAF]|\xB1[\xA6-\xAF]|\xB3[\xA6-\xAF]|\xB5[\xA6-\xAF]|\xB9[\x90-\x99]|\xBB[\x90-\x99]|\xBC[\xA0-\xA9]))';
$ChExtender = '(?:\xC2\xB7|\xCB[\x90\x91]|\xCE\x87|\xD9\x80|\xE0(?:\xB9\x86|\xBB\x86)|\xE3(?:\x80[\x85\xB1-\xB5]|\x82[\x9D\x9E]|\x83[\xBC-\xBE]))';
$ChCombiningChar = '(?:\xCC[\x80-\xBF]|\xCD[\x80-\x85\xA0\xA1]|\xD2[\x83-\x86]|\xD6[\x91-\xA1\xA3-\xB9\xBB-\xBD\xBF]|\xD7[\x81\x82\x84]|\xD9[\x8B-\x92\xB0]|\xDB[\x96-\xA4\xA7\xA8\xAA-\xAD]|\xE0(?:\xA4[\x81-\x83\xBC\xBE\xBF]|\xA5[\x80-\x8D\x91-\x94\xA2\xA3]|\xA6[\x81-\x83\xBC\xBE\xBF]|\xA7[\x80-\x84\x87\x88\x8B-\x8D\x97\xA2\xA3]|\xA8[\x82\xBC\xBE\xBF]|\xA9[\x80-\x82\x87\x88\x8B-\x8D\xB0\xB1]|\xAA[\x81-\x83\xBC\xBE\xBF]|\xAB[\x80-\x85\x87-\x89\x8B-\x8D]|\xAC[\x81-\x83\xBC\xBE\xBF]|\xAD[\x80-\x83\x87\x88\x8B-\x8D\x96\x97]|\xAE[\x82\x83\xBE\xBF]|\xAF[\x80-\x82\x86-\x88\x8A-\x8D\x97]|\xB0[\x81-\x83\xBE\xBF]|\xB1[\x80-\x84\x86-\x88\x8A-\x8D\x95\x96]|\xB2[\x82\x83\xBE\xBF]|\xB3[\x80-\x84\x86-\x88\x8A-\x8D\x95\x96]|\xB4[\x82\x83\xBE\xBF]|\xB5[\x80-\x83\x86-\x88\x8A-\x8D\x97]|\xB8[\xB1\xB4-\xBA]|\xB9[\x87-\x8E]|\xBA[\xB1\xB4-\xB9\xBB\xBC]|\xBB[\x88-\x8D]|\xBC[\x98\x99\xB5\xB7\xB9\xBE\xBF]|\xBD[\xB1-\xBF]|\xBE[\x80-\x84\x86-\x8B\x90-\x95\x97\x99-\xAD\xB1-\xB7\xB9])|\xE2\x83[\x90-\x9C\xA1]|\xE3(?:\x80[\xAA-\xAF]|\x82[\x99\x9A]))';
$ChLetter = "(?:$ChBaseChar|$ChIdeographic)";
$ChNameChar = "(?:[-._:]|$ChLetter|$ChDigit|$ChCombiningChar|$ChExtender)";
$ReName = "(?:(?:[:_]|$ChLetter)$ChNameChar*)";
$ReNmToken = "(?:$ChNameChar)+";
$ReEntityRef = "(?:\&$ReName;)";
$ReCharRef = "(?:\&#(?:\d+|x[0-9a-fA-F]+);)";
$ReReference = "(?:$ReEntityRef|$ReCharRef)";
#?? what if it contains entity references?
$ReAttValue = "(?:\"(?:[^\"&<]*|$ReReference)\"|'(?:[^\'&<]|$ReReference)*')";
%DefaultEntities =
(
"quot" => '"',
"gt" => ">",
"lt" => "<",
"apos" => "'",
"amp" => "&"
);
%DecodeDefaultEntity =
(
'"' => """,
">" => ">",
"<" => "<",
"'" => "'",
"&" => "&"
);
sub encodeCDATA
{
my ($str) = shift;
$str =~ s/]]>/]]>/go;
$str;
}
#
# PI may not contain "?>"
#
sub encodeProcessingInstruction
{
my ($str) = shift;
$str =~ s/\?>/?>/go;
$str;
}
#
#?? Not sure if this is right - must prevent double minus somehow...
#
sub encodeComment
{
my ($str) = shift;
return undef unless defined $str;
$str =~ s/--/--/go;
$str;
}
# for debugging
sub toHex
{
my $str = shift;
my $len = length($str);
my @a = unpack ("C$len", $str);
my $s = "";
for (@a)
{
$s .= sprintf ("%02x", $_);
}
$s;
}
#
# 2nd parameter $default: list of Default Entity characters that need to be
# converted (e.g. "&<" for conversion to "&" and "<" resp.)
#
sub encodeText
{
my ($str, $default) = @_;
return undef unless defined $str;
$str =~ s/([\xC0-\xDF].|[\xE0-\xEF]..|[\xF0-\xFF]...)|([$default])|(]]>)/
defined($1) ? XmlUtf8Decode ($1) :
defined ($2) ? $DecodeDefaultEntity{$2} : "]]>" /egs;
#?? could there be references that should not be expanded?
# e.g. should not replace &#nn; ¯ and &abc;
# $str =~ s/&(?!($ReName|#[0-9]+|#x[0-9a-fA-F]+);)/&/go;
$str;
}
# Used by AttDef - default value
sub encodeAttrValue
{
encodeText (shift, '"&<');
}
#
# Converts an integer (Unicode - ISO/IEC 10646) to a UTF-8 encoded character
# sequence.
# Used when converting e.g. { or Ͽ to a string value.
#
# Algorithm borrowed from expat/xmltok.c/XmlUtf8Encode()
#
# not checking for bad characters: < 0, x00-x08, x0B-x0C, x0E-x1F, xFFFE-xFFFF
sub XmlUtf8Encode
{
my $n = shift;
if ($n < 0x80)
{
return chr ($n);
}
elsif ($n < 0x800)
{
return pack ("CC", (($n >> 6) | 0xc0), (($n & 0x3f) | 0x80));
}
elsif ($n < 0x10000)
{
return pack ("CCC", (($n >> 12) | 0xe0), ((($n >> 6) & 0x3f) | 0x80),
(($n & 0x3f) | 0x80));
}
elsif ($n < 0x110000)
{
return pack ("CCCC", (($n >> 18) | 0xf0), ((($n >> 12) & 0x3f) | 0x80),
((($n >> 6) & 0x3f) | 0x80), (($n & 0x3f) | 0x80));
}
croak "number is too large for Unicode [$n] in &XmlUtf8Encode";
}
#
# Opposite of XmlUtf8Decode plus it adds prefix "&#" or "&#x" and suffix ";"
# The 2nd parameter ($hex) indicates whether the result is hex encoded or not.
#
sub XmlUtf8Decode
{
my ($str, $hex) = @_;
my $len = length ($str);
my $n;
if ($len == 2)
{
my @n = unpack "C2", $str;
$n = (($n[0] & 0x3f) << 6) + ($n[1] & 0x3f);
}
elsif ($len == 3)
{
my @n = unpack "C3", $str;
$n = (($n[0] & 0x1f) << 12) + (($n[1] & 0x3f) << 6) +
($n[2] & 0x3f);
}
elsif ($len == 4)
{
my @n = unpack "C4", $str;
$n = (($n[0] & 0x0f) << 18) + (($n[1] & 0x3f) << 12) +
(($n[2] & 0x3f) << 6) + ($n[3] & 0x3f);
}
elsif ($len == 1) # just to be complete...
{
$n = ord ($str);
}
else
{
croak "bad value [$str] for XmlUtf8Decode";
}
$hex ? sprintf ("&#x%x;", $n) : "&#$n;";
}
$IgnoreReadOnly = 0;
$SafeMode = 1;
sub getIgnoreReadOnly
{
$IgnoreReadOnly;
}
# The global flag $IgnoreReadOnly is set to the specified value and the old
# value of $IgnoreReadOnly is returned.
#
# To temporarily disable read-only related exceptions (i.e. when parsing
# XML or temporarily), do the following:
#
# my $oldIgnore = XML::DOM::ignoreReadOnly (1);
# ... do whatever you want ...
# XML::DOM::ignoreReadOnly ($oldIgnore);
#
sub ignoreReadOnly
{
my $i = $IgnoreReadOnly;
$IgnoreReadOnly = $_[0];
return $i;
}
# XML spec seems to break its own rules... (see ENTITY xmlpio)
sub forgiving_isValidName
{
$_[0] =~ /^$ReName$/o;
}
# Don't allow names starting with xml (either case)
sub picky_isValidName
{
$_[0] =~ /^$ReName$/o and $_[0] !~ /^xml/i;
}
# Be forgiving by default,
*isValidName = \&forgiving_isValidName;
sub allowReservedNames
{
*isValidName = ($_[0] ? \&forgiving_isValidName : \&picky_isValidName);
}
sub getAllowReservedNames
{
*isValidName == \&forgiving_isValidName;
}
# Always compress empty tags by default
# This is used by Element::print.
$TagStyle = sub { 0 };
sub setTagCompression
{
$TagStyle = shift;
}
######################################################################
package XML::DOM::PrintToFileHandle;
######################################################################
#
# Used by XML::DOM::Node::printToFileHandle
#
sub new
{
my($class, $fn) = @_;
bless $fn, $class;
}
sub print
{
my ($self, $str) = @_;
print $self $str;
}
######################################################################
package XML::DOM::PrintToString;
######################################################################
#
# Used by XML::DOM::Node::toString to concatenate strings
#
sub new
{
my($class) = @_;
my $str = "";
bless \$str, $class;
}
sub print
{
my ($self, $str) = @_;
$$self .= $str;
}
sub toString
{
my $self = shift;
$$self;
}
sub reset
{
${$_[0]} = "";
}
*Singleton = \(new XML::DOM::PrintToString);
######################################################################
package XML::DOM::DOMException;
######################################################################
use Exporter;
use overload '""' => \&stringify;
use vars qw ( @ISA @EXPORT @ErrorNames );
BEGIN
{
@ISA = qw( Exporter );
@EXPORT = qw( INDEX_SIZE_ERR
DOMSTRING_SIZE_ERR
HIERARCHY_REQUEST_ERR
WRONG_DOCUMENT_ERR
INVALID_CHARACTER_ERR
NO_DATA_ALLOWED_ERR
NO_MODIFICATION_ALLOWED_ERR
NOT_FOUND_ERR
NOT_SUPPORTED_ERR
INUSE_ATTRIBUTE_ERR
);
}
sub UNKNOWN_ERR () {0;} # not in the DOM Spec!
sub INDEX_SIZE_ERR () {1;}
sub DOMSTRING_SIZE_ERR () {2;}
sub HIERARCHY_REQUEST_ERR () {3;}
sub WRONG_DOCUMENT_ERR () {4;}
sub INVALID_CHARACTER_ERR () {5;}
sub NO_DATA_ALLOWED_ERR () {6;}
sub NO_MODIFICATION_ALLOWED_ERR () {7;}
sub NOT_FOUND_ERR () {8;}
sub NOT_SUPPORTED_ERR () {9;}
sub INUSE_ATTRIBUTE_ERR () {10;}
@ErrorNames = (
"UNKNOWN_ERR",
"INDEX_SIZE_ERR",
"DOMSTRING_SIZE_ERR",
"HIERARCHY_REQUEST_ERR",
"WRONG_DOCUMENT_ERR",
"INVALID_CHARACTER_ERR",
"NO_DATA_ALLOWED_ERR",
"NO_MODIFICATION_ALLOWED_ERR",
"NOT_FOUND_ERR",
"NOT_SUPPORTED_ERR",
"INUSE_ATTRIBUTE_ERR"
);
sub new
{
my ($type, $code, $msg) = @_;
my $self = bless {Code => $code}, $type;
$self->{Message} = $msg if defined $msg;
# print "=> Exception: " . $self->stringify . "\n";
$self;
}
sub getCode
{
$_[0]->{Code};
}
#------------------------------------------------------------
# Extra method implementations
sub getName
{
$ErrorNames[$_[0]->{Code}];
}
sub getMessage
{
$_[0]->{Message};
}
sub stringify
{
my $self = shift;
"XML::DOM::DOMException(Code=" . $self->getCode . ", Name=" .
$self->getName . ", Message=" . $self->getMessage . ")";
}
######################################################################
package XML::DOM::NamedNodeMap;
######################################################################
BEGIN
{
import Carp;
import XML::DOM::DOMException;
}
use vars qw( $Special );
# Constant definition:
# Note: a real Name should have at least 1 char, so nobody else should use this
$Special = "";
sub new
{
my ($class, %args) = @_;
$args{Values} = new XML::DOM::NodeList;
# Store all NamedNodeMap properties in element $Special
bless { $Special => \%args}, $class;
}
sub getNamedItem
{
# Don't return the $Special item!
($_[1] eq $Special) ? undef : $_[0]->{$_[1]};
}
sub setNamedItem
{
my ($self, $node) = @_;
my $prop = $self->{$Special};
my $name = $node->getNodeName;
if ($XML::DOM::SafeMode)
{
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR)
if $self->isReadOnly;
croak new XML::DOM::DOMException (WRONG_DOCUMENT_ERR)
if $node->{Doc} != $prop->{Doc};
croak new XML::DOM::DOMException (INUSE_ATTRIBUTE_ERR)
if defined ($node->{UsedIn});
croak new XML::DOM::DOMException (INVALID_CHARACTER_ERR,
"can't add name with NodeName [$name] to NamedNodeMap")
if $name eq $Special;
}
my $values = $prop->{Values};
my $index = -1;
my $prev = $self->{$name};
if (defined $prev)
{
# decouple previous node
delete $prev->{UsedIn};
# find index of $prev
$index = 0;
for my $val (@{$values})
{
last if ($val == $prev);
$index++;
}
}
$self->{$name} = $node;
$node->{UsedIn} = $self;
if ($index == -1)
{
push (@{$values}, $node);
}
else # replace previous node with new node
{
splice (@{$values}, $index, 1, $node);
}
$prev;
}
sub removeNamedItem
{
my ($self, $name) = @_;
# Be careful that user doesn't delete $Special node!
croak new XML::DOM::DOMException (NOT_FOUND_ERR)
if $name eq $Special;
my $node = $self->{$name};
croak new XML::DOM::DOMException (NOT_FOUND_ERR)
unless defined $node;
# The DOM Spec doesn't mention this Exception - I think it's an oversight
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR)
if $self->isReadOnly;
delete $node->{UsedIn};
delete $self->{$name};
# remove node from Values list
my $values = $self->getValues;
my $index = 0;
for my $val (@{$values})
{
if ($val == $node)
{
splice (@{$values}, $index, 1, ());
last;
}
$index++;
}
$node;
}
# The following 2 are really bogus. DOM should use an iterator instead (Clark)
sub item
{
my ($self, $item) = @_;
$self->{$Special}->{Values}->[$item];
}
sub getLength
{
my ($self) = @_;
my $vals = $self->{$Special}->{Values};
int (@$vals);
}
#------------------------------------------------------------
# Extra method implementations
sub isReadOnly
{
return 0 if $XML::DOM::IgnoreReadOnly;
my $used = $_[0]->{$Special}->{UsedIn};
defined $used ? $used->isReadOnly : 0;
}
sub cloneNode
{
my ($self, $deep) = @_;
my $prop = $self->{$Special};
my $map = new XML::DOM::NamedNodeMap (Doc => $prop->{Doc});
# Not copying Parent property on purpose!
my $oldIgnore = XML::DOM::ignoreReadOnly (1); # temporarily...
for my $val (@{$prop->{Values}})
{
my $key = $val->getNodeName;
my $newNode = $val->cloneNode ($deep);
$newNode->{UsedIn} = $map;
$map->{$key} = $newNode;
push (@{$map->{$Special}->{Values}}, $newNode);
}
XML::DOM::ignoreReadOnly ($oldIgnore); # restore previous value
$map;
}
sub setOwnerDocument
{
my ($self, $doc) = @_;
my $special = $self->{$Special};
$special->{Doc} = $doc;
for my $kid (@{$special->{Values}})
{
$kid->setOwnerDocument ($doc);
}
}
sub getChildIndex
{
my ($self, $attr) = @_;
my $i = 0;
for my $kid (@{$self->{$Special}->{Values}})
{
return $i if $kid == $attr;
$i++;
}
-1; # not found
}
sub getValues
{
wantarray ? @{ $_[0]->{$Special}->{Values} } : $_[0]->{$Special}->{Values};
}
# Remove circular dependencies. The NamedNodeMap and its values should
# not be used afterwards.
sub dispose
{
my $self = shift;
for my $kid (@{$self->getValues})
{
delete $kid->{UsedIn};
$kid->dispose;
}
delete $self->{$Special}->{Doc};
delete $self->{$Special}->{Parent};
delete $self->{$Special}->{Values};
for my $key (keys %$self)
{
delete $self->{$key};
}
}
sub setParentNode
{
$_[0]->{$Special}->{Parent} = $_[1];
}
sub getProperty
{
$_[0]->{$Special}->{$_[1]};
}
#?? remove after debugging
sub toString
{
my ($self) = @_;
my $str = "NamedNodeMap[";
while (my ($key, $val) = each %$self)
{
if ($key eq $Special)
{
$str .= "##Special (";
while (my ($k, $v) = each %$val)
{
if ($k eq "Values")
{
$str .= $k . " => [";
for my $a (@$v)
{
# $str .= $a->getNodeName . "=" . $a . ",";
$str .= $a->toString . ",";
}
$str .= "], ";
}
else
{
$str .= $k . " => " . $v . ", ";
}
}
$str .= "), ";
}
else
{
$str .= $key . " => " . $val . ", ";
}
}
$str . "]";
}
######################################################################
package XML::DOM::NodeList;
######################################################################
use vars qw ( $EMPTY );
# Empty NodeList
$EMPTY = new XML::DOM::NodeList;
sub new
{
bless [], $_[0];
}
sub item
{
$_[0]->[$_[1]];
}
sub getLength
{
int (@{$_[0]});
}
#------------------------------------------------------------
# Extra method implementations
sub dispose
{
my $self = shift;
for my $kid (@{$self})
{
$kid->dispose;
}
}
sub setOwnerDocument
{
my ($self, $doc) = @_;
for my $kid (@{$self})
{
$kid->setOwnerDocument ($doc);
}
}
######################################################################
package XML::DOM::DOMImplementation;
######################################################################
$XML::DOM::DOMImplementation::Singleton =
bless \$XML::DOM::DOMImplementation::Singleton, 'XML::DOM::DOMImplementation';
sub hasFeature
{
my ($self, $feature, $version) = @_;
$feature eq 'XML' and $version eq '1.0';
}
######################################################################
package XML::DOM::Node;
######################################################################
use vars qw( @NodeNames @EXPORT @ISA );
BEGIN
{
import XML::DOM::DOMException;
import Carp;
require FileHandle;
@ISA = qw( Exporter );
@EXPORT = qw(
UNKNOWN_NODE
ELEMENT_NODE
ATTRIBUTE_NODE
TEXT_NODE
CDATA_SECTION_NODE
ENTITY_REFERENCE_NODE
ENTITY_NODE
PROCESSING_INSTRUCTION_NODE
COMMENT_NODE
DOCUMENT_NODE
DOCUMENT_TYPE_NODE
DOCUMENT_FRAGMENT_NODE
NOTATION_NODE
ELEMENT_DECL_NODE
ATT_DEF_NODE
XML_DECL_NODE
ATTLIST_DECL_NODE
);
}
#---- Constant definitions
# Node types
sub UNKNOWN_NODE () {0;} # not in the DOM Spec
sub ELEMENT_NODE () {1;}
sub ATTRIBUTE_NODE () {2;}
sub TEXT_NODE () {3;}
sub CDATA_SECTION_NODE () {4;}
sub ENTITY_REFERENCE_NODE () {5;}
sub ENTITY_NODE () {6;}
sub PROCESSING_INSTRUCTION_NODE () {7;}
sub COMMENT_NODE () {8;}
sub DOCUMENT_NODE () {9;}
sub DOCUMENT_TYPE_NODE () {10;}
sub DOCUMENT_FRAGMENT_NODE () {11;}
sub NOTATION_NODE () {12;}
sub ELEMENT_DECL_NODE () {13;} # not in the DOM Spec
sub ATT_DEF_NODE () {14;} # not in the DOM Spec
sub XML_DECL_NODE () {15;} # not in the DOM Spec
sub ATTLIST_DECL_NODE () {16;} # not in the DOM Spec
@NodeNames = (
"UNKNOWN_NODE", # not in the DOM Spec!
"ELEMENT_NODE",
"ATTRIBUTE_NODE",
"TEXT_NODE",
"CDATA_SECTION_NODE",
"ENTITY_REFERENCE_NODE",
"ENTITY_NODE",
"PROCESSING_INSTRUCTION_NODE",
"COMMENT_NODE",
"DOCUMENT_NODE",
"DOCUMENT_TYPE_NODE",
"DOCUMENT_FRAGMENT_NODE",
"NOTATION_NODE",
"ELEMENT_DECL_NODE",
"ATT_DEF_NODE",
"XML_DECL_NODE",
"ATTLIST_DECL_NODE"
);
sub getParentNode
{
$_[0]->{Parent};
}
sub appendChild
{
my ($self, $node) = @_;
# REC 7473
if ($XML::DOM::SafeMode)
{
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR,
"node is ReadOnly")
if $self->isReadOnly;
}
my $isFrag = $node->isDocumentFragmentNode;
my $doc = $self->{Doc};
if ($isFrag)
{
if ($XML::DOM::SafeMode)
{
for my $n (@{$node->{C}})
{
croak new XML::DOM::DOMException (WRONG_DOCUMENT_ERR,
"nodes belong to different documents")
if $doc != $n->{Doc};
croak new XML::DOM::DOMException (HIERARCHY_REQUEST_ERR,
"node is ancestor of parent node")
if $n->isAncestor ($self);
croak new XML::DOM::DOMException (HIERARCHY_REQUEST_ERR,
"bad node type")
if $self->rejectChild ($n);
}
}
my @list = @{$node->{C}}; # don't try to compress this
for my $n (@list)
{
$n->setParentNode ($self);
}
push @{$self->{C}}, @list;
}
else
{
if ($XML::DOM::SafeMode)
{
croak new XML::DOM::DOMException (WRONG_DOCUMENT_ERR,
"nodes belong to different documents")
if $doc != $node->{Doc};
croak new XML::DOM::DOMException (HIERARCHY_REQUEST_ERR,
"node is ancestor of parent node")
if $node->isAncestor ($self);
croak new XML::DOM::DOMException (HIERARCHY_REQUEST_ERR,
"bad node type")
if $self->rejectChild ($node);
}
$node->setParentNode ($self);
push @{$self->{C}}, $node;
}
$node;
}
sub getChildNodes
{
# NOTE: if node can't have children, $self->{C} is undef.
my $kids = $_[0]->{C};
# Return a list if called in list context.
wantarray ? (defined ($kids) ? @{ $kids } : ()) :
(defined ($kids) ? $kids : $XML::DOM::NodeList::EMPTY);
}
sub hasChildNodes
{
my $kids = $_[0]->{C};
defined ($kids) && @$kids > 0;
}
# This method is overriden in Document
sub getOwnerDocument
{
$_[0]->{Doc};
}
sub getFirstChild
{
my $kids = $_[0]->{C};
defined $kids ? $kids->[0] : undef;
}
sub getLastChild
{
my $kids = $_[0]->{C};
defined $kids ? $kids->[-1] : undef;
}
sub getPreviousSibling
{
my $self = shift;
my $pa = $self->{Parent};
return undef unless $pa;
my $index = $pa->getChildIndex ($self);
return undef unless $index;
$pa->getChildAtIndex ($index - 1);
}
sub getNextSibling
{
my $self = shift;
my $pa = $self->{Parent};
return undef unless $pa;
$pa->getChildAtIndex ($pa->getChildIndex ($self) + 1);
}
sub insertBefore
{
my ($self, $node, $refNode) = @_;
return $self->appendChild ($node) unless $refNode; # append at the end
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR,
"node is ReadOnly")
if $self->isReadOnly;
my @nodes = ($node);
@nodes = @{$node->{C}}
if $node->getNodeType == DOCUMENT_FRAGMENT_NODE;
my $doc = $self->{Doc};
for my $n (@nodes)
{
croak new XML::DOM::DOMException (WRONG_DOCUMENT_ERR,
"nodes belong to different documents")
if $doc != $n->{Doc};
croak new XML::DOM::DOMException (HIERARCHY_REQUEST_ERR,
"node is ancestor of parent node")
if $n->isAncestor ($self);
croak new XML::DOM::DOMException (HIERARCHY_REQUEST_ERR,
"bad node type")
if $self->rejectChild ($n);
}
my $index = $self->getChildIndex ($refNode);
croak new XML::DOM::DOMException (NOT_FOUND_ERR,
"reference node not found")
if $index == -1;
for my $n (@nodes)
{
$n->setParentNode ($self);
}
splice (@{$self->{C}}, $index, 0, @nodes);
$node;
}
sub replaceChild
{
my ($self, $node, $refNode) = @_;
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR,
"node is ReadOnly")
if $self->isReadOnly;
my @nodes = ($node);
@nodes = @{$node->{C}}
if $node->getNodeType == DOCUMENT_FRAGMENT_NODE;
for my $n (@nodes)
{
croak new XML::DOM::DOMException (WRONG_DOCUMENT_ERR,
"nodes belong to different documents")
if $self->{Doc} != $n->{Doc};
croak new XML::DOM::DOMException (HIERARCHY_REQUEST_ERR,
"node is ancestor of parent node")
if $n->isAncestor ($self);
croak new XML::DOM::DOMException (HIERARCHY_REQUEST_ERR,
"bad node type")
if $self->rejectChild ($n);
}
my $index = $self->getChildIndex ($refNode);
croak new XML::DOM::DOMException (NOT_FOUND_ERR,
"reference node not found")
if $index == -1;
for my $n (@nodes)
{
$n->setParentNode ($self);
}
splice (@{$self->{C}}, $index, 1, @nodes);
$refNode->removeChildHoodMemories;
$refNode;
}
sub removeChild
{
my ($self, $node) = @_;
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR,
"node is ReadOnly")
if $self->isReadOnly;
my $index = $self->getChildIndex ($node);
croak new XML::DOM::DOMException (NOT_FOUND_ERR,
"reference node not found")
if $index == -1;
splice (@{$self->{C}}, $index, 1, ());
$node->removeChildHoodMemories;
$node;
}
# Merge all subsequent Text nodes in this subtree
sub normalize
{
my ($self) = shift;
my $prev = undef; # previous Text node
return unless defined $self->{C};
my @nodes = @{$self->{C}};
my $i = 0;
my $n = @nodes;
while ($i < $n)
{
my $node = $self->getChildAtIndex($i);
my $type = $node->getNodeType;
if (defined $prev)
{
# It should not merge CDATASections. Dom Spec says:
# Adjacent CDATASections nodes are not merged by use
# of the Element.normalize() method.
if ($type == TEXT_NODE)
{
$prev->appendData ($node->getData);
$self->removeChild ($node);
$i--;
$n--;
}
else
{
$prev = undef;
if ($type == ELEMENT_NODE)
{
$node->normalize;
for my $attr (@{$node->getAttributes->getValues})
{
$attr->normalize;
}
}
}
}
else
{
if ($type == TEXT_NODE)
{
$prev = $node;
}
elsif ($type == ELEMENT_NODE)
{
$node->normalize;
for my $attr (@{$node->getAttributes->getValues})
{
$attr->normalize;
}
}
}
$i++;
}
}
# Return all Element nodes in the subtree that have the specified tagName.
# If tagName is "*", all Element nodes are returned.
# NOTE: the DOM Spec does not specify a 3rd or 4th parameter
sub getElementsByTagName
{
my ($self, $tagName, $recurse, $list) = @_;
$recurse = 1 unless defined $recurse;
$list = (wantarray ? [] : new XML::DOM::NodeList) unless defined $list;
return unless defined $self->{C};
# preorder traversal: check parent node first
for my $kid (@{$self->{C}})
{
if ($kid->isElementNode)
{
if ($tagName eq "*" || $tagName eq $kid->getTagName)
{
push @{$list}, $kid;
}
$kid->getElementsByTagName ($tagName, $recurse, $list) if $recurse;
}
}
wantarray ? @{ $list } : $list;
}
sub getNodeValue
{
undef;
}
sub setNodeValue
{
# no-op
}
# Redefined by XML::DOM::Element
sub getAttributes
{
undef;
}
#------------------------------------------------------------
# Extra method implementations
sub setOwnerDocument
{
my ($self, $doc) = @_;
$self->{Doc} = $doc;
return unless defined $self->{C};
for my $kid (@{$self->{C}})
{
$kid->setOwnerDocument ($doc);
}
}
sub cloneChildren
{
my ($self, $node, $deep) = @_;
return unless $deep;
return unless defined $self->{C};
my $oldIgnore = XML::DOM::ignoreReadOnly (1); # temporarily...
for my $kid (@{$node->{C}})
{
my $newNode = $kid->cloneNode ($deep);
push @{$self->{C}}, $newNode;
$newNode->setParentNode ($self);
}
XML::DOM::ignoreReadOnly ($oldIgnore); # restore previous value
}
# For internal use only!
sub removeChildHoodMemories
{
my ($self) = @_;
#????? remove?
delete $self->{Parent};
}
# Remove circular dependencies. The Node and its children should
# not be used afterwards.
sub dispose
{
my $self = shift;
$self->removeChildHoodMemories;
if (defined $self->{C})
{
$self->{C}->dispose;
delete $self->{C};
}
delete $self->{Doc};
}
# For internal use only!
sub setParentNode
{
my ($self, $parent) = @_;
# REC 7473
my $oldParent = $self->{Parent};
if (defined $oldParent)
{
# remove from current parent
my $index = $oldParent->getChildIndex ($self);
splice (@{$oldParent->{C}}, $index, 1, ());
$self->removeChildHoodMemories;
}
$self->{Parent} = $parent;
}
# This function can return 3 values:
# 1: always readOnly
# 0: never readOnly
# undef: depends on parent node
#
# Returns 1 for DocumentType, Notation, Entity, EntityReference, Attlist,
# ElementDecl, AttDef.
# The first 4 are readOnly according to the DOM Spec, the others are always
# children of DocumentType. (Naturally, children of a readOnly node have to be
# readOnly as well...)
# These nodes are always readOnly regardless of who their ancestors are.
# Other nodes, e.g. Comment, are readOnly only if their parent is readOnly,
# which basically means that one of its ancestors has to be one of the
# aforementioned node types.
# Document and DocumentFragment return 0 for obvious reasons.
# Attr, Element, CDATASection, Text return 0. The DOM spec says that they can
# be children of an Entity, but I don't think that that's possible
# with the current XML::Parser.
# Attr uses a {ReadOnly} property, which is only set if it's part of a AttDef.
# Always returns 0 if ignoreReadOnly is set.
sub isReadOnly
{
# default implementation for Nodes that are always readOnly
! $XML::DOM::IgnoreReadOnly;
}
sub rejectChild
{
1;
}
sub getNodeTypeName
{
$NodeNames[$_[0]->getNodeType];
}
sub getChildIndex
{
my ($self, $node) = @_;
my $i = 0;
return -1 unless defined $self->{C};
for my $kid (@{$self->{C}})
{
return $i if $kid == $node;
$i++;
}
-1;
}
sub getChildAtIndex
{
my $kids = $_[0]->{C};
defined ($kids) ? $kids->[$_[1]] : undef;
}
sub isAncestor
{
my ($self, $node) = @_;
do
{
return 1 if $self == $node;
$node = $node->{Parent};
}
while (defined $node);
0;
}
# Added for optimization. Overriden in XML::DOM::Text
sub isTextNode
{
0;
}
# Added for optimization. Overriden in XML::DOM::DocumentFragment
sub isDocumentFragmentNode
{
0;
}
# Added for optimization. Overriden in XML::DOM::Element
sub isElementNode
{
0;
}
# Add a Text node with the specified value or append the text to the
# previous Node if it is a Text node.
sub addText
{
# REC 9456 (if it was called)
my ($self, $str) = @_;
my $node = ${$self->{C}}[-1]; # $self->getLastChild
if (defined ($node) && $node->isTextNode)
{
# REC 5475 (if it was called)
$node->appendData ($str);
}
else
{
$node = $self->{Doc}->createTextNode ($str);
$self->appendChild ($node);
}
$node;
}
# Add a CDATASection node with the specified value or append the text to the
# previous Node if it is a CDATASection node.
sub addCDATA
{
my ($self, $str) = @_;
my $node = ${$self->{C}}[-1]; # $self->getLastChild
if (defined ($node) && $node->getNodeType == CDATA_SECTION_NODE)
{
# REC 5475
$node->appendData ($str);
}
else
{
$node = $self->{Doc}->createCDATASection ($str);
$self->appendChild ($node);
}
$node;
}
sub removeChildNodes
{
my $self = shift;
my $cref = $self->{C};
return unless defined $cref;
my $kid;
while ($kid = pop @{$cref})
{
delete $kid->{Parent};
}
}
sub toString
{
my $self = shift;
my $pr = $XML::DOM::PrintToString::Singleton;
$pr->reset;
$self->print ($pr);
$pr->toString;
}
sub printToFile
{
my ($self, $fileName) = @_;
my $fh = new FileHandle ($fileName, "w") ||
croak "printToFile - can't open output file $fileName";
$self->print ($fh);
$fh->close;
}
# Use print to print to a FileHandle object (see printToFile code)
sub printToFileHandle
{
my ($self, $FH) = @_;
my $pr = new XML::DOM::PrintToFileHandle ($FH);
$self->print ($pr);
}
# Used by AttDef::setDefault to convert unexpanded default attribute value
sub expandEntityRefs
{
my ($self, $str) = @_;
my $doctype = $self->{Doc}->getDoctype;
$str =~ s/&($XML::DOM::ReName|(#([0-9]+)|#x([0-9a-fA-F]+)));/
defined($2) ? XML::DOM::XmlUtf8Encode ($3 || hex ($4))
: expandEntityRef ($1, $doctype)/ego;
$str;
}
sub expandEntityRef
{
my ($entity, $doctype) = @_;
my $expanded = $XML::DOM::DefaultEntities{$entity};
return $expanded if defined $expanded;
$expanded = $doctype->getEntity ($entity);
return $expanded->getValue if (defined $expanded);
#?? is this an error?
croak "Could not expand entity reference of [$entity]\n";
# return "&$entity;"; # entity not found
}
######################################################################
package XML::DOM::Attr;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
import Carp;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::Node );
sub new
{
my ($class, $doc, $name, $value, $specified) = @_;
if ($XML::DOM::SafeMode)
{
croak new XML::DOM::DOMException (INVALID_CHARACTER_ERR,
"bad Attr name [$name]")
unless XML::DOM::isValidName ($name);
}
my $self = bless {Doc => $doc,
C => new XML::DOM::NodeList,
Name => $name}, $class;
if (defined $value)
{
$self->setValue ($value);
$self->{Specified} = (defined $specified) ? $specified : 1;
}
else
{
$self->{Specified} = 0;
}
$self;
}
sub getNodeType
{
ATTRIBUTE_NODE;
}
sub isSpecified
{
$_[0]->{Specified};
}
sub getName
{
$_[0]->{Name};
}
sub getValue
{
my $self = shift;
my $value = "";
for my $kid (@{$self->{C}})
{
$value .= $kid->getData;
}
$value;
}
sub setValue
{
my ($self, $value) = @_;
# REC 1147
$self->removeChildNodes;
$self->appendChild ($self->{Doc}->createTextNode ($value));
$self->{Specified} = 1;
}
sub getNodeName
{
$_[0]->getName;
}
sub getNodeValue
{
$_[0]->getValue;
}
sub setNodeValue
{
$_[0]->setValue ($_[1]);
}
sub cloneNode
{
my ($self) = @_; # parameter deep is ignored
my $node = $self->{Doc}->createAttribute ($self->getName);
$node->{Specified} = $self->{Specified};
$node->{ReadOnly} = 1 if $self->{ReadOnly};
$node->cloneChildren ($self, 1);
$node;
}
#------------------------------------------------------------
# Extra method implementations
#
sub isReadOnly
{
# ReadOnly property is set if it's part of a AttDef
! $XML::DOM::IgnoreReadOnly && defined ($_[0]->{ReadOnly});
}
sub print
{
my ($self, $FILE) = @_;
my $name = $self->{Name};
$FILE->print ("$name=\"");
for my $kid (@{$self->{C}})
{
if ($kid->getNodeType == TEXT_NODE)
{
$FILE->print (XML::DOM::encodeAttrValue ($kid->getData));
}
else # ENTITY_REFERENCE_NODE
{
$kid->print ($FILE);
}
}
$FILE->print ("\"");
}
sub rejectChild
{
my $t = $_[1]->getNodeType;
$t != TEXT_NODE && $t != ENTITY_REFERENCE_NODE;
}
######################################################################
package XML::DOM::ProcessingInstruction;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
import Carp;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::Node );
sub new
{
my ($class, $doc, $target, $data) = @_;
croak new XML::DOM::DOMException (INVALID_CHARACTER_ERR,
"bad ProcessingInstruction Target [$target]")
unless (XML::DOM::isValidName ($target) && $target !~ /^xml$/io);
bless {Doc => $doc,
Target => $target,
Data => $data}, $class;
}
sub getNodeType
{
PROCESSING_INSTRUCTION_NODE;
}
sub getTarget
{
$_[0]->{Target};
}
sub getData
{
$_[0]->{Data};
}
sub setData
{
my ($self, $data) = @_;
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR,
"node is ReadOnly")
if $self->isReadOnly;
$self->{Data} = $data;
}
sub getNodeName
{
$_[0]->{Target};
}
sub getNodeValue
{
$_[0]->getData;
}
sub setNodeValue
{
$_[0]->setData ($_[1]);
}
sub cloneNode
{
my $self = shift;
$self->{Doc}->createProcessingInstruction ($self->getTarget,
$self->getData);
}
#------------------------------------------------------------
# Extra method implementations
sub isReadOnly
{
return 0 if $XML::DOM::IgnoreReadOnly;
my $pa = $_[0]->{Parent};
defined ($pa) ? $pa->isReadOnly : 0;
}
sub print
{
my ($self, $FILE) = @_;
$FILE->print ("<?");
$FILE->print ($self->{Target});
$FILE->print (" ");
$FILE->print (XML::DOM::encodeProcessingInstruction ($self->{Data}));
$FILE->print ("?>");
}
######################################################################
package XML::DOM::Notation;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
import Carp;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::Node );
sub new
{
my ($class, $doc, $name, $base, $sysId, $pubId) = @_;
croak new XML::DOM::DOMException (INVALID_CHARACTER_ERR,
"bad Notation Name [$name]")
unless XML::DOM::isValidName ($name);
bless {Doc => $doc,
Name => $name,
Base => $base,
SysId => $sysId,
PubId => $pubId}, $class;
}
sub getNodeType
{
NOTATION_NODE;
}
sub getPubId
{
$_[0]->{PubId};
}
sub setPubId
{
$_[0]->{PubId} = $_[1];
}
sub getSysId
{
$_[0]->{SysId};
}
sub setSysId
{
$_[0]->{SysId} = $_[1];
}
sub getName
{
$_[0]->{Name};
}
sub setName
{
$_[0]->{Name} = $_[1];
}
sub getBase
{
$_[0]->{Base};
}
sub getNodeName
{
$_[0]->{Name};
}
sub print
{
my ($self, $FILE) = @_;
my $name = $self->{Name};
my $sysId = $self->{SysId};
my $pubId = $self->{PubId};
$FILE->print ("<!NOTATION $name ");
if (defined $pubId)
{
$FILE->print (" PUBLIC \"$pubId\"");
}
if (defined $sysId)
{
$FILE->print (" SYSTEM \"$sysId\"");
}
$FILE->print (">");
}
sub cloneNode
{
my ($self) = @_;
$self->{Doc}->createNotation ($self->{Name}, $self->{Base},
$self->{SysId}, $self->{PubId});
}
######################################################################
package XML::DOM::Entity;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
import Carp;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::Node );
sub new
{
my ($class, $doc, $par, $notationName, $value, $sysId, $pubId, $ndata) = @_;
croak new XML::DOM::DOMException (INVALID_CHARACTER_ERR,
"bad Entity Name [$notationName]")
unless XML::DOM::isValidName ($notationName);
bless {Doc => $doc,
NotationName => $notationName,
Parameter => $par,
Value => $value,
Ndata => $ndata,
SysId => $sysId,
PubId => $pubId}, $class;
#?? maybe Value should be a Text node
}
sub getNodeType
{
ENTITY_NODE;
}
sub getPubId
{
$_[0]->{PubId};
}
sub getSysId
{
$_[0]->{SysId};
}
# Dom Spec says:
# For unparsed entities, the name of the notation for the
# entity. For parsed entities, this is null.
#?? do we have unparsed entities?
sub getNotationName
{
$_[0]->{NotationName};
}
sub getNodeName
{
$_[0]->{NotationName};
}
sub cloneNode
{
my $self = shift;
$self->{Doc}->createEntity ($self->{Parameter},
$self->{NotationName}, $self->{Value},
$self->{SysId}, $self->{PubId},
$self->{Ndata});
}
sub rejectChild
{
return 1;
#?? if value is split over subnodes, recode this section
# also add: c => new XML::DOM::NodeList,
my $t = $_[1];
return $t == TEXT_NODE
|| $t == ENTITY_REFERENCE_NODE
|| $t == PROCESSING_INSTRUCTION_NODE
|| $t == COMMENT_NODE
|| $t == CDATA_SECTION_NODE
|| $t == ELEMENT_NODE;
}
sub getValue
{
$_[0]->{Value};
}
sub isParameterEntity
{
$_[0]->{Parameter};
}
sub getNdata
{
$_[0]->{Ndata};
}
sub print
{
my ($self, $FILE) = @_;
my $name = $self->{NotationName};
my $par = $self->isParameterEntity ? "% " : "";
$FILE->print ("<!ENTITY $par$name");
my $value = $self->{Value};
my $sysId = $self->{SysId};
my $pubId = $self->{PubId};
my $ndata = $self->{Ndata};
if (defined $value)
{
#?? Not sure what to do if it contains both single and double quote
$value = ($value =~ /\"/) ? "'$value'" : "\"$value\"";
$FILE->print (" $value");
}
if (defined $pubId)
{
$FILE->print (" PUBLIC \"$pubId\"");
}
elsif (defined $sysId)
{
$FILE->print (" SYSTEM");
}
if (defined $sysId)
{
$FILE->print (" \"$sysId\"");
}
$FILE->print (" NDATA $ndata") if defined $ndata;
$FILE->print (">");
}
######################################################################
package XML::DOM::EntityReference;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
import Carp;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::Node );
sub new
{
my ($class, $doc, $name, $parameter) = @_;
croak new XML::DOM::DOMException (INVALID_CHARACTER_ERR,
"bad Entity Name [$name] in EntityReference")
unless XML::DOM::isValidName ($name);
bless {Doc => $doc,
EntityName => $name,
Parameter => ($parameter || 0)}, $class;
}
sub getNodeType
{
ENTITY_REFERENCE_NODE;
}
sub getNodeName
{
$_[0]->{EntityName};
}
#------------------------------------------------------------
# Extra method implementations
sub getEntityName
{
$_[0]->{EntityName};
}
sub isParameterEntity
{
$_[0]->{Parameter};
}
sub getData
{
my $self = shift;
my $name = $self->{EntityName};
my $parameter = $self->{Parameter};
my $data = $self->{Doc}->expandEntity ($name, $parameter);
unless (defined $data)
{
#?? this is probably an error
my $pc = $parameter ? "%" : "&";
$data = "$pc$name;";
}
$data;
}
sub print
{
my ($self, $FILE) = @_;
my $name = $self->{EntityName};
#?? or do we expand the entities?
my $pc = $self->{Parameter} ? "%" : "&";
$FILE->print ("$pc$name;");
}
# Dom Spec says:
# [...] but if such an Entity exists, then
# the child list of the EntityReference node is the same as that of the
# Entity node.
#
# The resolution of the children of the EntityReference (the replacement
# value of the referenced Entity) may be lazily evaluated; actions by the
# user (such as calling the childNodes method on the EntityReference
# node) are assumed to trigger the evaluation.
sub getChildNodes
{
my $self = shift;
my $entity = $self->{Doc}->getEntity ($self->{EntityName});
defined ($entity) ? $entity->getChildNodes : new XML::DOM::NodeList;
}
sub cloneNode
{
my $self = shift;
$self->{Doc}->createEntityReference ($self->{EntityName},
$self->{Parameter});
}
# NOTE: an EntityReference can't really have children, so rejectChild
# is not reimplemented (i.e. it always returns 0.)
######################################################################
package XML::DOM::AttDef;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
import Carp;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::Node );
#------------------------------------------------------------
# Extra method implementations
# AttDef is not part of DOM Spec
sub new
{
my ($class, $doc, $name, $attrType, $default, $fixed) = @_;
croak new XML::DOM::DOMException (INVALID_CHARACTER_ERR,
"bad Attr name in AttDef [$name]")
unless XML::DOM::isValidName ($name);
my $self = bless {Doc => $doc,
Name => $name,
Type => $attrType}, $class;
if (defined $default)
{
if ($default eq "#REQUIRED")
{
$self->{Required} = 1;
}
elsif ($default eq "#IMPLIED")
{
$self->{Implied} = 1;
}
else
{
# strip off quotes - see Attlist handler in XML::Parser
$default =~ m#^(["'])(.*)['"]$#;
$self->{Quote} = $1; # keep track of the quote character
$self->{Default} = $self->setDefault ($2);
#?? should default value be decoded - what if it contains e.g. "&"
}
}
$self->{Fixed} = $fixed if defined $fixed;
$self;
}
sub getNodeType
{
ATT_DEF_NODE;
}
sub getName
{
$_[0]->{Name};
}
# So it can be added to a NamedNodeMap
sub getNodeName
{
$_[0]->{Name};
}
sub getDefault
{
$_[0]->{Default};
}
sub setDefault
{
my ($self, $value) = @_;
# specified=0, it's the default !
my $attr = $self->{Doc}->createAttribute ($self->{Name}, undef, 0);
$attr->{ReadOnly} = 1;
#?? this should be split over Text and EntityReference nodes, just like other
# Attr nodes - just expand the text for now
$value = $self->expandEntityRefs ($value);
$attr->addText ($value);
#?? reimplement in NoExpand mode!
$attr;
}
sub isFixed
{
$_[0]->{Fixed} || 0;
}
sub isRequired
{
$_[0]->{Required} || 0;
}
sub isImplied
{
$_[0]->{Implied} || 0;
}
sub print
{
my ($self, $FILE) = @_;
my $name = $self->{Name};
my $type = $self->{Type};
my $fixed = $self->{Fixed};
my $default = $self->{Default};
$FILE->print ("$name $type");
$FILE->print (" #FIXED") if defined $fixed;
if ($self->{Required})
{
$FILE->print (" #REQUIRED");
}
elsif ($self->{Implied})
{
$FILE->print (" #IMPLIED");
}
elsif (defined ($default))
{
my $quote = $self->{Quote};
$FILE->print (" $quote");
for my $kid (@{$default->{C}})
{
$kid->print ($FILE);
}
$FILE->print ($quote);
}
}
sub getDefaultString
{
my $self = shift;
my $default;
if ($self->{Required})
{
return "#REQUIRED";
}
elsif ($self->{Implied})
{
return "#IMPLIED";
}
elsif (defined ($default = $self->{Default}))
{
my $quote = $self->{Quote};
$default = $default->toString;
return "$quote$default$quote";
}
undef;
}
sub cloneNode
{
my $self = shift;
my $node = new XML::DOM::AttDef ($self->{Doc}, $self->{Name}, $self->{Type},
undef, $self->{Fixed});
$node->{Required} = 1 if $self->{Required};
$node->{Implied} = 1 if $self->{Implied};
$node->{Fixed} = $self->{Fixed} if defined $self->{Fixed};
if (defined $self->{Default})
{
$node->{Default} = $self->{Default}->cloneNode(1);
}
$node->{Quote} = $self->{Quote};
$node;
}
sub setOwnerDocument
{
my ($self, $doc) = @_;
$self->SUPER::setOwnerDocument ($doc);
if (defined $self->{Default})
{
$self->{Default}->setOwnerDocument ($doc);
}
}
######################################################################
package XML::DOM::AttlistDecl;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
import Carp;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::Node );
#------------------------------------------------------------
# Extra method implementations
# AttlistDecl is not part of the DOM Spec
sub new
{
my ($class, $doc, $name) = @_;
croak new XML::DOM::DOMException (INVALID_CHARACTER_ERR,
"bad Element TagName [$name] in AttlistDecl")
unless XML::DOM::isValidName ($name);
my $self = bless {Doc => $doc,
C => new XML::DOM::NodeList,
ReadOnly => 1,
Name => $name}, $class;
$self->{A} = new XML::DOM::NamedNodeMap (Doc => $doc,
ReadOnly => 1,
Parent => $self);
$self;
}
sub getNodeType
{
ATTLIST_DECL_NODE;
}
sub getName
{
$_[0]->{Name};
}
sub getNodeName
{
$_[0]->{Name};
}
sub getAttDef
{
my ($self, $attrName) = @_;
$self->{A}->getNamedItem ($attrName);
}
sub addAttDef
{
my ($self, $attrName, $type, $default, $fixed) = @_;
my $node = $self->getAttDef ($attrName);
if (defined $node)
{
# data will be ignored if already defined
my $elemName = $self->getName;
warn "multiple definitions of attribute $attrName for element $elemName, only first one is recognized";
}
else
{
$node = new XML::DOM::AttDef ($self->{Doc}, $attrName, $type,
$default, $fixed);
$self->{A}->setNamedItem ($node);
}
$node;
}
sub getDefaultAttrValue
{
my ($self, $attr) = @_;
my $attrNode = $self->getAttDef ($attr);
(defined $attrNode) ? $attrNode->getDefault : undef;
}
sub cloneNode
{
my ($self, $deep) = @_;
my $node = $self->{Doc}->createAttlistDecl ($self->{Name});
$node->{A} = $self->{A}->cloneNode ($deep);
$node;
}
sub setOwnerDocument
{
my ($self, $doc) = @_;
$self->SUPER::setOwnerDocument ($doc);
$self->{A}->setOwnerDocument ($doc);
}
sub print
{
my ($self, $FILE) = @_;
my $name = $self->getName;
my @attlist = @{$self->{A}->getValues};
if (@attlist > 0)
{
$FILE->print ("<!ATTLIST $name");
if (@attlist == 1)
{
$FILE->print (" ");
$attlist[0]->print ($FILE);
}
else
{
for my $attr (@attlist)
{
$FILE->print ("\x0A ");
$attr->print ($FILE);
}
}
$FILE->print (">");
}
}
######################################################################
package XML::DOM::ElementDecl;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
import Carp;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::Node );
#------------------------------------------------------------
# Extra method implementations
# ElementDecl is not part of the DOM Spec
sub new
{
my ($class, $doc, $name, $model) = @_;
croak new XML::DOM::DOMException (INVALID_CHARACTER_ERR,
"bad Element TagName [$name] in ElementDecl")
unless XML::DOM::isValidName ($name);
bless {Doc => $doc,
Name => $name,
ReadOnly => 1,
Model => $model}, $class;
}
sub getNodeType
{
ELEMENT_DECL_NODE;
}
sub getName
{
$_[0]->{Name};
}
sub getNodeName
{
$_[0]->{Name};
}
sub getModel
{
$_[0]->{Model};
}
sub setModel
{
my ($self, $model) = @_;
$self->{Model} = $model;
}
sub print
{
my ($self, $FILE) = @_;
my $name = $self->{Name};
my $model = $self->{Model};
$FILE->print ("<!ELEMENT $name $model>");
}
sub cloneNode
{
my $self = shift;
$self->{Doc}->createElementDecl ($self->{Name}, $self->{Model});
}
######################################################################
package XML::DOM::Element;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
import Carp;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::Node );
sub new
{
my ($class, $doc, $tagName) = @_;
if ($XML::DOM::SafeMode)
{
croak new XML::DOM::DOMException (INVALID_CHARACTER_ERR,
"bad Element TagName [$tagName]")
unless XML::DOM::isValidName ($tagName);
}
my $self = bless {Doc => $doc,
C => new XML::DOM::NodeList,
TagName => $tagName}, $class;
$self->{A} = new XML::DOM::NamedNodeMap (Doc => $doc,
Parent => $self);
$self;
}
sub getNodeType
{
ELEMENT_NODE;
}
sub getTagName
{
$_[0]->{TagName};
}
sub getNodeName
{
$_[0]->{TagName};
}
sub getAttributeNode
{
my ($self, $name) = @_;
$self->getAttributes->{$name};
}
sub getAttribute
{
my ($self, $name) = @_;
my $attr = $self->getAttributeNode ($name);
(defined $attr) ? $attr->getValue : "";
}
sub setAttribute
{
my ($self, $name, $val) = @_;
croak new XML::DOM::DOMException (INVALID_CHARACTER_ERR,
"bad Attr Name [$name]")
unless XML::DOM::isValidName ($name);
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR,
"node is ReadOnly")
if $self->isReadOnly;
my $node = $self->{A}->{$name};
if (defined $node)
{
$node->setValue ($val);
}
else
{
$node = $self->{Doc}->createAttribute ($name, $val);
$self->{A}->setNamedItem ($node);
}
}
sub setAttributeNode
{
my ($self, $node) = @_;
my $attr = $self->{A};
my $name = $node->getNodeName;
# REC 1147
if ($XML::DOM::SafeMode)
{
croak new XML::DOM::DOMException (WRONG_DOCUMENT_ERR,
"nodes belong to different documents")
if $self->{Doc} != $node->{Doc};
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR,
"node is ReadOnly")
if $self->isReadOnly;
my $attrParent = $node->{UsedIn};
croak new XML::DOM::DOMException (INUSE_ATTRIBUTE_ERR,
"Attr is already used by another Element")
if (defined ($attrParent) && $attrParent != $attr);
}
my $other = $attr->{$name};
$attr->removeNamedItem ($name) if defined $other;
$attr->setNamedItem ($node);
$other;
}
sub removeAttributeNode
{
my ($self, $node) = @_;
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR,
"node is ReadOnly")
if $self->isReadOnly;
my $attr = $self->{A};
my $name = $node->getNodeName;
my $attrNode = $attr->getNamedItem ($name);
#?? should it croak if it's the default value?
croak new XML::DOM::DOMException (NOT_FOUND_ERR)
unless $node == $attrNode;
# Not removing anything if it's the default value already
return undef unless $node->isSpecified;
$attr->removeNamedItem ($name);
# Substitute with default value if it's defined
my $default = $self->getDefaultAttrValue ($name);
if (defined $default)
{
my $oldIgnore = XML::DOM::ignoreReadOnly (1); # temporarily
$default = $default->cloneNode (1);
$attr->setNamedItem ($default);
XML::DOM::ignoreReadOnly ($oldIgnore); # restore previous value
}
$node;
}
sub removeAttribute
{
my ($self, $name) = @_;
my $node = $self->{A}->getNamedItem ($name);
#?? could use dispose() to remove circular references for gc, but what if
#?? somebody is referencing it?
$self->removeAttributeNode ($node) if defined $node;
}
sub cloneNode
{
my ($self, $deep) = @_;
my $node = $self->{Doc}->createElement ($self->getTagName);
# Always clone the Attr nodes, even if $deep == 0
$node->{A} = $self->{A}->cloneNode (1); # deep=1
$node->{A}->setParentNode ($node);
$node->cloneChildren ($self, $deep);
$node;
}
sub getAttributes
{
$_[0]->{A};
}
#------------------------------------------------------------
# Extra method implementations
# Added for convenience
sub setTagName
{
my ($self, $tagName) = @_;
croak new XML::DOM::DOMException (INVALID_CHARACTER_ERR,
"bad Element TagName [$tagName]")
unless XML::DOM::isValidName ($tagName);
$self->{TagName} = $tagName;
}
sub isReadOnly
{
0;
}
# Added for optimization.
sub isElementNode
{
1;
}
sub rejectChild
{
my $t = $_[1]->getNodeType;
$t != TEXT_NODE
&& $t != ENTITY_REFERENCE_NODE
&& $t != PROCESSING_INSTRUCTION_NODE
&& $t != COMMENT_NODE
&& $t != CDATA_SECTION_NODE
&& $t != ELEMENT_NODE;
}
sub getDefaultAttrValue
{
my ($self, $attr) = @_;
$self->{Doc}->getDefaultAttrValue ($self->{TagName}, $attr);
}
sub dispose
{
my $self = shift;
$self->{A}->dispose;
$self->SUPER::dispose;
}
sub setOwnerDocument
{
my ($self, $doc) = @_;
$self->SUPER::setOwnerDocument ($doc);
$self->{A}->setOwnerDocument ($doc);
}
sub print
{
my ($self, $FILE) = @_;
my $name = $self->{TagName};
$FILE->print ("<$name");
for my $att (@{$self->{A}->getValues})
{
# skip un-specified (default) Attr nodes
if ($att->isSpecified)
{
$FILE->print (" ");
$att->print ($FILE);
}
}
my @kids = @{$self->{C}};
if (@kids > 0)
{
$FILE->print (">");
for my $kid (@kids)
{
$kid->print ($FILE);
}
$FILE->print ("</$name>");
}
else
{
my $style = &$XML::DOM::TagStyle ($name, $self);
if ($style == 0)
{
$FILE->print ("/>");
}
elsif ($style == 1)
{
$FILE->print ("></$name>");
}
else
{
$FILE->print (" />");
}
}
}
######################################################################
package XML::DOM::CharacterData;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
import Carp;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::Node );
# CharacterData nodes should never be created directly, only subclassed!
sub appendData
{
my ($self, $data) = @_;
if ($XML::DOM::SafeMode)
{
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR,
"node is ReadOnly")
if $self->isReadOnly;
}
$self->{Data} .= $data;
}
sub deleteData
{
my ($self, $offset, $count) = @_;
croak new XML::DOM::DOMException (INDEX_SIZE_ERR,
"bad offset [$offset]")
if ($offset < 0 || $offset >= length ($self->{Data}));
#?? DOM Spec says >, but >= makes more sense!
croak new XML::DOM::DOMException (INDEX_SIZE_ERR,
"negative count [$count]")
if $count < 0;
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR,
"node is ReadOnly")
if $self->isReadOnly;
substr ($self->{Data}, $offset, $count) = "";
}
sub getData
{
$_[0]->{Data};
}
sub getLength
{
length $_[0]->{Data};
}
sub insertData
{
my ($self, $offset, $data) = @_;
croak new XML::DOM::DOMException (INDEX_SIZE_ERR,
"bad offset [$offset]")
if ($offset < 0 || $offset >= length ($self->{Data}));
#?? DOM Spec says >, but >= makes more sense!
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR,
"node is ReadOnly")
if $self->isReadOnly;
substr ($self->{Data}, $offset, 0) = $data;
}
sub replaceData
{
my ($self, $offset, $count, $data) = @_;
croak new XML::DOM::DOMException (INDEX_SIZE_ERR,
"bad offset [$offset]")
if ($offset < 0 || $offset >= length ($self->{Data}));
#?? DOM Spec says >, but >= makes more sense!
croak new XML::DOM::DOMException (INDEX_SIZE_ERR,
"negative count [$count]")
if $count < 0;
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR,
"node is ReadOnly")
if $self->isReadOnly;
substr ($self->{Data}, $offset, $count) = $data;
}
sub setData
{
my ($self, $data) = @_;
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR,
"node is ReadOnly")
if $self->isReadOnly;
$self->{Data} = $data;
}
sub substringData
{
my ($self, $offset, $count) = @_;
my $data = $self->{Data};
croak new XML::DOM::DOMException (INDEX_SIZE_ERR,
"bad offset [$offset]")
if ($offset < 0 || $offset >= length ($data));
#?? DOM Spec says >, but >= makes more sense!
croak new XML::DOM::DOMException (INDEX_SIZE_ERR,
"negative count [$count]")
if $count < 0;
substr ($data, $offset, $count);
}
sub getNodeValue
{
$_[0]->getData;
}
sub setNodeValue
{
$_[0]->setData ($_[1]);
}
######################################################################
package XML::DOM::CDATASection;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::CharacterData );
sub new
{
my ($class, $doc, $data) = @_;
bless {Doc => $doc,
Data => $data}, $class;
}
sub getNodeName
{
"#cdata-section";
}
sub getNodeType
{
CDATA_SECTION_NODE;
}
sub cloneNode
{
my $self = shift;
$self->{Doc}->createCDATASection ($self->getData);
}
#------------------------------------------------------------
# Extra method implementations
sub isReadOnly
{
0;
}
sub print
{
my ($self, $FILE) = @_;
$FILE->print ("<![CDATA[");
$FILE->print (XML::DOM::encodeCDATA ($self->getData));
$FILE->print ("]]>");
}
######################################################################
package XML::DOM::Comment;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
import Carp;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::CharacterData );
#?? setData - could check comment for double minus
sub new
{
my ($class, $doc, $data) = @_;
bless {Doc => $doc,
Data => $data}, $class;
}
sub getNodeType
{
COMMENT_NODE;
}
sub getNodeName
{
"#comment";
}
sub cloneNode
{
my $self = shift;
$self->{Doc}->createComment ($self->getData);
}
#------------------------------------------------------------
# Extra method implementations
sub isReadOnly
{
return 0 if $XML::DOM::IgnoreReadOnly;
my $pa = $_[0]->{Parent};
defined ($pa) ? $pa->isReadOnly : 0;
}
sub print
{
my ($self, $FILE) = @_;
my $comment = XML::DOM::encodeComment ($self->{Data});
$FILE->print ("<!--$comment-->");
}
######################################################################
package XML::DOM::Text;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
import Carp;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::CharacterData );
sub new
{
my ($class, $doc, $data) = @_;
bless {Doc => $doc,
Data => $data}, $class;
}
sub getNodeType
{
TEXT_NODE;
}
sub getNodeName
{
"#text";
}
sub splitText
{
my ($self, $offset) = @_;
my $data = $self->getData;
croak new XML::DOM::DOMException (INDEX_SIZE_ERR,
"bad offset [$offset]")
if ($offset < 0 || $offset >= length ($data));
#?? DOM Spec says >, but >= makes more sense!
croak new XML::DOM::DOMException (NO_MODIFICATION_ALLOWED_ERR,
"node is ReadOnly")
if $self->isReadOnly;
my $rest = substring ($data, $offset);
$self->setData (substring ($data, 0, $offset));
my $node = $self->{Doc}->createTextNode ($rest);
# insert new node after this node
$self->{Parent}->insertAfter ($node, $self);
$node;
}
sub cloneNode
{
my $self = shift;
$self->{Doc}->createTextNode ($self->getData);
}
#------------------------------------------------------------
# Extra method implementations
sub isReadOnly
{
0;
}
sub print
{
my ($self, $FILE) = @_;
$FILE->print (XML::DOM::encodeText ($self->getData, "<&"));
}
sub isTextNode
{
1;
}
######################################################################
package XML::DOM::XMLDecl;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::Node );
#------------------------------------------------------------
# Extra method implementations
# XMLDecl is not part of the DOM Spec
sub new
{
my ($class, $doc, $version, $encoding, $standalone) = @_;
my $self = bless {Doc => $doc}, $class;
$self->{Version} = $version if defined $version;
$self->{Encoding} = $encoding if defined $encoding;
$self->{Standalone} = $standalone if defined $standalone;
$self;
}
sub setVersion
{
if (defined $_[1])
{
$_[0]->{Version} = $_[1];
}
else
{
delete $_[0]->{Version};
}
}
sub getVersion
{
$_[0]->{Version};
}
sub setEncoding
{
if (defined $_[1])
{
$_[0]->{Encoding} = $_[1];
}
else
{
delete $_[0]->{Encoding};
}
}
sub getEncoding
{
$_[0]->{Encoding};
}
sub setStandalone
{
if (defined $_[1])
{
$_[0]->{Standalone} = $_[1];
}
else
{
delete $_[0]->{Standalone};
}
}
sub getStandalone
{
$_[0]->{Standalone};
}
sub getNodeType
{
XML_DECL_NODE;
}
sub cloneNode
{
my $self = shift;
new XML::DOM::XMLDecl ($self->{Doc}, $self->{Version},
$self->{Encoding}, $self->{Standalone});
}
sub print
{
my ($self, $FILE) = @_;
my $version = $self->{Version};
my $encoding = $self->{Encoding};
my $standalone = $self->{Standalone};
$standalone = ($standalone ? "yes" : "no") if defined $standalone;
$FILE->print ("<?xml");
$FILE->print (" version=\"$version\"") if defined $version;
$FILE->print (" encoding=\"$encoding\"") if defined $encoding;
$FILE->print (" standalone=\"$standalone\"") if defined $standalone;
$FILE->print ("?>");
}
######################################################################
package XML::DOM::DocumentType;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::Node );
sub new
{
my $class = shift;
my $doc = shift;
my $self = bless {Doc => $doc,
ReadOnly => 1,
C => new XML::DOM::NodeList}, $class;
$self->{Entities} = new XML::DOM::NamedNodeMap (Doc => $doc,
Parent => $self,
ReadOnly => 1);
$self->{Notations} = new XML::DOM::NamedNodeMap (Doc => $doc,
Parent => $self,
ReadOnly => 1);
$self->setParams (@_);
$self;
}
sub getNodeType
{
DOCUMENT_TYPE_NODE;
}
sub getNodeName
{
$_[0]->{Name};
}
sub getName
{
$_[0]->{Name};
}
sub getEntities
{
$_[0]->{Entities};
}
sub getNotations
{
$_[0]->{Notations};
}
sub setParentNode
{
my ($self, $parent) = @_;
$self->SUPER::setParentNode ($parent);
$parent->{Doctype} = $self
if $parent->getNodeType == DOCUMENT_NODE;
}
sub cloneNode
{
my ($self, $deep) = @_;
my $node = new XML::DOM::DocumentType ($self->{Doc}, $self->{Name},
$self->{SysId}, $self->{PubId},
$self->{Internal});
#?? does it make sense to make a shallow copy?
# clone the NamedNodeMaps
$node->{Entities} = $self->{Entities}->cloneNode ($deep);
$node->{Notations} = $self->{Notations}->cloneNode ($deep);
$node->cloneChildren ($self, $deep);
$node;
}
#------------------------------------------------------------
# Extra method implementations
sub getSysId
{
$_[0]->{SysId};
}
sub getPubId
{
$_[0]->{PubId};
}
sub setSysId
{
$_[0]->{SysId} = $_[1];
}
sub setPubId
{
$_[0]->{PubId} = $_[1];
}
sub setName
{
$_[0]->{Name} = $_[1];
}
sub removeChildHoodMemories
{
my ($self, $dontWipeReadOnly) = @_;
my $parent = $self->{Parent};
if (defined $parent && $parent->getNodeType == DOCUMENT_NODE)
{
delete $parent->{Doctype};
}
$self->SUPER::removeChildHoodMemories;
}
sub dispose
{
my $self = shift;
$self->{Entities}->dispose;
$self->{Notations}->dispose;
$self->SUPER::dispose;
}
sub setOwnerDocument
{
my ($self, $doc) = @_;
$self->SUPER::setOwnerDocument ($doc);
$self->{Entities}->setOwnerDocument ($doc);
$self->{Notations}->setOwnerDocument ($doc);
}
sub expandEntity
{
my ($self, $ent, $param) = @_;
my $kid = $self->{Entities}->getNamedItem ($ent);
return $kid->getValue
if (defined ($kid) && $param == $kid->isParameterEntity);
undef; # entity not found
}
sub getAttlistDecl
{
my ($self, $elemName) = @_;
for my $kid (@{$_[0]->{C}})
{
return $kid if ($kid->getNodeType == ATTLIST_DECL_NODE &&
$kid->getName eq $elemName);
}
undef; # not found
}
sub getElementDecl
{
my ($self, $elemName) = @_;
for my $kid (@{$_[0]->{C}})
{
return $kid if ($kid->getNodeType == ELEMENT_DECL_NODE &&
$kid->getName eq $elemName);
}
undef; # not found
}
sub addElementDecl
{
my ($self, $name, $model) = @_;
my $node = $self->getElementDecl ($name);
#?? could warn
unless (defined $node)
{
$node = $self->{Doc}->createElementDecl ($name, $model);
$self->appendChild ($node);
}
$node;
}
sub addAttlistDecl
{
my ($self, $name) = @_;
my $node = $self->getAttlistDecl ($name);
unless (defined $node)
{
$node = $self->{Doc}->createAttlistDecl ($name);
$self->appendChild ($node);
}
$node;
}
sub addNotation
{
my $self = shift;
my $node = $self->{Doc}->createNotation (@_);
$self->{Notations}->setNamedItem ($node);
$node;
}
sub addEntity
{
my $self = shift;
my $node = $self->{Doc}->createEntity (@_);
$self->{Entities}->setNamedItem ($node);
$node;
}
# All AttDefs for a certain Element are merged into a single ATTLIST
sub addAttDef
{
my $self = shift;
my $elemName = shift;
# create the AttlistDecl if it doesn't exist yet
my $elemDecl = $self->addAttlistDecl ($elemName);
$elemDecl->addAttDef (@_);
}
sub getDefaultAttrValue
{
my ($self, $elem, $attr) = @_;
my $elemNode = $self->getAttlistDecl ($elem);
(defined $elemNode) ? $elemNode->getDefaultAttrValue ($attr) : undef;
}
sub getEntity
{
my ($self, $entity) = @_;
$self->{Entities}->getNamedItem ($entity);
}
sub setParams
{
my ($self, $name, $sysid, $pubid, $internal) = @_;
$self->{Name} = $name;
#?? not sure if we need to hold on to these...
$self->{SysId} = $sysid if defined $sysid;
$self->{PubId} = $pubid if defined $pubid;
$self->{Internal} = $internal if defined $internal;
$self;
}
sub rejectChild
{
# DOM Spec says: DocumentType -- no children
not $XML::DOM::IgnoreReadOnly;
}
sub print
{
my ($self, $FILE) = @_;
my $name = $self->{Name};
my $sysId = $self->{SysId};
my $pubId = $self->{PubId};
$FILE->print ("<!DOCTYPE $name");
if (defined $pubId)
{
$FILE->print (" PUBLIC \"$pubId\" \"$sysId\"");
}
elsif (defined $sysId)
{
$FILE->print (" SYSTEM \"$sysId\"");
}
my @entities = @{$self->{Entities}->getValues};
my @notations = @{$self->{Notations}->getValues};
my @kids = @{$self->{C}};
if (@entities || @notations || @kids)
{
$FILE->print (" [\x0A");
for my $kid (@entities)
{
$FILE->print (" ");
$kid->print ($FILE);
$FILE->print ("\x0A");
}
for my $kid (@notations)
{
$FILE->print (" ");
$kid->print ($FILE);
$FILE->print ("\x0A");
}
for my $kid (@kids)
{
$FILE->print (" ");
$kid->print ($FILE);
$FILE->print ("\x0A");
}
$FILE->print ("]");
}
$FILE->print (">");
}
######################################################################
package XML::DOM::DocumentFragment;
######################################################################
BEGIN
{
import XML::DOM::Node;
import XML::DOM::DOMException;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::Node );
sub new
{
my ($class, $doc) = @_;
bless {Doc => $doc,
C => new XML::DOM::NodeList}, $class;
}
sub getNodeType
{
DOCUMENT_FRAGMENT_NODE;
}
sub getNodeName
{
"#document-fragment";
}
sub cloneNode
{
my ($self, $deep) = @_;
my $node = $self->{Doc}->createDocumentFragment;
$node->cloneChildren ($self, $deep);
$node;
}
#------------------------------------------------------------
# Extra method implementations
sub isReadOnly
{
0;
}
sub print
{
my ($self, $FILE) = @_;
for my $node (@{$self->{C}})
{
$node->print ($FILE);
}
}
sub rejectChild
{
my $t = $_[1]->getNodeType;
$t != TEXT_NODE
&& $t != ENTITY_REFERENCE_NODE
&& $t != PROCESSING_INSTRUCTION_NODE
&& $t != COMMENT_NODE
&& $t != CDATA_SECTION_NODE
&& $t != ELEMENT_NODE;
}
sub isDocumentFragmentNode
{
1;
}
######################################################################
package XML::DOM::Document;
######################################################################
BEGIN
{
import Carp;
import XML::DOM::Node;
import XML::DOM::DOMException;
}
use vars qw( @ISA );
@ISA = qw( XML::DOM::Node );
sub new
{
my ($class) = @_;
my $self = bless {C => new XML::DOM::NodeList}, $class;
# keep Doc pointer, even though getOwnerDocument returns undef
$self->{Doc} = $self;
$self;
}
sub getNodeType
{
DOCUMENT_NODE;
}
sub getNodeName
{
"#document";
}
#?? not sure about keeping a fixed order of these nodes....
sub getDoctype
{
$_[0]->{Doctype};
}
sub getDocumentElement
{
my ($self) = @_;
for my $kid (@{$self->{C}})
{
return $kid if $kid->isElementNode;
}
undef;
}
sub getOwnerDocument
{
undef;
}
sub getImplementation
{
$XML::DOM::DOMImplementation::Singleton;
}
#
# Added extra parameters ($val, $specified) that are passed straight to the
# Attr constructor
#
sub createAttribute
{
new XML::DOM::Attr (@_);
}
sub createCDATASection
{
new XML::DOM::CDATASection (@_);
}
sub createComment
{
new XML::DOM::Comment (@_);
}
sub createElement
{
new XML::DOM::Element (@_);
}
sub createTextNode
{
new XML::DOM::Text (@_);
}
sub createProcessingInstruction
{
new XML::DOM::ProcessingInstruction (@_);
}
sub createEntityReference
{
new XML::DOM::EntityReference (@_);
}
sub createDocumentFragment
{
new XML::DOM::DocumentFragment (@_);
}
sub createDocumentType
{
new XML::DOM::DocumentType (@_);
}
sub cloneNode
{
my ($self, $deep) = @_;
my $node = new XML::DOM::Document;
$node->cloneChildren ($self, $deep);
my $xmlDecl = $self->{XmlDecl};
$node->{XmlDecl} = $xmlDecl->cloneNode ($deep) if defined $xmlDecl;
$node;
}
sub appendChild
{
my ($self, $node) = @_;
# Extra check: make sure sure we don't end up with more than 1 Elements.
# Don't worry about multiple DocType nodes, because DocumentFragment
# can't contain DocType nodes.
my @nodes = ($node);
@nodes = @{$node->{C}}
if $node->getNodeType == DOCUMENT_FRAGMENT_NODE;
my $elem = 0;
for my $n (@nodes)
{
$elem++ if $n->isElementNode;
}
if ($elem > 0 && defined ($self->getDocumentElement))
{
croak new XML::DOM::DOMException (HIERARCHY_REQUEST_ERR,
"document can have only 1 Element");
}
$self->SUPER::appendChild ($node);
}
sub insertBefore
{
my ($self, $node, $refNode) = @_;
# Extra check: make sure sure we don't end up with more than 1 Elements.
# Don't worry about multiple DocType nodes, because DocumentFragment
# can't contain DocType nodes.
my @nodes = ($node);
@nodes = @{$node->{C}}
if $node->getNodeType == DOCUMENT_FRAGMENT_NODE;
my $elem = 0;
for my $n (@nodes)
{
$elem++ if $n->isElementNode;
}
if ($elem > 0 && defined ($self->getDocumentElement))
{
croak new XML::DOM::DOMException (HIERARCHY_REQUEST_ERR,
"document can have only 1 Element");
}
$self->SUPER::insertBefore ($node, $refNode);
}
sub replaceChild
{
my ($self, $node, $refNode) = @_;
# Extra check: make sure sure we don't end up with more than 1 Elements.
# Don't worry about multiple DocType nodes, because DocumentFragment
# can't contain DocType nodes.
my @nodes = ($node);
@nodes = @{$node->{C}}
if $node->getNodeType == DOCUMENT_FRAGMENT_NODE;
my $elem = 0;
$elem-- if $refNode->isElementNode;
for my $n (@nodes)
{
$elem++ if $n->isElementNode;
}
if ($elem > 0 && defined ($self->getDocumentElement))
{
croak new XML::DOM::DOMException (HIERARCHY_REQUEST_ERR,
"document can have only 1 Element");
}
$self->SUPER::appendChild ($node, $refNode);
}
#------------------------------------------------------------
# Extra method implementations
sub isReadOnly
{
0;
}
sub print
{
my ($self, $FILE) = @_;
my $xmlDecl = $self->getXMLDecl;
if (defined $xmlDecl)
{
$xmlDecl->print ($FILE);
$FILE->print ("\x0A");
}
for my $node (@{$self->{C}})
{
$node->print ($FILE);
$FILE->print ("\x0A");
}
}
sub setDoctype
{
my ($self, $doctype) = @_;
my $oldDoctype = $self->{Doctype};
if (defined $oldDoctype)
{
$self->replaceChild ($doctype, $oldDoctype);
}
else
{
#?? before root element!
$self->appendChild ($doctype);
}
$_[0]->{Doctype} = $_[1];
}
sub removeDoctype
{
my $self = shift;
my $doctype = $self->removeChild ($self->{Doctype});
delete $self->{Doctype};
$doctype;
}
sub rejectChild
{
my $t = $_[1]->getNodeType;
$t != ELEMENT_NODE
&& $t != PROCESSING_INSTRUCTION_NODE
&& $t != COMMENT_NODE
&& $t != DOCUMENT_TYPE_NODE;
}
sub expandEntity
{
my ($self, $ent, $param) = @_;
my $doctype = $self->getDoctype;
(defined $doctype) ? $doctype->expandEntity ($ent, $param) : undef;
}
sub getDefaultAttrValue
{
my ($self, $elem, $attr) = @_;
my $doctype = $self->getDoctype;
(defined $doctype) ? $doctype->getDefaultAttrValue ($elem, $attr) : undef;
}
sub getEntity
{
my ($self, $entity) = @_;
my $doctype = $self->getDoctype;
(defined $doctype) ? $doctype->getEntity ($entity) : undef;
}
sub dispose
{
my $self = shift;
$self->{XmlDecl}->dispose if defined $self->{XmlDecl};
delete $self->{XmlDecl};
delete $self->{Doctype};
$self->SUPER::dispose;
}
sub setOwnerDocument
{
# Do nothing, you can't change the owner document!
}
sub getXMLDecl
{
$_[0]->{XmlDecl};
}
sub setXMLDecl
{
$_[0]->{XmlDecl} = $_[1];
}
sub createXMLDecl
{
new XML::DOM::XMLDecl (@_);
}
sub createNotation
{
new XML::DOM::Notation (@_);
}
sub createElementDecl
{
new XML::DOM::ElementDecl (@_);
}
sub createAttlistDecl
{
new XML::DOM::AttlistDecl (@_);
}
sub createEntity
{
new XML::DOM::Entity (@_);
}
######################################################################
package XML::DOM::Parser;
######################################################################
use vars qw ( @ISA );
@ISA = qw( XML::Parser );
sub new
{
my ($class, %args) = @_;
$args{Style} = 'Dom';
$class->SUPER::new (%args);
}
# This method needed to be overriden so we can restore some global
# variables when an exception is thrown
sub parse
{
my $self = shift;
local $XML::Parser::Dom::_DP_doc;
local $XML::Parser::Dom::_DP_elem;
local $XML::Parser::Dom::_DP_doctype;
local $XML::Parser::Dom::_DP_in_prolog;
local $XML::Parser::Dom::_DP_end_doc;
local $XML::Parser::Dom::_DP_saw_doctype;
local $XML::Parser::Dom::_DP_in_CDATA;
local $XML::Parser::Dom::_DP_keep_CDATA;
local $XML::Parser::Dom::_DP_last_text;
# Temporarily disable checks that Expat already does (for performance)
local $XML::DOM::SafeMode = 0;
# Temporarily disable ReadOnly checks
local $XML::DOM::IgnoreReadOnly = 1;
my $ret;
eval {
$ret = $self->SUPER::parse (@_);
};
my $err = $@;
if ($err)
{
my $doc = $XML::Parser::Dom::_DP_doc;
if ($doc)
{
$doc->dispose;
}
die $err;
}
$ret;
}
######################################################################
package XML::Parser::Dom;
######################################################################
use vars qw( $_DP_doc
$_DP_elem
$_DP_doctype
$_DP_in_prolog
$_DP_end_doc
$_DP_saw_doctype
$_DP_in_CDATA
$_DP_keep_CDATA
$_DP_last_text
);
# This adds a new Style to the XML::Parser class.
# From now on you can say: $parser = new XML::Parser ('Style' => 'Dom' );
# but that is *NOT* how a regular user should use it!
$XML::Parser::Built_In_Styles{Dom} = 1;
sub Init
{
$_DP_elem = $_DP_doc = new XML::DOM::Document();
$_DP_doctype = new XML::DOM::DocumentType ($_DP_doc);
$_DP_doc->setDoctype ($_DP_doctype);
$_DP_keep_CDATA = $_[0]->{KeepCDATA};
# Prepare for document prolog
$_DP_in_prolog = 1;
# $expat->{DOM_inProlog} = 1;
# We haven't passed the root element yet
$_DP_end_doc = 0;
undef $_DP_last_text;
}
sub Final
{
unless ($_DP_saw_doctype)
{
my $doctype = $_DP_doc->removeDoctype;
$doctype->dispose;
}
$_DP_doc;
}
sub Char
{
my $str = $_[1];
if ($_DP_in_CDATA && $_DP_keep_CDATA)
{
undef $_DP_last_text;
# Merge text with previous node if possible
$_DP_elem->addCDATA ($str);
}
else
{
# Merge text with previous node if possible
# Used to be: $expat->{DOM_Element}->addText ($str);
if ($_DP_last_text)
{
$_DP_last_text->{Data} .= $str;
}
else
{
$_DP_last_text = $_DP_doc->createTextNode ($str);
$_DP_last_text->{Parent} = $_DP_elem;
push @{$_DP_elem->{C}}, $_DP_last_text;
}
}
}
sub Start
{
my ($expat, $elem, @attr) = @_;
my $parent = $_DP_elem;
my $doc = $_DP_doc;
if ($parent == $doc)
{
# End of document prolog, i.e. start of first Element
$_DP_in_prolog = 0;
}
undef $_DP_last_text;
my $node = $doc->createElement ($elem);
$_DP_elem = $node;
$parent->appendChild ($node);
my $first_default = $expat->specified_attr;
my $i = 0;
my $n = @attr;
while ($i < $n)
{
my $specified = $i < $first_default;
my $name = $attr[$i++];
undef $_DP_last_text;
my $attr = $doc->createAttribute ($name, $attr[$i++], $specified);
$node->setAttributeNode ($attr);
}
}
sub End
{
$_DP_elem = $_DP_elem->{Parent};
undef $_DP_last_text;
# Check for end of root element
$_DP_end_doc = 1 if ($_DP_elem == $_DP_doc);
}
# Called at end of file, i.e. whitespace following last closing tag
# Also for Entity references
# May also be called at other times...
sub Default
{
my ($expat, $str) = @_;
# shift; deb ("Default", @_);
if ($_DP_in_prolog) # still processing Document prolog...
{
#?? could try to store this text later
#?? I've only seen whitespace here so far
}
elsif (!$_DP_end_doc) # ignore whitespace at end of Document
{
# if ($expat->{NoExpand})
# {
$str =~ /^&(.+);$/os;
return unless defined ($1);
# Got a TextDecl (<?xml ...?>) from an external entity here once
$_DP_elem->appendChild (
$_DP_doc->createEntityReference ($1));
undef $_DP_last_text;
# }
# else
# {
# $expat->{DOM_Element}->addText ($str);
# }
}
}
# XML::Parser 2.19 added support for CdataStart and CdataEnd handlers
# If they are not defined, the Default handler is called instead
# with the text "<![CDATA[" and "]]"
sub CdataStart
{
$_DP_in_CDATA = 1;
}
sub CdataEnd
{
$_DP_in_CDATA = 0;
}
sub Comment
{
undef $_DP_last_text;
my $comment = $_DP_doc->createComment ($_[1]);
$_DP_elem->appendChild ($comment);
}
sub deb
{
return;
my $name = shift;
print "$name (" . join(",", map {defined($_)?$_ : "(undef)"} @_) . ")\n";
}
sub Doctype
{
my $expat = shift;
# deb ("Doctype", @_);
$_DP_doctype->setParams (@_);
$_DP_saw_doctype = 1;
}
sub Attlist
{
my $expat = shift;
# deb ("Attlist", @_);
$_DP_doctype->addAttDef (@_);
}
sub XMLDecl
{
my $expat = shift;
# deb ("XMLDecl", @_);
undef $_DP_last_text;
$_DP_doc->setXMLDecl (new XML::DOM::XMLDecl ($_DP_doc, @_));
}
sub Entity
{
my $expat = shift;
# deb ("Entity", @_);
# Parameter Entities names are passed starting with '%'
my $parameter = 0;
if ($_[0] =~ /^%(.*)/s)
{
$_[0] = $1;
$parameter = 1;
}
undef $_DP_last_text;
$_DP_doctype->addEntity ($parameter, @_);
}
# Unparsed is called when it encounters e.g:
#
# <!ENTITY logo SYSTEM "http://server/logo.gif" NDATA gif>
#
sub Unparsed
{
Entity (@_); # same as regular ENTITY, as far as DOM is concerned
}
sub Element
{
shift;
# deb ("Element", @_);
undef $_DP_last_text;
$_DP_doctype->addElementDecl (@_);
}
sub Notation
{
shift;
# deb ("Notation", @_);
undef $_DP_last_text;
$_DP_doctype->addNotation (@_);
}
sub Proc
{
shift;
# deb ("Proc", @_);
undef $_DP_last_text;
$_DP_elem->appendChild (new XML::DOM::ProcessingInstruction ($_DP_doc, @_));
}
# ExternEnt is called when an external entity, such as:
#
# <!ENTITY externalEntity PUBLIC "-//Enno//TEXT Enno's description//EN"
# "http://server/descr.txt">
#
# is referenced in the document, e.g. with: &externalEntity;
# If ExternEnt is not specified, the entity reference is passed to the Default
# handler as e.g. "&externalEntity;", where an EntityReference onbject is added.
#
#sub ExternEnt
#{
# deb ("ExternEnt", @_);
#}
1; # module return code
__END__
=head1 NAME
XML::DOM - A perl module for building DOM Level 1 compliant document structures
=head1 SYNOPSIS
use XML::DOM;
my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile ("file.xml");
# print all HREF attributes of all CODEBASE elements
my $nodes = $doc->getElementsByTagName ("CODEBASE");
my $n = $nodes->getLength;
for (my $i = 0; $i < $n; $i++)
{
my $node = $nodes->item ($i);
my $href = $node->getAttribute ("HREF");
print $href->getValue . "\n";
}
$doc->printToFile ("out.xml");
print $doc->toString;
=head1 DESCRIPTION
This module extends the XML::Parser module by Clark Cooper.
The XML::Parser module is built on top of XML::Parser::Expat,
which is a lower level interface to James Clark's expat library.
XML::DOM::Parser is derived from XML::Parser. It parses XML strings or files
and builds a data structure that conforms to the API of the Document Object
Model as described at http://www.w3.org/TR/REC-DOM-Level-1.
See the XML::Parser manpage for other available features of the
XML::DOM::Parser class.
Note that the 'Style' property should not be used (it is set internally.)
The XML::Parser I<NoExpand> option is more or less supported, in that it will
generate EntityReference objects whenever an entity reference is encountered
in character data. I'm not sure how useful this is. Any comments are welcome.
As described in the synopsis, when you create an XML::DOM::Parser object,
the parse and parsefile methods create an I<XML::DOM::Document> object
from the specified input. This Document object can then be examined, modified and
written back out to a file or converted to a string.
When using XML::DOM with XML::Parser version 2.19 and up, setting the
XML::DOM::Parser option I<KeepCDATA> to 1 will store CDATASections in
CDATASection nodes, instead of converting them to Text nodes.
Subsequent CDATASection nodes will be merged into one. Let me know if this
is a problem.
A Document has a tree structure consisting of I<Node> objects. A Node may contain
other nodes, depending on its type.
A Document may have Element, Text, Comment, and CDATASection nodes.
Element nodes may have Attr, Element, Text, Comment, and CDATASection nodes.
The other nodes may not have any child nodes.
This module adds several node types that are not part of the DOM spec (yet.)
These are: ElementDecl (for <!ELEMENT ...> declarations), AttlistDecl (for
<!ATTLIST ...> declarations), XMLDecl (for <?xml ...?> declarations) and AttDef
(for attribute definitions in an AttlistDecl.)
=head1 DOM API
=over 4
=item XML::DOM
=over 4
=item Constant definitions
The following predefined constants indicate which type of node it is.
=back
UNKNOWN_NODE (0) The node type is unknown (not part of DOM)
ELEMENT_NODE (1) The node is an Element.
ATTRIBUTE_NODE (2) The node is an Attr.
TEXT_NODE (3) The node is a Text node.
CDATA_SECTION_NODE (4) The node is a CDATASection.
ENTITY_REFERENCE_NODE (5) The node is an EntityReference.
ENTITY_NODE (6) The node is an Entity.
PROCESSING_INSTRUCTION_NODE (7) The node is a ProcessingInstruction.
COMMENT_NODE (8) The node is a Comment.
DOCUMENT_NODE (9) The node is a Document.
DOCUMENT_TYPE_NODE (10) The node is a DocumentType.
DOCUMENT_FRAGMENT_NODE (11) The node is a DocumentFragment.
NOTATION_NODE (12) The node is a Notation.
ELEMENT_DECL_NODE (13) The node is an ElementDecl (not part of DOM)
ATT_DEF_NODE (14) The node is an AttDef (not part of DOM)
XML_DECL_NODE (15) The node is an XMLDecl (not part of DOM)
ATTLIST_DECL_NODE (16) The node is an AttlistDecl (not part of DOM)
Usage:
if ($node->getNodeType == ELEMENT_NODE)
{
print "It's an Element";
}
B<Not In DOM Spec>: The DOM Spec does not mention UNKNOWN_NODE and,
quite frankly, you should never encounter it. The last 4 node types were added
to support the 4 added node classes.
=head2 Global Variables
=over 4
=item $VERSION
The variable $XML::DOM::VERSION contains the version number of this
implementation, e.g. "1.07".
=head2 Additional methods not in the DOM Spec
=item getIgnoreReadOnly and ignoreReadOnly (readOnly)
The DOM Level 1 Spec does not allow you to edit certain sections of the document,
e.g. the DocumentType, so by default this implementation throws DOMExceptions
(i.e. NO_MODIFICATION_ALLOWED_ERR) when you try to edit a readonly node.
These readonly checks can be disabled by (temporarily) setting the global
IgnoreReadOnly flag.
The ignoreReadOnly method sets the global IgnoreReadOnly flag and returns its
previous value. The getIgnoreReadOnly method simply returns its current value.
my $oldIgnore = XML::DOM::ignoreReadOnly (1);
eval {
... do whatever you want, catching any other exceptions ...
};
XML::DOM::ignoreReadOnly ($oldIgnore); # restore previous value
=item isValidName (name)
Whether the specified name is a valid "Name" as specified in the XML spec.
Characters with Unicode values > 127 are now also supported.
=item getAllowReservedNames and allowReservedNames (boolean)
The first method returns whether reserved names are allowed.
The second takes a boolean argument and sets whether reserved names are allowed.
The initial value is 1 (i.e. allow reserved names.)
The XML spec states that "Names" starting with (X|x)(M|m)(L|l)
are reserved for future use. (Amusingly enough, the XML version of the XML spec
(REC-xml-19980210.xml) breaks that very rule by defining an ENTITY with the name
'xmlpio'.)
A "Name" in this context means the Name token as found in the BNF rules in the
XML spec.
XML::DOM only checks for errors when you modify the DOM tree, not when the
DOM tree is built by the XML::DOM::Parser.
=item setTagCompression (funcref)
There are 3 possible styles for printing empty Element tags:
=over 4
=item Style 0
<empty/> or <empty attr="val"/>
XML::DOM uses this style by default for all Elements.
=item Style 1
<empty></empty> or <empty attr="val"></empty>
=item Style 2
<empty /> or <empty attr="val" />
This style is sometimes desired when using XHTML.
(Note the extra space before the slash "/")
See http://www.w3.org/TR/WD-html-in-xml Appendix C for more details.
=back
By default XML::DOM compresses all empty Element tags (style 0.)
You can control which style is used for a particular Element by calling
XML::DOM::setTagCompression with a reference to a function that takes
2 arguments. The first is the tag name of the Element, the second is the
XML::DOM::Element that is being printed.
The function should return 0, 1 or 2 to indicate which style should be used to
print the empty tag. E.g.
XML::DOM::setTagCompression (\&my_tag_compression);
sub my_tag_compression
{
my ($tag, $elem) = @_;
# Print empty br, hr and img tags like this: <br />
return 2 if $tag =~ /^(br|hr|img)$/;
# Print other empty tags like this: <empty></empty>
return 1;
}
=back
=item XML::DOM::Node
=head2 Global Variables
=over 4
=item @NodeNames
The variable @XML::DOM::Node::NodeNames maps the node type constants to strings.
It is used by XML::DOM::Node::getNodeTypeName.
=head2 Methods
=item getNodeType
Return an integer indicating the node type. See XML::DOM constants.
=item getNodeName
Return a property or a hardcoded string, depending on the node type.
Here are the corresponding functions or values:
Attr getName
AttDef getName
AttlistDecl getName
CDATASection "#cdata-section"
Comment "#comment"
Document "#document"
DocumentType getNodeName
DocumentFragment "#document-fragment"
Element getTagName
ElementDecl getName
EntityReference getEntityName
Entity getNotationName
Notation getName
ProcessingInstruction getTarget
Text "#text"
XMLDecl "#xml-declaration"
B<Not In DOM Spec>: AttDef, AttlistDecl, ElementDecl and XMLDecl were added for
completeness.
=item getNodeValue and setNodeValue (value)
Returns a string or undef, depending on the node type. This method is provided
for completeness. In other languages it saves the programmer an upcast.
The value is either available thru some other method defined in the subclass, or
else undef is returned. Here are the corresponding methods:
Attr::getValue, Text::getData, CDATASection::getData, Comment::getData,
ProcessingInstruction::getData.
=item getParentNode and setParentNode (parentNode)
The parent of this node. All nodes, except Document,
DocumentFragment, and Attr may have a parent. However, if a
node has just been created and not yet added to the tree, or
if it has been removed from the tree, this is undef.
=item getChildNodes
A NodeList that contains all children of this node. If there
are no children, this is a NodeList containing no nodes. The
content of the returned NodeList is "live" in the sense that,
for instance, changes to the children of the node object that
it was created from are immediately reflected in the nodes
returned by the NodeList accessors; it is not a static
snapshot of the content of the node. This is true for every
NodeList, including the ones returned by the
getElementsByTagName method.
NOTE: this implementation does not return a "live" NodeList for
getElementsByTagName. See L<CAVEATS>.
When this method is called in a list context, it returns a regular perl list
containing the child nodes. Note that this list is not "live". E.g.
@list = $node->getChildNodes; # returns a perl list
$nodelist = $node->getChildNodes; # returns a NodeList (object reference)
for my $kid ($node->getChildNodes) # iterate over the children of $node
=item getFirstChild
The first child of this node. If there is no such node, this returns undef.
=item getLastChild
The last child of this node. If there is no such node, this returns undef.
=item getPreviousSibling
The node immediately preceding this node. If there is no such
node, this returns undef.
=item getNextSibling
The node immediately following this node. If there is no such node, this returns
undef.
=item getAttributes
A NamedNodeMap containing the attributes (Attr nodes) of this node
(if it is an Element) or undef otherwise.
Note that adding/removing attributes from the returned object, also adds/removes
attributes from the Element node that the NamedNodeMap came from.
=item getOwnerDocument
The Document object associated with this node. This is also
the Document object used to create new nodes. When this node
is a Document this is undef.
=item insertBefore (newChild, refChild)
Inserts the node newChild before the existing child node
refChild. If refChild is undef, insert newChild at the end of
the list of children.
If newChild is a DocumentFragment object, all of its children
are inserted, in the same order, before refChild. If the
newChild is already in the tree, it is first removed.
Return Value: The node being inserted.
DOMExceptions:
=over 4
=item * HIERARCHY_REQUEST_ERR
Raised if this node is of a type that does not allow children of the type of
the newChild node, or if the node to insert is one of this node's ancestors.
=item * WRONG_DOCUMENT_ERR
Raised if newChild was created from a different document than the one that
created this node.
=item * NO_MODIFICATION_ALLOWED_ERR
Raised if this node is readonly.
=item * NOT_FOUND_ERR
Raised if refChild is not a child of this node.
=back
=item replaceChild (newChild, oldChild)
Replaces the child node oldChild with newChild in the list of
children, and returns the oldChild node. If the newChild is
already in the tree, it is first removed.
Return Value: The node replaced.
DOMExceptions:
=over 4
=item * HIERARCHY_REQUEST_ERR
Raised if this node is of a type that does not allow children of the type of
the newChild node, or it the node to put in is one of this node's ancestors.
=item * WRONG_DOCUMENT_ERR
Raised if newChild was created from a different document than the one that
created this node.
=item * NO_MODIFICATION_ALLOWED_ERR
Raised if this node is readonly.
=item * NOT_FOUND_ERR
Raised if oldChild is not a child of this node.
=back
=item removeChild (oldChild)
Removes the child node indicated by oldChild from the list of
children, and returns it.
Return Value: The node removed.
DOMExceptions:
=over 4
=item * NO_MODIFICATION_ALLOWED_ERR
Raised if this node is readonly.
=item * NOT_FOUND_ERR
Raised if oldChild is not a child of this node.
=back
=item appendChild (newChild)
Adds the node newChild to the end of the list of children of
this node. If the newChild is already in the tree, it is
first removed. If it is a DocumentFragment object, the entire contents of
the document fragment are moved into the child list of this node
Return Value: The node added.
DOMExceptions:
=over 4
=item * HIERARCHY_REQUEST_ERR
Raised if this node is of a type that does not allow children of the type of
the newChild node, or if the node to append is one of this node's ancestors.
=item * WRONG_DOCUMENT_ERR
Raised if newChild was created from a different document than the one that
created this node.
=item * NO_MODIFICATION_ALLOWED_ERR
Raised if this node is readonly.
=back
=item hasChildNodes
This is a convenience method to allow easy determination of
whether a node has any children.
Return Value: 1 if the node has any children, 0 otherwise.
=item cloneNode (deep)
Returns a duplicate of this node, i.e., serves as a generic
copy constructor for nodes. The duplicate node has no parent
(parentNode returns undef.).
Cloning an Element copies all attributes and their values,
including those generated by the XML processor to represent
defaulted attributes, but this method does not copy any text
it contains unless it is a deep clone, since the text is
contained in a child Text node. Cloning any other type of
node simply returns a copy of this node.
Parameters:
I<deep> If true, recursively clone the subtree under the specified node.
If false, clone only the node itself (and its attributes, if it is an Element).
Return Value: The duplicate node.
=item normalize
Puts all Text nodes in the full depth of the sub-tree
underneath this Element into a "normal" form where only
markup (e.g., tags, comments, processing instructions, CDATA
sections, and entity references) separates Text nodes, i.e.,
there are no adjacent Text nodes. This can be used to ensure
that the DOM view of a document is the same as if it were
saved and re-loaded, and is useful when operations (such as
XPointer lookups) that depend on a particular document tree
structure are to be used.
B<Not In DOM Spec>: In the DOM Spec this method is defined in the Element and
Document class interfaces only, but it doesn't hurt to have it here...
=item getElementsByTagName (name [, recurse])
Returns a NodeList of all descendant elements with a given
tag name, in the order in which they would be encountered in
a preorder traversal of the Element tree.
Parameters:
I<name> The name of the tag to match on. The special value "*" matches all tags.
I<recurse> Whether it should return only direct child nodes (0) or any descendant that matches the tag name (1). This argument is optional and defaults to 1. It is not part of the DOM spec.
Return Value: A list of matching Element nodes.
NOTE: this implementation does not return a "live" NodeList for
getElementsByTagName. See L<CAVEATS>.
When this method is called in a list context, it returns a regular perl list
containing the result nodes. E.g.
@list = $node->getElementsByTagName("tag"); # returns a perl list
$nodelist = $node->getElementsByTagName("tag"); # returns a NodeList (object ref.)
for my $elem ($node->getElementsByTagName("tag")) # iterate over the result nodes
=head2 Additional methods not in the DOM Spec
=item getNodeTypeName
Return the string describing the node type.
E.g. returns "ELEMENT_NODE" if getNodeType returns ELEMENT_NODE.
It uses @XML::DOM::Node::NodeNames.
=item toString
Returns the entire subtree as a string.
=item printToFile (filename)
Prints the entire subtree to the file with the specified filename.
Croaks: if the file could not be opened for writing.
=item printToFileHandle (handle)
Prints the entire subtree to the file handle.
E.g. to print to STDOUT:
$node->printToFileHandle (\*STDOUT);
=item print (obj)
Prints the entire subtree using the object's print method. E.g to print to a
FileHandle object:
$f = new FileHandle ("file.out", "w");
$node->print ($f);
=item getChildIndex (child)
Returns the index of the child node in the list returned by getChildNodes.
Return Value: the index or -1 if the node is not found.
=item getChildAtIndex (index)
Returns the child node at the specifed index or undef.
=item addText (text)
Appends the specified string to the last child if it is a Text node, or else
appends a new Text node (with the specified text.)
Return Value: the last child if it was a Text node or else the new Text node.
=item dispose
Removes all circular references in this node and its descendants so the
objects can be claimed for garbage collection. The objects should not be used
afterwards.
=item setOwnerDocument (doc)
Sets the ownerDocument property of this node and all its children (and
attributes etc.) to the specified document.
This allows the user to cut and paste document subtrees between different
XML::DOM::Documents. The node should be removed from the original document
first, before calling setOwnerDocument.
This method does nothing when called on a Document node.
=item isAncestor (parent)
Returns 1 if parent is an ancestor of this node or if it is this node itself.
=item expandEntityRefs (str)
Expands all the entity references in the string and returns the result.
The entity references can be character references (e.g. "{" or "ῂ"),
default entity references (""", ">", "<", "'" and "&") or
entity references defined in Entity objects as part of the DocumentType of
the owning Document. Character references are expanded into UTF-8.
Parameter entity references (e.g. %ent;) are not expanded.
=back
=item Interface XML::DOM::NodeList
The NodeList interface provides the abstraction of an ordered
collection of nodes, without defining or constraining how this
collection is implemented.
The items in the NodeList are accessible via an integral index,
starting from 0.
Although the DOM spec states that all NodeLists are "live" in that they
allways reflect changes to the DOM tree, the NodeList returned by
getElementsByTagName is not live in this implementation. See L<CAVEATS>
for details.
=over 4
=item item (index)
Returns the indexth item in the collection. If index is
greater than or equal to the number of nodes in the list,
this returns undef.
=item getLength
The number of nodes in the list. The range of valid child
node indices is 0 to length-1 inclusive.
=head2 Additional methods not in the DOM Spec
=item dispose
Removes all circular references in this NodeList and its descendants so the
objects can be claimed for garbage collection. The objects should not be used
afterwards.
=back
=item Interface XML::DOM::NamedNodeMap
Objects implementing the NamedNodeMap interface are used to represent
collections of nodes that can be accessed by name. Note that
NamedNodeMap does not inherit from NodeList; NamedNodeMaps are not
maintained in any particular order. Objects contained in an object
implementing NamedNodeMap may also be accessed by an ordinal index, but
this is simply to allow convenient enumeration of the contents of a
NamedNodeMap, and does not imply that the DOM specifies an order to
these Nodes.
Note that in this implementation, the objects added to a NamedNodeMap
are kept in order.
=over 4
=item getNamedItem (name)
Retrieves a node specified by name.
Return Value: A Node (of any type) with the specified name, or undef if
the specified name did not identify any node in the map.
=item setNamedItem (arg)
Adds a node using its nodeName attribute.
As the nodeName attribute is used to derive the name which
the node must be stored under, multiple nodes of certain
types (those that have a "special" string value) cannot be
stored as the names would clash. This is seen as preferable
to allowing nodes to be aliased.
Parameters:
I<arg> A node to store in a named node map.
The node will later be accessible using the value of the nodeName
attribute of the node. If a node with that name is
already present in the map, it is replaced by the new one.
Return Value: If the new Node replaces an existing node with the same
name the previously existing Node is returned, otherwise undef is returned.
DOMExceptions:
=over 4
=item * WRONG_DOCUMENT_ERR
Raised if arg was created from a different document than the one that
created the NamedNodeMap.
=item * NO_MODIFICATION_ALLOWED_ERR
Raised if this NamedNodeMap is readonly.
=item * INUSE_ATTRIBUTE_ERR
Raised if arg is an Attr that is already an attribute of another Element object.
The DOM user must explicitly clone Attr nodes to re-use them in other elements.
=back
=item removeNamedItem (name)
Removes a node specified by name. If the removed node is an
Attr with a default value it is immediately replaced.
Return Value: The node removed from the map or undef if no node with
such a name exists.
DOMException:
=over 4
=item * NOT_FOUND_ERR
Raised if there is no node named name in the map.
=back
=item item (index)
Returns the indexth item in the map. If index is greater than
or equal to the number of nodes in the map, this returns undef.
Return Value: The node at the indexth position in the NamedNodeMap, or
undef if that is not a valid index.
=item getLength
Returns the number of nodes in the map. The range of valid child node
indices is 0 to length-1 inclusive.
=head2 Additional methods not in the DOM Spec
=item getValues
Returns a NodeList with the nodes contained in the NamedNodeMap.
The NodeList is "live", in that it reflects changes made to the NamedNodeMap.
When this method is called in a list context, it returns a regular perl list
containing the values. Note that this list is not "live". E.g.
@list = $map->getValues; # returns a perl list
$nodelist = $map->getValues; # returns a NodeList (object ref.)
for my $val ($map->getValues) # iterate over the values
=item getChildIndex (node)
Returns the index of the node in the NodeList as returned by getValues, or -1
if the node is not in the NamedNodeMap.
=item dispose
Removes all circular references in this NamedNodeMap and its descendants so the
objects can be claimed for garbage collection. The objects should not be used
afterwards.
=back
=item Interface XML::DOM::CharacterData extends XML::DOM::Node
The CharacterData interface extends Node with a set of attributes and
methods for accessing character data in the DOM. For clarity this set
is defined here rather than on each object that uses these attributes
and methods. No DOM objects correspond directly to CharacterData,
though Text, Comment and CDATASection do inherit the interface from it.
All offsets in this interface start from 0.
=over 4
=item getData and setData (data)
The character data of the node that implements this
interface. The DOM implementation may not put arbitrary
limits on the amount of data that may be stored in a
CharacterData node. However, implementation limits may mean
that the entirety of a node's data may not fit into a single
DOMString. In such cases, the user may call substringData to
retrieve the data in appropriately sized pieces.
=item getLength
The number of characters that are available through data and
the substringData method below. This may have the value zero,
i.e., CharacterData nodes may be empty.
=item substringData (offset, count)
Extracts a range of data from the node.
Parameters:
I<offset> Start offset of substring to extract.
I<count> The number of characters to extract.
Return Value: The specified substring. If the sum of offset and count
exceeds the length, then all characters to the end of
the data are returned.
=item appendData (str)
Appends the string to the end of the character data of the
node. Upon success, data provides access to the concatenation
of data and the DOMString specified.
=item insertData (offset, arg)
Inserts a string at the specified character offset.
Parameters:
I<offset> The character offset at which to insert.
I<arg> The DOMString to insert.
=item deleteData (offset, count)
Removes a range of characters from the node.
Upon success, data and length reflect the change.
If the sum of offset and count exceeds length then all characters
from offset to the end of the data are deleted.
Parameters:
I<offset> The offset from which to remove characters.
I<count> The number of characters to delete.
=item replaceData (offset, count, arg)
Replaces the characters starting at the specified character
offset with the specified string.
Parameters:
I<offset> The offset from which to start replacing.
I<count> The number of characters to replace.
I<arg> The DOMString with which the range must be replaced.
If the sum of offset and count exceeds length, then all characters to the end of
the data are replaced (i.e., the effect is the same as a remove method call with
the same range, followed by an append method invocation).
=back
=item XML::DOM::Attr extends XML::DOM::Node
=over 4
The Attr nodes built by the XML::DOM::Parser always have one child node
which is a Text node containing the expanded string value (i.e. EntityReferences
are always expanded.) EntityReferences may be added when modifying or creating
a new Document.
The Attr interface represents an attribute in an Element object.
Typically the allowable values for the attribute are defined in a
document type definition.
Attr objects inherit the Node interface, but since they are not
actually child nodes of the element they describe, the DOM does not
consider them part of the document tree. Thus, the Node attributes
parentNode, previousSibling, and nextSibling have a undef value for Attr
objects. The DOM takes the view that attributes are properties of
elements rather than having a separate identity from the elements they
are associated with; this should make it more efficient to implement
such features as default attributes associated with all elements of a
given type. Furthermore, Attr nodes may not be immediate children of a
DocumentFragment. However, they can be associated with Element nodes
contained within a DocumentFragment. In short, users and implementors
of the DOM need to be aware that Attr nodes have some things in common
with other objects inheriting the Node interface, but they also are
quite distinct.
The attribute's effective value is determined as follows: if this
attribute has been explicitly assigned any value, that value is the
attribute's effective value; otherwise, if there is a declaration for
this attribute, and that declaration includes a default value, then
that default value is the attribute's effective value; otherwise, the
attribute does not exist on this element in the structure model until
it has been explicitly added. Note that the nodeValue attribute on the
Attr instance can also be used to retrieve the string version of the
attribute's value(s).
In XML, where the value of an attribute can contain entity references,
the child nodes of the Attr node provide a representation in which
entity references are not expanded. These child nodes may be either
Text or EntityReference nodes. Because the attribute type may be
unknown, there are no tokenized attribute values.
=item getValue
On retrieval, the value of the attribute is returned as a string.
Character and general entity references are replaced with their values.
=item setValue (str)
DOM Spec: On setting, this creates a Text node with the unparsed contents of the
string.
=item getName
Returns the name of this attribute.
=back
=item XML::DOM::Element extends XML::DOM::Node
By far the vast majority of objects (apart from text) that authors
encounter when traversing a document are Element nodes. Assume the
following XML document:
<elementExample id="demo">
<subelement1/>
<subelement2><subsubelement/></subelement2>
</elementExample>
When represented using DOM, the top node is an Element node for
"elementExample", which contains two child Element nodes, one for
"subelement1" and one for "subelement2". "subelement1" contains no
child nodes.
Elements may have attributes associated with them; since the Element
interface inherits from Node, the generic Node interface method
getAttributes may be used to retrieve the set of all attributes for an
element. There are methods on the Element interface to retrieve either
an Attr object by name or an attribute value by name. In XML, where an
attribute value may contain entity references, an Attr object should be
retrieved to examine the possibly fairly complex sub-tree representing
the attribute value. On the other hand, in HTML, where all attributes
have simple string values, methods to directly access an attribute
value can safely be used as a convenience.
=over 4
=item getTagName
The name of the element. For example, in:
<elementExample id="demo">
...
</elementExample>
tagName has the value "elementExample". Note that this is
case-preserving in XML, as are all of the operations of the
DOM.
=item getAttribute (name)
Retrieves an attribute value by name.
Return Value: The Attr value as a string, or the empty string if that
attribute does not have a specified or default value.
=item setAttribute (name, value)
Adds a new attribute. If an attribute with that name is
already present in the element, its value is changed to be
that of the value parameter. This value is a simple string,
it is not parsed as it is being set. So any markup (such as
syntax to be recognized as an entity reference) is treated as
literal text, and needs to be appropriately escaped by the
implementation when it is written out. In order to assign an
attribute value that contains entity references, the user
must create an Attr node plus any Text and EntityReference
nodes, build the appropriate subtree, and use
setAttributeNode to assign it as the value of an attribute.
DOMExceptions:
=over 4
=item * INVALID_CHARACTER_ERR
Raised if the specified name contains an invalid character.
=item * NO_MODIFICATION_ALLOWED_ERR
Raised if this node is readonly.
=back
=item removeAttribute (name)
Removes an attribute by name. If the removed attribute has a
default value it is immediately replaced.
DOMExceptions:
=over 4
=item * NO_MODIFICATION_ALLOWED_ERR
Raised if this node is readonly.
=back
=item getAttributeNode
Retrieves an Attr node by name.
Return Value: The Attr node with the specified attribute name or undef
if there is no such attribute.
=item setAttributeNode (attr)
Adds a new attribute. If an attribute with that name is
already present in the element, it is replaced by the new one.
Return Value: If the newAttr attribute replaces an existing attribute
with the same name, the previously existing Attr node is
returned, otherwise undef is returned.
DOMExceptions:
=over 4
=item * WRONG_DOCUMENT_ERR
Raised if newAttr was created from a different document than the one that created
the element.
=item * NO_MODIFICATION_ALLOWED_ERR
Raised if this node is readonly.
=item * INUSE_ATTRIBUTE_ERR
Raised if newAttr is already an attribute of another Element object. The DOM
user must explicitly clone Attr nodes to re-use them in other elements.
=back
=item removeAttributeNode (oldAttr)
Removes the specified attribute. If the removed Attr has a default value it is
immediately replaced. If the Attr already is the default value, nothing happens
and nothing is returned.
Parameters:
I<oldAttr> The Attr node to remove from the attribute list.
Return Value: The Attr node that was removed.
DOMExceptions:
=over 4
=item * NO_MODIFICATION_ALLOWED_ERR
Raised if this node is readonly.
=item * NOT_FOUND_ERR
Raised if oldAttr is not an attribute of the element.
=back
=head2 Additional methods not in the DOM Spec
=item setTagName (newTagName)
Sets the tag name of the Element. Note that this method is not portable
between DOM implementations.
DOMExceptions:
=over 4
=item * INVALID_CHARACTER_ERR
Raised if the specified name contains an invalid character.
=back
=back
=item XML::DOM::Text extends XML::DOM::CharacterData
The Text interface represents the textual content (termed character
data in XML) of an Element or Attr. If there is no markup inside an
element's content, the text is contained in a single object
implementing the Text interface that is the only child of the element.
If there is markup, it is parsed into a list of elements and Text nodes
that form the list of children of the element.
When a document is first made available via the DOM, there is only one
Text node for each block of text. Users may create adjacent Text nodes
that represent the contents of a given element without any intervening
markup, but should be aware that there is no way to represent the
separations between these nodes in XML or HTML, so they will not (in
general) persist between DOM editing sessions. The normalize() method
on Element merges any such adjacent Text objects into a single node for
each block of text; this is recommended before employing operations
that depend on a particular document structure, such as navigation with
XPointers.
B<Not Implemented>: The XML::DOM::Parser converts all CDATASections to regular
text, so as far as I know, there is know way to preserve them.
If you add CDATASection nodes to a Document yourself, they will be preserved.
=over 4
=item splitText (offset)
Breaks this Text node into two Text nodes at the specified
offset, keeping both in the tree as siblings. This node then
only contains all the content up to the offset point. And a
new Text node, which is inserted as the next sibling of this
node, contains all the content at and after the offset point.
Parameters:
I<offset> The offset at which to split, starting from 0.
Return Value: The new Text node.
DOMExceptions:
=over 4
=item * INDEX_SIZE_ERR
Raised if the specified offset is negative or greater than the number of
characters in data.
=item * NO_MODIFICATION_ALLOWED_ERR
Raised if this node is readonly.
=back
=back
=item XML::DOM::Comment extends XML::DOM::CharacterData
This represents the content of a comment, i.e., all the characters
between the starting '<!--' and ending '-->'. Note that this is the
definition of a comment in XML, and, in practice, HTML, although some
HTML tools may implement the full SGML comment structure.
=item XML::DOM::CDATASection extends XML::DOM::CharacterData
CDATA sections are used to escape blocks of text containing characters
that would otherwise be regarded as markup. The only delimiter that is
recognized in a CDATA section is the "]]>" string that ends the CDATA
section. CDATA sections can not be nested. The primary purpose is for
including material such as XML fragments, without needing to escape all
the delimiters.
The DOMString attribute of the Text node holds the text that is
contained by the CDATA section. Note that this may contain characters
that need to be escaped outside of CDATA sections and that, depending
on the character encoding ("charset") chosen for serialization, it may
be impossible to write out some characters as part of a CDATA section.
The CDATASection interface inherits the CharacterData interface through
the Text interface. Adjacent CDATASections nodes are not merged by use
of the Element.normalize() method.
B<Not Implemented>: see Text node comments about CDATASections being converted
to Text nodes when parsing XML input.
=item XML::DOM::ProcessingInstruction extends XML::DOM::Node
The ProcessingInstruction interface represents a "processing
instruction", used in XML as a way to keep processor-specific
information in the text of the document. An example:
<?PI processing instruction?>
Here, "PI" is the target and "processing instruction" is the data.
=over 4
=item getTarget
The target of this processing instruction. XML defines this
as being the first token following the markup that begins the
processing instruction.
=item getData and setData (data)
The content of this processing instruction. This is from the
first non white space character after the target to the
character immediately preceding the ?>.
=back
=item XML::DOM::Notation extends XML::DOM::Node
This node represents a Notation, e.g.
<!NOTATION gs SYSTEM "GhostScript">
<!NOTATION name PUBLIC "pubId">
<!NOTATION name PUBLIC "pubId" "sysId">
<!NOTATION name SYSTEM "sysId">
=over 4
=item getName and setName (name)
Returns (or sets) the Notation name, which is the first token after the
NOTATION keyword.
=item getSysId and setSysId (sysId)
Returns (or sets) the system ID, which is the token after the optional
SYSTEM keyword.
=item getPubId and setPubId (pubId)
Returns (or sets) the public ID, which is the token after the optional
PUBLIC keyword.
=item getBase
This is passed by XML::Parser in the Notation handler.
I don't know what it is yet.
=item getNodeName
Returns the same as getName.
=back
=item XML::DOM::Entity extends XML::DOM::Node
This node represents an Entity declaration, e.g.
<!ENTITY % draft 'INCLUDE'>
<!ENTITY hatch-pic SYSTEM "../grafix/OpenHatch.gif" NDATA gif>
The first one is called a parameter entity and is referenced like this: %draft;
The 2nd is a (regular) entity and is referenced like this: &hatch-pic;
=over 4
=item getNotationName
Returns the name of the notation for the entity.
I<Not Implemented> The DOM Spec says: For unparsed entities, the name of the
notation for the entity. For parsed entities, this is null.
(This implementation does not support unparsed entities.)
=item getSysId
Returns the system id, or undef.
=item getPubId
Returns the public id, or undef.
=head2 Additional methods not in the DOM Spec
=item isParameterEntity
Whether it is a parameter entity (%ent;) or not (&ent;)
=item getValue
Returns the entity value.
=item getNdata
Returns the NDATA declaration (for general unparsed entities), or undef.
=back
=item XML::DOM::DocumentType extends XML::DOM::Node
Each Document has a doctype attribute whose value is either null or a
DocumentType object. The DocumentType interface in the DOM Level 1 Core
provides an interface to the list of entities that are defined for the
document, and little else because the effect of namespaces and the
various XML scheme efforts on DTD representation are not clearly
understood as of this writing.
The DOM Level 1 doesn't support editing DocumentType nodes.
B<Not In DOM Spec>: This implementation has added a lot of extra
functionality to the DOM Level 1 interface.
To allow editing of the DocumentType nodes, see XML::DOM::ignoreReadOnly.
=over 4
=item getName
Returns the name of the DTD, i.e. the name immediately following the
DOCTYPE keyword.
=item getEntities
A NamedNodeMap containing the general entities, both external
and internal, declared in the DTD. Duplicates are discarded.
For example in:
<!DOCTYPE ex SYSTEM "ex.dtd" [
<!ENTITY foo "foo">
<!ENTITY bar "bar">
<!ENTITY % baz "baz">
]>
<ex/>
the interface provides access to foo and bar but not baz.
Every node in this map also implements the Entity interface.
The DOM Level 1 does not support editing entities, therefore
entities cannot be altered in any way.
B<Not In DOM Spec>: See XML::DOM::ignoreReadOnly to edit the DocumentType etc.
=item getNotations
A NamedNodeMap containing the notations declared in the DTD.
Duplicates are discarded. Every node in this map also
implements the Notation interface.
The DOM Level 1 does not support editing notations, therefore
notations cannot be altered in any way.
B<Not In DOM Spec>: See XML::DOM::ignoreReadOnly to edit the DocumentType etc.
=head2 Additional methods not in the DOM Spec
=item Creating and setting the DocumentType
A new DocumentType can be created with:
$doctype = $doc->createDocumentType ($name, $sysId, $pubId, $internal);
To set (or replace) the DocumentType for a particular document, use:
$doc->setDocType ($doctype);
=item getSysId and setSysId (sysId)
Returns or sets the system id.
=item getPubId and setPubId (pudId)
Returns or sets the public id.
=item setName (name)
Sets the name of the DTD, i.e. the name immediately following the
DOCTYPE keyword. Note that this should always be the same as the element
tag name of the root element.
=item getAttlistDecl (elemName)
Returns the AttlistDecl for the Element with the specified name, or undef.
=item getElementDecl (elemName)
Returns the ElementDecl for the Element with the specified name, or undef.
=item getEntity (entityName)
Returns the Entity with the specified name, or undef.
=item addAttlistDecl (elemName)
Adds a new AttDecl node with the specified elemName if one doesn't exist yet.
Returns the AttlistDecl (new or existing) node.
=item addElementDecl (elemName, model)
Adds a new ElementDecl node with the specified elemName and model if one doesn't
exist yet.
Returns the AttlistDecl (new or existing) node. The model is ignored if one
already existed.
=item addEntity (parameter, notationName, value, sysId, pubId, ndata)
Adds a new Entity node. Don't use createEntity and appendChild, because it should
be added to the internal NamedNodeMap containing the entities.
Parameters:
I<parameter> whether it is a parameter entity (%ent;) or not (&ent;).
I<notationName> the entity name.
I<value> the entity value.
I<sysId> the system id (if any.)
I<pubId> the public id (if any.)
I<ndata> the NDATA declaration (if any, for general unparsed entities.)
SysId, pubId and ndata may be undefined.
DOMExceptions:
=over 4
=item * INVALID_CHARACTER_ERR
Raised if the notationName does not conform to the XML spec.
=back
=item addNotation (name, base, sysId, pubId)
Adds a new Notation object.
Parameters:
I<name> the notation name.
I<base> the base to be used for resolving a relative URI.
I<sysId> the system id.
I<pubId> the public id.
Base, sysId, and pubId may all be undefined.
(These parameters are passed by the XML::Parser Notation handler.)
DOMExceptions:
=over 4
=item * INVALID_CHARACTER_ERR
Raised if the notationName does not conform to the XML spec.
=back
=item addAttDef (elemName, attrName, type, default, fixed)
Adds a new attribute definition. It will add the AttDef node to the AttlistDecl
if it exists. If an AttDef with the specified attrName already exists for the
given elemName, this function only generates a warning.
See XML::DOM::AttDef::new for the other parameters.
=item getDefaultAttrValue (elem, attr)
Returns the default attribute value as a string or undef, if none is available.
Parameters:
I<elem> The element tagName.
I<attr> The attribute name.
=item expandEntity (entity [, parameter])
Expands the specified entity or parameter entity (if parameter=1) and returns
its value as a string, or undef if the entity does not exist.
(The entity name should not contain the '%', '&' or ';' delimiters.)
=back
=item XML::DOM::DocumentFragment extends XML::DOM::Node
DocumentFragment is a "lightweight" or "minimal" Document object. It is
very common to want to be able to extract a portion of a document's
tree or to create a new fragment of a document. Imagine implementing a
user command like cut or rearranging a document by moving fragments
around. It is desirable to have an object which can hold such fragments
and it is quite natural to use a Node for this purpose. While it is
true that a Document object could fulfil this role, a Document object
can potentially be a heavyweight object, depending on the underlying
implementation. What is really needed for this is a very lightweight
object. DocumentFragment is such an object.
Furthermore, various operations -- such as inserting nodes as children
of another Node -- may take DocumentFragment objects as arguments; this
results in all the child nodes of the DocumentFragment being moved to
the child list of this node.
The children of a DocumentFragment node are zero or more nodes
representing the tops of any sub-trees defining the structure of the
document. DocumentFragment nodes do not need to be well-formed XML
documents (although they do need to follow the rules imposed upon
well-formed XML parsed entities, which can have multiple top nodes).
For example, a DocumentFragment might have only one child and that
child node could be a Text node. Such a structure model represents
neither an HTML document nor a well-formed XML document.
When a DocumentFragment is inserted into a Document (or indeed any
other Node that may take children) the children of the DocumentFragment
and not the DocumentFragment itself are inserted into the Node. This
makes the DocumentFragment very useful when the user wishes to create
nodes that are siblings; the DocumentFragment acts as the parent of
these nodes so that the user can use the standard methods from the Node
interface, such as insertBefore() and appendChild().
=item XML::DOM::DOMImplementation
The DOMImplementation interface provides a number of methods for
performing operations that are independent of any particular instance
of the document object model.
The DOM Level 1 does not specify a way of creating a document instance,
and hence document creation is an operation specific to an
implementation. Future Levels of the DOM specification are expected to
provide methods for creating documents directly.
=over 4
=item hasFeature (feature, version)
Returns 1 if and only if feature equals "XML" and version equals "1.0".
=back
=item XML::DOM::Document extends XML::DOM::Node
This is the main root of the document structure as returned by
XML::DOM::Parser::parse and XML::DOM::Parser::parsefile.
Since elements, text nodes, comments, processing instructions, etc.
cannot exist outside the context of a Document, the Document interface
also contains the factory methods needed to create these objects. The
Node objects created have a getOwnerDocument method which associates
them with the Document within whose context they were created.
=over 4
=item getDocumentElement
This is a convenience method that allows direct access to
the child node that is the root Element of the document.
=item getDoctype
The Document Type Declaration (see DocumentType) associated
with this document. For HTML documents as well as XML
documents without a document type declaration this returns
undef. The DOM Level 1 does not support editing the Document
Type Declaration.
B<Not In DOM Spec>: This implementation allows editing the doctype.
See I<XML::DOM::ignoreReadOnly> for details.
=item getImplementation
The DOMImplementation object that handles this document. A
DOM application may use objects from multiple implementations.
=item createElement (tagName)
Creates an element of the type specified. Note that the
instance returned implements the Element interface, so
attributes can be specified directly on the returned object.
DOMExceptions:
=over 4
=item * INVALID_CHARACTER_ERR
Raised if the tagName does not conform to the XML spec.
=back
=item createTextNode (data)
Creates a Text node given the specified string.
=item createComment (data)
Creates a Comment node given the specified string.
=item createCDATASection (data)
Creates a CDATASection node given the specified string.
=item createAttribute (name [, value [, specified ]])
Creates an Attr of the given name. Note that the Attr
instance can then be set on an Element using the setAttribute method.
B<Not In DOM Spec>: The DOM Spec does not allow passing the value or the
specified property in this method. In this implementation they are optional.
Parameters:
I<value> The attribute's value. See Attr::setValue for details.
If the value is not supplied, the specified property is set to 0.
I<specified> Whether the attribute value was specified or whether the default
value was used. If not supplied, it's assumed to be 1.
DOMExceptions:
=over 4
=item * INVALID_CHARACTER_ERR
Raised if the name does not conform to the XML spec.
=back
=item createProcessingInstruction (target, data)
Creates a ProcessingInstruction node given the specified name and data strings.
Parameters:
I<target> The target part of the processing instruction.
I<data> The data for the node.
DOMExceptions:
=over 4
=item * INVALID_CHARACTER_ERR
Raised if the target does not conform to the XML spec.
=back
=item createDocumentFragment
Creates an empty DocumentFragment object.
=item createEntityReference (name)
Creates an EntityReference object.
=head2 Additional methods not in the DOM Spec
=item getXMLDecl and setXMLDecl (xmlDecl)
Returns the XMLDecl for this Document or undef if none was specified.
Note that XMLDecl is not part of the list of child nodes.
=item setDoctype (doctype)
Sets or replaces the DocumentType.
B<NOTE>: Don't use appendChild or insertBefore to set the DocumentType.
Even though doctype will be part of the list of child nodes, it is handled
specially.
=item getDefaultAttrValue (elem, attr)
Returns the default attribute value as a string or undef, if none is available.
Parameters:
I<elem> The element tagName.
I<attr> The attribute name.
=item getEntity (name)
Returns the Entity with the specified name.
=item createXMLDecl (version, encoding, standalone)
Creates an XMLDecl object. All parameters may be undefined.
=item createDocumentType (name, sysId, pubId)
Creates a DocumentType object. SysId and pubId may be undefined.
=item createNotation (name, base, sysId, pubId)
Creates a new Notation object. Consider using
XML::DOM::DocumentType::addNotation!
=item createEntity (parameter, notationName, value, sysId, pubId, ndata)
Creates an Entity object. Consider using XML::DOM::DocumentType::addEntity!
=item createElementDecl (name, model)
Creates an ElementDecl object.
DOMExceptions:
=over 4
=item * INVALID_CHARACTER_ERR
Raised if the element name (tagName) does not conform to the XML spec.
=back
=item createAttlistDecl (name)
Creates an AttlistDecl object.
DOMExceptions:
=over 4
=item * INVALID_CHARACTER_ERR
Raised if the element name (tagName) does not conform to the XML spec.
=back
=item expandEntity (entity [, parameter])
Expands the specified entity or parameter entity (if parameter=1) and returns
its value as a string, or undef if the entity does not exist.
(The entity name should not contain the '%', '&' or ';' delimiters.)
=back
=head1 EXTRA NODE TYPES
=item XML::DOM::XMLDecl extends XML::DOM::Node
This node contains the XML declaration, e.g.
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
See also XML::DOM::Document::getXMLDecl.
=over 4
=item getVersion and setVersion (version)
Returns and sets the XML version. At the time of this writing the version should
always be "1.0"
=item getEncoding and setEncoding (encoding)
undef may be specified for the encoding value.
=item getStandalone and setStandalone (standalone)
undef may be specified for the standalone value.
=back
=item XML::DOM::ElementDecl extends XML::DOM::Node
This node represents an Element declaration, e.g.
<!ELEMENT address (street+, city, state, zip, country?)>
=over 4
=item getName
Returns the Element tagName.
=item getModel and setModel (model)
Returns and sets the model as a string, e.g.
"(street+, city, state, zip, country?)" in the above example.
=back
=item XML::DOM::AttlistDecl extends XML::DOM::Node
This node represents an ATTLIST declaration, e.g.
<!ATTLIST person
sex (male|female) #REQUIRED
hair CDATA "bold"
eyes (none|one|two) "two"
species (human) #FIXED "human">
Each attribute definition is stored a separate AttDef node. The AttDef nodes can
be retrieved with getAttDef and added with addAttDef.
(The AttDef nodes are stored in a NamedNodeMap internally.)
=over 4
=item getName
Returns the Element tagName.
=item getAttDef (attrName)
Returns the AttDef node for the attribute with the specified name.
=item addAttDef (attrName, type, default, [ fixed ])
Adds a AttDef node for the attribute with the specified name.
Parameters:
I<attrName> the attribute name.
I<type> the attribute type (e.g. "CDATA" or "(male|female)".)
I<default> the default value enclosed in quotes (!), the string #IMPLIED or
the string #REQUIRED.
I<fixed> whether the attribute is '#FIXED' (default is 0.)
=back
=item XML::DOM::AttDef extends XML::DOM::Node
Each object of this class represents one attribute definition in an AttlistDecl.
=over 4
=item getName
Returns the attribute name.
=item getDefault
Returns the default value, or undef.
=item isFixed
Whether the attribute value is fixed (see #FIXED keyword.)
=item isRequired
Whether the attribute value is required (see #REQUIRED keyword.)
=item isImplied
Whether the attribute value is implied (see #IMPLIED keyword.)
=back
=head1 IMPLEMENTATION DETAILS
=over 4
=item * Perl Mappings
The value undef was used when the DOM Spec said null.
The DOM Spec says: Applications must encode DOMString using UTF-16 (defined in
Appendix C.3 of [UNICODE] and Amendment 1 of [ISO-10646]).
In this implementation we use plain old Perl strings encoded in UTF-8 instead of
UTF-16.
=item * Text and CDATASection nodes
The Expat parser expands EntityReferences and CDataSection sections to
raw strings and does not indicate where it was found.
This implementation does therefore convert both to Text nodes at parse time.
CDATASection and EntityReference nodes that are added to an existing Document
(by the user) will be preserved.
Also, subsequent Text nodes are always merged at parse time. Text nodes that are
added later can be merged with the normalize method. Consider using the addText
method when adding Text nodes.
=item * Printing and toString
When printing (and converting an XML Document to a string) the strings have to
encoded differently depending on where they occur. E.g. in a CDATASection all
substrings are allowed except for "]]>". In regular text, certain characters are
not allowed, e.g. ">" has to be converted to ">".
These routines should be verified by someone who knows the details.
=item * Quotes
Certain sections in XML are quoted, like attribute values in an Element.
XML::Parser strips these quotes and the print methods in this implementation
always uses double quotes, so when parsing and printing a document, single quotes
may be converted to double quotes. The default value of an attribute definition
(AttDef) in an AttlistDecl, however, will maintain its quotes.
=item * AttlistDecl
Attribute declarations for a certain Element are always merged into a single
AttlistDecl object.
=item * Comments
Comments in the DOCTYPE section are not kept in the right place. They will become
child nodes of the Document.
=back
=head1 SEE ALSO
The Japanese version of this document by Takanori Kawai (Hippo2000)
at http://member.nifty.ne.jp/hippo2000/perltips/xml/dom.htm
The DOM Level 1 specification at http://www.w3.org/TR/REC-DOM-Level-1
The XML spec (Extensible Markup Language 1.0) at http://www.w3.org/TR/REC-xml
The XML::Parser and XML::Parser::Expat manual pages.
=head1 CAVEATS
The method getElementsByTagName() does not return a "live" NodeList.
Whether this is an actual caveat is debatable, but a few people on the
www-dom mailing list seemed to think so. I haven't decided yet. It's a pain
to implement, it slows things down and the benefits seem marginal.
Let me know what you think.
(To subscribe to the www-dom mailing list send an email with the subject
"subscribe" to www-dom-request@w3.org. I only look here occasionally, so don't
send bug reports or suggestions about XML::DOM to this list, send them
to enno@att.com instead.)
=head1 AUTHORS
Enno Derksen <F<enno@att.com>> and Clark Cooper <F<coopercl@sch.ge.com>>.
Please send bugs, comments and suggestions to Enno.
=cut
|