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
|
//------------------------------------------------------------------------------
// <copyright file="Infer.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="false" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------
using System;
using System.IO;
using System.Xml;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.Security.Permissions;
namespace System.Xml.Schema
{
/// <include file='doc\Infer.uex' path='docs/doc[@for="Infer"]/*' />
/// <summary>
/// Infer class serves for infering XML Schema from given XML instance document.
/// </summary>
public sealed class XmlSchemaInference
{
internal static XmlQualifiedName ST_boolean = new XmlQualifiedName("boolean", XmlSchema.Namespace);
internal static XmlQualifiedName ST_byte = new XmlQualifiedName("byte", XmlSchema.Namespace);
internal static XmlQualifiedName ST_unsignedByte = new XmlQualifiedName("unsignedByte", XmlSchema.Namespace);
internal static XmlQualifiedName ST_short = new XmlQualifiedName("short", XmlSchema.Namespace);
internal static XmlQualifiedName ST_unsignedShort = new XmlQualifiedName("unsignedShort", XmlSchema.Namespace);
internal static XmlQualifiedName ST_int = new XmlQualifiedName("int", XmlSchema.Namespace);
internal static XmlQualifiedName ST_unsignedInt = new XmlQualifiedName("unsignedInt", XmlSchema.Namespace);
internal static XmlQualifiedName ST_long = new XmlQualifiedName("long", XmlSchema.Namespace);
internal static XmlQualifiedName ST_unsignedLong = new XmlQualifiedName("unsignedLong", XmlSchema.Namespace);
internal static XmlQualifiedName ST_integer = new XmlQualifiedName("integer", XmlSchema.Namespace);
internal static XmlQualifiedName ST_decimal = new XmlQualifiedName("decimal", XmlSchema.Namespace);
internal static XmlQualifiedName ST_float = new XmlQualifiedName("float", XmlSchema.Namespace);
internal static XmlQualifiedName ST_double = new XmlQualifiedName("double", XmlSchema.Namespace);
internal static XmlQualifiedName ST_duration = new XmlQualifiedName("duration", XmlSchema.Namespace);
internal static XmlQualifiedName ST_dateTime = new XmlQualifiedName("dateTime", XmlSchema.Namespace);
internal static XmlQualifiedName ST_time = new XmlQualifiedName("time", XmlSchema.Namespace);
internal static XmlQualifiedName ST_date = new XmlQualifiedName("date", XmlSchema.Namespace);
internal static XmlQualifiedName ST_gYearMonth = new XmlQualifiedName("gYearMonth", XmlSchema.Namespace);
internal static XmlQualifiedName ST_string = new XmlQualifiedName("string", XmlSchema.Namespace);
internal static XmlQualifiedName ST_anySimpleType = new XmlQualifiedName("anySimpleType", XmlSchema.Namespace);
internal static XmlQualifiedName[] SimpleTypes =
{
ST_boolean,
ST_byte,
ST_unsignedByte,
ST_short,
ST_unsignedShort,
ST_int,
ST_unsignedInt,
ST_long,
ST_unsignedLong,
ST_integer,
ST_decimal,
ST_float,
ST_double,
ST_duration,
ST_dateTime,
ST_time,
ST_date,
ST_gYearMonth,
ST_string
};
internal const short HC_ST_boolean = 0;
internal const short HC_ST_byte = 1;
internal const short HC_ST_unsignedByte = 2;
internal const short HC_ST_short = 3;
internal const short HC_ST_unsignedShort = 4;
internal const short HC_ST_int = 5;
internal const short HC_ST_unsignedInt = 6;
internal const short HC_ST_long = 7;
internal const short HC_ST_unsignedLong = 8;
internal const short HC_ST_integer = 9;
internal const short HC_ST_decimal = 10;
internal const short HC_ST_float = 11;
internal const short HC_ST_double = 12;
internal const short HC_ST_duration = 13;
internal const short HC_ST_dateTime = 14;
internal const short HC_ST_time = 15;
internal const short HC_ST_date = 16;
internal const short HC_ST_gYearMonth = 17;
internal const short HC_ST_string = 18;
internal const short HC_ST_Count = HC_ST_string +1;
internal const int TF_boolean = 1<<HC_ST_boolean;
internal const int TF_byte = 1<<HC_ST_byte;
internal const int TF_unsignedByte = 1<<HC_ST_unsignedByte;
internal const int TF_short = 1<<HC_ST_short;
internal const int TF_unsignedShort = 1<<HC_ST_unsignedShort;
internal const int TF_int = 1<<HC_ST_int;
internal const int TF_unsignedInt = 1<<HC_ST_unsignedInt;
internal const int TF_long = 1<<HC_ST_long;
internal const int TF_unsignedLong = 1<<HC_ST_unsignedLong;
internal const int TF_integer = 1<<HC_ST_integer;
internal const int TF_decimal = 1<<HC_ST_decimal;
internal const int TF_float = 1<<HC_ST_float;
internal const int TF_double = 1<<HC_ST_double;
internal const int TF_duration = 1<<HC_ST_duration;
internal const int TF_dateTime = 1<<HC_ST_dateTime;
internal const int TF_time = 1<<HC_ST_time;
internal const int TF_date = 1<<HC_ST_date;
internal const int TF_gYearMonth = 1<<HC_ST_gYearMonth;
internal const int TF_string = 1<<HC_ST_string;
XmlSchema rootSchema = null; //(XmlSchema) xsc[TargetNamespace];
private XmlSchemaSet schemaSet;
private XmlReader xtr;
private NameTable nametable;
private string TargetNamespace;
private XmlNamespaceManager NamespaceManager;
//private Hashtable schemas; //contains collection of schemas before they get added to the XmlSchemaSet xsc
//private bool bRefine = false; //indicates if we are going to infer or refine schema when InferSchema is called
ArrayList schemaList;
InferenceOption occurrence = InferenceOption.Restricted;
InferenceOption typeInference = InferenceOption.Restricted;
/* internal struct ReplaceList
{
internal XmlSchemaObjectCollection col;
internal int position;
internal ReplaceList(XmlSchemaObjectCollection col, int position)
{
this.col = col;
this.position = position;
}
}*/
/// <include file='doc\Infer.uex' path='docs/doc[@for="InferenceOption"]/*' />
public enum InferenceOption
{
/// <include file='doc\Infer.uex' path='docs/doc[@for="InferenceOption.Restricted"]/*' />
Restricted,
/// <include file='doc\Infer.uex' path='docs/doc[@for="InferenceOption.Relaxed"]/*' />
Relaxed
};
/// <include file='doc\Infer.uex' path='docs/doc[@for="Infer.Occurrence"]/*' />
public InferenceOption Occurrence
{
set
{
this.occurrence = value;
}
get
{
return this.occurrence;
}
}
/// <include file='doc\Infer.uex' path='docs/doc[@for="Infer.TypeInference"]/*' />
public InferenceOption TypeInference
{
set
{
this.typeInference = value;
}
get
{
return this.typeInference;
}
}
/// <include file='doc\Infer.uex' path='docs/doc[@for="Infer.Infer"]/*' />
public XmlSchemaInference()
{
this.nametable = new NameTable();
this.NamespaceManager = new XmlNamespaceManager(nametable);
this.NamespaceManager.AddNamespace("xs", XmlSchema.Namespace);
this.schemaList = new ArrayList();
}
/// <include file='doc\Infer.uex' path='docs/doc[@for="Infer.InferSchema"]/*' />
public XmlSchemaSet InferSchema(XmlReader instanceDocument)
{
return InferSchema1(instanceDocument, new XmlSchemaSet(nametable));
}
/// <include file='doc\Infer.uex' path='docs/doc[@for="Infer.InferSchema1"]/*' />
public XmlSchemaSet InferSchema(XmlReader instanceDocument, XmlSchemaSet schemas)
{
if (schemas == null)
{
schemas = new XmlSchemaSet(nametable);
}
return InferSchema1(instanceDocument, schemas);
}
internal XmlSchemaSet InferSchema1(XmlReader instanceDocument, XmlSchemaSet schemas)
{
if (instanceDocument == null)
{
throw new ArgumentNullException("instanceDocument");
}
this.rootSchema = null;
xtr = instanceDocument;
schemas.Compile();
this.schemaSet = schemas;
//schemas = new Hashtable();
//while(xtr.Read())
while (xtr.NodeType != XmlNodeType.Element && xtr.Read()) ;
if (xtr.NodeType == XmlNodeType.Element)
{
//Create and process the root element
TargetNamespace = xtr.NamespaceURI;
if ( xtr.NamespaceURI == XmlSchema.Namespace)
{
throw new XmlSchemaInferenceException(Res.SchInf_schema, 0, 0);
}
XmlSchemaElement xse = null;
foreach (XmlSchemaElement elem in schemas.GlobalElements.Values)
{
if (elem.Name == xtr.LocalName && elem.QualifiedName.Namespace == xtr.NamespaceURI)
{
rootSchema = elem.Parent as XmlSchema;
xse = elem;
break;
}
}
if (rootSchema == null)
{
//rootSchema = CreateXmlSchema(xtr.NamespaceURI);
xse = AddElement(xtr.LocalName, xtr.Prefix, xtr.NamespaceURI, null, null, -1);
}
else
{
//bRefine = true;
InferElement(xse, false, rootSchema);
}
/* foreach (ReplaceList listItem in schemaList)
{
if (listItem.position < listItem.col.Count)
{
XmlSchemaElement particle = listItem.col[listItem.position] as XmlSchemaElement;
if (particle != null && (particle.RefName.Namespace == XmlSchema.Namespace))
{
XmlSchemaAny any = new XmlSchemaAny();
if (particle.MaxOccurs != 1)
{
any.MaxOccurs = particle.MaxOccurs;
}
if (particle.MinOccurs != 1)
{
any.MinOccurs = particle.MinOccurs;
}
any.ProcessContents = XmlSchemaContentProcessing.Skip;
any.MinOccurs = decimal.Zero;
any.Namespace = particle.RefName.Namespace;
listItem.col[listItem.position] = any;
}
}
}*/
foreach(String prefix in this.NamespaceManager)
{
if (!prefix.Equals("xml") && !prefix.Equals("xmlns"))
{
String ns = this.NamespaceManager.LookupNamespace(this.nametable.Get(prefix));
if (ns.Length != 0) { //Do not add xmlns=""
rootSchema.Namespaces.Add(prefix, ns);
}
}
}
Debug.Assert(this.rootSchema != null, "rootSchema is null");
schemas.Reprocess(rootSchema);
schemas.Compile();
//break;
}
else
{
throw new XmlSchemaInferenceException(Res.SchInf_NoElement, 0, 0);
}
return schemas;
}
private XmlSchemaAttribute AddAttribute(string localName, string prefix, string childURI, string attrValue, bool bCreatingNewType, XmlSchema parentSchema, XmlSchemaObjectCollection addLocation, XmlSchemaObjectTable compiledAttributes)
{
if (childURI == XmlSchema.Namespace)
{
throw new XmlSchemaInferenceException(Res.SchInf_schema, 0, 0);
}
XmlSchemaAttribute xsa = null;
int AttributeType = -1;
XmlSchemaAttribute returnedAttribute = null; //this value will change to attributeReference if childURI!= parentURI
XmlSchema xs = null;
bool add = true;
Debug.Assert(compiledAttributes != null); //AttributeUses is never null
// First we need to look into the already compiled attributes
// (they come from the schemaset which we got on input)
// If there are none or we don't find it there, then we must search the list of attributes
// where we are going to add a new one (if it doesn't exist).
// This is necessary to avoid adding duplicate attribute declarations.
ICollection searchCollectionPrimary, searchCollectionSecondary;
if (compiledAttributes.Count > 0) {
searchCollectionPrimary = compiledAttributes.Values;
searchCollectionSecondary = addLocation;
}
else {
searchCollectionPrimary = addLocation;
searchCollectionSecondary = null;
}
if (childURI == XmlReservedNs.NsXml)
{
XmlSchemaAttribute attributeReference = null;
//see if the reference exists
attributeReference = FindAttributeRef(searchCollectionPrimary, localName, childURI);
if (attributeReference == null && searchCollectionSecondary != null) {
attributeReference = FindAttributeRef(searchCollectionSecondary, localName, childURI);
}
if (attributeReference == null)
{
attributeReference = new XmlSchemaAttribute();
attributeReference.RefName = new XmlQualifiedName(localName, childURI);
if (bCreatingNewType && this.Occurrence == InferenceOption.Restricted)
{
attributeReference.Use = XmlSchemaUse.Required;
}
else
{
attributeReference.Use = XmlSchemaUse.Optional;
}
addLocation.Add(attributeReference);
}
returnedAttribute = attributeReference;
}
else
{
if (childURI.Length == 0)
{
xs = parentSchema;
add = false;
}
else if (childURI != null && !schemaSet.Contains(childURI))
{
/*if (parentSchema.AttributeFormDefault = XmlSchemaForm.Unqualified && childURI.Length == 0)
{
xs = parentSchema;
add = false;
break;
}*/
xs = new XmlSchema();
xs.AttributeFormDefault = XmlSchemaForm.Unqualified;
xs.ElementFormDefault = XmlSchemaForm.Qualified;
if (childURI.Length != 0)
xs.TargetNamespace = childURI;
//schemas.Add(childURI, xs);
this.schemaSet.Add(xs);
if (prefix.Length != 0 && String.Compare(prefix, "xml", StringComparison.OrdinalIgnoreCase) != 0)
NamespaceManager.AddNamespace(prefix, childURI);
}
else
{
ArrayList col = this.schemaSet.Schemas(childURI) as ArrayList;
if (col != null && col.Count > 0)
{
xs = col[0] as XmlSchema;
}
}
if (childURI.Length != 0) //
{
XmlSchemaAttribute attributeReference = null;
//see if the reference exists
attributeReference = FindAttributeRef(searchCollectionPrimary, localName, childURI);
if (attributeReference == null & searchCollectionSecondary != null) {
attributeReference = FindAttributeRef(searchCollectionSecondary, localName, childURI);
}
if (attributeReference == null)
{
attributeReference = new XmlSchemaAttribute();
attributeReference.RefName = new XmlQualifiedName(localName, childURI);
if (bCreatingNewType && this.Occurrence == InferenceOption.Restricted)
{
attributeReference.Use = XmlSchemaUse.Required;
}
else
{
attributeReference.Use = XmlSchemaUse.Optional;
}
addLocation.Add(attributeReference);
}
returnedAttribute = attributeReference;
//see if the attribute exists on the global level
xsa = FindAttribute(xs.Items, localName);
if (xsa == null)
{
xsa = new XmlSchemaAttribute();
xsa.Name = localName;
xsa.SchemaTypeName = RefineSimpleType(attrValue, ref AttributeType);
xsa.LineNumber = AttributeType; //we use LineNumber to store flags of valid types
xs.Items.Add(xsa);
}
else
{
if (xsa.Parent == null)
{
AttributeType = xsa.LineNumber; // we use LineNumber to store flags of valid types
}
else
{
AttributeType = GetSchemaType(xsa.SchemaTypeName);
xsa.Parent = null;
}
xsa.SchemaTypeName = RefineSimpleType(attrValue, ref AttributeType);
xsa.LineNumber = AttributeType; // we use LineNumber to store flags of valid types
}
}
else
{
xsa = FindAttribute(searchCollectionPrimary, localName);
if (xsa == null && searchCollectionSecondary != null) {
xsa = FindAttribute(searchCollectionSecondary, localName);
}
if (xsa == null)
{
xsa = new XmlSchemaAttribute();
xsa.Name = localName;
xsa.SchemaTypeName = RefineSimpleType(attrValue, ref AttributeType);
xsa.LineNumber = AttributeType; // we use LineNumber to store flags of valid types
if (bCreatingNewType && this.Occurrence == InferenceOption.Restricted)
xsa.Use = XmlSchemaUse.Required;
else
xsa.Use = XmlSchemaUse.Optional;
addLocation.Add(xsa);
if (xs.AttributeFormDefault != XmlSchemaForm.Unqualified)
{
xsa.Form = XmlSchemaForm.Unqualified;
}
}
else
{
if (xsa.Parent == null)
{
AttributeType = xsa.LineNumber; // we use LineNumber to store flags of valid types
}
else
{
AttributeType = GetSchemaType(xsa.SchemaTypeName);
xsa.Parent = null;
}
xsa.SchemaTypeName = RefineSimpleType(attrValue, ref AttributeType);
xsa.LineNumber = AttributeType; // we use LineNumber to store flags of valid types
}
returnedAttribute = xsa;
}
}
string nullString = null;
if (add && childURI != parentSchema.TargetNamespace)
{
for (int i = 0; i < parentSchema.Includes.Count; ++i)
{
XmlSchemaImport import = parentSchema.Includes[i] as XmlSchemaImport;
if (import == null)
{
continue;
}
if (import.Namespace == childURI)
{
add = false;
}
}
if (add)
{
XmlSchemaImport import = new XmlSchemaImport();
import.Schema = xs;
if (childURI.Length != 0)
{
nullString = childURI;
}
import.Namespace = nullString;
parentSchema.Includes.Add(import);
}
}
return returnedAttribute;
}
private XmlSchema CreateXmlSchema(string targetNS)
{
Debug.Assert(targetNS == null || targetNS.Length > 0 , "targetns for schema is empty");
XmlSchema xs = new XmlSchema();
xs.AttributeFormDefault = XmlSchemaForm.Unqualified;
xs.ElementFormDefault = XmlSchemaForm.Qualified;
xs.TargetNamespace = targetNS;
this.schemaSet.Add(xs);
return xs;
}
private XmlSchemaElement AddElement(string localName, string prefix, string childURI, XmlSchema parentSchema, XmlSchemaObjectCollection addLocation, int positionWithinCollection)
{
if (childURI == XmlSchema.Namespace)
{
throw new XmlSchemaInferenceException(Res.SchInf_schema, 0, 0);
}
XmlSchemaElement xse = null;
XmlSchemaElement returnedElement = xse; //this value will change to elementReference if childURI!= parentURI
XmlSchema xs = null;
bool bCreatingNewType = true;
if (childURI == String.Empty)
{
childURI = null;
}
// The new element belongs to the same ns as parent and addlocation is not null
if (parentSchema != null && childURI == parentSchema.TargetNamespace)
{
xse = new XmlSchemaElement();
xse.Name = localName;
xs = parentSchema;
if (xs.ElementFormDefault != XmlSchemaForm.Qualified && addLocation != null)
{
xse.Form = XmlSchemaForm.Qualified;
}
}
else if (schemaSet.Contains(childURI))
{
xse = this.FindGlobalElement(childURI, localName, out xs);
if (xse == null)
{
ArrayList col = this.schemaSet.Schemas(childURI)as ArrayList;
if (col != null && col.Count > 0 )
{
xs = col[0] as XmlSchema;
}
xse = new XmlSchemaElement();
xse.Name = localName;
xs.Items.Add(xse);
}
else
bCreatingNewType = false;
}
else
{
xs = CreateXmlSchema(childURI);
if (prefix.Length!=0)
NamespaceManager.AddNamespace(prefix, childURI);
xse=new XmlSchemaElement();
xse.Name = localName;
xs.Items.Add(xse); //add global element declaration only when creating new schema
}
if (parentSchema == null)
{
parentSchema = xs;
this.rootSchema = parentSchema;
}
if (childURI != parentSchema.TargetNamespace )
{
bool add = true;
for (int i = 0; i < parentSchema.Includes.Count; ++i)
{
XmlSchemaImport import = parentSchema.Includes[i] as XmlSchemaImport;
if (import == null)
{
continue;
}
//Debug.WriteLine(import.Schema.TargetNamespace);
if (import.Namespace == childURI)
{
add = false;
}
}
if (add)
{
XmlSchemaImport import = new XmlSchemaImport();
import.Schema = xs;
import.Namespace = childURI;
parentSchema.Includes.Add(import);
}
}
returnedElement = xse;
if (addLocation != null)
{
if (childURI == parentSchema.TargetNamespace )
{
if (this.Occurrence == InferenceOption.Relaxed /*&& parentSchema.Items != addLocation*/)
{
xse.MinOccurs = 0;
}
if (positionWithinCollection == -1)
{
positionWithinCollection = addLocation.Add(xse);
}
else
{
addLocation.Insert(positionWithinCollection, xse);
}
}
else
{
XmlSchemaElement elementReference = new XmlSchemaElement();
elementReference.RefName = new XmlQualifiedName(localName, childURI);
if (this.Occurrence == InferenceOption.Relaxed)
{
elementReference.MinOccurs = 0;
}
if (positionWithinCollection == -1)
{
positionWithinCollection = addLocation.Add(elementReference);
}
else
{
addLocation.Insert(positionWithinCollection, elementReference);
}
returnedElement = elementReference;
/* if (childURI == XmlSchema.Namespace)
{
schemaList.Add(new ReplaceList(addLocation, positionWithinCollection));
}*/
}
}
InferElement(xse, bCreatingNewType, xs);
return returnedElement;
}
/// <summary>
/// Sets type of the xse based on the currently read element.
/// If the type is already set, verifies that it matches the instance and if not, updates the type to validate the instance.
/// </summary>
/// <param name="xse">XmlSchemaElement corresponding to the element just read by the xtr XmlTextReader</param>
/// <param name="bCreatingNewType">true if the type is newly created, false if the type already existed and matches the current element name</param>
/// <param name="nsContext">namespaceURI of the parent element. Used to distinguish if ref= should be used when parent is in different ns than child.</param>
internal void InferElement(XmlSchemaElement xse, bool bCreatingNewType, XmlSchema parentSchema)
{
bool bEmptyElement = xtr.IsEmptyElement;
int lastUsedSeqItem = -1;
Hashtable table = new Hashtable();
XmlSchemaType schemaType = GetEffectiveSchemaType(xse, bCreatingNewType);
XmlSchemaComplexType ct = schemaType as XmlSchemaComplexType;
//infer type based on content of the current element
if (xtr.MoveToFirstAttribute())
{
ProcessAttributes(ref xse, schemaType, bCreatingNewType, parentSchema);
}
else
{
if (!bCreatingNewType && ct != null)
{ //if type already exists and can potentially have attributes
MakeExistingAttributesOptional(ct, null);
}
}
if (ct == null || ct == XmlSchemaComplexType.AnyType) { //It was null or simple type, after processing attributes, this might have been set
ct = xse.SchemaType as XmlSchemaComplexType;
}
//xse's type is set either to complex type if attributes exist or null
if (bEmptyElement) //<element attr="3232" />
{
if (!bCreatingNewType)
{
if (null != ct)
{
if (null!= ct.Particle )
{
ct.Particle.MinOccurs = 0;
}
else if (null != ct.ContentModel)
{
XmlSchemaSimpleContentExtension sce = CheckSimpleContentExtension(ct);
sce.BaseTypeName = ST_string;
sce.LineNumber = TF_string;
}
}
else if (!xse.SchemaTypeName.IsEmpty)
{
xse.LineNumber = TF_string;
xse.SchemaTypeName = ST_string;
}
}
else
{
xse.LineNumber = TF_string;
//xse.SchemaTypeName = ST_string; //My change
}
return; //We are done processing this element - all attributes are already added
}
bool bWhiteSpace = false;
do
{
xtr.Read();
if (xtr.NodeType == XmlNodeType.Whitespace)
{
bWhiteSpace = true;
}
if (xtr.NodeType == XmlNodeType.EntityReference)
{
throw new XmlSchemaInferenceException(Res.SchInf_entity, 0, 0);
}
} while( (!xtr.EOF) && (xtr.NodeType != XmlNodeType.EndElement) && (xtr.NodeType != XmlNodeType.CDATA)&&(xtr.NodeType != XmlNodeType.Element)&&(xtr.NodeType != XmlNodeType.Text) );
if (xtr.NodeType == XmlNodeType.EndElement)
{
if (bWhiteSpace) {
if (ct != null)
{
if (null != ct.ContentModel)
{
XmlSchemaSimpleContentExtension sce = CheckSimpleContentExtension(ct);
sce.BaseTypeName = ST_string;
sce.LineNumber = TF_string;
}
else if (bCreatingNewType)
{
//attributes exist, but both Particle and ContentModel == null - this must be complex type with simpleContent extension
XmlSchemaSimpleContent sc = new XmlSchemaSimpleContent();
ct.ContentModel = sc;
XmlSchemaSimpleContentExtension sce = new XmlSchemaSimpleContentExtension();
sc.Content = sce;
MoveAttributes(ct, sce, bCreatingNewType);
sce.BaseTypeName = ST_string;
sce.LineNumber = TF_string;
}
else
ct.IsMixed = true;
}
else
{
xse.SchemaTypeName = ST_string;
xse.LineNumber = TF_string;
}
}
if (bCreatingNewType)
{
xse.LineNumber = TF_string;
//xse.SchemaTypeName = ST_string; //my change
}
else
{
if (null != ct)
{
if (null!= ct.Particle)
{
ct.Particle.MinOccurs = 0;
}
else if (null != ct.ContentModel)
{
XmlSchemaSimpleContentExtension sce = CheckSimpleContentExtension(ct);
sce.BaseTypeName = ST_string;
sce.LineNumber = TF_string;
}
}
else if (!xse.SchemaTypeName.IsEmpty)
{
xse.LineNumber = TF_string;
xse.SchemaTypeName = ST_string;
}
}
return; //<element attr="232"></element>
}
int iChildNumber = 0;
bool bCreatingNewSequence = false;
while (!xtr.EOF && (xtr.NodeType != XmlNodeType.EndElement))
{
bool bNextNodeAlreadyRead = false; //In some cases we have to look ahead one node. If true means that we did look ahead.
iChildNumber++;
if ((xtr.NodeType == XmlNodeType.Text) || (xtr.NodeType == XmlNodeType.CDATA) ) //node can be simple type, complex with simple content or complex with mixed content
{
if (null != ct)
{
if (null != ct.Particle)
{
ct.IsMixed = true;
if (iChildNumber==1)
{
//if this is the only child and other elements do not follow, we must set particle minOccurs="0"
do{ xtr.Read();} while( (!xtr.EOF) && ((xtr.NodeType == XmlNodeType.CDATA)||(xtr.NodeType == XmlNodeType.Text) || (xtr.NodeType == XmlNodeType.Comment) || (xtr.NodeType == XmlNodeType.ProcessingInstruction) || (xtr.NodeType == XmlNodeType.Whitespace) || (xtr.NodeType == XmlNodeType.SignificantWhitespace) || (xtr.NodeType == XmlNodeType.XmlDeclaration)));
bNextNodeAlreadyRead = true;
if (xtr.NodeType == XmlNodeType.EndElement)
ct.Particle.MinOccurs=decimal.Zero;
}
}
else if (null!=ct.ContentModel)
{ //complexType with simpleContent
XmlSchemaSimpleContentExtension sce = CheckSimpleContentExtension(ct);
if ((xtr.NodeType == XmlNodeType.Text) && (iChildNumber==1))
{
int SimpleType = -1;
if (xse.Parent == null)
{
SimpleType = sce.LineNumber; // we use LineNumber to represent valid type flags
}
else
{
SimpleType = GetSchemaType(sce.BaseTypeName);
xse.Parent = null;
}
sce.BaseTypeName = RefineSimpleType(xtr.Value, ref SimpleType);
sce.LineNumber = SimpleType; // we use LineNumber to represent valid type flags
}
else
{
sce.BaseTypeName = ST_string;
sce.LineNumber = TF_string;
}
}
else
{
//attributes exist, but both Particle and ContentModel == null - this must be complex type with simpleContent extension
XmlSchemaSimpleContent sc = new XmlSchemaSimpleContent();
ct.ContentModel = sc;
XmlSchemaSimpleContentExtension sce = new XmlSchemaSimpleContentExtension();
sc.Content = sce;
MoveAttributes(ct, sce, bCreatingNewType);
if (xtr.NodeType == XmlNodeType.Text)
{
int TypeFlags;
if(!bCreatingNewType)
//previously this was empty element
TypeFlags=TF_string;
else
TypeFlags = -1;
sce.BaseTypeName = RefineSimpleType(xtr.Value, ref TypeFlags);
sce.LineNumber = TypeFlags; // we use LineNumber to store flags of valid types
}
else
{
sce.BaseTypeName = ST_string;
sce.LineNumber = TF_string;
}
}
}
else
{ //node is currently empty or with SimpleType
//node will become simple type
if (iChildNumber>1)
{
//more than one consecutive text nodes probably with PI in between
xse.SchemaTypeName = ST_string;
xse.LineNumber = TF_string;// we use LineNumber to store flags of valid types
}
else
{
int TypeFlags = -1;
if (bCreatingNewType)
if (xtr.NodeType == XmlNodeType.Text)
{
xse.SchemaTypeName = RefineSimpleType(xtr.Value, ref TypeFlags);
xse.LineNumber = TypeFlags; // we use LineNumber to store flags of valid types
}
else
{
xse.SchemaTypeName = ST_string;
xse.LineNumber = TF_string;// we use LineNumber to store flags of valid types
}
else if (xtr.NodeType == XmlNodeType.Text)
{
if (xse.Parent == null)
{
TypeFlags = xse.LineNumber;
}
else
{
TypeFlags = GetSchemaType(xse.SchemaTypeName);
if (TypeFlags == -1 && xse.LineNumber == TF_string) { //Since schemaTypeName is not set for empty elements (<e></e>)
TypeFlags = TF_string;
}
xse.Parent = null;
}
xse.SchemaTypeName = RefineSimpleType(xtr.Value, ref TypeFlags); //simple type
xse.LineNumber = TypeFlags; // we use LineNumber to store flags of valid types
}
else
{
xse.SchemaTypeName = ST_string;
xse.LineNumber = TF_string;// we use LineNumber to store flags of valid types
}
}
}
}
else if (xtr.NodeType == XmlNodeType.Element)
{
XmlQualifiedName qname = new XmlQualifiedName(xtr.LocalName, xtr.NamespaceURI);
bool Maxoccursflag = false;
if (table.Contains(qname))
{
Maxoccursflag = true;
}
else
{
table.Add(qname, null);
}
if (ct==null)
{ //untill now the element was empty or SimpleType - it now becomes complex type
ct = new XmlSchemaComplexType();
xse.SchemaType = ct;
if (!xse.SchemaTypeName.IsEmpty) //
{
ct.IsMixed=true;
xse.SchemaTypeName = XmlQualifiedName.Empty;
}
}
if (ct.ContentModel !=null)
{ //type was previously identified as simple content extension - we need to convert it to sequence
XmlSchemaSimpleContentExtension sce = CheckSimpleContentExtension(ct);
MoveAttributes(sce, ct);
ct.ContentModel = null;
ct.IsMixed = true;
if (ct.Particle != null)
throw new XmlSchemaInferenceException(Res.SchInf_particle, 0, 0);
ct.Particle = new XmlSchemaSequence();
bCreatingNewSequence = true;
XmlSchemaElement subelement = AddElement(xtr.LocalName, xtr.Prefix, xtr.NamespaceURI, parentSchema,((XmlSchemaSequence)ct.Particle).Items, -1);
lastUsedSeqItem = 0;
if (!bCreatingNewType)
ct.Particle.MinOccurs=0; //previously this was simple type so subelements did not exist
}
else if (ct.Particle == null)
{
ct.Particle = new XmlSchemaSequence();
bCreatingNewSequence = true;
XmlSchemaElement subelement = AddElement(xtr.LocalName, xtr.Prefix, xtr.NamespaceURI, parentSchema,((XmlSchemaSequence)ct.Particle).Items, -1);
if (!bCreatingNewType)
{
((XmlSchemaSequence)ct.Particle).MinOccurs = decimal.Zero;
// subelement.MinOccurs = decimal.Zero;
}
lastUsedSeqItem = 0;
}
else
{
bool bParticleChanged = false;
XmlSchemaElement subelement = FindMatchingElement(bCreatingNewType || bCreatingNewSequence, xtr, ct, ref lastUsedSeqItem, ref bParticleChanged, parentSchema, Maxoccursflag);
}
}
else if (xtr.NodeType == XmlNodeType.Text)
{
if (ct==null)
throw new XmlSchemaInferenceException(Res.SchInf_ct, 0, 0);
ct.IsMixed = true;
}
do
{
if (xtr.NodeType == XmlNodeType.EntityReference)
{
throw new XmlSchemaInferenceException(Res.SchInf_entity, 0, 0);
}
if (!bNextNodeAlreadyRead)
{
xtr.Read();
}
else
{
bNextNodeAlreadyRead = false;
}
} while( (!xtr.EOF) && (xtr.NodeType != XmlNodeType.EndElement) && (xtr.NodeType != XmlNodeType.CDATA)&&(xtr.NodeType != XmlNodeType.Element)&&(xtr.NodeType != XmlNodeType.Text));
}
if (lastUsedSeqItem != -1)
{
//Verify if all elements in a sequence exist, if not set MinOccurs=0
while (++lastUsedSeqItem < ((XmlSchemaSequence)ct.Particle).Items.Count)
{
if (((XmlSchemaSequence)ct.Particle).Items[lastUsedSeqItem].GetType() != typeof (XmlSchemaElement))
throw new XmlSchemaInferenceException(Res.SchInf_seq, 0 , 0);
XmlSchemaElement subElement = (XmlSchemaElement) ((XmlSchemaSequence)ct.Particle).Items[lastUsedSeqItem];
subElement.MinOccurs = 0;
}
}
}
private XmlSchemaSimpleContentExtension CheckSimpleContentExtension(XmlSchemaComplexType ct) {
XmlSchemaSimpleContent sc = ct.ContentModel as XmlSchemaSimpleContent;
if (sc == null) {
throw new XmlSchemaInferenceException(Res.SchInf_simplecontent, 0, 0);
}
XmlSchemaSimpleContentExtension sce = sc.Content as XmlSchemaSimpleContentExtension;
if (sce == null) {
throw new XmlSchemaInferenceException(Res.SchInf_extension, 0, 0);
}
return sce;
}
private XmlSchemaType GetEffectiveSchemaType(XmlSchemaElement elem, bool bCreatingNewType) {
XmlSchemaType effectiveSchemaType = null;
if (!bCreatingNewType && elem.ElementSchemaType != null) {
effectiveSchemaType = elem.ElementSchemaType;
}
else { //creating new type, hence look up pre-compiled info
Debug.Assert(elem.ElementDecl == null);
if (elem.SchemaType != null) {
effectiveSchemaType = elem.SchemaType;
}
else if (elem.SchemaTypeName != XmlQualifiedName.Empty) {
effectiveSchemaType = schemaSet.GlobalTypes[elem.SchemaTypeName] as XmlSchemaType;
if (effectiveSchemaType == null) {
effectiveSchemaType = XmlSchemaType.GetBuiltInSimpleType(elem.SchemaTypeName);
}
if (effectiveSchemaType == null) {
effectiveSchemaType = XmlSchemaType.GetBuiltInComplexType(elem.SchemaTypeName);
}
}
}
return effectiveSchemaType;
}
/// <summary>
/// Verifies that the current element has its corresponding element in the sequence and order is the same.
/// If the order is not the same, it changes the particle from Sequence to Sequence with Choice.
/// If there is more elements of the same kind in the sequence, sets maxOccurs to unbounded
/// </summary>
/// <param name="bCreatingNewType">True if this is a new type. This is important for setting minOccurs=0 for elements that did not exist in a particle.</param>
/// <param name="xtr">text reader positioned to the current element</param>
/// <param name="ct">complex type with Sequence or Choice Particle</param>
/// <param name="lastUsedSeqItem">ordinal number in the sequence to indicate current sequence position</param>
/// <param name="itemsMadeOptional">hashtable of elements with minOccurs changed to 0 in order to satisfy sequence requirements. These elements will be rolled back if Sequence becomes Sequence of Choice.</param>
/// <param name="bParticleChanged">This indicates to the caller if Sequence was changed to Choice</param>
internal XmlSchemaElement FindMatchingElement(bool bCreatingNewType, XmlReader xtr,XmlSchemaComplexType ct, ref int lastUsedSeqItem, ref bool bParticleChanged, XmlSchema parentSchema, bool setMaxoccurs)
{
if (xtr.NamespaceURI == XmlSchema.Namespace)
{
throw new XmlSchemaInferenceException(Res.SchInf_schema, 0, 0);
}
bool bItemNotUsedYet = ((lastUsedSeqItem == -1)? true: false);
XmlSchemaObjectCollection minOccursCandidates = new XmlSchemaObjectCollection(); //elements that are skipped in the sequence and need minOccurs modified.
if (ct.Particle.GetType() == typeof (XmlSchemaSequence))
{
string childURI = xtr.NamespaceURI;
if (childURI.Length == 0)
{
childURI = null;
}
XmlSchemaSequence xss = (XmlSchemaSequence) ct.Particle;
if (xss.Items.Count < 1 && !bCreatingNewType)
{
lastUsedSeqItem = 0;
XmlSchemaElement e = AddElement(xtr.LocalName, xtr.Prefix, xtr.NamespaceURI, parentSchema, xss.Items,-1);
e.MinOccurs = 0;
return e;
}
if (xss.Items[0].GetType() == typeof (XmlSchemaChoice))
{ // <sequence minOccurs="0" maxOccurs="unbounded"><choice><element>...</choice></sequence>
XmlSchemaChoice xsch = (XmlSchemaChoice) xss.Items[0];
for (int i = 0; i < xsch.Items.Count; ++i)
{
XmlSchemaElement el = xsch.Items[i] as XmlSchemaElement;
if (el == null)
{
throw new XmlSchemaInferenceException(Res.SchInf_UnknownParticle, 0, 0);
}
if ((el.Name == xtr.LocalName) &&( parentSchema.TargetNamespace == childURI))
{ // element is in the same namespace
InferElement(el, false, parentSchema);
SetMinMaxOccurs(el, setMaxoccurs);
return el;
}
else if ((el.RefName.Name == xtr.LocalName) && (el.RefName.Namespace == xtr.NamespaceURI))
{
XmlSchemaElement referencedElement = FindGlobalElement(childURI, xtr.LocalName, out parentSchema);
InferElement(referencedElement, false, parentSchema);
SetMinMaxOccurs(el, setMaxoccurs);
return referencedElement;
}
}
XmlSchemaElement subElement = AddElement(xtr.LocalName, xtr.Prefix, xtr.NamespaceURI, parentSchema, xsch.Items,-1);
return subElement;
}
else
{ //this should be sequence of elements
int iSeqItem = 0; //iterator through schema sequence items
if (lastUsedSeqItem >= 0)
iSeqItem = lastUsedSeqItem;
XmlSchemaParticle particle = xss.Items[iSeqItem] as XmlSchemaParticle;
XmlSchemaElement el = particle as XmlSchemaElement;
if (el == null)
{
throw new XmlSchemaInferenceException(Res.SchInf_UnknownParticle, 0, 0);
}
if (el.Name == xtr.LocalName && parentSchema.TargetNamespace == childURI)
{
if (!bItemNotUsedYet) //read: if item was already used one or more times
el.MaxOccurs = decimal.MaxValue; //set it to unbounded
lastUsedSeqItem = iSeqItem;
InferElement(el, false, parentSchema);
SetMinMaxOccurs(el, false);
return el;
}
else if (el.RefName.Name == xtr.LocalName && el.RefName.Namespace == xtr.NamespaceURI)
{
if (!bItemNotUsedYet) //read: if item was already used one or more times
el.MaxOccurs = decimal.MaxValue; //set it to unbounded
lastUsedSeqItem = iSeqItem;
XmlSchemaElement referencedElement = FindGlobalElement(childURI, xtr.LocalName, out parentSchema);
InferElement(referencedElement, false, parentSchema);
SetMinMaxOccurs(el, false);
return el;
}
if (bItemNotUsedYet && el.MinOccurs!=decimal.Zero)
minOccursCandidates.Add(el);
iSeqItem++;
while (iSeqItem < xss.Items.Count)
{
particle = xss.Items[iSeqItem] as XmlSchemaParticle;
el = particle as XmlSchemaElement;
if (el == null)
{
throw new XmlSchemaInferenceException(Res.SchInf_UnknownParticle, 0, 0);
}
if (el.Name == xtr.LocalName && parentSchema.TargetNamespace == childURI)
{
lastUsedSeqItem = iSeqItem;
for (int i = 0; i < minOccursCandidates.Count; ++i)
{
((XmlSchemaElement) minOccursCandidates[i]).MinOccurs = decimal.Zero;
}
InferElement(el, false, parentSchema);
SetMinMaxOccurs(el, setMaxoccurs);
return el;
}
else if (el.RefName.Name == xtr.LocalName && el.RefName.Namespace == xtr.NamespaceURI)
{
lastUsedSeqItem = iSeqItem;
for (int i = 0; i < minOccursCandidates.Count; ++i)
{
((XmlSchemaElement) minOccursCandidates[i]).MinOccurs = decimal.Zero;
}
XmlSchemaElement referencedElement = FindGlobalElement(childURI, xtr.LocalName, out parentSchema);
InferElement(referencedElement, false, parentSchema);
SetMinMaxOccurs(el, setMaxoccurs);
return referencedElement;
}
minOccursCandidates.Add(el);
iSeqItem++;
}
//element not found in the sequence order, if it is found out of order change Sequence of elements to Sequence of Choices otherwise insert into sequence as optional
XmlSchemaElement subElement = null;
XmlSchemaElement actualElement = null;
//
if (parentSchema.TargetNamespace == childURI)
{
subElement = FindElement(xss.Items, xtr.LocalName);
actualElement = subElement;
}
else
{
subElement = FindElementRef(xss.Items, xtr.LocalName, xtr.NamespaceURI);
if (subElement != null)
{
actualElement = FindGlobalElement(childURI, xtr.LocalName, out parentSchema);
}
}
if (null != subElement)
{
XmlSchemaChoice xsc = new XmlSchemaChoice();
xsc.MaxOccurs = decimal.MaxValue;
SetMinMaxOccurs(subElement, setMaxoccurs);
InferElement(actualElement, false, parentSchema);
for (int i = 0; i < xss.Items.Count; ++i)
{
xsc.Items.Add(CreateNewElementforChoice((XmlSchemaElement) xss.Items[i]));
}
xss.Items.Clear();
xss.Items.Add(xsc);
return subElement;
}
else
{
subElement = AddElement(xtr.LocalName, xtr.Prefix, xtr.NamespaceURI, parentSchema, xss.Items, ++lastUsedSeqItem);
if (!bCreatingNewType)
subElement.MinOccurs = decimal.Zero;
return subElement;
}
}
}
else
{
throw new XmlSchemaInferenceException(Res.SchInf_noseq, 0, 0);
}
}
internal void ProcessAttributes(ref XmlSchemaElement xse, XmlSchemaType effectiveSchemaType, bool bCreatingNewType, XmlSchema parentSchema)
{
XmlSchemaObjectCollection attributesSeen = new XmlSchemaObjectCollection();
XmlSchemaComplexType ct = effectiveSchemaType as XmlSchemaComplexType;
Debug.Assert(xtr.NodeType == XmlNodeType.Attribute);
do
{
if (xtr.NamespaceURI == XmlSchema.Namespace)
{
throw new XmlSchemaInferenceException(Res.SchInf_schema, 0, 0);
}
if (xtr.NamespaceURI == XmlReservedNs.NsXmlNs)
{
if (xtr.Prefix=="xmlns")
NamespaceManager.AddNamespace(xtr.LocalName, xtr.Value);
}
else if (xtr.NamespaceURI == XmlReservedNs.NsXsi)
{
string localName = xtr.LocalName;
if (localName == "nil")
{
xse.IsNillable = true;
}
else if (localName != "type" && localName != "schemaLocation" && localName != "noNamespaceSchemaLocation")
{
throw new XmlSchemaInferenceException(Res.Sch_NotXsiAttribute,localName);
}
}
else
{
if (ct == null || ct == XmlSchemaComplexType.AnyType)
{
ct = new XmlSchemaComplexType();
xse.SchemaType = ct;
}
XmlSchemaAttribute xsa=null;
//The earlier assumption of checking just schemaTypeName !Empty is not correct for schemas that are not generated by us, schemaTypeName can point to any complex type as well
//Check that it is a simple type by checking typeCode
//Switch to complex type simple content extension
if (effectiveSchemaType != null && effectiveSchemaType.Datatype != null && !xse.SchemaTypeName.IsEmpty)
{
//type was previously simple type, now it will become complex with simple type extension
Debug.Assert(ct != null);
XmlSchemaSimpleContent sc = new XmlSchemaSimpleContent();
ct.ContentModel = sc;
XmlSchemaSimpleContentExtension sce = new XmlSchemaSimpleContentExtension();
sc.Content = sce;
sce.BaseTypeName = xse.SchemaTypeName;
sce.LineNumber = xse.LineNumber;
xse.LineNumber = 0;
xse.SchemaTypeName = XmlQualifiedName.Empty; //re-set the name
}
Debug.Assert(ct != null); //either the user-defined type itself is a complex type or we switched from a simple type to a complex type
if (ct.ContentModel != null)
{
XmlSchemaSimpleContentExtension sce = CheckSimpleContentExtension(ct);
Debug.Assert(sce != null);
xsa = AddAttribute(xtr.LocalName, xtr.Prefix, xtr.NamespaceURI, xtr.Value, bCreatingNewType, parentSchema, sce.Attributes, ct.AttributeUses);
}
else //add atributes directly to complex type
{
xsa = AddAttribute(xtr.LocalName, xtr.Prefix, xtr.NamespaceURI, xtr.Value, bCreatingNewType, parentSchema, ct.Attributes, ct.AttributeUses);
}
if (xsa != null) {
attributesSeen.Add(xsa);
}
}
} while (xtr.MoveToNextAttribute());
if (!bCreatingNewType)
{
//make attributes that did not appear this time optional
if (ct!=null) {
MakeExistingAttributesOptional(ct, attributesSeen);
}
}
}
private void MoveAttributes(XmlSchemaSimpleContentExtension scExtension, XmlSchemaComplexType ct) {
//copy all attributes from the simple content to the complex type
//This is ok since when we move from complex type to simple content extension we copy from AttributeUses property
for (int i = 0; i < scExtension.Attributes.Count; ++i) //since simpleContent is being cleared
{
ct.Attributes.Add(scExtension.Attributes[i]);
}
}
private void MoveAttributes(XmlSchemaComplexType ct, XmlSchemaSimpleContentExtension simpleContentExtension, bool bCreatingNewType) {
//copy all attributes from the complex type to the simple content
ICollection sourceCollection;
if (!bCreatingNewType && ct.AttributeUses.Count > 0) {
sourceCollection = ct.AttributeUses.Values;
}
else {
sourceCollection = ct.Attributes;
}
foreach (XmlSchemaAttribute attr in sourceCollection) {
simpleContentExtension.Attributes.Add(attr);
}
ct.Attributes.Clear(); //Clear from pre-compiled property, post compiled will be cleared on Re-process and Compile()
}
internal XmlSchemaAttribute FindAttribute(ICollection attributes, string attrName)
{
foreach(XmlSchemaObject xsa in attributes)
{
XmlSchemaAttribute schemaAttribute = xsa as XmlSchemaAttribute;
if (schemaAttribute != null)
{
if (schemaAttribute.Name == attrName)
{
return schemaAttribute;
}
}
}
return null;
}
internal XmlSchemaElement FindGlobalElement(string namespaceURI, string localName, out XmlSchema parentSchema)
{
ICollection col = this.schemaSet.Schemas(namespaceURI);
XmlSchemaElement xse = null;
parentSchema = null;
foreach(XmlSchema schema in col)
{
xse = FindElement(schema.Items, localName);
if (xse != null)
{
parentSchema = schema;
return xse;
}
}
return null;
}
internal XmlSchemaElement FindElement(XmlSchemaObjectCollection elements, string elementName)
{
for (int i = 0; i < elements.Count; ++i)
{
XmlSchemaElement xse = elements[i] as XmlSchemaElement;
if (xse != null && xse.RefName != null)
{
if (xse.Name == elementName)
{
return xse;
}
}
}
return null;
}
internal XmlSchemaAttribute FindAttributeRef(ICollection attributes, string attributeName, string nsURI)
{
foreach(XmlSchemaObject xsa in attributes)
{
XmlSchemaAttribute schemaAttribute = xsa as XmlSchemaAttribute;
if (schemaAttribute != null)
{
if (schemaAttribute.RefName.Name == attributeName && schemaAttribute.RefName.Namespace == nsURI) {
return schemaAttribute;
}
}
}
return null;
}
internal XmlSchemaElement FindElementRef(XmlSchemaObjectCollection elements, string elementName, string nsURI)
{
for (int i = 0; i < elements.Count; ++i)
{
XmlSchemaElement xse = elements[i] as XmlSchemaElement;
if (xse != null && xse.RefName != null)
{
if (xse.RefName.Name == elementName && xse.RefName.Namespace == nsURI)
{
return xse;
}
}
}
return null;
}
internal void MakeExistingAttributesOptional(XmlSchemaComplexType ct, XmlSchemaObjectCollection attributesInInstance) {
if (ct == null) {
throw new XmlSchemaInferenceException(Res.SchInf_noct, 0, 0);
}
if (ct.ContentModel != null) {
XmlSchemaSimpleContentExtension xssce = CheckSimpleContentExtension(ct);
SwitchUseToOptional(xssce.Attributes, attributesInInstance);
}
else { //either <xs:attribute> as child of xs:complexType or the attributes are within the content model
SwitchUseToOptional(ct.Attributes, attributesInInstance);
}
}
private void SwitchUseToOptional(XmlSchemaObjectCollection attributes, XmlSchemaObjectCollection attributesInInstance) {
for (int i = 0; i < attributes.Count; ++i)
{
XmlSchemaAttribute attr = attributes[i] as XmlSchemaAttribute;
if (attr != null) {
if (attributesInInstance != null) {
if (attr.RefName.Name.Length == 0) { //If the attribute is not present in this instance, make it optional
if (null == FindAttribute(attributesInInstance, attr.Name)) {
attr.Use = XmlSchemaUse.Optional;
}
}
else {
if (null == FindAttributeRef(attributesInInstance, attr.RefName.Name, attr.RefName.Namespace)) {
attr.Use = XmlSchemaUse.Optional;
}
}
}
else {
attr.Use = XmlSchemaUse.Optional;
}
}
}
}
internal XmlQualifiedName RefineSimpleType(string s, ref int iTypeFlags)
{
bool bNeedsRangeCheck = false;
s = s.Trim();
if (iTypeFlags == TF_string || this.typeInference == InferenceOption.Relaxed)
return ST_string;
iTypeFlags &= InferSimpleType(s, ref bNeedsRangeCheck);
if (iTypeFlags == TF_string)
return ST_string;
if (bNeedsRangeCheck)
{
if ((iTypeFlags & TF_byte) != 0)
{
try
{
XmlConvert.ToSByte(s);
//sbyte.Parse(s);
if ((iTypeFlags & TF_unsignedByte) != 0)
return ST_unsignedByte; //number is positive and fits byte -> it also fits unsignedByte
else
return ST_byte;
}
catch (FormatException)
{}
catch (OverflowException)
{}
iTypeFlags &= (~TF_byte);
}
if ((iTypeFlags & TF_unsignedByte) != 0)
{
try
{
XmlConvert.ToByte(s);
//byte.Parse(s);
return ST_unsignedByte;
}
catch (FormatException)
{}
catch (OverflowException)
{}
iTypeFlags &= (~TF_unsignedByte);
}
if ((iTypeFlags & TF_short) != 0)
{
try
{
XmlConvert.ToInt16(s);
//short.Parse(s);
if ((iTypeFlags & TF_unsignedShort) != 0)
return ST_unsignedShort; //number is positive and fits short -> it also fits unsignedShort
else
return ST_short;
}
catch (FormatException)
{}
catch (OverflowException)
{}
iTypeFlags &= (~TF_short);
}
if ((iTypeFlags & TF_unsignedShort) != 0)
{
try
{
XmlConvert.ToUInt16(s);
//ushort.Parse(s);
return ST_unsignedShort;
}
catch (FormatException)
{}
catch (OverflowException)
{}
iTypeFlags &= (~TF_unsignedShort);
}
if ((iTypeFlags & TF_int) != 0)
{
try
{
XmlConvert.ToInt32(s);
//int.Parse(s);
if ((iTypeFlags & TF_unsignedInt) != 0)
return ST_unsignedInt; //number is positive and fits int -> it also fits unsignedInt
else
return ST_int;
}
catch (FormatException)
{}
catch (OverflowException)
{}
iTypeFlags &= (~TF_int);
}
if ((iTypeFlags & TF_unsignedInt) != 0)
{
try
{
XmlConvert.ToUInt32(s);
//uint.Parse(s);
return ST_unsignedInt;
}
catch (FormatException)
{}
catch (OverflowException)
{}
iTypeFlags &= (~TF_unsignedInt);
}
if ((iTypeFlags & TF_long) != 0)
{
try
{
XmlConvert.ToInt64(s);
//long.Parse(s);
if ((iTypeFlags & TF_unsignedLong) != 0)
return ST_unsignedLong; //number is positive and fits long -> it also fits unsignedLong
else
return ST_long;
}
catch (FormatException)
{}
catch (OverflowException)
{}
iTypeFlags &= (~TF_long);
}
if ((iTypeFlags & TF_unsignedLong) != 0)
{
try
{
XmlConvert.ToUInt64(s);
//ulong.Parse(s);
return ST_unsignedLong;
}
catch (FormatException)
{}
catch (OverflowException)
{}
iTypeFlags &= (~TF_unsignedLong);
}
if ((iTypeFlags & TF_double) != 0)
{
try
{
double dbValue = XmlConvert.ToDouble(s);
if ((iTypeFlags & TF_integer) != 0)
return ST_integer;
else if ((iTypeFlags & TF_decimal) != 0)
return ST_decimal;
else {
// The value fits into double, but it could be float as well
if ((iTypeFlags & TF_float) != 0) {
// We used to default to float in this case, so try to fit it into float first
try {
float flValue = XmlConvert.ToSingle(s);
// Compare the float and double values. We can't do simple value comparison
// as conversion from float to double introduces imprecissions which cause problems.
// Instead we will convert both back to string and compare the strings.
if (string.Compare(XmlConvert.ToString(flValue), XmlConvert.ToString(dbValue),
StringComparison.OrdinalIgnoreCase) == 0) {
// If we can convert the original string to the exact same value
// and it still fits into float then we treat it as float
return ST_float;
}
}
catch (FormatException)
{}
catch (OverflowException)
{}
}
iTypeFlags &= (~TF_float);
return ST_double;
}
}
catch (FormatException)
{}
catch (OverflowException)
{}
iTypeFlags &= (~TF_double);
}
if ((iTypeFlags & TF_float) != 0) {
try {
XmlConvert.ToSingle(s);
if ((iTypeFlags & TF_integer) != 0)
return ST_integer;
else if ((iTypeFlags & TF_decimal) != 0)
return ST_decimal;
else
return ST_float;
}
catch (FormatException) { }
catch (OverflowException) { }
iTypeFlags &= (~TF_float);
}
if ((iTypeFlags & TF_integer) != 0)
return ST_integer;
else if ((iTypeFlags & TF_decimal) != 0)
return ST_decimal;
else if (iTypeFlags == (TF_gYearMonth | TF_string) )
{
try
{
XmlConvert.ToDateTime(s, XmlDateTimeSerializationMode.RoundtripKind);
return ST_gYearMonth;
}
catch (FormatException)
{}
catch (OverflowException)
{}
iTypeFlags = TF_string;
return ST_string;
}
else if (iTypeFlags == (TF_duration | TF_string) )
{
try
{
XmlConvert.ToTimeSpan(s);
return ST_duration;
}
catch (FormatException)
{}
catch (OverflowException)
{}
iTypeFlags = TF_string;
return ST_string;
}
else if (iTypeFlags == (TF_boolean | TF_string))
{
return ST_boolean;
}
}
switch (iTypeFlags)
{
case TF_string:
return ST_string;
case TF_boolean:
return ST_boolean;
case TF_byte:
return ST_byte;
case TF_unsignedByte:
return ST_unsignedByte;
case TF_short:
return ST_short;
case TF_unsignedShort:
return ST_unsignedShort;
case TF_int:
return ST_int;
case TF_unsignedInt:
return ST_unsignedInt;
case TF_long:
return ST_long;
case TF_unsignedLong:
return ST_unsignedLong;
case TF_integer:
return ST_integer;
case TF_decimal:
return ST_decimal;
case TF_float:
return ST_float;
case TF_double:
return ST_double;
case TF_duration:
return ST_duration;
case TF_dateTime:
return ST_dateTime;
case TF_time:
return ST_time;
case TF_date:
return ST_date;
case TF_gYearMonth:
return ST_gYearMonth;
case TF_boolean | TF_string:
return ST_boolean;
case TF_dateTime | TF_string:
return ST_dateTime;
case TF_date | TF_string:
return ST_date;
case TF_time | TF_string:
return ST_time;
case TF_float | TF_double | TF_string:
return ST_float;
case TF_double | TF_string:
return ST_double;
default:
Debug.Assert(false, "Expected type not matched");
return ST_string;
}
/* if (currentType == null)
return SimpleTypes[newType];
else
return SimpleTypes[ST_Map[newType,(short) ST_Codes[currentType]]];
*/
}
internal static int InferSimpleType(string s, ref bool bNeedsRangeCheck)
{
bool bNegative = false;
bool bPositive = false;
bool bDate = false;
bool bTime = false;
bool bMissingDay = false;
#if !BOOTSTRAP_BASIC
if (s.Length==0) return TF_string;
int i = 0;
switch (s[i])
{
case 't':
case 'f':
if (s == "true")
return TF_boolean | TF_string;
else if (s == "false")
return TF_boolean | TF_string;
else
return TF_string;
case 'N': //try to match "NaN"
if (s == "NaN")
return TF_float | TF_double | TF_string;
else
return TF_string;
//else
case 'I': //try to match "INF"
INF:
if (s.Substring(i) == "INF")
return TF_float | TF_double | TF_string;
else return TF_string;
case '.': //try to match ".9999" decimal/float/double
FRACTION:
bNeedsRangeCheck = true;
i++;
if (i==s.Length)
{
if ((i==1) || (i==2 && (bPositive || bNegative))) //"." "-." "+."
return TF_string;
else
return TF_decimal | TF_float | TF_double | TF_string;
}
switch (s[i])
{
case 'e':
case 'E':
goto EXPONENT;
default:
if (s[i]>='0' && s[i]<='9')
goto DEC_PART;
else
return TF_string;
}
DEC_PART:
i++; if (i==s.Length) return TF_decimal | TF_float | TF_double | TF_string; //"9999.9" was matched
switch (s[i])
{
case 'e':
case 'E':
goto EXPONENT;
default:
if (s[i]>='0' && s[i]<='9')
goto DEC_PART;
else
return TF_string;
}
EXPONENT:
i++; if (i==s.Length) return TF_string;
switch (s[i])
{
case '+':
case '-':
goto E1;
default:
if (s[i]>='0' && s[i]<='9')
goto EXP_PART;
else
return TF_string;
}
E1:
i++; if (i==s.Length) return TF_string; //".9999e+" was matched
if (s[i]>='0' && s[i]<='9')
goto EXP_PART;
else
return TF_string; //".999e+X was matched
EXP_PART:
i++; if (i==s.Length) return TF_float | TF_double | TF_string; //".9999e+99" was matched
if (s[i]>='0' && s[i]<='9') //".9999e+9
goto EXP_PART;
else
return TF_string; //".9999e+999X" was matched
case '-':
bNegative = true;
i++; if (i==s.Length) return TF_string;
switch (s[i])
{
case 'I': //try to match "-INF"
goto INF;
case '.': //try to match "-.9999"
goto FRACTION;
case 'P':
goto DURATION;
default:
if (s[i]>='0' && s[i]<='9') //-9
goto NUMBER;
else return TF_string;
}
case '+':
bPositive = true;
i++; if (i==s.Length) return TF_string;
switch (s[i])
{
case '.': //try to match "+.9999"
goto FRACTION;
case 'P':
goto DURATION;
default:
if (s[i]>='0' && s[i]<='9') //"+9
goto NUMBER;
else return TF_string;
}
case 'P': //try to match duration
DURATION:
i++; if (i==s.Length) return TF_string;
switch (s[i])
{
case 'T':
goto D7;
default:
if (s[i]>='0' && s[i]<='9') //"P9"
goto D1;
else return TF_string;
}
D1:
i++; if (i==s.Length) return TF_string; //"P999" was matched
switch (s[i])
{
case 'Y':
goto D2;
case 'M':
goto D4;
case 'D':
goto D6;
default:
if (s[i]>='0' && s[i]<='9')
goto D1;
else
return TF_string;
}
D2:
i++;
if (i==s.Length)
{
bNeedsRangeCheck = true;
return TF_duration | TF_string; //"P999Y" was matched
}
switch (s[i])
{
case 'T':
goto D7;
default:
if (s[i]>='0' && s[i]<='9')
goto D3;
else
return TF_string;
}
D3:
i++; if (i==s.Length) return TF_string; //"P999Y9" was matched
switch (s[i])
{
case 'M':
goto D4;
case 'D':
goto D6;
default:
if (s[i]>='0' && s[i]<='9')
goto D3;
else
return TF_string;
}
D4:
i++;
if (i==s.Length)
{
bNeedsRangeCheck = true;
return TF_duration | TF_string; //"P999Y999M" was matched
}
switch (s[i])
{
case 'T':
goto D7;
default:
if (s[i]>='0' && s[i]<='9')
goto D5;
else
return TF_string;
}
D5:
i++; if (i==s.Length) return TF_string; //"P999Y999M9" was matched
switch (s[i])
{
case 'D':
goto D6;
default:
if (s[i]>='0' && s[i]<='9')
goto D5;
else
return TF_string;
}
D6:
i++;
if (i==s.Length)
{
bNeedsRangeCheck = true;
return TF_duration | TF_string; //"P999Y999M999D" was matched
}
switch (s[i])
{
case 'T':
goto D7;
default:
return TF_string;
}
D7:
i++; if (i==s.Length) return TF_string; //"P999Y999M9999DT" was matched
if (s[i]>='0' && s[i]<='9')
goto D8;
else
return TF_string;
D8:
i++; if (i==s.Length) return TF_string; //"___T9" was matched
switch (s[i])
{
case 'H':
goto D9;
case 'M':
goto D11;
case '.':
goto D13;
case 'S':
goto D15;
default:
if (s[i]>='0' && s[i]<='9')
goto D8;
else
return TF_string;
}
D9:
i++;
if (i==s.Length)
{
bNeedsRangeCheck = true;
return TF_duration | TF_string; //"___T999H" was matched
}
if (s[i]>='0' && s[i]<='9')
goto D10;
else
return TF_string;
D10:
i++; if (i==s.Length) return TF_string; //"___T999H9" was matched
switch (s[i])
{
case 'M':
goto D11;
case '.':
goto D13;
case 'S':
goto D15;
default:
if (s[i]>='0' && s[i]<='9')
goto D10;
else
return TF_string;
}
D11:
i++;
if (i==s.Length)
{
bNeedsRangeCheck = true;
return TF_duration | TF_string; //"___T999H999M" was matched
}
if (s[i]>='0' && s[i]<='9')
goto D12;
else
return TF_string;
D12:
i++; if (i==s.Length) return TF_string; //"___T999H999M9" was matched
switch (s[i])
{
case '.':
goto D13;
case 'S':
goto D15;
default:
if (s[i]>='0' && s[i]<='9')
goto D12;
else
return TF_string;
}
D13:
i++;
if (i==s.Length)
{
bNeedsRangeCheck = true;
return TF_duration | TF_string; //"___T999H999M999." was matched
}
if (s[i]>='0' && s[i]<='9')
goto D14;
else
return TF_string;
D14:
i++; if (i==s.Length) return TF_string; //"___T999H999M999.9" was matched
switch (s[i])
{
case 'S':
goto D15;
default:
if (s[i]>='0' && s[i]<='9')
goto D14;
else
return TF_string;
}
D15:
i++;
if (i==s.Length)
{
bNeedsRangeCheck = true;
return TF_duration | TF_string; //"___T999H999M999.999S" was matched
}
else return TF_string;
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
NUMBER:
i++;
if (i==s.Length)
{
bNeedsRangeCheck = true;
if (bNegative || bPositive)
return TF_byte | TF_short | TF_int | TF_long | TF_integer | TF_decimal | TF_float | TF_double | TF_string; //"-9"
else
{
if (s=="0" || s=="1")
return TF_boolean | TF_byte | TF_short | TF_int | TF_long | TF_integer | TF_decimal | TF_float | TF_double |
TF_unsignedByte | TF_unsignedShort | TF_unsignedInt | TF_unsignedLong | TF_string;
else
return TF_byte | TF_short | TF_int | TF_long | TF_integer | TF_decimal | TF_float | TF_double |
TF_unsignedByte | TF_unsignedShort | TF_unsignedInt | TF_unsignedLong | TF_string;
}
}
switch (s[i])
{
case '.':
goto FRACTION;
case 'e':
case 'E':
bNeedsRangeCheck = true;
return TF_float | TF_double | TF_string;
default:
if (s[i]>='0' && s[i]<='9')
goto N2;
else
return TF_string;
}
N2:
i++;
if (i==s.Length)
{
bNeedsRangeCheck = true;
if (bNegative || bPositive)
return TF_byte | TF_short | TF_int | TF_long | TF_integer | TF_decimal | TF_float | TF_double | TF_string; //"-9"
else
return TF_byte | TF_short | TF_int | TF_long | TF_integer | TF_decimal | TF_float | TF_double |
TF_unsignedByte | TF_unsignedShort | TF_unsignedInt | TF_unsignedLong | TF_string;
}
switch (s[i])
{
case '.':
goto FRACTION;
case ':':
bTime = true;
goto MINUTE;
case 'e':
case 'E':
bNeedsRangeCheck = true;
return TF_float | TF_double | TF_string;
default:
if (s[i]>='0' && s[i]<='9')
goto N3;
else
return TF_string;
}
N3:
i++;
if (i==s.Length)
{
bNeedsRangeCheck = true; //three digits may not fit byte and unsignedByte
if (bNegative || bPositive)
return TF_byte | TF_short | TF_int | TF_long | TF_integer | TF_decimal | TF_float | TF_double | TF_string; //"-9"
else
return TF_byte | TF_short | TF_int | TF_long | TF_integer | TF_decimal | TF_float | TF_double |
TF_unsignedByte | TF_unsignedShort | TF_unsignedInt | TF_unsignedLong | TF_string;
}
switch (s[i])
{
case '.':
goto FRACTION;
case 'e':
case 'E':
bNeedsRangeCheck = true;
return TF_float | TF_double | TF_string;
default:
if (s[i]>='0' && s[i]<='9')
goto N4;
else
return TF_string;
}
N4:
i++;
if (i==s.Length)
{
bNeedsRangeCheck = true;
if (bNegative || bPositive)
return TF_byte | TF_short | TF_int | TF_long | TF_integer | TF_decimal | TF_float | TF_double | TF_string; //"-9"
else
return TF_byte | TF_short | TF_int | TF_long | TF_integer | TF_decimal | TF_float | TF_double |
TF_unsignedByte | TF_unsignedShort | TF_unsignedInt | TF_unsignedLong | TF_string;
}
switch (s[i])
{
case '-':
bDate = true;
goto DATE;
case '.':
goto FRACTION;
case 'e':
case 'E':
bNeedsRangeCheck = true;
return TF_float | TF_double | TF_string;
default:
if (s[i]>='0' && s[i]<='9')
goto N4;
else
return TF_string;
}
DATE:
i++; if (i==s.Length) return TF_string; //"9999-"
if (s[i]<'0' || s[i]>'9')
return TF_string;
i++; if (i==s.Length) return TF_string; //"9999-9"
if (s[i]<'0' || s[i]>'9')
return TF_string;
i++;
if (i==s.Length)
{
bNeedsRangeCheck = true;
return TF_gYearMonth | TF_string; //"9999-99"
}
switch (s[i])
{
case '-':
goto DAY;
case 'Z':
case 'z':
bMissingDay = true;
goto ZULU;
case '+':
bMissingDay = true;
goto ZONE_SHIFT;
default:
return TF_string;
}
DAY:
i++; if (i==s.Length) return TF_string; //"9999-99-"
if (s[i]<'0' || s[i]>'9')
return TF_string;
i++; if (i==s.Length) return TF_string; //"9999-99-9"
if (s[i]<'0' || s[i]>'9')
return TF_string;
i++; if (i==s.Length) return DateTime(s, bDate, bTime); //"9999-99-99"
switch (s[i])
{
case 'Z':
case 'z':
goto ZULU;
case '+':
case '-':
goto ZONE_SHIFT;
case 'T':
bTime=true;
goto TIME;
case ':':
bMissingDay = true;
goto ZONE_SHIFT_MINUTE;
default:
return TF_string;
}
ZULU:
i++;
if (i==s.Length)
{
if (bMissingDay)
{
bNeedsRangeCheck=true;
return TF_gYearMonth | TF_string;
}
else
{
return DateTime(s, bDate, bTime);
}
}
else
return TF_string;
ZONE_SHIFT:
i++; if (i==s.Length) return TF_string;
if (s[i]<'0' || s[i]>'9')
return TF_string;
i++; if (i==s.Length) return TF_string;
if (s[i]<'0' || s[i]>'9')
return TF_string;
i++; if (i==s.Length) return TF_string;
if (s[i] != ':')
return TF_string;
ZONE_SHIFT_MINUTE:
i++; if (i==s.Length) return TF_string;
if (s[i]<'0' || s[i]>'9')
return TF_string;
i++; if (i==s.Length) return TF_string;
if (s[i]<'0' || s[i]>'9')
return TF_string;
i++;
if (i==s.Length)
{
if (bMissingDay)
{
bNeedsRangeCheck=true;
return TF_gYearMonth | TF_string;
}
else
{
return DateTime(s, bDate, bTime);
}
}
else return TF_string;
TIME:
i++; if (i==s.Length) return TF_string;
if (s[i]<'0' || s[i]>'9')
return TF_string;
i++; if (i==s.Length) return TF_string;
if (s[i]<'0' || s[i]>'9')
return TF_string;
i++; if (i==s.Length) return TF_string;
if (s[i] != ':')
return TF_string;
MINUTE:
i++; if (i==s.Length) return TF_string;
if (s[i]<'0' || s[i]>'9')
return TF_string;
i++; if (i==s.Length) return TF_string;
if (s[i]<'0' || s[i]>'9')
return TF_string;
i++; if (i==s.Length) return TF_string;
if (s[i] != ':')
return TF_string;
i++; if (i==s.Length) return TF_string;
if (s[i]<'0' || s[i]>'9')
return TF_string;
i++; if (i==s.Length) return TF_string;
if (s[i]<'0' || s[i]>'9')
return TF_string;
i++; if (i==s.Length) return DateTime(s, bDate, bTime);
switch (s[i])
{
case 'Z':
case 'z':
goto ZULU;
case '+':
case '-':
goto ZONE_SHIFT;
case '.':
goto SECOND_FRACTION;
default:
return TF_string;
}
SECOND_FRACTION:
i++; if (i==s.Length) return TF_string;
if (s[i]<'0' || s[i]>'9')
return TF_string;
FRACT_DIGITS:
i++; if (i==s.Length) return DateTime(s, bDate, bTime);
switch (s[i])
{
case 'Z':
case 'z':
goto ZULU;
case '+':
case '-':
goto ZONE_SHIFT;
default:
if (s[i]>='0' && s[i]<='9')
goto FRACT_DIGITS;
else
return TF_string;
}
default:
return TF_string;
}
#else // BOOTSTRAP_BASIC
return TF_string;
#endif
}
internal static int DateTime(string s, bool bDate, bool bTime)
{
try
{
XmlConvert.ToDateTime(s, XmlDateTimeSerializationMode.RoundtripKind);
}
catch (FormatException)
{
return TF_string;
}
if (bDate && bTime)
return TF_dateTime | TF_string;
else if (bDate)
return TF_date | TF_string;
else if (bTime)
return TF_time | TF_string;
else
{
Debug.Assert(false, "Expected date, time or dateTime");
return TF_string;
}
}
XmlSchemaElement CreateNewElementforChoice(XmlSchemaElement copyElement)
{
XmlSchemaElement newElement = new XmlSchemaElement();
newElement.Annotation = copyElement.Annotation;
newElement.Block = copyElement.Block;
newElement.DefaultValue = copyElement.DefaultValue;
newElement.Final = copyElement.Final;
newElement.FixedValue = copyElement.FixedValue;
newElement.Form = copyElement.Form;
newElement.Id = copyElement.Id;
// newElement.IsAbstract = copyElement.IsAbstract;
if (copyElement.IsNillable)
{
newElement.IsNillable = copyElement.IsNillable;
}
newElement.LineNumber = copyElement.LineNumber;
newElement.LinePosition = copyElement.LinePosition;
newElement.Name = copyElement.Name;
newElement.Namespaces = copyElement.Namespaces;
newElement.RefName = copyElement.RefName;
newElement.SchemaType = copyElement.SchemaType;
newElement.SchemaTypeName = copyElement.SchemaTypeName;
newElement.SourceUri = copyElement.SourceUri;
newElement.SubstitutionGroup = copyElement.SubstitutionGroup;
newElement.UnhandledAttributes = copyElement.UnhandledAttributes;
if (copyElement.MinOccurs != Decimal.One && this.Occurrence == InferenceOption.Relaxed)
{
newElement.MinOccurs = copyElement.MinOccurs;
}
if (copyElement.MaxOccurs != Decimal.One) {
newElement.MaxOccurs = copyElement.MaxOccurs;
}
return newElement;
}
private static int GetSchemaType(XmlQualifiedName qname)
{
if (qname == SimpleTypes[HC_ST_boolean])
{
return TF_boolean | TF_string;
}
if (qname == SimpleTypes[HC_ST_byte])
{
return TF_byte | TF_short | TF_int | TF_long | TF_integer | TF_decimal | TF_float | TF_double | TF_string;
}
if (qname == SimpleTypes[HC_ST_unsignedByte])
{
return TF_byte | TF_short | TF_int | TF_long | TF_integer | TF_decimal | TF_float | TF_double |
TF_unsignedByte | TF_unsignedShort | TF_unsignedInt | TF_unsignedLong | TF_string;
}
if (qname == SimpleTypes[HC_ST_short])
{
return TF_short | TF_int | TF_long | TF_integer | TF_decimal | TF_float | TF_double | TF_string;
}
if (qname == SimpleTypes[HC_ST_unsignedShort])
{
return TF_short | TF_int | TF_long | TF_integer | TF_decimal | TF_float | TF_double |
TF_unsignedShort | TF_unsignedInt | TF_unsignedLong | TF_string;
}
if (qname == SimpleTypes[HC_ST_int])
{
return TF_int | TF_long | TF_integer | TF_decimal | TF_float | TF_double | TF_string;
}
if (qname == SimpleTypes[HC_ST_unsignedInt])
{
return TF_int | TF_long | TF_integer | TF_decimal | TF_float | TF_double |
TF_unsignedInt | TF_unsignedLong | TF_string;
}
if (qname == SimpleTypes[HC_ST_long])
{
return TF_long | TF_integer | TF_decimal | TF_float | TF_double | TF_string;
}
if (qname == SimpleTypes[HC_ST_unsignedLong])
{
return TF_long | TF_integer | TF_decimal | TF_float | TF_double |
TF_unsignedLong | TF_string;
}
if (qname == SimpleTypes[HC_ST_integer])
{
return TF_integer |TF_decimal | TF_float | TF_double | TF_string;
}
if (qname == SimpleTypes[HC_ST_decimal])
{
return TF_decimal | TF_float | TF_double | TF_string;
}
if (qname == SimpleTypes[HC_ST_float])
{
return TF_float | TF_double | TF_string;
}
if (qname == SimpleTypes[HC_ST_double])
{
return TF_double | TF_string;
}
if (qname == SimpleTypes[HC_ST_duration])
{
return TF_duration | TF_string;
}
if (qname == SimpleTypes[HC_ST_dateTime])
{
return TF_dateTime | TF_string;
}
if (qname == SimpleTypes[HC_ST_time])
{
return TF_time | TF_string;
}
if (qname == SimpleTypes[HC_ST_date])
{
return TF_date;
}
if (qname == SimpleTypes[HC_ST_gYearMonth])
{
return TF_gYearMonth;
}
if (qname == SimpleTypes[HC_ST_string])
{
return TF_string;
}
if (qname == null || qname.IsEmpty) {
return -1;
}
throw new XmlSchemaInferenceException(Res.SchInf_schematype, 0, 0);
}
internal void SetMinMaxOccurs(XmlSchemaElement el, bool setMaxOccurs)
{
if (this.Occurrence == InferenceOption.Relaxed)
{
if (setMaxOccurs || el.MaxOccurs > 1)
{
el.MaxOccurs = decimal.MaxValue; //set it to unbounded
}
el.MinOccurs = 0;
}
else if (el.MinOccurs > 1)
{
el.MinOccurs = 1;
}
}
}
}
|