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
|
//------------------------------------------------------------------------------
// <copyright file="MgmtConfigurationRecord.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Configuration {
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration.Internal;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Xml;
using System.Runtime.Versioning;
internal sealed class MgmtConfigurationRecord : BaseConfigurationRecord {
private const int DEFAULT_INDENT = 4;
private const int MAX_INDENT = 10;
private Hashtable _sectionGroups; // ConfigurationSectionGroups that have been evaluated,
// which may or may not be defined in this web.config file.
// config key -> ConfigurationSectionGroup
private Hashtable _sectionFactories; // All inherited section declarations
// configKey -> FactoryId
private Hashtable _sectionGroupFactories; // All inherited section group declarations
// configKey -> FactoryId
private Hashtable _removedSections; // Sections that have been removed with ConfigurationSectionCollection.Remove()
// configKey -> configKey
private Hashtable _removedSectionGroups; // Section groups that have been removed with ConfigurationSectionCollection.Remove()
// configKey -> configKey
private Hashtable _locationTags; // List of all location tags encountered, even if empty
// locationSubPath -> locationSubPath
private HybridDictionary _streamInfoUpdates; // List of StreamInfo, including the main config file, the configSource this record uses, and
// new configSource stream added thru API
static internal MgmtConfigurationRecord Create(
IInternalConfigRoot configRoot,
IInternalConfigRecord parent,
string configPath,
string locationSubPath) {
MgmtConfigurationRecord configRecord = new MgmtConfigurationRecord();
configRecord.Init(configRoot, parent, configPath, locationSubPath);
return configRecord;
}
// don't allow instantiation except by Create
private MgmtConfigurationRecord() {
}
private void Init(
IInternalConfigRoot configRoot,
IInternalConfigRecord parent,
string configPath,
string locationSubPath) {
base.Init(configRoot, (BaseConfigurationRecord) parent, configPath, locationSubPath);
if ( IsLocationConfig &&
(MgmtParent._locationTags == null || !MgmtParent._locationTags.Contains(_locationSubPath))) {
// By instantiating a "new" LocationSubPath class, we have implicitly
// asked for one to be created
_flags[ForceLocationWritten] = true;
}
// Copy all stream information so that we can model changes to ConfigSource
InitStreamInfoUpdates();
}
private void InitStreamInfoUpdates() {
_streamInfoUpdates = new HybridDictionary(true);
if (ConfigStreamInfo.HasStreamInfos) {
foreach (StreamInfo streamInfo in ConfigStreamInfo.StreamInfos.Values) {
_streamInfoUpdates.Add(streamInfo.StreamName, streamInfo.Clone());
}
}
}
// The parent config record cast to this type
private MgmtConfigurationRecord MgmtParent {
get {
return(MgmtConfigurationRecord) _parent;
}
}
// The IInternalConfigHost cast to UpdateConfigHost.
private UpdateConfigHost UpdateConfigHost {
get {
return (UpdateConfigHost) Host;
}
}
// Class flags
static readonly SimpleBitVector32 MgmtClassFlags = new SimpleBitVector32(
ClassSupportsKeepInputs
| ClassIgnoreLocalErrors);
override protected SimpleBitVector32 ClassFlags {
get {
return MgmtClassFlags;
}
}
//
// Create the factory object that is used to create new instances of a ConfigurationSection.
// Our factory is a ConstructorInfo that creates the section.
//
override protected object CreateSectionFactory(FactoryRecord factoryRecord) {
// Get the type of the factory
Type type = TypeUtil.GetTypeWithReflectionPermission(Host, factoryRecord.FactoryTypeName, true);
//
// If the type is not a ConfigurationSection, use the DefaultSection if the type
// implements IConfigurationSectionHandler.
//
if (!typeof(ConfigurationSection).IsAssignableFrom(type)) {
TypeUtil.VerifyAssignableType(typeof(IConfigurationSectionHandler), type, true);
type = typeof(DefaultSection);
}
ConstructorInfo ctor = TypeUtil.GetConstructorWithReflectionPermission(type, typeof(ConfigurationSection), true);
return ctor;
}
//
// Create the ConfigurationSection.
//
override protected object CreateSection(bool inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, SectionInput sectionInput, object parentConfig, ConfigXmlReader reader) {
// Create an instance of the ConfigurationSection
ConstructorInfo ctor = (ConstructorInfo) factoryRecord.Factory;
ConfigurationSection configSection = (ConfigurationSection) TypeUtil.InvokeCtorWithReflectionPermission(ctor);
// Attach the ConfigurationSection to this record
configSection.SectionInformation.AttachToConfigurationRecord(this, factoryRecord, sectionRecord);
configSection.CallInit();
// Initialize the ConfigurationSection with XML or just its parent.
ConfigurationSection parentConfigSection = (ConfigurationSection) parentConfig;
configSection.Reset(parentConfigSection);
if (reader != null) {
configSection.DeserializeSection(reader);
}
if (sectionInput != null && sectionInput.ConfigBuilder != null) {
configSection = CallHostProcessConfigurationSection(configSection, sectionInput.ConfigBuilder);
}
// Clear the modified bit.
configSection.ResetModified();
return configSection;
}
//
// Create the type used to create new instances of a ConfigurationSectionGroup.
// Our factory is a ConstructorInfo that creates the section group.
//
private ConstructorInfo CreateSectionGroupFactory(FactoryRecord factoryRecord) {
Type type;
if (String.IsNullOrEmpty(factoryRecord.FactoryTypeName)) {
type = typeof(ConfigurationSectionGroup);
}
else {
type = TypeUtil.GetTypeWithReflectionPermission(Host, factoryRecord.FactoryTypeName, true);
}
ConstructorInfo ctor = TypeUtil.GetConstructorWithReflectionPermission(type, typeof(ConfigurationSectionGroup), true);
return ctor;
}
//
// Ensure the existence of a section group factory, and return it.
//
private ConstructorInfo EnsureSectionGroupFactory(FactoryRecord factoryRecord) {
ConstructorInfo factory = (ConstructorInfo) factoryRecord.Factory;
if (factory == null) {
factory = CreateSectionGroupFactory(factoryRecord);
factoryRecord.Factory = factory;
}
return factory;
}
//
// Create a new ConfigurationSection with the same values as the parent.
// We must use a different instance than the parent, as the parent is cached
// by the config system and the child ConfigurationSection may change due to
// user interaction.
//
override protected object UseParentResult(string configKey, object parentResult, SectionRecord sectionRecord) {
FactoryRecord factoryRecord = FindFactoryRecord(configKey, false);
if (factoryRecord == null) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_unrecognized_configuration_section, configKey));
}
object result = CallCreateSection(false, factoryRecord, sectionRecord, null, parentResult, null);
return result;
}
//
// There is no runtime object at designtime - always return the result.
//
override protected object GetRuntimeObject(object result) {
return result;
}
//
// Return the section result cast to a ConfigurationSection,
// or null if the section does not exist or has not been evaluated.
//
private ConfigurationSection GetConfigSection(string configKey) {
SectionRecord sectionRecord = GetSectionRecord(configKey, false);
if (sectionRecord != null && sectionRecord.HasResult) {
return (ConfigurationSection) sectionRecord.Result;
}
else {
return null;
}
}
//
// Return the collection of ConfigurationSectionGroups.
//
private Hashtable SectionGroups {
get {
if (_sectionGroups == null) {
_sectionGroups = new Hashtable();
}
return _sectionGroups;
}
}
//
// Return the collection of removed sections.
//
private Hashtable RemovedSections {
get {
if (_removedSections == null) {
_removedSections = new Hashtable();
}
return _removedSections;
}
}
//
// Return the collection of removed section groups.
//
private Hashtable RemovedSectionGroups {
get {
if (_removedSectionGroups == null) {
_removedSectionGroups = new Hashtable();
}
return _removedSectionGroups;
}
}
//
// Lookup a section group. Return null if it doesn't exist or hasn't been evaluated.
//
internal ConfigurationSectionGroup LookupSectionGroup(string configKey) {
ConfigurationSectionGroup configSectionGroup = null;
if (_sectionGroups != null) {
configSectionGroup = (ConfigurationSectionGroup) _sectionGroups[configKey];
}
return configSectionGroup;
}
// Returns the ConfigurationSectionGroup of the configKey.
// The ConfigurationSectionGroup is created if it doesn't exist.
// This method only returns null if a FactoryRecord does not exist for the
// desired configKey.
internal ConfigurationSectionGroup GetSectionGroup(string configKey) {
ConfigurationSectionGroup configSectionGroup = LookupSectionGroup(configKey);
if (configSectionGroup == null) {
BaseConfigurationRecord configRecord;
FactoryRecord factoryRecord = FindFactoryRecord(configKey, false, out configRecord);
if (factoryRecord == null) {
return null;
}
if (!factoryRecord.IsGroup) {
throw ExceptionUtil.ParameterInvalid("sectionGroupName");
}
if (factoryRecord.FactoryTypeName == null) {
//
// If no type is defined for the section group, return a base ConfigurationSectionGroup.
// For example:
// <configSections>
// <sectionGroup name="mySectionGroup" />
// </configSections>
//
configSectionGroup = new ConfigurationSectionGroup();
}
else {
//
// Create the section group of the desired type.
// For example:
// <configSections>
// <sectionGroup name="mySectionGroup" type="MySectionGroupType, acme" />
// </configSections>
//
ConstructorInfo ctor = EnsureSectionGroupFactory(factoryRecord);
try {
configSectionGroup = (ConfigurationSectionGroup) TypeUtil.InvokeCtorWithReflectionPermission(ctor);
}
catch (Exception e) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_exception_creating_section_handler, factoryRecord.ConfigKey),
e, factoryRecord);
}
}
configSectionGroup.AttachToConfigurationRecord(this, factoryRecord);
// Add it to the collection
SectionGroups[configKey] = configSectionGroup;
}
return configSectionGroup;
}
//
// Create a collection of all location tags encountered in the file.
//
internal ConfigurationLocationCollection GetLocationCollection(Configuration config) {
ArrayList locations = new ArrayList();
// Now add the other empty location sections we recorded
if (_locationTags != null) {
foreach (string subPath in _locationTags.Values) {
locations.Add(new ConfigurationLocation(config, subPath));
}
}
return new ConfigurationLocationCollection(locations);
}
//
// AddLocation
//
// Record all location tags in the config file, even if they are empty.
//
protected override void AddLocation(string locationSubPath) {
if (_locationTags == null) {
_locationTags = new Hashtable(StringComparer.OrdinalIgnoreCase);
}
_locationTags[locationSubPath] = locationSubPath;
}
//
// Collection of all section factories, both in this file and inherited.
//
internal Hashtable SectionFactories {
get {
if (_sectionFactories == null) {
_sectionFactories = GetAllFactories(false);
}
return _sectionFactories;
}
}
//
// Collection of all section groups, both in this file and inherited.
//
internal Hashtable SectionGroupFactories {
get {
if (_sectionGroupFactories == null) {
_sectionGroupFactories = GetAllFactories(true);
}
return _sectionGroupFactories;
}
}
//
// Get all the factories available, both in this file and inherited.
//
private Hashtable GetAllFactories(bool isGroup) {
Hashtable factories = new Hashtable();
MgmtConfigurationRecord configRecord = this;
do {
if (configRecord._factoryRecords != null) {
foreach (FactoryRecord factoryRecord in configRecord._factoryRecords.Values) {
if (factoryRecord.IsGroup == isGroup) {
string configKey = factoryRecord.ConfigKey;
factories[configKey] = new FactoryId(factoryRecord.ConfigKey, factoryRecord.Group, factoryRecord.Name);
}
}
}
configRecord = configRecord.MgmtParent;
} while (!configRecord.IsRootConfig);
return factories;
}
internal ConfigurationSection FindImmediateParentSection(ConfigurationSection section) {
ConfigurationSection result = null;
string configKey = section.SectionInformation.SectionName;
SectionRecord sectionRecord = GetSectionRecord(configKey, false);
if (sectionRecord.HasLocationInputs) {
SectionInput input = sectionRecord.LastLocationInput;
Debug.Assert(input.HasResult, "input.HasResult");
result = (ConfigurationSection) input.Result;
}
else if (sectionRecord.HasIndirectLocationInputs) {
Debug.Assert(IsLocationConfig, "Indirect location inputs exist only in location config record");
SectionInput input = sectionRecord.LastIndirectLocationInput;
Debug.Assert(input != null);
Debug.Assert(input.HasResult, "input.HasResult");
result = (ConfigurationSection) input.Result;
}
else if (IsRootDeclaration(configKey, true)) {
FactoryRecord factoryRecord = GetFactoryRecord(configKey, false);
object resultObject;
object resultRuntimeObject;
CreateSectionDefault(configKey, false, factoryRecord, null, out resultObject, out resultRuntimeObject);
result = (ConfigurationSection) resultObject;
}
else {
MgmtConfigurationRecord current = this.MgmtParent;
while (!current.IsRootConfig) {
sectionRecord = current.GetSectionRecord(configKey, false);
if (sectionRecord != null && sectionRecord.HasResult) {
result = (ConfigurationSection) sectionRecord.Result;
break;
}
current = current.MgmtParent;
}
Debug.Assert(!current.IsRootConfig, "An immediate parent result should have been found");
}
if (!result.IsReadOnly()) {
result.SetReadOnly();
}
return result;
}
//
// Get the immediate parent configuration section, and clone it.
//
internal ConfigurationSection FindAndCloneImmediateParentSection(ConfigurationSection configSection) {
string configKey = configSection.SectionInformation.ConfigKey;
ConfigurationSection parentSection = FindImmediateParentSection(configSection);
SectionRecord sectionRecord = GetSectionRecord(configKey, false);
ConfigurationSection clone = (ConfigurationSection) UseParentResult(configKey, parentSection, sectionRecord);
return clone;
}
//
// Revert the ConfigurationSection to the value of its parent.
//
internal void RevertToParent(ConfigurationSection configSection) {
// Remove any RawXml set by ConfigurationSection.SetRawXml
configSection.SectionInformation.RawXml = null;
try {
// Reset to parent value
ConfigurationSection parentConfigSection = FindImmediateParentSection(configSection);
configSection.Reset(parentConfigSection);
// Consider it to be unmodified
configSection.ResetModified();
}
catch (Exception e) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_exception_in_config_section_handler, configSection.SectionInformation.SectionName),
e, ConfigStreamInfo.StreamName, 0);
}
// Record that the section is to be removed.
configSection.SectionInformation.Removed = true;
}
//
// Return the outer XML of a section as a string.
// Return null if the section does not exist in the file.
//
internal string GetRawXml(string configKey) {
// Get the section record created during Init
SectionRecord sectionRecord = GetSectionRecord(configKey, false);
if (sectionRecord == null || !sectionRecord.HasFileInput) {
return null;
}
// The section exists, so find and return its RawXml.
string [] keys = configKey.Split(ConfigPathSeparatorParams);
ConfigXmlReader reader = GetSectionXmlReader(keys, sectionRecord.FileInput);
return reader.RawXml;
}
//
// Update the section with the XML provided.
//
// This method will throw out any changes made to the section up to this point.
//
// If xmlElement is null or empty, it is equivalent to calling RevertToParent
//
internal void SetRawXml(ConfigurationSection configSection, string xmlElement) {
// Null or empty is equivalent to RevertToParent().
if (string.IsNullOrEmpty(xmlElement)) {
RevertToParent(configSection);
return;
}
ValidateSectionXml(xmlElement, configSection.SectionInformation.Name);
// Reset the ConfigurationSection with the XML.
ConfigurationSection parentConfigSection = FindImmediateParentSection(configSection);
ConfigXmlReader reader = new ConfigXmlReader(xmlElement, null, 0);
// Store the raw XML.
configSection.SectionInformation.RawXml = xmlElement;
// Update the section with the xml
try {
try {
bool wasPresent = configSection.ElementPresent;
PropertySourceInfo saveInfo = configSection.ElementInformation.PropertyInfoInternal();
configSection.Reset(parentConfigSection);
configSection.DeserializeSection(reader);
configSection.ResetModified();
configSection.ElementPresent = wasPresent;
configSection.ElementInformation.ChangeSourceAndLineNumber(saveInfo);
}
catch {
configSection.SectionInformation.RawXml = null;
throw;
}
}
catch (Exception e) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_exception_in_config_section_handler, configSection.SectionInformation.SectionName),
e, null, 0);
}
// Ignore previous attempts to remove the section.
configSection.SectionInformation.Removed = false;
}
//
// Return true if a stream is being used by a configSource directive in other input.
//
private bool IsStreamUsed(string oldStreamName) {
MgmtConfigurationRecord current = this;
if (IsLocationConfig) {
//
// For a location configuration, the input we need to check
// are the section records and location sections in the file,
// which are available in the parent record.
//
current = MgmtParent;
//
// Check whether a file section is using the configsource directive.
//
if (current._sectionRecords != null) {
foreach (SectionRecord sectionRecord in current._sectionRecords.Values) {
if ( sectionRecord.HasFileInput &&
StringUtil.EqualsIgnoreCase(sectionRecord.FileInput.SectionXmlInfo.ConfigSourceStreamName, oldStreamName)) {
return true;
}
}
}
}
//
// Check whether a location is using the configsource directive.
//
if (current._locationSections != null) {
foreach (LocationSectionRecord locationSectionRecord in current._locationSections) {
if (StringUtil.EqualsIgnoreCase(locationSectionRecord.SectionXmlInfo.ConfigSourceStreamName, oldStreamName)) {
return true;
}
}
}
return false;
}
//
// Set the configSource attribute on a ConfigurationSection
//
internal void ChangeConfigSource(
SectionInformation sectionInformation,
string oldConfigSource,
string oldConfigSourceStreamName,
string newConfigSource) {
if (String.IsNullOrEmpty(oldConfigSource)) {
oldConfigSource = null;
}
if (String.IsNullOrEmpty(newConfigSource)) {
newConfigSource = null;
}
// Check if there is a change to config source
if (StringUtil.EqualsIgnoreCase(oldConfigSource, newConfigSource))
return;
if (String.IsNullOrEmpty(ConfigStreamInfo.StreamName)) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_source_requires_file));
}
string newConfigSourceStreamName = null;
if (newConfigSource != null) {
newConfigSourceStreamName = Host.GetStreamNameForConfigSource(ConfigStreamInfo.StreamName, newConfigSource);
}
// Add the stream to the updates
if (newConfigSourceStreamName != null) {
//
// Ensure that no parent is using the same config source stream
//
ValidateUniqueChildConfigSource(sectionInformation.ConfigKey, newConfigSourceStreamName, newConfigSource, null);
StreamInfo streamInfo = (StreamInfo) _streamInfoUpdates[newConfigSourceStreamName];
if (streamInfo != null) {
//
// Detect if another section in this file is using the same configSource
// with has a different section name.
//
if (streamInfo.SectionName != sectionInformation.ConfigKey) {
throw new ConfigurationErrorsException(
SR.GetString(SR.Config_source_cannot_be_shared, newConfigSource));
}
}
else {
//
// Add stream to updates
//
streamInfo = new StreamInfo(sectionInformation.ConfigKey, newConfigSource, newConfigSourceStreamName);
_streamInfoUpdates.Add(newConfigSourceStreamName, streamInfo);
}
}
// remove old streamname if no longer referenced
if (oldConfigSourceStreamName != null && !IsStreamUsed(oldConfigSourceStreamName)) {
_streamInfoUpdates.Remove(oldConfigSourceStreamName);
}
// update the configSourceStreamName
sectionInformation.ConfigSourceStreamName = newConfigSourceStreamName;
}
//
// Verify that the string is valid xml, begins with the expected section name,
// and contains no more or less than a single element.
//
// Throws a ConfigurationErrorsException if there is an error.
//
private void ValidateSectionXml(string xmlElement, string configKey) {
if (string.IsNullOrEmpty(xmlElement))
return;
XmlTextReader reader = null;
try {
XmlParserContext context = new XmlParserContext(null, null, null, XmlSpace.Default, Encoding.Unicode);
reader = new XmlTextReader(xmlElement, XmlNodeType.Element, context);
// Verify that the it is an element
reader.Read();
if (reader.NodeType != XmlNodeType.Element) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_unexpected_node_type, reader.NodeType));
}
// Verify the name of the element is a section
string group, name;
SplitConfigKey(configKey, out group, out name);
if (reader.Name != name) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_unexpected_element_name, reader.Name));
}
for (;;) {
if (!reader.Read()) {
// ensure there is a matching end element
if (reader.Depth != 0) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_unexpected_element_end),reader);
}
break;
}
switch (reader.NodeType) {
// disallowed node types within a section
case XmlNodeType.XmlDeclaration:
case XmlNodeType.DocumentType:
throw new ConfigurationErrorsException(SR.GetString(SR.Config_invalid_node_type),reader);
default:
break;
}
// don't allow XML after the end element
if (reader.Depth <= 0 && reader.NodeType != XmlNodeType.EndElement) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_more_data_than_expected),reader);
}
}
}
finally {
if (reader != null) {
reader.Close();
}
}
}
//
// Add a new configuration section to this config file.
// This adds both the section declaration and definition to the config file.
//
// Called from ConfigurationSectionCollection.Add().
// Note this method DOES NOT update the associated ConfigurationSectionCollection.
//
internal void AddConfigurationSection(string group, string name, ConfigurationSection configSection) {
// <configSections> is not permitted within a <location> tag.
if (IsLocationConfig) {
throw new InvalidOperationException(SR.GetString(SR.Config_add_configurationsection_in_location_config));
}
VerifySectionName(name, null, false);
if (configSection == null) {
throw new ArgumentNullException("configSection");
}
// Ensure the section is not already part of the configuration hierarchy.
if (configSection.SectionInformation.Attached) {
throw new InvalidOperationException(SR.GetString(SR.Config_add_configurationsection_already_added));
}
string configKey = BaseConfigurationRecord.CombineConfigKey(group, name);
// Ensure the section is not already declared.
FactoryRecord factoryRecord = FindFactoryRecord(configKey, true);
if (factoryRecord != null) {
throw new ArgumentException(SR.GetString(SR.Config_add_configurationsection_already_exists));
}
// Add the configSource if needed.
if (!String.IsNullOrEmpty(configSection.SectionInformation.ConfigSource)) {
ChangeConfigSource(configSection.SectionInformation, null, null, configSection.SectionInformation.ConfigSource);
}
// Add to list of all sections.
if (_sectionFactories != null) {
_sectionFactories.Add(configKey, new FactoryId(configKey, group, name));
}
// Get the type name.
string typeName = configSection.SectionInformation.Type;
if (typeName == null) {
typeName = Host.GetConfigTypeName(configSection.GetType());
}
// Add a factory record for the section.
factoryRecord = new FactoryRecord(configKey,
group,
name,
typeName,
configSection.SectionInformation.AllowLocation,
configSection.SectionInformation.AllowDefinition,
configSection.SectionInformation.AllowExeDefinition,
configSection.SectionInformation.OverrideModeDefaultSetting,
configSection.SectionInformation.RestartOnExternalChanges,
configSection.SectionInformation.RequirePermission,
_flags[IsTrusted],
false, // isUndeclared
ConfigStreamInfo.StreamName,
-1);
// Construct a factory for the section
factoryRecord.Factory = TypeUtil.GetConstructorWithReflectionPermission(
configSection.GetType(), typeof(ConfigurationSection), true);
factoryRecord.IsFactoryTrustedWithoutAptca = TypeUtil.IsTypeFromTrustedAssemblyWithoutAptca(configSection.GetType());
EnsureFactories()[configKey] = factoryRecord;
// Add a section record for the section.
// Since we are adding a new definition, it cannot be locked.
SectionRecord sectionRecord = EnsureSectionRecordUnsafe(configKey, false);
sectionRecord.Result = configSection;
sectionRecord.ResultRuntimeObject = configSection;
// Undo any previous removals of the section.
if (_removedSections != null) {
_removedSections.Remove(configKey);
}
// Attach the section to the configuration record.
configSection.SectionInformation.AttachToConfigurationRecord(this, factoryRecord, sectionRecord);
//
// If there is rawXml, set it now. Note this will override any other changes to the section
// definition made after the call to SetXml.
//
string rawXml = configSection.SectionInformation.RawXml;
if (!String.IsNullOrEmpty(rawXml)) {
configSection.SectionInformation.RawXml = null;
configSection.SectionInformation.SetRawXml(rawXml);
}
}
//
// Remove a configuration section from this config file.
// This removes both the section declaration and definition from the config file.
// Note, however, that if a parent config file declares the section,
// a new instance of the section can be retrieved having the value of the
// immediate parent.
//
// Called from ConfigurationSectionCollection.Remove().
// Note this method DOES NOT update the associated ConfigurationSectionCollection.
//
internal void RemoveConfigurationSection(string group, string name) {
bool sectionIsUsed = false; // Is section used in our record
VerifySectionName(name, null, true);
string configKey = BaseConfigurationRecord.CombineConfigKey(group, name);
// If it's already removed, don't try to remove it again.
if (RemovedSections.Contains(configKey)) {
return;
}
// If it's not a registered section, there's nothing to do.
if (FindFactoryRecord(configKey, true) == null) {
return;
}
// Detach from this record
ConfigurationSection configSection = GetConfigSection(configKey);
if (configSection != null) {
configSection.SectionInformation.DetachFromConfigurationRecord();
}
// Remove from list of all sections if this is the root declaration.
bool isRootDeclaration = IsRootDeclaration(configKey, false);
if (_sectionFactories != null && isRootDeclaration) {
_sectionFactories.Remove(configKey);
}
// Remove from collection of factory records.
if (!IsLocationConfig && _factoryRecords != null && _factoryRecords.Contains(configKey)) {
sectionIsUsed = true;
_factoryRecords.Remove(configKey);
}
// Remove from collection of section records.
if (_sectionRecords != null && _sectionRecords.Contains(configKey)) {
sectionIsUsed = true;
_sectionRecords.Remove(configKey);
}
// Remove all location section records for this section in this file.
if (_locationSections != null) {
int i = 0;
while (i < _locationSections.Count) {
LocationSectionRecord locationSectionRecord = (LocationSectionRecord) _locationSections[i];
if (locationSectionRecord.ConfigKey != configKey) {
i++;
}
else {
sectionIsUsed = true;
_locationSections.RemoveAt(i);
}
}
}
if (sectionIsUsed) {
// Add to RemovedSections since we need to remove
// it from the file later.
RemovedSections.Add(configKey, configKey);
}
//
// Remove all references from configSource
// Note that we can't remove an item while enumerating it.
//
List<string> streamsToRemove = new List<string>();
foreach (StreamInfo streamInfo in _streamInfoUpdates.Values) {
if (streamInfo.SectionName == configKey) {
streamsToRemove.Add(streamInfo.StreamName);
}
}
foreach (string stream in streamsToRemove) {
_streamInfoUpdates.Remove(stream);
}
}
//
// Add a new configuration section group to this config file.
//
// Called from ConfigurationSectionGroupCollection.Add().
// Note this method DOES NOT update the associated ConfigurationSectionGroupCollection.
//
internal void AddConfigurationSectionGroup(string group, string name, ConfigurationSectionGroup configSectionGroup) {
// <location> tags can't have a <configSections> declaration.
if (IsLocationConfig) {
throw new InvalidOperationException(SR.GetString(SR.Config_add_configurationsectiongroup_in_location_config));
}
// Validate name argument.
VerifySectionName(name, null, false);
// Validate configSectionGroup argument.
if (configSectionGroup == null) {
throw ExceptionUtil.ParameterInvalid("name");
}
// A section group can only belong to one section group collection.
if (configSectionGroup.Attached) {
throw new InvalidOperationException(SR.GetString(SR.Config_add_configurationsectiongroup_already_added));
}
string configKey = BaseConfigurationRecord.CombineConfigKey(group, name);
// Do not add if the section group already exists, even if it is of a different type.
FactoryRecord factoryRecord = FindFactoryRecord(configKey, true);
if (factoryRecord != null) {
throw new ArgumentException(SR.GetString(SR.Config_add_configurationsectiongroup_already_exists));
}
// Add to list of all section groups.
if (_sectionGroupFactories != null) {
_sectionGroupFactories.Add(configKey, new FactoryId(configKey, group, name));
}
// Get the type name - if it is not specified explicitly, get it from the type of the object.
string typeName = configSectionGroup.Type;
if (typeName == null) {
typeName = Host.GetConfigTypeName(configSectionGroup.GetType());
}
// Create a factory record and add it to the collection of factory records.
factoryRecord = new FactoryRecord(configKey, group, name, typeName, ConfigStreamInfo.StreamName, -1);
EnsureFactories()[configKey] = factoryRecord;
// Add it to list of evaluated configuration section groups.
SectionGroups[configKey] = configSectionGroup;
// Remove it from RemovedSectionGroups if it was previously removed.
if (_removedSectionGroups != null) {
_removedSectionGroups.Remove(configKey);
}
// Attach to the configuration record.
configSectionGroup.AttachToConfigurationRecord(this, factoryRecord);
}
//
// Return a list of all FactoryRecords of sections that are descendents of
// a section group.
//
private ArrayList GetDescendentSectionFactories(string configKey) {
ArrayList sectionGroups = new ArrayList();
string configKeyAncestor;
if (configKey.Length == 0) {
configKeyAncestor = string.Empty;
}
else {
configKeyAncestor = configKey + "/";
}
foreach (FactoryId factoryId in SectionFactories.Values) {
if (factoryId.Group == configKey || StringUtil.StartsWith(factoryId.Group, configKeyAncestor)) {
sectionGroups.Add(factoryId);
}
}
return sectionGroups;
}
//
// Return a list of all FactoryRecords of section groups that are descendents of
// a section group, including the section group itself.
//
private ArrayList GetDescendentSectionGroupFactories(string configKey) {
ArrayList sectionGroups = new ArrayList();
string configKeyAncestor;
if (configKey.Length == 0) {
configKeyAncestor = string.Empty;
}
else {
configKeyAncestor = configKey + "/";
}
foreach (FactoryId factoryId in SectionGroupFactories.Values) {
if (factoryId.ConfigKey == configKey || StringUtil.StartsWith(factoryId.ConfigKey, configKeyAncestor)) {
sectionGroups.Add(factoryId);
}
}
return sectionGroups;
}
//
// Remove a configuration section group from this config file.
// This removes both the section group declaration and definition from the config file,
// along with all descendent groups and sections.
//
// Note, however, that if a parent config file declares the section group,
// a new instance of the section can be retrieved having the value of the
// immediate parent.
//
// Called from ConfigurationSectionGroupCollection.Remove().
// Note this method DOES NOT update the associated ConfigurationSectionCollection.
//
internal void RemoveConfigurationSectionGroup(string group, string name) {
// Validate arguments
VerifySectionName(name, null, false);
string configKey = BaseConfigurationRecord.CombineConfigKey(group, name);
// If it's not a registered section, there's nothing to do.
if (FindFactoryRecord(configKey, true) == null) {
return;
}
// Remove all descendent sections.
ArrayList sections = GetDescendentSectionFactories(configKey);
foreach (FactoryId descendent in sections) {
RemoveConfigurationSection(descendent.Group, descendent.Name);
}
// Remove all descendent sections groups, including the configKey group.
ArrayList sectionGroups = GetDescendentSectionGroupFactories(configKey);
foreach (FactoryId descendent in sectionGroups) {
//
// If it's already removed, don't try to remove it again.
// We don't do this test above the loop for configKey, because
// the section groups contained within the section group may
// be changed by the user once added.
//
if (RemovedSectionGroups.Contains(descendent.ConfigKey)) {
continue;
}
// If the section group has been evaluated, detatch it.
ConfigurationSectionGroup sectionGroup = LookupSectionGroup(descendent.ConfigKey);
if (sectionGroup != null) {
sectionGroup.DetachFromConfigurationRecord();
}
// Remove from list of all section group factories if this is the root declaration.
bool isRootDeclaration = IsRootDeclaration(descendent.ConfigKey, false);
if (_sectionGroupFactories != null && isRootDeclaration) {
_sectionGroupFactories.Remove(descendent.ConfigKey);
}
// Remove from list of factory records.
if (!IsLocationConfig && _factoryRecords != null) {
_factoryRecords.Remove(descendent.ConfigKey);
}
// Remove from evaluated section groups.
if (_sectionGroups != null) {
_sectionGroups.Remove(descendent.ConfigKey);
}
//
// Add to list of section groups that are removed
// Note that this will add section groups that might not be used
// in this config file. That just results in some extra work during
// save, it is not harmful.
//
RemovedSectionGroups.Add(descendent.ConfigKey, descendent.ConfigKey);
}
}
//
// Return the file path to this configuration file.
//
internal string ConfigurationFilePath {
get {
string filepath = UpdateConfigHost.GetNewStreamname(ConfigStreamInfo.StreamName);
if (filepath == null) {
filepath = String.Empty;
}
if (!String.IsNullOrEmpty(filepath)) {
new FileIOPermission(FileIOPermissionAccess.PathDiscovery, filepath).Demand();
}
return filepath;
}
}
//
// Update the config file with the changes in each ConfigurationSection
//
internal void SaveAs(string filename, ConfigurationSaveMode saveMode, bool forceUpdateAll) {
// Get the updates.
SectionUpdates declarationUpdates = GetConfigDeclarationUpdates(saveMode, forceUpdateAll);
ConfigDefinitionUpdates definitionUpdates;
ArrayList configSourceUpdates;
bool checkedConfigForUpdates = false;
bool requireUpdates = (filename != null);
GetConfigDefinitionUpdates(requireUpdates, saveMode, forceUpdateAll, out definitionUpdates, out configSourceUpdates);
if (filename != null) {
Debug.Assert(filename.Length > 0, "The caller should make sure that filename is not empty");
//
// Verify that the filename is not being used.
//
// Note that if we are using a remote host, all the streamName's in _streamInfoUpdates
// are actually fullpaths on the remote machine. In this case there is no way to
// detect if we have a conflict or not.
if (!Host.IsRemote && _streamInfoUpdates.Contains(filename)) {
throw new ArgumentException(SR.GetString(SR.Filename_in_SaveAs_is_used_already, filename));
}
//
// If there was no config file for this config record,
// record the new stream name and version.
//
if (String.IsNullOrEmpty(ConfigStreamInfo.StreamName)) {
StreamInfo streamInfo = new StreamInfo(null, null, filename);
_streamInfoUpdates.Add(filename, streamInfo);
ConfigStreamInfo.StreamName = filename;
ConfigStreamInfo.StreamVersion = MonitorStream(null, null, ConfigStreamInfo.StreamName);
}
//
// Update the host to redirect filenames
//
UpdateConfigHost.AddStreamname(ConfigStreamInfo.StreamName, filename, Host.IsRemote);
// Redirect also all configSource filenames
foreach (StreamInfo streamInfo in _streamInfoUpdates.Values) {
if (!String.IsNullOrEmpty(streamInfo.SectionName)) {
// Get the new configSource streamName based on the new filename path
string newStreamName = InternalConfigHost.StaticGetStreamNameForConfigSource(
filename, streamInfo.ConfigSource);
// Ask UpdateConfigHost to intercept them.
UpdateConfigHost.AddStreamname(streamInfo.StreamName, newStreamName, Host.IsRemote);
}
}
}
if (!requireUpdates) {
// Check if there are any updates needed for the
// configuration record itself.
requireUpdates = RecordItselfRequiresUpdates;
}
if (declarationUpdates != null || definitionUpdates != null || requireUpdates) {
// Copy the input stream before opening the output stream.
byte[] readBuffer = null;
Encoding encoding = null;
if (ConfigStreamInfo.HasStream) {
using (Stream streamRead = Host.OpenStreamForRead(ConfigStreamInfo.StreamName)) {
if (streamRead == null) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_file_has_changed), ConfigStreamInfo.StreamName, 0);
}
readBuffer = new byte[streamRead.Length];
int count = streamRead.Read(readBuffer, 0, (int) streamRead.Length);
if (count != streamRead.Length) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_data_read_count_mismatch));
}
}
// Read the first byte so that we can determine the encoding.
try {
using (StreamReader reader = new StreamReader(ConfigStreamInfo.StreamName)) {
if (reader.Peek() >= 0) {
reader.Read();
}
// Dev10 bug 687017 - Handle only UTF-16 explicitly, so that handling of other
// encodings are not affected.
if (reader.CurrentEncoding is UnicodeEncoding) {
encoding = reader.CurrentEncoding;
}
}
}
catch {
// Ignore any errors, encoding will remain null.
}
}
string changedStreamName = FindChangedConfigurationStream();
if (changedStreamName != null) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_file_has_changed), changedStreamName, 0);
}
checkedConfigForUpdates = true;
// Write the changes to the output stream.
object writeContext = null;
bool streamOpened = false;
try {
try {
using (Stream streamWrite = Host.OpenStreamForWrite(ConfigStreamInfo.StreamName, null, ref writeContext)) {
streamOpened = true;
// Use the default StreamWriter constructor if encoding is null,
// otherwise specify the encoding.
using (StreamWriter streamWriter = encoding == null ? new StreamWriter(streamWrite) : new StreamWriter(streamWrite, encoding)) {
XmlUtilWriter utilWriter = new XmlUtilWriter(streamWriter, true);
if (ConfigStreamInfo.HasStream) {
CopyConfig(declarationUpdates, definitionUpdates, readBuffer,
ConfigStreamInfo.StreamName, NamespaceChangeNeeded, utilWriter);
}
else {
CreateNewConfig(declarationUpdates, definitionUpdates, NamespaceChangeNeeded, utilWriter);
}
}
}
}
catch {
if (streamOpened) {
Host.WriteCompleted(ConfigStreamInfo.StreamName, false, writeContext);
}
throw;
}
}
//
// Guarantee that exceptions contain at least the name of the stream by wrapping them
// in a ConfigurationException.
//
catch (Exception e) {
throw ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_error_loading_XML_file), e, ConfigStreamInfo.StreamName, 0);
}
Host.WriteCompleted(ConfigStreamInfo.StreamName, true, writeContext);
// Update stream information for the config file
ConfigStreamInfo.HasStream = true;
ConfigStreamInfo.ClearStreamInfos();
ConfigStreamInfo.StreamVersion = MonitorStream(null, null, ConfigStreamInfo.StreamName);
}
if (configSourceUpdates != null) {
// If we haven't checked before, check now
if (!checkedConfigForUpdates) {
string changedStreamName = FindChangedConfigurationStream();
if (changedStreamName != null) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_file_has_changed), changedStreamName, 0);
}
}
// write updates
foreach (DefinitionUpdate update in configSourceUpdates) {
SaveConfigSource(update);
}
}
// Update state to reflect the changes to the config file
UpdateRecords();
}
private bool AreDeclarationAttributesModified(FactoryRecord factoryRecord, ConfigurationSection configSection) {
return factoryRecord.FactoryTypeName != configSection.SectionInformation.Type
|| factoryRecord.AllowLocation != configSection.SectionInformation.AllowLocation
|| factoryRecord.RestartOnExternalChanges != configSection.SectionInformation.RestartOnExternalChanges
|| factoryRecord.RequirePermission != configSection.SectionInformation.RequirePermission
|| factoryRecord.AllowDefinition != configSection.SectionInformation.AllowDefinition
|| factoryRecord.AllowExeDefinition != configSection.SectionInformation.AllowExeDefinition
|| factoryRecord.OverrideModeDefault.OverrideMode != configSection.SectionInformation.OverrideModeDefaultSetting.OverrideMode // Compare the value only
|| configSection.SectionInformation.IsModifiedFlags();
}
private void AppendAttribute(StringBuilder sb, string key, string value) {
sb.Append(key);
sb.Append("=\"");
sb.Append(value);
sb.Append("\" ");
}
private string GetUpdatedSectionDeclarationXml(FactoryRecord factoryRecord, ConfigurationSection configSection, ConfigurationSaveMode saveMode) {
StringBuilder sb = new StringBuilder();
sb.Append('<');
sb.Append(KEYWORD_SECTION);
sb.Append(' ');
string type = (configSection.SectionInformation.Type != null) ? configSection.SectionInformation.Type : factoryRecord.FactoryTypeName;
if (TypeStringTransformerIsSet)
type = TypeStringTransformer(type);
AppendAttribute(sb, KEYWORD_SECTION_NAME, configSection.SectionInformation.Name);
AppendAttribute(sb, KEYWORD_SECTION_TYPE, type);
if ( !configSection.SectionInformation.AllowLocation ||
(saveMode == ConfigurationSaveMode.Full) ||
((saveMode == ConfigurationSaveMode.Modified) &&
configSection.SectionInformation.AllowLocationModified)) {
AppendAttribute(sb,
KEYWORD_SECTION_ALLOWLOCATION,
configSection.SectionInformation.AllowLocation ?
KEYWORD_TRUE :
KEYWORD_FALSE);
}
if ((configSection.SectionInformation.AllowDefinition != ConfigurationAllowDefinition.Everywhere) ||
(saveMode == ConfigurationSaveMode.Full) ||
(saveMode == ConfigurationSaveMode.Modified && configSection.SectionInformation.AllowDefinitionModified)) {
string v = null;
switch (configSection.SectionInformation.AllowDefinition) {
case ConfigurationAllowDefinition.Everywhere:
v = KEYWORD_SECTION_ALLOWDEFINITION_EVERYWHERE;
break;
case ConfigurationAllowDefinition.MachineOnly:
v = KEYWORD_SECTION_ALLOWDEFINITION_MACHINEONLY;
break;
case ConfigurationAllowDefinition.MachineToWebRoot:
v = KEYWORD_SECTION_ALLOWDEFINITION_MACHINETOWEBROOT;
break;
case ConfigurationAllowDefinition.MachineToApplication:
v = KEYWORD_SECTION_ALLOWDEFINITION_MACHINETOAPPLICATION;
break;
}
AppendAttribute(sb, KEYWORD_SECTION_ALLOWDEFINITION, v);
}
if ((configSection.SectionInformation.AllowExeDefinition !=
ConfigurationAllowExeDefinition.MachineToApplication ) ||
(saveMode == ConfigurationSaveMode.Full) ||
(saveMode == ConfigurationSaveMode.Modified && configSection.SectionInformation.AllowExeDefinitionModified)) {
AppendAttribute( sb,
KEYWORD_SECTION_ALLOWEXEDEFINITION,
ExeDefinitionToString(
configSection.SectionInformation.AllowExeDefinition )
);
}
if ( (configSection.SectionInformation.OverrideModeDefaultSetting.IsDefaultForSection == false) ||
(saveMode == ConfigurationSaveMode.Full) ||
(saveMode == ConfigurationSaveMode.Modified && configSection.SectionInformation.OverrideModeDefaultModified)) {
AppendAttribute( sb,
KEYWORD_SECTION_OVERRIDEMODEDEFAULT,
configSection.SectionInformation.OverrideModeDefaultSetting.OverrideModeXmlValue);
}
if (!configSection.SectionInformation.RestartOnExternalChanges) {
AppendAttribute(sb, KEYWORD_SECTION_RESTARTONEXTERNALCHANGES, KEYWORD_FALSE);
}
else if ((saveMode == ConfigurationSaveMode.Full) ||
(saveMode == ConfigurationSaveMode.Modified && configSection.SectionInformation.RestartOnExternalChangesModified)) {
AppendAttribute(sb, KEYWORD_SECTION_RESTARTONEXTERNALCHANGES, KEYWORD_TRUE);
}
if (!configSection.SectionInformation.RequirePermission) {
AppendAttribute(sb, KEYWORD_SECTION_REQUIREPERMISSION, KEYWORD_FALSE);
}
else if ((saveMode == ConfigurationSaveMode.Full) ||
(saveMode == ConfigurationSaveMode.Modified && configSection.SectionInformation.RequirePermissionModified)) {
AppendAttribute(sb, KEYWORD_SECTION_REQUIREPERMISSION, KEYWORD_TRUE);
}
sb.Append("/>");
return sb.ToString();
}
// ExeDefinitionToString
//
// Take an ExeDefinition and translate it to a string
//
private string ExeDefinitionToString(
ConfigurationAllowExeDefinition allowDefinition ) {
switch (allowDefinition) {
case ConfigurationAllowExeDefinition.MachineOnly:
return KEYWORD_SECTION_ALLOWDEFINITION_MACHINEONLY;
case ConfigurationAllowExeDefinition.MachineToApplication:
return KEYWORD_SECTION_ALLOWDEFINITION_MACHINETOAPPLICATION;
case ConfigurationAllowExeDefinition.MachineToRoamingUser:
return KEYWORD_SECTION_ALLOWEXEDEFINITION_MACHTOROAMING;
case ConfigurationAllowExeDefinition.MachineToLocalUser:
return KEYWORD_SECTION_ALLOWEXEDEFINITION_MACHTOLOCAL;
}
throw ExceptionUtil.PropertyInvalid("AllowExeDefinition");
}
private string GetUpdatedSectionGroupDeclarationXml(FactoryRecord factoryRecord, ConfigurationSectionGroup configSectionGroup) {
if (TargetFramework != null && !configSectionGroup.ShouldSerializeSectionGroupInTargetVersion(TargetFramework))
return null;
StringBuilder sb = new StringBuilder();
sb.Append('<');
sb.Append(KEYWORD_SECTIONGROUP);
sb.Append(' ');
AppendAttribute(sb, KEYWORD_SECTIONGROUP_NAME, configSectionGroup.Name);
string type = (configSectionGroup.Type != null) ? configSectionGroup.Type : factoryRecord.FactoryTypeName;
if (TypeStringTransformerIsSet)
type = TypeStringTransformer(type);
AppendAttribute(sb, KEYWORD_SECTIONGROUP_TYPE, type);
sb.Append('>');
return sb.ToString();
}
private bool HasRemovedSectionsOrGroups {
get {
return(_removedSections != null && _removedSections.Count > 0)
|| (_removedSectionGroups != null && _removedSectionGroups.Count > 0);
}
}
// HasRemovedSections
//
// Does this MgmtConfigrationRecord have any sections to be removed?
//
private bool HasRemovedSections {
get {
return( ( _removedSections != null ) &&
( _removedSections.Count > 0 ) );
}
}
// Gather all the updates to the configuration section declarations.
private SectionUpdates GetConfigDeclarationUpdates(ConfigurationSaveMode saveMode, bool forceUpdateAll) {
if (IsLocationConfig)
return null;
// hasChanged will be set to true if there is any change that will impact the current config file.
bool hasChanged = HasRemovedSectionsOrGroups;
SectionUpdates sectionUpdates = new SectionUpdates(string.Empty);
if (_factoryRecords != null) {
foreach (FactoryRecord factoryRecord in _factoryRecords.Values) {
if (!factoryRecord.IsGroup) {
string updatedXml = null;
// Never write out an undeclared section.
if (factoryRecord.IsUndeclared) {
continue;
}
// Note that GetConfigSection will return only those sections that have a sectionRecord
// and has a result. In another word, only sections that have been accessed.
ConfigurationSection configSection = GetConfigSection(factoryRecord.ConfigKey);
if (configSection != null) {
// We should skip this section declaration only if all below hold true:
// 1. The section should not be declared at this level. Reasons:
// i. The section is originally not declared at this level, or
// ii. The user calls SectionInformation.ForceDeclaration(false)
// 2. It's not machine.config. Otherwise we must declare it even if the user called ForceDeclaration(false)
// 3. It's already declared higher up.
// 4. It's not valid in the current Target Framework version
if (!configSection.SectionInformation.IsDeclared
&& !MgmtParent.IsRootConfig
&& MgmtParent.FindFactoryRecord(factoryRecord.ConfigKey, false) != null) {
if (factoryRecord.HasFile) {
hasChanged = true;
}
continue;
}
if (TargetFramework != null &&
!configSection.ShouldSerializeSectionInTargetVersion(TargetFramework))
{
continue;
}
if (AreDeclarationAttributesModified(factoryRecord, configSection) || !factoryRecord.HasFile) {
updatedXml = GetUpdatedSectionDeclarationXml(factoryRecord, configSection, saveMode);
if (!string.IsNullOrEmpty(updatedXml))
hasChanged = true;
}
}
DeclarationUpdate update = new DeclarationUpdate(factoryRecord.ConfigKey, !factoryRecord.HasFile, updatedXml);
sectionUpdates.AddSection(update);
}
else {
bool addGroupUpdate = false;
// LookupSectionGroup will return an object only if the group has been accessed
ConfigurationSectionGroup configSectionGroup = LookupSectionGroup(factoryRecord.ConfigKey);
if (!factoryRecord.HasFile) {
// Not in the file, so it means the group is added programmatically.
addGroupUpdate = true;
}
else if (configSectionGroup != null && configSectionGroup.IsDeclarationRequired) {
// The section group is declared in this config file
addGroupUpdate = true;
}
else if (factoryRecord.FactoryTypeName != null || configSectionGroup != null) {
FactoryRecord parentFactoryRecord = null;
if (!MgmtParent.IsRootConfig) {
parentFactoryRecord = MgmtParent.FindFactoryRecord(factoryRecord.ConfigKey, false);
}
// Add it if declaration is required. Please note this check is identical to the check
// for _declarationRequired in ConfigurationSectionGroup.AttachToConfigurationRecord.
addGroupUpdate = (parentFactoryRecord == null || parentFactoryRecord.FactoryTypeName == null);
}
if (addGroupUpdate) {
string updatedXml = null;
if (!factoryRecord.HasFile
|| (configSectionGroup != null && configSectionGroup.Type != factoryRecord.FactoryTypeName)) {
updatedXml = GetUpdatedSectionGroupDeclarationXml(factoryRecord, configSectionGroup);
if (!string.IsNullOrEmpty(updatedXml)) {
hasChanged = true;
}
}
Debug.Assert(!factoryRecord.IsUndeclared, "!factoryRecord.IsUndeclared");
Debug.Assert(!IsImplicitSection(factoryRecord.ConfigKey), "We should never write out an implicit section");
DeclarationUpdate update = new DeclarationUpdate(factoryRecord.ConfigKey, !factoryRecord.HasFile, updatedXml);
sectionUpdates.AddSectionGroup(update);
}
}
}
}
if (_sectionRecords != null) {
foreach (SectionRecord sectionRecord in _sectionRecords.Values) {
if (GetFactoryRecord(sectionRecord.ConfigKey, false) != null || !sectionRecord.HasResult) {
// Skip because this factory is defined locally ( in
// which case we handled above), or it was not used
continue;
}
ConfigurationSection configSection = (ConfigurationSection) sectionRecord.Result;
FactoryRecord factoryRecord = MgmtParent.FindFactoryRecord(sectionRecord.ConfigKey, false);
// Add this section declaration if:
// 1. The section is not declared locally (otherwise it's handled above)
// 2. SectionInformation.IsDeclared is true (i.e. user called SectionInformation.ForceDeclaration(true))
if (configSection.SectionInformation.IsDeclared) {
Debug.Assert(!IsImplicitSection(sectionRecord.ConfigKey), "We should never write out an implicit section");
Debug.Assert(!factoryRecord.IsUndeclared, "!factoryRecord.IsUndeclared");
string updatedXml = GetUpdatedSectionDeclarationXml(factoryRecord, configSection, saveMode);
if (!string.IsNullOrEmpty(updatedXml)) {
hasChanged = true;
DeclarationUpdate update = new DeclarationUpdate(factoryRecord.ConfigKey, true, updatedXml);
sectionUpdates.AddSection(update);
}
}
}
}
if (_sectionGroups != null) {
foreach (ConfigurationSectionGroup configSectionGroup in _sectionGroups.Values) {
if (GetFactoryRecord(configSectionGroup.SectionGroupName, false) != null) {
continue;
}
FactoryRecord factoryRecord = MgmtParent.FindFactoryRecord(configSectionGroup.SectionGroupName, false);
if ( configSectionGroup.IsDeclared ||
(factoryRecord != null && configSectionGroup.Type != factoryRecord.FactoryTypeName)) {
string updatedXml = GetUpdatedSectionGroupDeclarationXml(factoryRecord, configSectionGroup);
if (!string.IsNullOrEmpty(updatedXml)) {
hasChanged = true;
DeclarationUpdate update = new DeclarationUpdate(factoryRecord.ConfigKey, true, updatedXml);
sectionUpdates.AddSectionGroup(update);
}
}
}
}
if (hasChanged) {
return sectionUpdates;
}
else {
return null;
}
}
private bool AreLocationAttributesModified(SectionRecord sectionRecord, ConfigurationSection configSection) {
OverrideModeSetting overrideMode = OverrideModeSetting.LocationDefault;
bool inheritInChildApplications = true;
if (sectionRecord.HasFileInput) {
SectionXmlInfo sectionXmlInfo = sectionRecord.FileInput.SectionXmlInfo;
overrideMode = sectionXmlInfo.OverrideModeSetting;
inheritInChildApplications = !sectionXmlInfo.SkipInChildApps;
}
// We will use IsSameForLocation tag so that we flag modes that cant go into the same location tag
// as different. If we don't do that it will appear like the mode was not changed which will
// case conflict later when determining if the section is moved ( when writing the new config updates )
return
(!OverrideModeSetting.CanUseSameLocationTag(overrideMode, configSection.SectionInformation.OverrideModeSetting))
|| (inheritInChildApplications != configSection.SectionInformation.InheritInChildApplications);
}
private bool AreSectionAttributesModified(SectionRecord sectionRecord, ConfigurationSection configSection) {
string configSource;
string protectionProviderName;
string configBuilderName;
if (sectionRecord.HasFileInput) {
SectionXmlInfo sectionXmlInfo = sectionRecord.FileInput.SectionXmlInfo;
configSource = sectionXmlInfo.ConfigSource;
protectionProviderName = sectionXmlInfo.ProtectionProviderName;
configBuilderName = sectionXmlInfo.ConfigBuilderName;
}
else {
configSource = null;
protectionProviderName = null;
configBuilderName = null;
}
return
!StringUtil.EqualsNE(configSource, configSection.SectionInformation.ConfigSource)
|| !StringUtil.EqualsNE(protectionProviderName, configSection.SectionInformation.ProtectionProviderName)
|| !StringUtil.EqualsNE(configBuilderName, configSection.SectionInformation.ConfigBuilderName)
|| AreLocationAttributesModified(sectionRecord, configSection);
}
private bool IsConfigSectionMoved(SectionRecord sectionRecord, ConfigurationSection configSection) {
if (!sectionRecord.HasFileInput)
return true;
return AreLocationAttributesModified(sectionRecord, configSection);
}
// Gather all the updates to the configuration section definitions.
private void GetConfigDefinitionUpdates(
bool requireUpdates, ConfigurationSaveMode saveMode, bool forceSaveAll,
out ConfigDefinitionUpdates definitionUpdates, out ArrayList configSourceUpdates) {
definitionUpdates = new ConfigDefinitionUpdates();
configSourceUpdates = null;
bool hasChanged = HasRemovedSections;
// Loop through all the section records.
if (_sectionRecords != null) {
InitProtectedConfigurationSection(); // Make sure we have the initialized the protected config section, otherwise the foreach loop may ---- up
foreach (DictionaryEntry de in _sectionRecords) {
string configKey = (string)de.Key;
SectionRecord sectionRecord = (SectionRecord) de.Value;
sectionRecord.AddUpdate = false;
bool addUpdate = sectionRecord.HasFileInput; // If true, add this section to definitionUpdates, and optinally to configSourceUpdates
OverrideModeSetting overrideMode = OverrideModeSetting.LocationDefault;
bool inheritInChildApplications = true;
bool moved = false;
string updatedXml = null;
bool addToConfigSourceUpdates = false; // If true, we have to update the external config file for this section
if (!sectionRecord.HasResult) {
if (sectionRecord.HasFileInput) {
SectionXmlInfo sectionXmlInfo = sectionRecord.FileInput.SectionXmlInfo;
overrideMode = sectionXmlInfo.OverrideModeSetting;
inheritInChildApplications = !sectionXmlInfo.SkipInChildApps;
addToConfigSourceUpdates = requireUpdates && !String.IsNullOrEmpty(sectionXmlInfo.ConfigSource);
}
}
else {
ConfigurationSection configSection = (ConfigurationSection) sectionRecord.Result;
if (TargetFramework != null && !configSection.ShouldSerializeSectionInTargetVersion(TargetFramework))
continue;
overrideMode = configSection.SectionInformation.OverrideModeSetting;
inheritInChildApplications = configSection.SectionInformation.InheritInChildApplications;
// it is an error to require a location section when the type doesn't allow locations.
if (!configSection.SectionInformation.AllowLocation && (!overrideMode.IsDefaultForLocationTag || !inheritInChildApplications)) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_inconsistent_location_attributes, configKey));
}
addToConfigSourceUpdates = requireUpdates && !String.IsNullOrEmpty(configSection.SectionInformation.ConfigSource);
try {
bool isModified = configSection.SectionInformation.ForceSave ||
configSection.IsModified() ||
(forceSaveAll && !configSection.SectionInformation.IsLocked);
bool sectionAttributesModified = AreSectionAttributesModified(sectionRecord, configSection);
bool sectionContentModified = (isModified || configSection.SectionInformation.RawXml != null);
// Get the updated XML if the section has been modified.
if (sectionContentModified || sectionAttributesModified) {
configSection.SectionInformation.VerifyIsEditable();
configSection.SectionInformation.Removed = false;
addUpdate = true;
moved = IsConfigSectionMoved(sectionRecord, configSection);
if (!addToConfigSourceUpdates) {
addToConfigSourceUpdates =
!String.IsNullOrEmpty(configSection.SectionInformation.ConfigSource)
&& (sectionContentModified || configSection.SectionInformation.ConfigSourceModified);
}
if ( isModified ||
configSection.SectionInformation.RawXml == null ||
saveMode == ConfigurationSaveMode.Full) {
// Note: we won't use RawXml if saveMode == Full because Full means we want to
// write all properties, and RawXml may not have all properties.
// 'System.Windows.Forms.ApplicationConfiguration' is declared as an implicit section. Saving this section definition without declaration in machine .config file is causing IIS
// and "wcfservice' project creation failed. See bug #297811 for more details. Following fix exclude this section empty definition in the machine.config while saving it.
if (String.Equals(configSection.SectionInformation.Name, ConfigurationStringConstants.WinformsApplicationConfigurationSectionName, StringComparison.Ordinal) &&
String.Equals(configSection._configRecord.ConfigPath, ClientConfigurationHost.MachineConfigName, StringComparison.Ordinal)) {
updatedXml = null;
}
else {
ConfigurationSection parentConfigSection = FindImmediateParentSection(configSection);
updatedXml = configSection.SerializeSection(parentConfigSection, configSection.SectionInformation.Name, saveMode);
}
ValidateSectionXml(updatedXml, configKey);
}
else {
updatedXml = configSection.SectionInformation.RawXml;
}
if (string.IsNullOrEmpty(updatedXml)) {
//
// We always need to emit a section, even if empty, when:
// * The section has configSoure
// * The section is in a location section that has non-default attributes
// * The section is encrypted.
//
if ( !String.IsNullOrEmpty(configSection.SectionInformation.ConfigSource) ||
!configSection.SectionInformation.LocationAttributesAreDefault ||
(configSection.SectionInformation.ProtectionProvider != null)) {
updatedXml = WriteEmptyElement(configSection.SectionInformation.Name);
}
}
if (string.IsNullOrEmpty(updatedXml)) {
configSection.SectionInformation.Removed = true;
// configSection.ElementPresent = false;
updatedXml = null;
addUpdate = false;
if (sectionRecord.HasFileInput) {
hasChanged = true;
// VSWhidbey 580658: When a section is to be removed, its corresponding file
// input should be cleared as well so this section will be indicated as "moved"
// next time something is added back to the section. Without marking it as "moved",
// adding new content to a removed section fails as the bug describes.
sectionRecord.RemoveFileInput();
}
}
else {
// configSection.ElementPresent = true;
if (sectionAttributesModified || moved || String.IsNullOrEmpty(configSection.SectionInformation.ConfigSource)) {
hasChanged = true;
}
// Encrypt if required.
if (configSection.SectionInformation.ProtectionProvider != null) {
ProtectedConfigurationSection protectedConfig = GetSection(BaseConfigurationRecord.RESERVED_SECTION_PROTECTED_CONFIGURATION) as ProtectedConfigurationSection;
try {
string encryptedSection = Host.EncryptSection(updatedXml, configSection.SectionInformation.ProtectionProvider, protectedConfig);
// VsWhidbey 495120: The config host is responsible for encrypting a section, but it is the job of
// System.Configuration to format an encrypted section during write (and to detect an encrypted section during read.)
updatedXml = ProtectedConfigurationSection.FormatEncryptedSection(encryptedSection, configSection.SectionInformation.Name, configSection.SectionInformation.ProtectionProvider.Name);
}
catch (Exception e) {
throw new ConfigurationErrorsException(
SR.GetString(SR.Encryption_failed, configSection.SectionInformation.SectionName, configSection.SectionInformation.ProtectionProvider.Name, e.Message),
e);
}
}
}
}
else if (configSection.SectionInformation.Removed) {
addUpdate = false;
if (sectionRecord.HasFileInput) {
hasChanged = true;
}
}
}
catch (Exception e) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_exception_in_config_section_handler, configSection.SectionInformation.SectionName), e);
}
}
if (addUpdate) {
// Make sure we are not addingh a definition of a locked section
if (GetSectionLockedMode(sectionRecord.ConfigKey) == OverrideMode.Deny) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_section_locked), (IConfigErrorInfo)(null));
}
sectionRecord.AddUpdate = true;
DefinitionUpdate definitionUpdate = definitionUpdates.AddUpdate(overrideMode, inheritInChildApplications, moved, updatedXml, sectionRecord);
if (addToConfigSourceUpdates) {
if (configSourceUpdates == null) {
configSourceUpdates = new ArrayList();
}
configSourceUpdates.Add(definitionUpdate);
}
}
}
}
if (_flags[ ForceLocationWritten ]) {
// We must write the location tag
hasChanged = true;
definitionUpdates.RequireLocation = true;
}
if (_flags[ SuggestLocationRemoval ]) {
// We should try to remove location
hasChanged = true;
}
if (hasChanged) {
definitionUpdates.CompleteUpdates();
}
else {
definitionUpdates = null;
}
}
//
// WriteEmptyElement
//
// Take a element name, and create an xml string that contains
// that element in an empty state
//
private string WriteEmptyElement( string ElementName ) {
StringBuilder sb;
sb = new StringBuilder();
// Create element
sb.Append('<');
sb.Append(ElementName);
sb.Append(" />");
return sb.ToString();
}
// After the config file has been written out, update the section records
// to reflect changes that were made in the config file.
private void UpdateRecords() {
if (_factoryRecords != null) {
foreach (FactoryRecord factoryRecord in _factoryRecords.Values) {
// Update stream information
if (String.IsNullOrEmpty(factoryRecord.Filename)) {
factoryRecord.Filename = ConfigStreamInfo.StreamName;
}
factoryRecord.LineNumber = 0;
ConfigurationSection configSection = GetConfigSection(factoryRecord.ConfigKey);
if (configSection != null) {
if (configSection.SectionInformation.Type != null) {
factoryRecord.FactoryTypeName = configSection.SectionInformation.Type;
}
factoryRecord.AllowLocation = configSection.SectionInformation.AllowLocation;
factoryRecord.RestartOnExternalChanges = configSection.SectionInformation.RestartOnExternalChanges;
factoryRecord.RequirePermission = configSection.SectionInformation.RequirePermission;
factoryRecord.AllowDefinition = configSection.SectionInformation.AllowDefinition;
factoryRecord.AllowExeDefinition = configSection.SectionInformation.AllowExeDefinition;
}
}
}
if (_sectionRecords != null) {
string definitionConfigPath = (IsLocationConfig) ? _parent.ConfigPath : ConfigPath;
foreach (SectionRecord sectionRecord in _sectionRecords.Values) {
string configSource;
string configSourceStreamName;
object configSourceStreamVersion;
ConfigurationSection configSection;
if (sectionRecord.HasResult) {
configSection = (ConfigurationSection) sectionRecord.Result;
configSource = configSection.SectionInformation.ConfigSource;
if (String.IsNullOrEmpty(configSource)) {
configSource = null;
}
configSourceStreamName = configSection.SectionInformation.ConfigSourceStreamName;
if (String.IsNullOrEmpty(configSourceStreamName)) {
configSourceStreamName = null;
}
}
else {
configSection = null;
configSource = null;
configSourceStreamName = null;
// If there is no result, then the only way there could be a
// section record is:
// 1. For there to be input in the file.
// 2. A location tag applies to this record
Debug.Assert(sectionRecord.HasFileInput || sectionRecord.HasLocationInputs, "sectionRecord.HasFileInput || sectionRecord.HasLocationInputs");
// Note that if it's a location input, we don't need to monitor the configSource because
// that stream is monitored by one of our parent's config record
if (sectionRecord.HasFileInput) {
SectionXmlInfo sectionXmlInfo = sectionRecord.FileInput.SectionXmlInfo;
configSource = sectionXmlInfo.ConfigSource;
configSourceStreamName = sectionXmlInfo.ConfigSourceStreamName;
}
}
if (!String.IsNullOrEmpty(configSource)) {
configSourceStreamVersion = MonitorStream(sectionRecord.ConfigKey, configSource, configSourceStreamName);
}
else {
configSourceStreamVersion = null;
}
if (!sectionRecord.HasResult) {
Debug.Assert(sectionRecord.HasFileInput || sectionRecord.HasLocationInputs, "sectionRecord.HasFileInput || sectionRecord.HasLocationInputs");
// Note that if it's a location input, we don't need to monitor the configSource because
// that stream is monitored by one of our parent's config record
if (sectionRecord.HasFileInput) {
SectionXmlInfo sectionXmlInfo = sectionRecord.FileInput.SectionXmlInfo;
sectionXmlInfo.StreamVersion = ConfigStreamInfo.StreamVersion;
sectionXmlInfo.ConfigSourceStreamVersion = configSourceStreamVersion;
}
}
else {
configSection.SectionInformation.RawXml = null;
bool addUpdate = sectionRecord.AddUpdate;
sectionRecord.AddUpdate = false;
if (addUpdate) {
SectionInput fileInput = sectionRecord.FileInput;
if (fileInput == null) {
SectionXmlInfo sectionXmlInfo = new SectionXmlInfo(
sectionRecord.ConfigKey, definitionConfigPath, _configPath, _locationSubPath,
ConfigStreamInfo.StreamName, 0, ConfigStreamInfo.StreamVersion, null,
configSource, configSourceStreamName, configSourceStreamVersion,
configSection.SectionInformation.ConfigBuilderName,
configSection.SectionInformation.ProtectionProviderName,
configSection.SectionInformation.OverrideModeSetting,
!configSection.SectionInformation.InheritInChildApplications);
fileInput = new SectionInput(sectionXmlInfo, null);
fileInput.Result = configSection;
fileInput.ResultRuntimeObject = configSection;
sectionRecord.AddFileInput(fileInput);
}
else {
SectionXmlInfo sectionXmlInfo = fileInput.SectionXmlInfo;
sectionXmlInfo.LineNumber = 0;
sectionXmlInfo.StreamVersion = ConfigStreamInfo.StreamVersion;
sectionXmlInfo.RawXml = null;
sectionXmlInfo.ConfigSource = configSource;
sectionXmlInfo.ConfigSourceStreamName = configSourceStreamName;
sectionXmlInfo.ConfigSourceStreamVersion = configSourceStreamVersion;
sectionXmlInfo.ConfigBuilderName = configSection.SectionInformation.ConfigBuilderName;
sectionXmlInfo.ProtectionProviderName = configSection.SectionInformation.ProtectionProviderName;
sectionXmlInfo.OverrideModeSetting = configSection.SectionInformation.OverrideModeSetting;
sectionXmlInfo.SkipInChildApps = !configSection.SectionInformation.InheritInChildApplications;
}
fileInput.ProtectionProvider = configSection.SectionInformation.ProtectionProvider;
}
try {
configSection.ResetModified();
}
catch (Exception e) {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_exception_in_config_section_handler, sectionRecord.ConfigKey), e, ConfigStreamInfo.StreamName, 0);
}
}
}
}
// Copy remaining stream updates, which correspond to streams used by location sections
foreach (StreamInfo streamInfo in _streamInfoUpdates.Values) {
if (!ConfigStreamInfo.StreamInfos.Contains(streamInfo.StreamName)) {
MonitorStream(streamInfo.SectionName, streamInfo.ConfigSource, streamInfo.StreamName);
}
}
// reinitialize _streamInfoUpdates
InitStreamInfoUpdates();
// Update namespace value
_flags[ NamespacePresentInFile ] = _flags[ NamespacePresentCurrent ];
// You only have one chance to force the location config, now you
// will have to recreate the object
_flags[ ForceLocationWritten ] = false;
_flags[ SuggestLocationRemoval ] = false;
// Handle removed location sections
if (!IsLocationConfig && _locationSections != null && _removedSections != null && _removedSections.Count > 0) {
int i = 0;
while (i < _locationSections.Count) {
LocationSectionRecord locationSectionRecord = (LocationSectionRecord) _locationSections[i];
if (_removedSections.Contains(locationSectionRecord.ConfigKey)) {
_locationSections.RemoveAt(i);
}
else {
i++;
}
}
}
_removedSections = null;
_removedSectionGroups = null;
}
// Create a new config file.
private void CreateNewConfig(
SectionUpdates declarationUpdates,
ConfigDefinitionUpdates definitionUpdates,
NamespaceChange namespaceChange,
XmlUtilWriter utilWriter) {
int linePosition = DEFAULT_INDENT + 1;
int indent = DEFAULT_INDENT;
// Write Header
utilWriter.Write(string.Format(CultureInfo.InvariantCulture,
FORMAT_NEWCONFIGFILE,
ConfigStreamInfo.StreamEncoding.WebName));
// Write <configuration> tag
if (namespaceChange == NamespaceChange.Add) {
utilWriter.Write(string.Format(CultureInfo.InvariantCulture,
FORMAT_CONFIGURATION_NAMESPACE,
KEYWORD_CONFIGURATION_NAMESPACE));
}
else {
utilWriter.Write(FORMAT_CONFIGURATION);
}
if (declarationUpdates != null) {
WriteNewConfigDeclarations(declarationUpdates, utilWriter, linePosition, indent, false);
}
WriteNewConfigDefinitions(definitionUpdates, utilWriter, linePosition, indent);
utilWriter.Write(FORMAT_CONFIGURATION_ENDELEMENT);
}
private void WriteNewConfigDeclarations(SectionUpdates declarationUpdates, XmlUtilWriter utilWriter, int linePosition, int indent, bool skipFirstIndent) {
if (!skipFirstIndent) {
utilWriter.AppendSpacesToLinePosition(linePosition);
}
utilWriter.Write("<configSections>\r\n");
WriteUnwrittenConfigDeclarations(declarationUpdates, utilWriter, linePosition + indent, indent, false);
utilWriter.AppendSpacesToLinePosition(linePosition);
utilWriter.Write("</configSections>\r\n");
if (skipFirstIndent) {
utilWriter.AppendSpacesToLinePosition(linePosition);
}
}
private void WriteUnwrittenConfigDeclarations(SectionUpdates declarationUpdates, XmlUtilWriter utilWriter, int linePosition, int indent, bool skipFirstIndent) {
WriteUnwrittenConfigDeclarationsRecursive(declarationUpdates, utilWriter, linePosition, indent, skipFirstIndent);
}
private void WriteUnwrittenConfigDeclarationsRecursive(SectionUpdates declarationUpdates, XmlUtilWriter utilWriter, int linePosition, int indent, bool skipFirstIndent) {
string[] unretrievedSectionNames = declarationUpdates.GetUnretrievedSectionNames();
if (unretrievedSectionNames != null) {
foreach (string configKey in unretrievedSectionNames) {
Debug.Assert(!IsImplicitSection(configKey), "We should never write out an implicit section");
if (!skipFirstIndent) {
utilWriter.AppendSpacesToLinePosition(linePosition);
}
skipFirstIndent = false;
DeclarationUpdate update = declarationUpdates.GetDeclarationUpdate(configKey);
if (update != null && !string.IsNullOrEmpty(update.UpdatedXml)) {
utilWriter.Write(update.UpdatedXml);
utilWriter.AppendNewLine();
}
}
}
string[] unretrievedGroupNames = declarationUpdates.GetUnretrievedGroupNames();
if (unretrievedGroupNames != null) {
foreach (string group in unretrievedGroupNames) {
if (TargetFramework != null) {
ConfigurationSectionGroup g = GetSectionGroup(group);
if (g != null && !g.ShouldSerializeSectionGroupInTargetVersion(TargetFramework)){
declarationUpdates.MarkGroupAsRetrieved(group);
continue;
}
}
if (!skipFirstIndent) {
utilWriter.AppendSpacesToLinePosition(linePosition);
}
skipFirstIndent = false;
SectionUpdates declarationUpdatesChild = declarationUpdates.GetSectionUpdatesForGroup(group);
DeclarationUpdate groupUpdate = declarationUpdatesChild.GetSectionGroupUpdate();
if (groupUpdate == null) {
utilWriter.Write("<sectionGroup name=\"" + group + "\">");
}
else {
utilWriter.Write(groupUpdate.UpdatedXml);
}
utilWriter.AppendNewLine();
WriteUnwrittenConfigDeclarationsRecursive(declarationUpdatesChild, utilWriter, linePosition + indent, indent, false);
utilWriter.AppendSpacesToLinePosition(linePosition);
utilWriter.Write("</sectionGroup>\r\n");
}
}
}
private void WriteNewConfigDefinitions(ConfigDefinitionUpdates configDefinitionUpdates, XmlUtilWriter utilWriter, int linePosition, int indent) {
if (configDefinitionUpdates == null)
return;
foreach (LocationUpdates locationUpdates in configDefinitionUpdates.LocationUpdatesList) {
SectionUpdates sectionUpdates = locationUpdates.SectionUpdates;
if (sectionUpdates.IsEmpty || !sectionUpdates.IsNew)
continue;
configDefinitionUpdates.FlagLocationWritten();
bool writeLocationTag = _locationSubPath != null || !locationUpdates.IsDefault;
int recurseLinePosition = linePosition;
utilWriter.AppendSpacesToLinePosition(linePosition);
if (writeLocationTag) {
// write the <location> start tag
if (_locationSubPath == null) {
utilWriter.Write(String.Format(CultureInfo.InvariantCulture, FORMAT_LOCATION_NOPATH, locationUpdates.OverrideMode.LocationTagXmlString, BoolToString(locationUpdates.InheritInChildApps)));
}
else {
utilWriter.Write(String.Format(CultureInfo.InvariantCulture, FORMAT_LOCATION_PATH, locationUpdates.OverrideMode.LocationTagXmlString, BoolToString(locationUpdates.InheritInChildApps), _locationSubPath));
}
recurseLinePosition += indent;
utilWriter.AppendSpacesToLinePosition(recurseLinePosition);
}
// Invoke the recursive write.
WriteNewConfigDefinitionsRecursive(utilWriter, locationUpdates.SectionUpdates, recurseLinePosition, indent, true);
if (writeLocationTag) {
// Write the location end tag
utilWriter.AppendSpacesToLinePosition(linePosition);
utilWriter.Write(FORMAT_LOCATION_ENDELEMENT);
utilWriter.AppendNewLine();
}
}
if (configDefinitionUpdates.RequireLocation) {
Debug.Assert(IsLocationConfig, "IsLocationConfig");
// If we still require this to be written, then we must write it out now
configDefinitionUpdates.FlagLocationWritten();
utilWriter.AppendSpacesToLinePosition(linePosition);
utilWriter.Write(String.Format(CultureInfo.InvariantCulture, FORMAT_LOCATION_PATH, OverrideModeSetting.LocationDefault.LocationTagXmlString, KEYWORD_TRUE, _locationSubPath));
utilWriter.AppendSpacesToLinePosition(linePosition);
utilWriter.Write(FORMAT_LOCATION_ENDELEMENT);
utilWriter.AppendNewLine();
}
}
// Recursively write new sections for each section group.
private bool WriteNewConfigDefinitionsRecursive(XmlUtilWriter utilWriter, SectionUpdates sectionUpdates, int linePosition, int indent, bool skipFirstIndent) {
bool wroteASection = false;
string[] movedSectionNames = sectionUpdates.GetMovedSectionNames();
if (movedSectionNames != null) {
wroteASection = true;
foreach (string configKey in movedSectionNames) {
DefinitionUpdate update = sectionUpdates.GetDefinitionUpdate(configKey);
WriteSectionUpdate(utilWriter, update, linePosition, indent, skipFirstIndent);
utilWriter.AppendNewLine();
skipFirstIndent = false;
}
}
string[] newGroupNames = sectionUpdates.GetNewGroupNames();
if (newGroupNames != null) {
foreach (string group in newGroupNames) {
if (TargetFramework != null) {
ConfigurationSectionGroup g = GetSectionGroup(group);
if (g != null && !g.ShouldSerializeSectionGroupInTargetVersion(TargetFramework)){
sectionUpdates.MarkGroupAsRetrieved(group);
continue;
}
}
if (!skipFirstIndent) {
utilWriter.AppendSpacesToLinePosition(linePosition);
}
skipFirstIndent = false;
utilWriter.Write("<" + group + ">\r\n");
bool recurseWroteASection = WriteNewConfigDefinitionsRecursive(
utilWriter, sectionUpdates.GetSectionUpdatesForGroup(group), linePosition + indent, indent, false);
if (recurseWroteASection) {
wroteASection = true;
}
utilWriter.AppendSpacesToLinePosition(linePosition);
utilWriter.Write("</" + group + ">\r\n");
}
}
sectionUpdates.IsNew = false;
return wroteASection;
}
private void CheckPreamble(byte[] preamble, XmlUtilWriter utilWriter, byte[] buffer) {
bool hasByteOrderMark = false;
using (Stream preambleStream = new MemoryStream(buffer)) {
byte[] streamStart = new byte[preamble.Length];
if (preambleStream.Read(streamStart, 0, streamStart.Length) == streamStart.Length) {
hasByteOrderMark = true;
for (int i = 0; i < streamStart.Length; i++) {
if (streamStart[i] != preamble[i]) {
hasByteOrderMark = false;
break;
}
}
}
}
if (!hasByteOrderMark) {
// Force the writer to emit byte order mark, then reset the stream
// so that it is written over.
object checkpoint = utilWriter.CreateStreamCheckpoint();
utilWriter.Write('x');
utilWriter.RestoreStreamCheckpoint(checkpoint);
}
}
//
// Calculate a new indent based on the position of the parent element and the current node.
//
private int UpdateIndent(int oldIndent, XmlUtil xmlUtil, XmlUtilWriter utilWriter, int parentLinePosition) {
int indent = oldIndent;
if (xmlUtil.Reader.NodeType == XmlNodeType.Element && utilWriter.IsLastLineBlank) {
int childLinePosition = xmlUtil.TrueLinePosition;
if (parentLinePosition < childLinePosition && childLinePosition <= parentLinePosition + MAX_INDENT) {
indent = childLinePosition - parentLinePosition;
}
}
return indent;
}
// Copy a config file, replacing sections with updates.
private void CopyConfig(SectionUpdates declarationUpdates, ConfigDefinitionUpdates definitionUpdates,
byte[] buffer, string filename, NamespaceChange namespaceChange, XmlUtilWriter utilWriter) {
CheckPreamble(ConfigStreamInfo.StreamEncoding.GetPreamble(), utilWriter, buffer);
using (Stream stream = new MemoryStream(buffer)) {
using (XmlUtil xmlUtil = new XmlUtil(stream, filename, false)) {
// copy up to the <configuration> node
XmlTextReader reader = xmlUtil.Reader;
reader.WhitespaceHandling = WhitespaceHandling.All;
reader.Read();
xmlUtil.CopyReaderToNextElement(utilWriter, false);
Debug.Assert(reader.NodeType == XmlNodeType.Element && reader.Name == KEYWORD_CONFIGURATION,
"reader.NodeType == XmlNodeType.Element && reader.Name == KEYWORD_CONFIGURATION");
int indent = DEFAULT_INDENT;
int configurationElementLinePosition = xmlUtil.TrueLinePosition;
bool isEmptyConfigurationElement = reader.IsEmptyElement;
// copy <configuration> node
// if the node is an empty element, we may need to open it.
string configurationStartElement;
if (namespaceChange == NamespaceChange.Add) {
configurationStartElement = string.Format(
CultureInfo.InvariantCulture, FORMAT_CONFIGURATION_NAMESPACE, KEYWORD_CONFIGURATION_NAMESPACE);
}
else if (namespaceChange == NamespaceChange.Remove) {
configurationStartElement = FORMAT_CONFIGURATION;
}
else {
configurationStartElement = null;
}
bool needsChildren = (declarationUpdates != null || definitionUpdates != null);
string configurationEndElement = xmlUtil.UpdateStartElement(utilWriter, configurationStartElement, needsChildren, configurationElementLinePosition, indent);
bool foundConfigSectionsElement = false;
if (!isEmptyConfigurationElement) {
// copy up to the first element under <configuration>
xmlUtil.CopyReaderToNextElement(utilWriter, true);
// updateIndent
indent = UpdateIndent(indent, xmlUtil, utilWriter, configurationElementLinePosition);
if (reader.NodeType == XmlNodeType.Element && reader.Name == KEYWORD_CONFIGSECTIONS) {
foundConfigSectionsElement = true;
int configSectionsElementLinePosition = xmlUtil.TrueLinePosition;
bool isEmptyConfigSectionsElement = reader.IsEmptyElement;
// if no updates, copy the entire <configSections> element
if (declarationUpdates == null) {
xmlUtil.CopyOuterXmlToNextElement(utilWriter, true);
}
else {
// copy <configSections>, and open it if it is an empty element
string configSectionsEndElement = xmlUtil.UpdateStartElement(
utilWriter, null, true, configSectionsElementLinePosition, indent);
if (!isEmptyConfigSectionsElement) {
// copy to next element under <configSections>, or up to closing </configSections>
xmlUtil.CopyReaderToNextElement(utilWriter, true);
// copy config declarations
CopyConfigDeclarationsRecursive(declarationUpdates, xmlUtil, utilWriter, string.Empty,
configSectionsElementLinePosition, indent);
Debug.Assert(reader.NodeType == XmlNodeType.EndElement && reader.Name == KEYWORD_CONFIGSECTIONS,
"reader.NodeType == XmlNodeType.EndElement && reader.Name == \"KEYWORD_CONFIGSECTIONS\"");
}
// write declarations not written by above copy
if (declarationUpdates.HasUnretrievedSections()) {
// determine the line position of the end element
int endElementLinePosition = 0;
if (configSectionsEndElement == null) {
endElementLinePosition = xmlUtil.TrueLinePosition;
}
// indent a new line
if (!utilWriter.IsLastLineBlank) {
utilWriter.AppendNewLine();
}
WriteUnwrittenConfigDeclarations(declarationUpdates, utilWriter, configSectionsElementLinePosition + indent, indent, false);
// restore spaces to end element
if (configSectionsEndElement == null) {
utilWriter.AppendSpacesToLinePosition(endElementLinePosition);
}
}
// Copy the </configSections> element
if (configSectionsEndElement == null) {
xmlUtil.CopyXmlNode(utilWriter);
}
else {
// note that configSectionsEndElement already contains the proper indenting
utilWriter.Write(configSectionsEndElement);
}
// copy up to the next element under <configuration>, or up to closing </configSections>
xmlUtil.CopyReaderToNextElement(utilWriter, true);
}
}
}
// Write new declarations
if (!foundConfigSectionsElement && declarationUpdates != null) {
bool skipFirstIndent = reader.Depth > 0 && reader.NodeType == XmlNodeType.Element;
int newConfigSectionsLinePosition;
if (skipFirstIndent) {
newConfigSectionsLinePosition = xmlUtil.TrueLinePosition;
}
else {
newConfigSectionsLinePosition = configurationElementLinePosition + indent;
}
WriteNewConfigDeclarations(declarationUpdates, utilWriter, newConfigSectionsLinePosition, indent, skipFirstIndent);
}
if (definitionUpdates != null) {
//
// Copy sections recursively. In the file we copy we start out at
// location path="." allowOverride="true" inheritInChildApps="true"
//
bool locationPathApplies = false;
LocationUpdates locationUpdates = null;
SectionUpdates sectionUpdates = null;
if (!IsLocationConfig) {
locationPathApplies = true;
locationUpdates = definitionUpdates.FindLocationUpdates(OverrideModeSetting.LocationDefault, true);
if (locationUpdates != null) {
sectionUpdates = locationUpdates.SectionUpdates;
}
}
CopyConfigDefinitionsRecursive(definitionUpdates, xmlUtil, utilWriter, locationPathApplies,
locationUpdates, sectionUpdates, true, string.Empty, configurationElementLinePosition, indent);
// Write new config sections from new groups.
WriteNewConfigDefinitions(definitionUpdates, utilWriter, configurationElementLinePosition + indent, indent);
#if DBG
Debug.Assert(configurationEndElement != null || (reader.NodeType == XmlNodeType.EndElement && reader.Name == KEYWORD_CONFIGURATION),
"configurationEndElement != null || (reader.NodeType == XmlNodeType.EndElement && reader.Name == KEYWORD_CONFIGURATION)");
#endif
#if DBG
{
foreach (LocationUpdates l in definitionUpdates.LocationUpdatesList) {
Debug.Assert(!l.SectionUpdates.HasUnretrievedSections(), "!l.SectionUpdates.HasUnretrievedSections()");
}
}
#endif
}
if (configurationEndElement != null) {
// If we have to add closing config tag, then do it now
// before copying extra whitespace/comments
if (!utilWriter.IsLastLineBlank) {
utilWriter.AppendNewLine();
}
utilWriter.Write(configurationEndElement);
}
//
// Copy the remainder of the file, the closing </configuration> node plus any whitespace
// and comments
//
while (xmlUtil.CopyXmlNode(utilWriter)) {
}
}
}
}
private bool CopyConfigDeclarationsRecursive(
SectionUpdates declarationUpdates, XmlUtil xmlUtil, XmlUtilWriter utilWriter, string group,
int parentLinePosition, int parentIndent) {
bool wroteASection = false;
XmlTextReader reader = xmlUtil.Reader;
int linePosition;
int indent;
int startingLinePosition;
indent = UpdateIndent(parentIndent, xmlUtil, utilWriter, parentLinePosition);
if (reader.NodeType == XmlNodeType.Element) {
linePosition = xmlUtil.TrueLinePosition;
startingLinePosition = linePosition;
}
else if (reader.NodeType == XmlNodeType.EndElement) {
linePosition = parentLinePosition + indent;
if (utilWriter.IsLastLineBlank) {
startingLinePosition = xmlUtil.TrueLinePosition;
}
else {
startingLinePosition = parentLinePosition;
}
}
else {
linePosition = parentLinePosition + indent;
startingLinePosition = 0;
}
//
// Write any new section declarations that apply to this group
//
if (declarationUpdates != null) {
string[] movedSectionNames = declarationUpdates.GetMovedSectionNames();
if (movedSectionNames != null) {
if (!utilWriter.IsLastLineBlank) {
utilWriter.AppendNewLine();
}
foreach (string configKey in movedSectionNames) {
DeclarationUpdate sectionUpdate = declarationUpdates.GetDeclarationUpdate(configKey);
Debug.Assert(!IsImplicitSection(configKey), "We should never write out an implicit section");
// Write the one line section declaration.
utilWriter.AppendSpacesToLinePosition(linePosition);
utilWriter.Write(sectionUpdate.UpdatedXml);
utilWriter.AppendNewLine();
wroteASection = true;
}
// Restore the whitespace we used for the first element, which is either a start or an end element.
utilWriter.AppendSpacesToLinePosition(startingLinePosition);
}
}
if (reader.NodeType == XmlNodeType.Element) {
//
// For each element at this depth, either:
// - Write the element verbatim and recurse due to a group hierarchy element.
// - Write the element verbatim because it is unchanged
// - Write the updated XML for the section.
// - Skip it because the section has been removed.
//
int depth = reader.Depth;
while (reader.Depth == depth) {
bool recurse = false;
DeclarationUpdate sectionUpdate = null;
DeclarationUpdate groupUpdate = null;
SectionUpdates declarationUpdatesChild = null;
SectionUpdates recurseDeclarationUpdates = declarationUpdates;
string recurseGroup = group;
// update the lineposition and indent for each element
indent = UpdateIndent(indent, xmlUtil, utilWriter, parentLinePosition);
linePosition = xmlUtil.TrueLinePosition;
string directive = reader.Name;
string name = reader.GetAttribute(KEYWORD_SECTIONGROUP_NAME);
string configKey = CombineConfigKey(group, name);
if (directive == KEYWORD_SECTIONGROUP) {
// it's a group - get the updates for children
declarationUpdatesChild = declarationUpdates.GetSectionUpdatesForGroup(name);
if (declarationUpdatesChild != null) {
// get the group update
groupUpdate = declarationUpdatesChild.GetSectionGroupUpdate();
// recurse if there are more sections to copy
if (declarationUpdatesChild.HasUnretrievedSections()) {
recurse = true;
recurseGroup = configKey;
recurseDeclarationUpdates = declarationUpdatesChild;
}
}
}
else {
// it is a section - get the update
Debug.Assert(!IsImplicitSection(configKey), "We should never write out an implicit section");
sectionUpdate = declarationUpdates.GetDeclarationUpdate(configKey);
}
bool writeGroupUpdate = (groupUpdate != null && groupUpdate.UpdatedXml != null);
if (recurse) {
#if DBG
string startElementName = reader.Name;
#endif
// create a checkpoint that we can revert to if no children are written
object checkpoint = utilWriter.CreateStreamCheckpoint();
string closingElement = null;
// Copy this element node and up to the first subelement
if (writeGroupUpdate) {
// replace the element with the updated xml
utilWriter.Write(groupUpdate.UpdatedXml);
// skip over the start element
reader.Read();
}
else {
closingElement= xmlUtil.UpdateStartElement(utilWriter, null, true, linePosition, indent);
}
if (closingElement == null) {
// Only if there is a closing element should
// we move to it
xmlUtil.CopyReaderToNextElement(utilWriter, true);
}
// Recurse
bool recurseWroteASection = CopyConfigDeclarationsRecursive(
recurseDeclarationUpdates, xmlUtil, utilWriter, recurseGroup, linePosition, indent);
if (closingElement != null) {
utilWriter.AppendSpacesToLinePosition(linePosition);
utilWriter.Write(closingElement);
// Since we already got to </configSections> in reader, lets
// indent so we can copy the element in the right place
utilWriter.AppendSpacesToLinePosition(parentLinePosition);
} else {
// Copy the end element
xmlUtil.CopyXmlNode(utilWriter);
}
if (recurseWroteASection || writeGroupUpdate) {
wroteASection = true;
}
else {
// back out the change
utilWriter.RestoreStreamCheckpoint(checkpoint);
}
// Copy up to the next element, or exit this level.
xmlUtil.CopyReaderToNextElement(utilWriter, true);
}
else {
bool skip;
bool skipChildElements = false;
if (sectionUpdate == null) {
skip = true;
if (writeGroupUpdate) {
// Insert an empty <sectionGroup type="typename" > node, to introduce the type
wroteASection = true;
utilWriter.Write(groupUpdate.UpdatedXml);
utilWriter.AppendNewLine();
utilWriter.AppendSpacesToLinePosition(linePosition);
utilWriter.Write(FORMAT_SECTIONGROUP_ENDELEMENT);
utilWriter.AppendNewLine();
utilWriter.AppendSpacesToLinePosition(linePosition);
}
else if (groupUpdate != null) {
// VSWhidbey 522450
// If groupUpdate exists, that means we've decided in GetConfigDeclarationUpdates
// that the section group should stay in the file.
Debug.Assert(groupUpdate.UpdatedXml == null, "groupUpdate.UpdatedXml == null");
Debug.Assert(!declarationUpdatesChild.HasUnretrievedSections(),
"If the group has any unretrieved section, we should have chosen the recursive code path above.");
wroteASection = true;
skip = false;
// We should skip all the child sections. If we indeed need to keep any child
// section, we should have chosen the recursive code path above.
skipChildElements = true;
}
}
else {
wroteASection = true;
if (sectionUpdate.UpdatedXml == null) {
skip = false;
}
else {
skip = true;
// Write the updated XML on a single line
utilWriter.Write(sectionUpdate.UpdatedXml);
}
}
if (skip) {
//
// Skip over the existing element, then
// copy up to the next element, or exit this level.
//
xmlUtil.SkipAndCopyReaderToNextElement(utilWriter, true);
}
else {
if (skipChildElements) {
xmlUtil.SkipChildElementsAndCopyOuterXmlToNextElement(utilWriter);
}
else {
// Copy this entire contents of this element and then to the next element, or exit this level.
xmlUtil.CopyOuterXmlToNextElement(utilWriter, true);
}
}
}
}
}
return wroteASection;
}
// Copy configuration sections from the original configuration file.
private bool CopyConfigDefinitionsRecursive(
ConfigDefinitionUpdates configDefinitionUpdates, XmlUtil xmlUtil, XmlUtilWriter utilWriter,
bool locationPathApplies, LocationUpdates locationUpdates, SectionUpdates sectionUpdates,
bool addNewSections, string group, int parentLinePosition, int parentIndent) {
bool wroteASection = false;
XmlTextReader reader = xmlUtil.Reader;
int linePosition;
int indent;
int startingLinePosition;
indent = UpdateIndent(parentIndent, xmlUtil, utilWriter, parentLinePosition);
if (reader.NodeType == XmlNodeType.Element) {
linePosition = xmlUtil.TrueLinePosition;
startingLinePosition = linePosition;
}
else if (reader.NodeType == XmlNodeType.EndElement) {
linePosition = parentLinePosition + indent;
if (utilWriter.IsLastLineBlank) {
startingLinePosition = xmlUtil.TrueLinePosition;
}
else {
startingLinePosition = parentLinePosition;
}
}
else {
linePosition = parentLinePosition + indent;
startingLinePosition = 0;
}
//
// Write any new sections that apply to this group
//
if (sectionUpdates != null && addNewSections) {
// Remove newness, so we won't write again
sectionUpdates.IsNew = false;
Debug.Assert(locationPathApplies, "locationPathApplies");
string[] movedSectionNames = sectionUpdates.GetMovedSectionNames();
if (movedSectionNames != null) {
if (!utilWriter.IsLastLineBlank) {
utilWriter.AppendNewLine();
}
utilWriter.AppendSpacesToLinePosition(linePosition);
bool skipFirstIndent = true;
foreach (string configKey in movedSectionNames) {
DefinitionUpdate update = sectionUpdates.GetDefinitionUpdate(configKey);
WriteSectionUpdate(utilWriter, update, linePosition, indent, skipFirstIndent);
skipFirstIndent = false;
utilWriter.AppendNewLine();
wroteASection = true;
}
// Restore the whitespace we used for the first element, which is either a start or an end element.
utilWriter.AppendSpacesToLinePosition(startingLinePosition);
}
}
if (reader.NodeType == XmlNodeType.Element) {
//
// For each element at this depth, either:
// - Write the element verbatim and recurse due to a location section or group hierarchy element.
// - Write the element verbatim because it is unchanged, or because the current location does
// not apply.
// - Write the updated XML for the section.
// - Skip it because the section has been removed.
//
int depth = reader.Depth;
while (reader.Depth == depth) {
bool recurse = false;
DefinitionUpdate update = null;
bool elementLocationPathApplies = locationPathApplies;
LocationUpdates recurseLocationUpdates = locationUpdates;
SectionUpdates recurseSectionUpdates = sectionUpdates;
bool recurseAddNewSections = addNewSections;
string recurseGroup = group;
bool removedSectionOrGroup = false;
// update the lineposition and indent for each element
indent = UpdateIndent(indent, xmlUtil, utilWriter, parentLinePosition);
linePosition = xmlUtil.TrueLinePosition;
string elementName = reader.Name;
if (elementName == KEYWORD_LOCATION) {
string locationSubPathAttribute = reader.GetAttribute(KEYWORD_LOCATION_PATH);
locationSubPathAttribute = NormalizeLocationSubPath(locationSubPathAttribute, xmlUtil);
elementLocationPathApplies = false;
OverrideModeSetting overrideMode = OverrideModeSetting.LocationDefault;
bool inheritInChildApps = true;
if (IsLocationConfig) {
// For location config we will compare config paths instead of location strings
// so that we dont end up comparing "1" with "Default Web Site" and ending up with the wrong result
if (locationSubPathAttribute == null) {
elementLocationPathApplies = false;
}
else {
elementLocationPathApplies = StringUtil.EqualsIgnoreCase(ConfigPath, Host.GetConfigPathFromLocationSubPath(Parent.ConfigPath, locationSubPathAttribute));
}
}
else {
Debug.Assert(LocationSubPath == null);
// This is the same as doing StringUtil.EqualsIgnoreCase(_locationSubPath, locationSubPathAttribute)
// but remember the first one is null. Also remember locationSubPathAttribute is already normalized
elementLocationPathApplies = (locationSubPathAttribute == null);
}
if (elementLocationPathApplies) {
// Retrieve overrideMode and InheritInChildApps
string allowOverrideAttribute = reader.GetAttribute(KEYWORD_LOCATION_ALLOWOVERRIDE);
if (allowOverrideAttribute != null) {
overrideMode = OverrideModeSetting.CreateFromXmlReadValue(Boolean.Parse(allowOverrideAttribute));
}
string overrideModeAttribute = reader.GetAttribute(KEYWORD_LOCATION_OVERRIDEMODE);
if (overrideModeAttribute != null) {
overrideMode = OverrideModeSetting.CreateFromXmlReadValue(OverrideModeSetting.ParseOverrideModeXmlValue(overrideModeAttribute, null));
Debug.Assert(allowOverrideAttribute == null, "allowOverride and overrideMode both detected in a <location> tag");
}
string inheritInChildAppsAttribute = reader.GetAttribute(KEYWORD_LOCATION_INHERITINCHILDAPPLICATIONS);
if (inheritInChildAppsAttribute != null) {
inheritInChildApps = Boolean.Parse(inheritInChildAppsAttribute);
}
// Flag that we already have one of these locations
configDefinitionUpdates.FlagLocationWritten();
}
if (reader.IsEmptyElement) {
if (elementLocationPathApplies &&
(configDefinitionUpdates.FindLocationUpdates(overrideMode,
inheritInChildApps) != null)) {
// If we are going to make updates here, then
// delete the one that is here (so we can update later)
elementLocationPathApplies = true;
}
else {
// If not lets leave it
elementLocationPathApplies = false;
}
}
else {
// recurse if this location applies to us
if (elementLocationPathApplies) {
if (configDefinitionUpdates != null) {
recurseLocationUpdates = configDefinitionUpdates.FindLocationUpdates(overrideMode, inheritInChildApps);
if (recurseLocationUpdates != null) {
recurse = true;
recurseSectionUpdates = recurseLocationUpdates.SectionUpdates;
// If this is <location path=".">, we don't want to add moved sections
// to it.
if (_locationSubPath == null && recurseLocationUpdates.IsDefault) {
recurseAddNewSections = false;
}
}
}
}
else {
// recurse if necessary to remove items in _removedSections and _removedGroups
if (HasRemovedSectionsOrGroups && !IsLocationConfig && Host.SupportsLocation) {
recurse = true;
recurseLocationUpdates = null;
recurseSectionUpdates = null;
recurseAddNewSections = false;
}
}
}
}
else {
string configKey = CombineConfigKey(group, elementName);
FactoryRecord factoryRecord = FindFactoryRecord(configKey, false);
if (factoryRecord == null) {
// The factory was deleted, so regardless of whether this is a
// section or sectionGroup, it can be skipped.
if (!elementLocationPathApplies && !IsLocationConfig) {
removedSectionOrGroup = true;
}
}
else if (factoryRecord.IsGroup) {
if (reader.IsEmptyElement) {
if (!elementLocationPathApplies && !IsLocationConfig) {
removedSectionOrGroup = true;
}
}
else {
// if the location path applies, recurse if there are updates
if (sectionUpdates != null) {
SectionUpdates sectionUpdatesChild = sectionUpdates.GetSectionUpdatesForGroup(elementName);
if (sectionUpdatesChild != null) {
recurse = true;
recurseGroup = configKey;
recurseSectionUpdates = sectionUpdatesChild;
}
}
else if (!elementLocationPathApplies && !IsLocationConfig) {
if (_removedSectionGroups != null && _removedSectionGroups.Contains(configKey)) {
removedSectionOrGroup = true;
}
else {
recurse = true;
recurseGroup = configKey;
recurseLocationUpdates = null;
recurseSectionUpdates = null;
recurseAddNewSections = false;
}
}
}
}
else {
// it is a section - get the update
if (sectionUpdates != null) {
update = sectionUpdates.GetDefinitionUpdate(configKey);
}
else if (!elementLocationPathApplies && !IsLocationConfig) {
if (_removedSections != null && _removedSections.Contains(configKey)) {
removedSectionOrGroup = true;
}
}
}
}
if (recurse) {
#if DBG
string startElementName = reader.Name;
#endif
// flush, and get length of underlying stream
object checkpoint = utilWriter.CreateStreamCheckpoint();
// Copy this element node and up to the first subelement
xmlUtil.CopyXmlNode(utilWriter);
xmlUtil.CopyReaderToNextElement(utilWriter, true);
// Recurse
bool recurseWroteASection = CopyConfigDefinitionsRecursive(
configDefinitionUpdates, xmlUtil, utilWriter, elementLocationPathApplies, recurseLocationUpdates, recurseSectionUpdates,
recurseAddNewSections, recurseGroup, linePosition, indent);
// Copy the end element
xmlUtil.CopyXmlNode(utilWriter);
if (recurseWroteASection) {
wroteASection = true;
}
else {
// back out the change
utilWriter.RestoreStreamCheckpoint(checkpoint);
}
// Copy up to the next element, or exit this level.
xmlUtil.CopyReaderToNextElement(utilWriter, true);
}
else {
bool skip;
if (update == null) {
// remove the section from the file if we're in the correct location,
// or if the section or group should be removed from all locations
skip = elementLocationPathApplies || removedSectionOrGroup;
}
else {
// replace the section if the xml for it has been updated
// if it is a configSource, don't write it unless the configSource parameters have changed
skip = false;
if (update.UpdatedXml != null) {
ConfigurationSection configSection = (ConfigurationSection) update.SectionRecord.Result;
if ( String.IsNullOrEmpty(configSection.SectionInformation.ConfigSource) ||
configSection.SectionInformation.ConfigSourceModified) {
skip = true;
WriteSectionUpdate(utilWriter, update, linePosition, indent, true);
wroteASection = true;
}
}
}
if (skip) {
//
// Skip over the existing element, then
// copy up to the next element, or exit this level.
//
xmlUtil.SkipAndCopyReaderToNextElement(utilWriter, true);
}
else {
// Copy this entire contents of this element and then to the next element, or exit this level.
xmlUtil.CopyOuterXmlToNextElement(utilWriter, true);
wroteASection = true;
}
}
}
}
//
// Write new section groups
//
if (sectionUpdates != null && addNewSections && sectionUpdates.HasNewSectionGroups()) {
// Add whitespace to align us with the other elements in this group
linePosition = parentLinePosition + indent;
if (reader.NodeType == XmlNodeType.EndElement) {
if (utilWriter.IsLastLineBlank) {
startingLinePosition = xmlUtil.TrueLinePosition;
}
else {
startingLinePosition = parentLinePosition;
}
}
else {
startingLinePosition = 0;
}
utilWriter.AppendSpacesToLinePosition(linePosition);
bool wroteNewSection = WriteNewConfigDefinitionsRecursive(utilWriter, sectionUpdates, linePosition, indent, true);
if (wroteNewSection) {
wroteASection = true;
}
// Restore the whitespace of the end element
utilWriter.AppendSpacesToLinePosition(startingLinePosition);
}
return wroteASection;
}
private void WriteSectionUpdate(XmlUtilWriter utilWriter, DefinitionUpdate update, int linePosition, int indent, bool skipFirstIndent) {
ConfigurationSection configSection = (ConfigurationSection) update.SectionRecord.Result;
string updatedXml;
if (!String.IsNullOrEmpty(configSection.SectionInformation.ConfigSource)) {
updatedXml = string.Format(CultureInfo.InvariantCulture, FORMAT_SECTION_CONFIGSOURCE, configSection.SectionInformation.Name, configSection.SectionInformation.ConfigSource);
}
else {
updatedXml = update.UpdatedXml;
}
string formattedXml = XmlUtil.FormatXmlElement(updatedXml, linePosition, indent, skipFirstIndent);
utilWriter.Write(formattedXml);
}
//
// SaveConfigSource
//
private void SaveConfigSource(DefinitionUpdate update) {
string configSourceStreamName;
if (update.SectionRecord.HasResult) {
ConfigurationSection configSection = (ConfigurationSection) update.SectionRecord.Result;
configSourceStreamName = configSection.SectionInformation.ConfigSourceStreamName;
}
else {
Debug.Assert(update.SectionRecord.HasFileInput, "update.SectionRecord.HasFileInput");
SectionInput fileInput = update.SectionRecord.FileInput;
configSourceStreamName = fileInput.SectionXmlInfo.ConfigSourceStreamName;
}
// Copy the input stream before opening the output stream.
byte[] readBuffer = null;
using (Stream streamRead = Host.OpenStreamForRead(configSourceStreamName)) {
if (streamRead != null) {
readBuffer = new byte[streamRead.Length];
int count = streamRead.Read(readBuffer, 0, (int) streamRead.Length);
if (count != streamRead.Length) {
throw new ConfigurationErrorsException();
}
}
}
// Write the changes to the output stream.
bool hasFile = (readBuffer != null);
object writeContext = null;
bool streamOpened = false;
try {
try {
string templateStreamName;
if (Host.IsRemote) {
// templateStreamName is used by OpenStreamForWrite for copying file attributes during saving.
// (for details, see WriteFileContext.Complete.)
//
// If we're using a remote host, then ConfigStreamInfo.StreamName is actually pointing to a
// full filepath on a remote machine. In this case, it's impossible to copy the attributes
// over, and thus we won't do it.
templateStreamName = null;
}
else {
templateStreamName = ConfigStreamInfo.StreamName;
}
using (Stream streamWrite = Host.OpenStreamForWrite(configSourceStreamName, templateStreamName, ref writeContext)) {
streamOpened = true;
if (update.UpdatedXml == null) {
Debug.Assert(hasFile, "hasFile");
if (hasFile) {
streamWrite.Write(readBuffer, 0, readBuffer.Length);
}
}
else {
using (StreamWriter streamWriter = new StreamWriter(streamWrite)) {
XmlUtilWriter utilWriter = new XmlUtilWriter(streamWriter, true);
if (hasFile) {
CopyConfigSource(utilWriter, update.UpdatedXml, configSourceStreamName, readBuffer);
}
else {
CreateNewConfigSource(utilWriter, update.UpdatedXml, DEFAULT_INDENT);
}
}
}
}
}
catch {
if (streamOpened) {
Host.WriteCompleted(configSourceStreamName, false, writeContext);
}
throw;
}
}
//
// Guarantee that exceptions contain at least the name of the stream by wrapping them
// in a ConfigurationException.
//
catch (Exception e) {
throw ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_error_loading_XML_file), e, configSourceStreamName, 0);
}
Host.WriteCompleted(configSourceStreamName, true, writeContext);
}
private void CopyConfigSource(XmlUtilWriter utilWriter, string updatedXml, string configSourceStreamName, byte[] buffer) {
// only copy the byte order mark if it exists in the current web.config
byte[] preamble;
using (Stream stream = new MemoryStream(buffer)) {
using (XmlUtil xmlUtil = new XmlUtil(stream, configSourceStreamName, true)) {
preamble = ConfigStreamInfo.StreamEncoding.GetPreamble();
}
}
CheckPreamble(preamble, utilWriter, buffer);
using (Stream stream = new MemoryStream(buffer)) {
using (XmlUtil xmlUtil = new XmlUtil(stream, configSourceStreamName, false)) {
XmlTextReader reader = xmlUtil.Reader;
// copy up to the first element
reader.WhitespaceHandling = WhitespaceHandling.All;
reader.Read();
// determine the indent to use for the element
int indent = DEFAULT_INDENT;
int linePosition = 1;
bool hasElement = xmlUtil.CopyReaderToNextElement(utilWriter, false);
if (hasElement) {
// find the indent of the first attribute, if any
int lineNumber = reader.LineNumber;
linePosition = reader.LinePosition - 1;
int attributeIndent = 0;
while (reader.MoveToNextAttribute()) {
if (reader.LineNumber > lineNumber) {
attributeIndent = reader.LinePosition - linePosition;
break;
}
}
// find the indent of the first sub element, if any
int elementIndent = 0;
reader.Read();
while (reader.Depth >= 1) {
if (reader.NodeType == XmlNodeType.Element) {
elementIndent = (reader.LinePosition - 1) - linePosition;
break;
}
reader.Read();
}
if (elementIndent > 0) {
indent = elementIndent;
}
else if (attributeIndent > 0) {
indent = attributeIndent;
}
}
// Write the config source
string formattedXml = XmlUtil.FormatXmlElement(updatedXml, linePosition, indent, true);
utilWriter.Write(formattedXml);
// Copy remaining contents
if (hasElement) {
// Skip over the existing element
while (reader.Depth > 0) {
reader.Read();
}
if (reader.IsEmptyElement || reader.NodeType == XmlNodeType.EndElement) {
reader.Read();
}
// Copy remainder of file
while (xmlUtil.CopyXmlNode(utilWriter)) {
}
}
}
}
}
private void CreateNewConfigSource(XmlUtilWriter utilWriter, string updatedXml, int indent) {
string formattedXml = XmlUtil.FormatXmlElement(updatedXml, 0, indent, true);
utilWriter.Write(string.Format(CultureInfo.InvariantCulture, FORMAT_CONFIGSOURCE_FILE, ConfigStreamInfo.StreamEncoding.WebName));
utilWriter.Write(formattedXml + NL);
}
private static string BoolToString(bool v) {
return v ? KEYWORD_TRUE : KEYWORD_FALSE;
}
// RemoveLocationWriteRequirement
//
// It is possible that we have set the flag to force this location
// to be written out. Allow a way to remove that
//
internal void RemoveLocationWriteRequirement() {
if (IsLocationConfig) {
_flags[ ForceLocationWritten ] = false;
_flags[ SuggestLocationRemoval ] = true;
}
}
// NamespacePresent
//
// Is the namespace present in the file or not? ...and do you
// want it to be?
//
internal bool NamespacePresent {
get {
return _flags[ NamespacePresentCurrent ];
}
set {
_flags[ NamespacePresentCurrent ] = value;
}
}
// NamespaceChangeNeeded
//
// On Update, do we need to add the namespace, remove it, or do nothing?
//
private NamespaceChange NamespaceChangeNeeded {
get {
if (_flags[ NamespacePresentCurrent ] ==
_flags[ NamespacePresentInFile ]) {
return NamespaceChange.None;
}
if (_flags[ NamespacePresentCurrent ]) {
return NamespaceChange.Add;
}
return NamespaceChange.Remove;
}
}
// RecordItselfRequiresUpdates
//
// Outside the scope of the sections and there definitions, does
// the record itself require an update.
//
private bool RecordItselfRequiresUpdates {
get {
return (NamespaceChangeNeeded != NamespaceChange.None);
}
}
}
}
|