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
|
//------------------------------------------------------------------------------
// <copyright file="DocumentXPathNavigator.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Diagnostics;
namespace System.Xml {
internal sealed class DocumentXPathNavigator : XPathNavigator, IHasXmlNode {
private XmlDocument document; // owner document
private XmlNode source; // navigator position
private int attributeIndex; // index in attribute collection for attribute
private XmlElement namespaceParent; // parent for namespace
public DocumentXPathNavigator(XmlDocument document, XmlNode node) {
this.document = document;
ResetPosition(node);
}
public DocumentXPathNavigator(DocumentXPathNavigator other) {
document = other.document;
source = other.source;
attributeIndex = other.attributeIndex;
namespaceParent = other.namespaceParent;
}
public override XPathNavigator Clone() {
return new DocumentXPathNavigator(this);
}
public override void SetValue(string value) {
if (value == null) {
throw new ArgumentNullException("value");
}
XmlNode node = source;
XmlNode end;
switch (node.NodeType) {
case XmlNodeType.Attribute:
if (((XmlAttribute)node).IsNamespace) {
goto default;
}
node.InnerText = value;
break;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
CalibrateText();
node = source;
end = TextEnd(node);
if (node != end) {
if (node.IsReadOnly) {
throw new InvalidOperationException(Res.GetString(Res.Xdom_Node_Modify_ReadOnly));
}
DeleteToFollowingSibling(node.NextSibling, end);
}
goto case XmlNodeType.Element;
case XmlNodeType.Element:
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.Comment:
node.InnerText = value;
break;
default:
throw new InvalidOperationException(Res.GetString(Res.Xpn_BadPosition));
}
}
public override XmlNameTable NameTable {
get {
return document.NameTable;
}
}
public override XPathNodeType NodeType {
get {
CalibrateText();
return (XPathNodeType)source.XPNodeType;
}
}
public override string LocalName {
get {
return source.XPLocalName;
}
}
public override string NamespaceURI {
get {
XmlAttribute attribute = source as XmlAttribute;
if (attribute != null
&& attribute.IsNamespace) {
return string.Empty;
}
return source.NamespaceURI;
}
}
public override string Name {
get {
switch (source.NodeType) {
case XmlNodeType.Element:
case XmlNodeType.ProcessingInstruction:
return source.Name;
case XmlNodeType.Attribute:
if (((XmlAttribute)source).IsNamespace) {
string localName = source.LocalName;
if (Ref.Equal(localName, document.strXmlns)) {
return string.Empty; // xmlns declaration
}
return localName; // xmlns:name declaration
}
return source.Name; // attribute
default:
return string.Empty;
}
}
}
public override string Prefix {
get {
XmlAttribute attribute = source as XmlAttribute;
if (attribute != null
&& attribute.IsNamespace) {
return string.Empty;
}
return source.Prefix;
}
}
public override string Value {
get {
switch (source.NodeType) {
case XmlNodeType.Element:
case XmlNodeType.DocumentFragment:
return source.InnerText;
case XmlNodeType.Document:
return ValueDocument;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
return ValueText;
default:
return source.Value;
}
}
}
private string ValueDocument {
get {
XmlElement element = document.DocumentElement;
if (element != null) {
return element.InnerText;
}
return string.Empty;
}
}
private string ValueText {
get {
CalibrateText();
string value = source.Value;
XmlNode nextSibling = NextSibling(source);
if (nextSibling != null
&& nextSibling.IsText) {
StringBuilder builder = new StringBuilder(value);
do {
builder.Append(nextSibling.Value);
nextSibling = NextSibling(nextSibling);
}
while (nextSibling != null
&& nextSibling.IsText);
value = builder.ToString();
}
return value;
}
}
public override string BaseURI {
get {
return source.BaseURI;
}
}
public override bool IsEmptyElement {
get {
XmlElement element = source as XmlElement;
if (element != null) {
return element.IsEmpty;
}
return false;
}
}
public override string XmlLang {
get {
return source.XmlLang;
}
}
public override object UnderlyingObject {
get {
CalibrateText();
return source;
}
}
public override bool HasAttributes {
get {
XmlElement element = source as XmlElement;
if (element != null
&& element.HasAttributes) {
XmlAttributeCollection attributes = element.Attributes;
for (int i = 0; i < attributes.Count; i++) {
XmlAttribute attribute = attributes[i];
if (!attribute.IsNamespace) {
return true;
}
}
}
return false;
}
}
public override string GetAttribute(string localName, string namespaceURI) {
return source.GetXPAttribute(localName, namespaceURI);
}
public override bool MoveToAttribute(string localName, string namespaceURI) {
XmlElement element = source as XmlElement;
if (element != null
&& element.HasAttributes) {
XmlAttributeCollection attributes = element.Attributes;
for (int i = 0; i < attributes.Count; i++) {
XmlAttribute attribute = attributes[i];
if (attribute.LocalName == localName
&& attribute.NamespaceURI == namespaceURI) {
if (!attribute.IsNamespace) {
source = attribute;
attributeIndex = i;
return true;
}
else {
return false;
}
}
}
}
return false;
}
public override bool MoveToFirstAttribute() {
XmlElement element = source as XmlElement;
if (element != null
&& element.HasAttributes) {
XmlAttributeCollection attributes = element.Attributes;
for (int i = 0; i < attributes.Count; i++) {
XmlAttribute attribute = attributes[i];
if (!attribute.IsNamespace) {
source = attribute;
attributeIndex = i;
return true;
}
}
}
return false;
}
public override bool MoveToNextAttribute() {
XmlAttribute attribute = source as XmlAttribute;
if (attribute == null
|| attribute.IsNamespace) {
return false;
}
XmlAttributeCollection attributes;
if (!CheckAttributePosition(attribute, out attributes, attributeIndex)
&& !ResetAttributePosition(attribute, attributes, out attributeIndex)) {
return false;
}
for (int i = attributeIndex + 1; i < attributes.Count; i++) {
attribute = attributes[i];
if (!attribute.IsNamespace) {
source = attribute;
attributeIndex = i;
return true;
}
}
return false;
}
public override string GetNamespace(string name) {
XmlNode node = source;
while (node != null
&& node.NodeType != XmlNodeType.Element) {
XmlAttribute attribute = node as XmlAttribute;
if (attribute != null) {
node = attribute.OwnerElement;
}
else {
node = node.ParentNode;
}
}
XmlElement element = node as XmlElement;
if (element != null) {
string localName;
if (name != null
&& name.Length != 0) {
localName = name;
}
else {
localName = document.strXmlns;
}
string namespaceUri = document.strReservedXmlns;
do
{
XmlAttribute attribute = element.GetAttributeNode(localName, namespaceUri);
if (attribute != null) {
return attribute.Value;
}
element = element.ParentNode as XmlElement;
}
while (element != null);
}
if (name == document.strXml) {
return document.strReservedXml;
}
else if (name == document.strXmlns) {
return document.strReservedXmlns;
}
return string.Empty;
}
public override bool MoveToNamespace(string name) {
if (name == document.strXmlns) {
return false;
}
XmlElement element = source as XmlElement;
if (element != null) {
string localName;
if (name != null
&& name.Length != 0) {
localName = name;
}
else {
localName = document.strXmlns;
}
string namespaceUri = document.strReservedXmlns;
do {
XmlAttribute attribute = element.GetAttributeNode(localName, namespaceUri);
if (attribute != null) {
namespaceParent = (XmlElement)source;
source = attribute;
return true;
}
element = element.ParentNode as XmlElement;
}
while (element != null);
if (name == document.strXml) {
namespaceParent = (XmlElement)source;
source = document.NamespaceXml;
return true;
}
}
return false;
}
public override bool MoveToFirstNamespace(XPathNamespaceScope scope) {
XmlElement element = source as XmlElement;
if (element == null) {
return false;
}
XmlAttributeCollection attributes;
int index = Int32.MaxValue;
switch (scope) {
case XPathNamespaceScope.Local:
if (!element.HasAttributes) {
return false;
}
attributes = element.Attributes;
if (!MoveToFirstNamespaceLocal(attributes, ref index)) {
return false;
}
source = attributes[index];
attributeIndex = index;
namespaceParent = element;
break;
case XPathNamespaceScope.ExcludeXml:
attributes = element.Attributes;
if (!MoveToFirstNamespaceGlobal(ref attributes, ref index)) {
return false;
}
XmlAttribute attribute = attributes[index];
while (Ref.Equal(attribute.LocalName, document.strXml)) {
if (!MoveToNextNamespaceGlobal(ref attributes, ref index)) {
return false;
}
attribute = attributes[index];
}
source = attribute;
attributeIndex = index;
namespaceParent = element;
break;
case XPathNamespaceScope.All:
attributes = element.Attributes;
if (!MoveToFirstNamespaceGlobal(ref attributes, ref index)) {
source = document.NamespaceXml;
// attributeIndex = 0;
}
else {
source = attributes[index];
attributeIndex = index;
}
namespaceParent = element;
break;
default:
Debug.Assert(false);
return false;
}
return true;
}
private static bool MoveToFirstNamespaceLocal(XmlAttributeCollection attributes, ref int index) {
Debug.Assert(attributes != null);
for (int i = attributes.Count - 1; i >= 0; i--) {
XmlAttribute attribute = attributes[i];
if (attribute.IsNamespace) {
index = i;
return true;
}
}
return false;
}
private static bool MoveToFirstNamespaceGlobal(ref XmlAttributeCollection attributes, ref int index) {
if (MoveToFirstNamespaceLocal(attributes, ref index)) {
return true;
}
Debug.Assert(attributes != null && attributes.parent != null);
XmlElement element = attributes.parent.ParentNode as XmlElement;
while (element != null) {
if (element.HasAttributes) {
attributes = element.Attributes;
if (MoveToFirstNamespaceLocal(attributes, ref index)) {
return true;
}
}
element = element.ParentNode as XmlElement;
}
return false;
}
public override bool MoveToNextNamespace(XPathNamespaceScope scope) {
XmlAttribute attribute = source as XmlAttribute;
if (attribute == null
|| !attribute.IsNamespace) {
return false;
}
XmlAttributeCollection attributes;
int index = attributeIndex;
if (!CheckAttributePosition(attribute, out attributes, index)
&& !ResetAttributePosition(attribute, attributes, out index)) {
return false;
}
Debug.Assert(namespaceParent != null);
switch (scope) {
case XPathNamespaceScope.Local:
if (attribute.OwnerElement != namespaceParent) {
return false;
}
if (!MoveToNextNamespaceLocal(attributes, ref index)) {
return false;
}
source = attributes[index];
attributeIndex = index;
break;
case XPathNamespaceScope.ExcludeXml:
string localName;
do {
if (!MoveToNextNamespaceGlobal(ref attributes, ref index)) {
return false;
}
attribute = attributes[index];
localName = attribute.LocalName;
}
while (PathHasDuplicateNamespace(attribute.OwnerElement, namespaceParent, localName)
|| Ref.Equal(localName, document.strXml));
source = attribute;
attributeIndex = index;
break;
case XPathNamespaceScope.All:
do {
if (!MoveToNextNamespaceGlobal(ref attributes, ref index)) {
if (PathHasDuplicateNamespace(null, namespaceParent, document.strXml)) {
return false;
}
else {
source = document.NamespaceXml;
// attributeIndex = 0;
return true;
}
}
attribute = attributes[index];
}
while (PathHasDuplicateNamespace(attribute.OwnerElement, namespaceParent, attribute.LocalName));
source = attribute;
attributeIndex = index;
break;
default:
Debug.Assert(false);
return false;
}
return true;
}
private static bool MoveToNextNamespaceLocal(XmlAttributeCollection attributes, ref int index) {
Debug.Assert(attributes != null);
Debug.Assert(0 <= index && index < attributes.Count);
for (int i = index - 1; i >= 0; i--) {
XmlAttribute attribute = attributes[i];
if (attribute.IsNamespace) {
index = i;
return true;
}
}
return false;
}
private static bool MoveToNextNamespaceGlobal(ref XmlAttributeCollection attributes, ref int index) {
if (MoveToNextNamespaceLocal(attributes, ref index)) {
return true;
}
Debug.Assert(attributes != null && attributes.parent != null);
XmlElement element = attributes.parent.ParentNode as XmlElement;
while (element != null) {
if (element.HasAttributes) {
attributes = element.Attributes;
if (MoveToFirstNamespaceLocal(attributes, ref index)) {
return true;
}
}
element = element.ParentNode as XmlElement;
}
return false;
}
private bool PathHasDuplicateNamespace(XmlElement top, XmlElement bottom, string localName) {
string namespaceUri = document.strReservedXmlns;
while (bottom != null
&& bottom != top) {
XmlAttribute attribute = bottom.GetAttributeNode(localName, namespaceUri);
if (attribute != null) {
return true;
}
bottom = bottom.ParentNode as XmlElement;
}
return false;
}
public override string LookupNamespace(string prefix) {
string ns = base.LookupNamespace(prefix);
if (ns != null) {
ns = this.NameTable.Add(ns);
}
return ns;
}
public override bool MoveToNext() {
XmlNode sibling = NextSibling(source);
if (sibling == null) {
return false;
}
if (sibling.IsText) {
if (source.IsText) {
sibling = NextSibling(TextEnd(sibling));
if (sibling == null) {
return false;
}
}
}
XmlNode parent = ParentNode(sibling);
Debug.Assert(parent != null);
while (!IsValidChild(parent, sibling)) {
sibling = NextSibling(sibling);
if (sibling == null) {
return false;
}
}
source = sibling;
return true;
}
public override bool MoveToPrevious() {
XmlNode sibling = PreviousSibling(source);
if (sibling == null) {
return false;
}
if (sibling.IsText) {
if (source.IsText) {
sibling = PreviousSibling(TextStart(sibling));
if (sibling == null) {
return false;
}
}
else {
sibling = TextStart(sibling);
}
}
XmlNode parent = ParentNode(sibling);
Debug.Assert(parent != null);
while (!IsValidChild(parent, sibling)) {
sibling = PreviousSibling(sibling);
if (sibling == null) {
return false;
}
// if (sibling.IsText) {
// sibling = TextStart(sibling);
// }
}
source = sibling;
return true;
}
public override bool MoveToFirst() {
if (source.NodeType == XmlNodeType.Attribute) {
return false;
}
XmlNode parent = ParentNode(source);
if (parent == null) {
return false;
}
XmlNode sibling = FirstChild(parent);
Debug.Assert(sibling != null);
while (!IsValidChild(parent, sibling)) {
sibling = NextSibling(sibling);
if (sibling == null) {
return false;
}
}
source = sibling;
return true;
}
public override bool MoveToFirstChild() {
XmlNode child;
switch (source.NodeType) {
case XmlNodeType.Element:
child = FirstChild(source);
if (child == null) {
return false;
}
break;
case XmlNodeType.DocumentFragment:
case XmlNodeType.Document:
child = FirstChild(source);
if (child == null) {
return false;
}
while (!IsValidChild(source, child)) {
child = NextSibling(child);
if (child == null) {
return false;
}
}
break;
default:
return false;
}
source = child;
return true;
}
public override bool MoveToParent() {
XmlNode parent = ParentNode(source);
if (parent != null) {
source = parent;
return true;
}
XmlAttribute attribute = source as XmlAttribute;
if (attribute != null) {
parent = attribute.IsNamespace ? namespaceParent : attribute.OwnerElement;
if (parent != null) {
source = parent;
namespaceParent = null;
return true;
}
}
return false;
}
public override void MoveToRoot() {
for (;;) {
XmlNode parent = source.ParentNode;
if (parent == null) {
XmlAttribute attribute = source as XmlAttribute;
if (attribute == null) {
break;
}
parent = attribute.IsNamespace ? namespaceParent : attribute.OwnerElement;
if (parent == null) {
break;
}
}
source = parent;
}
namespaceParent = null;
}
public override bool MoveTo(XPathNavigator other) {
DocumentXPathNavigator that = other as DocumentXPathNavigator;
if (that != null
&& document == that.document) {
source = that.source;
attributeIndex = that.attributeIndex;
namespaceParent = that.namespaceParent;
return true;
}
return false;
}
public override bool MoveToId(string id) {
XmlElement element = document.GetElementById(id);
if (element != null) {
source = element;
namespaceParent = null;
return true;
}
return false;
}
public override bool MoveToChild(string localName, string namespaceUri) {
if (source.NodeType == XmlNodeType.Attribute) {
return false;
}
XmlNode child = FirstChild(source);
if (child != null) {
do {
if (child.NodeType == XmlNodeType.Element
&& child.LocalName == localName
&& child.NamespaceURI == namespaceUri) {
source = child;
return true;
}
child = NextSibling(child);
}
while (child != null);
}
return false;
}
public override bool MoveToChild(XPathNodeType type) {
if (source.NodeType == XmlNodeType.Attribute) {
return false;
}
XmlNode child = FirstChild(source);
if (child != null) {
int mask = GetContentKindMask(type);
if (mask == 0) {
return false;
}
do {
if (((1 << (int)child.XPNodeType) & mask) != 0) {
source = child;
return true;
}
child = NextSibling(child);
}
while (child != null);
}
return false;
}
public override bool MoveToFollowing(string localName, string namespaceUri, XPathNavigator end) {
XmlNode pastFollowing = null;
DocumentXPathNavigator that = end as DocumentXPathNavigator;
if (that != null) {
if (document != that.document) {
return false;
}
switch (that.source.NodeType) {
case XmlNodeType.Attribute:
that = (DocumentXPathNavigator)that.Clone();
if (!that.MoveToNonDescendant()) {
return false;
}
break;
}
pastFollowing = that.source;
}
XmlNode following = source;
if (following.NodeType == XmlNodeType.Attribute) {
following = ((XmlAttribute)following).OwnerElement;
if (following == null) {
return false;
}
}
do {
XmlNode firstChild = following.FirstChild;
if (firstChild != null) {
following = firstChild;
}
else {
for (;;) {
XmlNode nextSibling = following.NextSibling;
if (nextSibling != null) {
following = nextSibling;
break;
}
else {
XmlNode parent = following.ParentNode;
if (parent != null) {
following = parent;
}
else {
return false;
}
}
}
}
if (following == pastFollowing) {
return false;
}
}
while (following.NodeType != XmlNodeType.Element
|| following.LocalName != localName
|| following.NamespaceURI != namespaceUri);
source = following;
return true;
}
public override bool MoveToFollowing(XPathNodeType type, XPathNavigator end) {
XmlNode pastFollowing = null;
DocumentXPathNavigator that = end as DocumentXPathNavigator;
if (that != null) {
if (document != that.document) {
return false;
}
switch (that.source.NodeType) {
case XmlNodeType.Attribute:
that = (DocumentXPathNavigator)that.Clone();
if (!that.MoveToNonDescendant()) {
return false;
}
break;
}
pastFollowing = that.source;
}
int mask = GetContentKindMask(type);
if (mask == 0) {
return false;
}
XmlNode following = source;
switch (following.NodeType) {
case XmlNodeType.Attribute:
following = ((XmlAttribute)following).OwnerElement;
if (following == null) {
return false;
}
break;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.SignificantWhitespace:
case XmlNodeType.Whitespace:
following = TextEnd(following);
break;
}
do {
XmlNode firstChild = following.FirstChild;
if (firstChild != null) {
following = firstChild;
}
else {
for (;;) {
XmlNode nextSibling = following.NextSibling;
if (nextSibling != null) {
following = nextSibling;
break;
}
else {
XmlNode parent = following.ParentNode;
if (parent != null) {
following = parent;
}
else {
return false;
}
}
}
}
if (following == pastFollowing) {
return false;
}
}
while (((1 << (int)following.XPNodeType) & mask) == 0);
source = following;
return true;
}
public override bool MoveToNext(string localName, string namespaceUri) {
XmlNode sibling = NextSibling(source);
if (sibling == null) {
return false;
}
do {
if (sibling.NodeType == XmlNodeType.Element
&& sibling.LocalName == localName
&& sibling.NamespaceURI == namespaceUri) {
source = sibling;
return true;
}
sibling = NextSibling(sibling);
}
while (sibling != null);
return false;
}
public override bool MoveToNext(XPathNodeType type) {
XmlNode sibling = NextSibling(source);
if (sibling == null) {
return false;
}
if (sibling.IsText
&& source.IsText) {
sibling = NextSibling(TextEnd(sibling));
if (sibling == null) {
return false;
}
}
int mask = GetContentKindMask(type);
if (mask == 0) {
return false;
}
do {
if (((1 << (int)sibling.XPNodeType) & mask) != 0) {
source = sibling;
return true;
}
sibling = NextSibling(sibling);
}
while (sibling != null);
return false;
}
public override bool HasChildren {
get {
XmlNode child;
switch (source.NodeType) {
case XmlNodeType.Element:
child = FirstChild(source);
if (child == null) {
return false;
}
return true;
case XmlNodeType.DocumentFragment:
case XmlNodeType.Document:
child = FirstChild(source);
if (child == null) {
return false;
}
while (!IsValidChild(source, child)) {
child = NextSibling(child);
if (child == null) {
return false;
}
}
return true;
default:
return false;
}
}
}
public override bool IsSamePosition(XPathNavigator other) {
DocumentXPathNavigator that = other as DocumentXPathNavigator;
if (that != null) {
this.CalibrateText();
that.CalibrateText();
return this.source == that.source
&& this.namespaceParent == that.namespaceParent;
}
return false;
}
public override bool IsDescendant(XPathNavigator other) {
DocumentXPathNavigator that = other as DocumentXPathNavigator;
if (that != null) {
return IsDescendant(this.source, that.source);
}
return false;
}
public override IXmlSchemaInfo SchemaInfo {
get {
return source.SchemaInfo;
}
}
public override bool CheckValidity(XmlSchemaSet schemas, ValidationEventHandler validationEventHandler) {
XmlDocument ownerDocument;
if (source.NodeType == XmlNodeType.Document) {
ownerDocument = (XmlDocument)source;
}
else {
ownerDocument = source.OwnerDocument;
if (schemas != null) {
throw new ArgumentException(Res.GetString(Res.XPathDocument_SchemaSetNotAllowed, null));
}
}
if (schemas == null && ownerDocument != null) {
schemas = ownerDocument.Schemas;
}
if (schemas == null || schemas.Count == 0) {
throw new InvalidOperationException(Res.GetString(Res.XmlDocument_NoSchemaInfo));
}
DocumentSchemaValidator validator = new DocumentSchemaValidator(ownerDocument, schemas, validationEventHandler);
validator.PsviAugmentation = false;
return validator.Validate(source);
}
private static XmlNode OwnerNode(XmlNode node) {
XmlNode parent = node.ParentNode;
if (parent != null) {
return parent;
}
XmlAttribute attribute = node as XmlAttribute;
if (attribute != null) {
return attribute.OwnerElement;
}
return null;
}
private static int GetDepth(XmlNode node) {
int depth = 0;
XmlNode owner = OwnerNode(node);
while (owner != null) {
depth++;
owner = OwnerNode(owner);
}
return depth;
}
//Assuming that node1 and node2 are in the same level; Except when they are namespace nodes, they should have the same parent node
//the returned value is node2's position corresponding to node1
private XmlNodeOrder Compare( XmlNode node1, XmlNode node2 ) {
Debug.Assert( node1 != null );
Debug.Assert( node2 != null );
Debug.Assert( node1 != node2, "Should be handled by ComparePosition()" );
//Attribute nodes come before other children nodes except namespace nodes
Debug.Assert( OwnerNode(node1) == OwnerNode(node2) );
if (node1.XPNodeType == XPathNodeType.Attribute) {
if (node2.XPNodeType == XPathNodeType.Attribute) {
XmlElement element = ((XmlAttribute)node1).OwnerElement;
if (element.HasAttributes) {
XmlAttributeCollection attributes = element.Attributes;
for (int i = 0; i < attributes.Count; i++) {
XmlAttribute attribute = attributes[i];
if (attribute == node1) {
return XmlNodeOrder.Before;
}
else if (attribute == node2) {
return XmlNodeOrder.After;
}
}
}
return XmlNodeOrder.Unknown;
}
else {
return XmlNodeOrder.Before;
}
}
if (node2.XPNodeType == XPathNodeType.Attribute) {
return XmlNodeOrder.After;
}
//neither of the node is Namespace node or Attribute node
XmlNode nextNode = node1.NextSibling;
while ( nextNode != null && nextNode != node2 )
nextNode = nextNode.NextSibling;
if ( nextNode == null )
//didn't meet node2 in the path to the end, thus it has to be in the front of node1
return XmlNodeOrder.After;
else
//met node2 in the path to the end, so node1 is at front
return XmlNodeOrder.Before;
}
public override XmlNodeOrder ComparePosition(XPathNavigator other) {
DocumentXPathNavigator that = other as DocumentXPathNavigator;
if (that == null) {
return XmlNodeOrder.Unknown;
}
this.CalibrateText();
that.CalibrateText();
if (this.source == that.source
&& this.namespaceParent == that.namespaceParent) {
return XmlNodeOrder.Same;
}
if (this.namespaceParent != null
|| that.namespaceParent != null) {
return base.ComparePosition(other);
}
XmlNode node1 = this.source;
XmlNode node2 = that.source;
XmlNode parent1 = OwnerNode(node1);
XmlNode parent2 = OwnerNode(node2);
if (parent1 == parent2) {
if (parent1 == null) {
return XmlNodeOrder.Unknown;
}
else {
Debug.Assert(node1 != node2);
return Compare(node1, node2);
}
}
int depth1 = GetDepth(node1);
int depth2 = GetDepth(node2);
if (depth2 > depth1) {
while (node2 != null
&& depth2 > depth1) {
node2 = OwnerNode(node2);
depth2--;
}
if (node1 == node2) {
return XmlNodeOrder.Before;
}
parent2 = OwnerNode(node2);
}
else if (depth1 > depth2) {
while (node1 != null
&& depth1 > depth2) {
node1 = OwnerNode(node1);
depth1--;
}
if (node1 == node2) {
return XmlNodeOrder.After;
}
parent1 = OwnerNode(node1);
}
while (parent1 != null
&& parent2 != null) {
if (parent1 == parent2) {
Debug.Assert(node1 != node2);
return Compare(node1, node2);
}
node1 = parent1;
node2 = parent2;
parent1 = OwnerNode(node1);
parent2 = OwnerNode(node2);
}
return XmlNodeOrder.Unknown;
}
//the function just for XPathNodeList to enumerate current Node.
XmlNode IHasXmlNode.GetNode() { return source; }
public override XPathNodeIterator SelectDescendants( string localName, string namespaceURI, bool matchSelf ) {
string nsAtom = document.NameTable.Get( namespaceURI );
if ( nsAtom == null || this.source.NodeType == XmlNodeType.Attribute )
return new DocumentXPathNodeIterator_Empty( this );
Debug.Assert( this.NodeType != XPathNodeType.Attribute && this.NodeType != XPathNodeType.Namespace && this.NodeType != XPathNodeType.All );
string localNameAtom = document.NameTable.Get( localName );
if ( localNameAtom == null )
return new DocumentXPathNodeIterator_Empty( this );
if ( localNameAtom.Length == 0 ) {
if ( matchSelf )
return new DocumentXPathNodeIterator_ElemChildren_AndSelf_NoLocalName( this, nsAtom );
return new DocumentXPathNodeIterator_ElemChildren_NoLocalName( this, nsAtom );
}
if ( matchSelf )
return new DocumentXPathNodeIterator_ElemChildren_AndSelf( this, localNameAtom, nsAtom );
return new DocumentXPathNodeIterator_ElemChildren( this, localNameAtom, nsAtom );
}
public override XPathNodeIterator SelectDescendants( XPathNodeType nt, bool includeSelf ) {
if ( nt == XPathNodeType.Element ) {
XmlNodeType curNT = source.NodeType;
if ( curNT != XmlNodeType.Document && curNT != XmlNodeType.Element ) {
//only Document, Entity, Element node can have Element node as children ( descendant )
//entity nodes should be invisible to XPath data model
return new DocumentXPathNodeIterator_Empty( this );
}
if ( includeSelf )
return new DocumentXPathNodeIterator_AllElemChildren_AndSelf( this );
return new DocumentXPathNodeIterator_AllElemChildren( this );
}
return base.SelectDescendants( nt, includeSelf );
}
public override bool CanEdit {
get {
return true;
}
}
public override XmlWriter PrependChild() {
switch (source.NodeType) {
case XmlNodeType.Element:
case XmlNodeType.Document:
case XmlNodeType.DocumentFragment:
break;
default:
throw new InvalidOperationException(Res.GetString(Res.Xpn_BadPosition));
}
DocumentXmlWriter writer = new DocumentXmlWriter(DocumentXmlWriterType.PrependChild, source, document);
writer.NamespaceManager = GetNamespaceManager(source, document);
return new XmlWellFormedWriter(writer, writer.Settings);
}
public override XmlWriter AppendChild() {
switch (source.NodeType) {
case XmlNodeType.Element:
case XmlNodeType.Document:
case XmlNodeType.DocumentFragment:
break;
default:
throw new InvalidOperationException(Res.GetString(Res.Xpn_BadPosition));
}
DocumentXmlWriter writer = new DocumentXmlWriter(DocumentXmlWriterType.AppendChild, source, document);
writer.NamespaceManager = GetNamespaceManager(source, document);
return new XmlWellFormedWriter(writer, writer.Settings);
}
public override XmlWriter InsertAfter() {
XmlNode node = source;
switch (node.NodeType) {
case XmlNodeType.Attribute:
case XmlNodeType.Document:
case XmlNodeType.DocumentFragment:
throw new InvalidOperationException(Res.GetString(Res.Xpn_BadPosition));
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.SignificantWhitespace:
case XmlNodeType.Whitespace:
node = TextEnd(node);
break;
default:
break;
}
DocumentXmlWriter writer = new DocumentXmlWriter(DocumentXmlWriterType.InsertSiblingAfter, node, document);
writer.NamespaceManager = GetNamespaceManager(node.ParentNode, document);
return new XmlWellFormedWriter(writer, writer.Settings);
}
public override XmlWriter InsertBefore() {
switch (source.NodeType) {
case XmlNodeType.Attribute:
case XmlNodeType.Document:
case XmlNodeType.DocumentFragment:
throw new InvalidOperationException(Res.GetString(Res.Xpn_BadPosition));
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.SignificantWhitespace:
case XmlNodeType.Whitespace:
CalibrateText();
break;
default:
break;
}
DocumentXmlWriter writer = new DocumentXmlWriter(DocumentXmlWriterType.InsertSiblingBefore, source, document);
writer.NamespaceManager = GetNamespaceManager(source.ParentNode, document);
return new XmlWellFormedWriter(writer, writer.Settings);
}
public override XmlWriter CreateAttributes() {
if (source.NodeType != XmlNodeType.Element) {
throw new InvalidOperationException(Res.GetString(Res.Xpn_BadPosition));
}
DocumentXmlWriter writer = new DocumentXmlWriter(DocumentXmlWriterType.AppendAttribute, source, document);
writer.NamespaceManager = GetNamespaceManager(source, document);
return new XmlWellFormedWriter(writer, writer.Settings);
}
public override XmlWriter ReplaceRange(XPathNavigator lastSiblingToReplace) {
DocumentXPathNavigator that = lastSiblingToReplace as DocumentXPathNavigator;
if (that == null) {
if (lastSiblingToReplace == null) {
throw new ArgumentNullException("lastSiblingToReplace");
}
else {
throw new NotSupportedException();
}
}
this.CalibrateText();
that.CalibrateText();
XmlNode node = this.source;
XmlNode end = that.source;
if (node == end) {
switch (node.NodeType) {
case XmlNodeType.Attribute:
case XmlNodeType.Document:
case XmlNodeType.DocumentFragment:
throw new InvalidOperationException(Res.GetString(Res.Xpn_BadPosition));
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.SignificantWhitespace:
case XmlNodeType.Whitespace:
end = that.TextEnd(end);
break;
default:
break;
}
}
else {
if (end.IsText) {
end = that.TextEnd(end);
}
if (!IsFollowingSibling(node, end)) {
throw new InvalidOperationException(Res.GetString(Res.Xpn_BadPosition));
}
}
DocumentXmlWriter writer = new DocumentXmlWriter(DocumentXmlWriterType.ReplaceToFollowingSibling, node, document);
writer.NamespaceManager = GetNamespaceManager(node.ParentNode, document);
writer.Navigator = this;
writer.EndNode = end;
return new XmlWellFormedWriter(writer, writer.Settings);
}
public override void DeleteRange(XPathNavigator lastSiblingToDelete) {
DocumentXPathNavigator that = lastSiblingToDelete as DocumentXPathNavigator;
if (that == null) {
if (lastSiblingToDelete == null) {
throw new ArgumentNullException("lastSiblingToDelete");
}
else {
throw new NotSupportedException();
}
}
this.CalibrateText();
that.CalibrateText();
XmlNode node = this.source;
XmlNode end = that.source;
if (node == end) {
switch (node.NodeType) {
case XmlNodeType.Attribute:
XmlAttribute attribute = (XmlAttribute)node;
if (attribute.IsNamespace) {
goto default;
}
XmlNode parent = OwnerNode(attribute);
DeleteAttribute(attribute, attributeIndex);
if (parent != null) {
ResetPosition(parent);
}
break;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.SignificantWhitespace:
case XmlNodeType.Whitespace:
end = that.TextEnd(end);
goto case XmlNodeType.Element;
case XmlNodeType.Element:
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.Comment:
parent = OwnerNode(node);
DeleteToFollowingSibling(node, end);
if (parent != null) {
ResetPosition(parent);
}
break;
default:
throw new InvalidOperationException(Res.GetString(Res.Xpn_BadPosition));
}
}
else {
if (end.IsText) {
end = that.TextEnd(end);
}
if (!IsFollowingSibling(node, end)) {
throw new InvalidOperationException(Res.GetString(Res.Xpn_BadPosition));
}
XmlNode parent = OwnerNode(node);
DeleteToFollowingSibling(node, end);
if (parent != null) {
ResetPosition(parent);
}
}
}
public override void DeleteSelf() {
XmlNode node = source;
XmlNode end = node;
switch (node.NodeType) {
case XmlNodeType.Attribute:
XmlAttribute attribute = (XmlAttribute)node;
if (attribute.IsNamespace) {
goto default;
}
XmlNode parent = OwnerNode(attribute);
DeleteAttribute(attribute, attributeIndex);
if (parent != null) {
ResetPosition(parent);
}
break;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.SignificantWhitespace:
case XmlNodeType.Whitespace:
CalibrateText();
node = source;
end = TextEnd(node);
goto case XmlNodeType.Element;
case XmlNodeType.Element:
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.Comment:
parent = OwnerNode(node);
DeleteToFollowingSibling(node, end);
if (parent != null) {
ResetPosition(parent);
}
break;
default:
throw new InvalidOperationException(Res.GetString(Res.Xpn_BadPosition));
}
}
private static void DeleteAttribute(XmlAttribute attribute, int index) {
XmlAttributeCollection attributes;
if (!CheckAttributePosition(attribute, out attributes, index)
&& !ResetAttributePosition(attribute, attributes, out index)) {
throw new InvalidOperationException(Res.GetString(Res.Xpn_MissingParent));
}
if (attribute.IsReadOnly) {
throw new InvalidOperationException(Res.GetString(Res.Xdom_Node_Modify_ReadOnly));
}
attributes.RemoveAt(index);
}
internal static void DeleteToFollowingSibling(XmlNode node, XmlNode end) {
XmlNode parent = node.ParentNode;
if (parent == null) {
throw new InvalidOperationException(Res.GetString(Res.Xpn_MissingParent));
}
if (node.IsReadOnly
|| end.IsReadOnly) {
throw new InvalidOperationException(Res.GetString(Res.Xdom_Node_Modify_ReadOnly));
}
while (node != end) {
XmlNode temp = node;
node = node.NextSibling;
parent.RemoveChild(temp);
}
parent.RemoveChild(node);
}
private static XmlNamespaceManager GetNamespaceManager(XmlNode node, XmlDocument document) {
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(document.NameTable);
List<XmlElement> elements = new List<XmlElement>();
while (node != null) {
XmlElement element = node as XmlElement;
if (element != null
&& element.HasAttributes) {
elements.Add(element);
}
node = node.ParentNode;
}
for (int i = elements.Count - 1; i >= 0; i--) {
namespaceManager.PushScope();
XmlAttributeCollection attributes = elements[i].Attributes;
for (int j = 0; j < attributes.Count; j++) {
XmlAttribute attribute = attributes[j];
if (attribute.IsNamespace) {
string prefix = attribute.Prefix.Length == 0 ? string.Empty : attribute.LocalName;
namespaceManager.AddNamespace(prefix, attribute.Value);
}
}
}
return namespaceManager;
}
internal void ResetPosition(XmlNode node) {
Debug.Assert(node != null, "Undefined navigator position");
Debug.Assert(node == document || node.OwnerDocument == document, "Navigator switched documents");
source = node;
XmlAttribute attribute = node as XmlAttribute;
if (attribute != null) {
XmlElement element = attribute.OwnerElement;
if (element != null) {
ResetAttributePosition(attribute, element.Attributes, out attributeIndex);
if (attribute.IsNamespace) {
namespaceParent = element;
}
}
}
}
private static bool ResetAttributePosition(XmlAttribute attribute, XmlAttributeCollection attributes, out int index) {
if (attributes != null) {
for (int i = 0; i < attributes.Count; i++) {
if (attribute == attributes[i]) {
index = i;
return true;
}
}
}
index = 0;
return false;
}
private static bool CheckAttributePosition(XmlAttribute attribute, out XmlAttributeCollection attributes, int index) {
XmlElement element = attribute.OwnerElement;
if (element != null) {
attributes = element.Attributes;
if (index >= 0
&& index < attributes.Count
&& attribute == attributes[index]) {
return true;
}
}
else {
attributes = null;
}
return false;
}
private void CalibrateText() {
XmlNode text = PreviousText(source);
while (text != null) {
ResetPosition(text);
text = PreviousText(text);
}
}
private XmlNode ParentNode(XmlNode node) {
XmlNode parent = node.ParentNode;
if (!document.HasEntityReferences) {
return parent;
}
return ParentNodeTail(parent);
}
private XmlNode ParentNodeTail(XmlNode parent) {
while (parent != null
&& parent.NodeType == XmlNodeType.EntityReference) {
parent = parent.ParentNode;
}
return parent;
}
private XmlNode FirstChild(XmlNode node) {
XmlNode child = node.FirstChild;
if (!document.HasEntityReferences) {
return child;
}
return FirstChildTail(child);
}
private XmlNode FirstChildTail(XmlNode child) {
while (child != null
&& child.NodeType == XmlNodeType.EntityReference) {
child = child.FirstChild;
}
return child;
}
private XmlNode NextSibling(XmlNode node) {
XmlNode sibling = node.NextSibling;
if (!document.HasEntityReferences) {
return sibling;
}
return NextSiblingTail(node, sibling);
}
private XmlNode NextSiblingTail(XmlNode node, XmlNode sibling) {
while (sibling == null) {
node = node.ParentNode;
if (node == null
|| node.NodeType != XmlNodeType.EntityReference) {
return null;
}
sibling = node.NextSibling;
}
while (sibling != null
&& sibling.NodeType == XmlNodeType.EntityReference) {
sibling = sibling.FirstChild;
}
return sibling;
}
private XmlNode PreviousSibling(XmlNode node) {
XmlNode sibling = node.PreviousSibling;
if (!document.HasEntityReferences) {
return sibling;
}
return PreviousSiblingTail(node, sibling);
}
private XmlNode PreviousSiblingTail(XmlNode node, XmlNode sibling) {
while (sibling == null) {
node = node.ParentNode;
if (node == null
|| node.NodeType != XmlNodeType.EntityReference) {
return null;
}
sibling = node.PreviousSibling;
}
while (sibling != null
&& sibling.NodeType == XmlNodeType.EntityReference) {
sibling = sibling.LastChild;
}
return sibling;
}
private XmlNode PreviousText(XmlNode node) {
XmlNode text = node.PreviousText;
if (!document.HasEntityReferences) {
return text;
}
return PreviousTextTail(node, text);
}
private XmlNode PreviousTextTail(XmlNode node, XmlNode text) {
if (text != null) {
return text;
}
if (!node.IsText) {
return null;
}
XmlNode sibling = node.PreviousSibling;
while (sibling == null) {
node = node.ParentNode;
if (node == null
|| node.NodeType != XmlNodeType.EntityReference) {
return null;
}
sibling = node.PreviousSibling;
}
while (sibling != null) {
switch (sibling.NodeType) {
case XmlNodeType.EntityReference:
sibling = sibling.LastChild;
break;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
return sibling;
default:
return null;
}
}
return null;
}
internal static bool IsFollowingSibling(XmlNode left, XmlNode right) {
for (;;) {
left = left.NextSibling;
if (left == null) {
break;
}
if (left == right) {
return true;
}
}
return false;
}
private static bool IsDescendant(XmlNode top, XmlNode bottom) {
for (;;) {
XmlNode parent = bottom.ParentNode;
if (parent == null) {
XmlAttribute attribute = bottom as XmlAttribute;
if (attribute == null) {
break;
}
parent = attribute.OwnerElement;
if (parent == null) {
break;
}
}
bottom = parent;
if (top == bottom) {
return true;
}
}
return false;
}
private static bool IsValidChild(XmlNode parent, XmlNode child) {
switch (parent.NodeType) {
case XmlNodeType.Element:
return true;
case XmlNodeType.DocumentFragment:
switch (child.NodeType) {
case XmlNodeType.Element:
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.Comment:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
return true;
}
break;
case XmlNodeType.Document:
switch (child.NodeType) {
case XmlNodeType.Element:
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.Comment:
return true;
}
break;
default:
break;
}
return false;
}
private XmlNode TextStart(XmlNode node) {
XmlNode start;
do {
start = node;
node = PreviousSibling(node);
}
while (node != null
&& node.IsText);
return start;
}
private XmlNode TextEnd(XmlNode node) {
XmlNode end;
do {
end = node;
node = NextSibling(node);
}
while (node != null
&& node.IsText);
return end;
}
}
// An iterator that matches no nodes
internal sealed class DocumentXPathNodeIterator_Empty : XPathNodeIterator {
private XPathNavigator nav;
internal DocumentXPathNodeIterator_Empty( DocumentXPathNavigator nav ) { this.nav = nav.Clone(); }
internal DocumentXPathNodeIterator_Empty( DocumentXPathNodeIterator_Empty other ) { this.nav = other.nav.Clone(); }
public override XPathNodeIterator Clone() { return new DocumentXPathNodeIterator_Empty( this ); }
public override bool MoveNext() { return false; }
public override XPathNavigator Current { get { return nav; } }
public override int CurrentPosition { get { return 0; } }
public override int Count { get { return 0; } }
}
// An iterator that can match any child elements that match the Match condition (overrided in the derived class)
internal abstract class DocumentXPathNodeIterator_ElemDescendants : XPathNodeIterator {
private DocumentXPathNavigator nav;
private int level;
private int position;
internal DocumentXPathNodeIterator_ElemDescendants( DocumentXPathNavigator nav ) {
this.nav = (DocumentXPathNavigator)(nav.Clone());
this.level = 0;
this.position = 0;
}
internal DocumentXPathNodeIterator_ElemDescendants( DocumentXPathNodeIterator_ElemDescendants other ) {
this.nav = (DocumentXPathNavigator)(other.nav.Clone());
this.level = other.level;
this.position = other.position;
}
protected abstract bool Match( XmlNode node );
public override XPathNavigator Current {
get { return nav; }
}
public override int CurrentPosition {
get { return position; }
}
protected void SetPosition( int pos ) {
position = pos;
}
public override bool MoveNext() {
for (;;) {
if (nav.MoveToFirstChild()) {
level++;
}
else {
if (level == 0) {
return false;
}
while (!nav.MoveToNext()) {
level--;
if (level == 0) {
return false;
}
if (!nav.MoveToParent()) {
return false;
}
}
}
XmlNode node = (XmlNode)nav.UnderlyingObject;
if (node.NodeType == XmlNodeType.Element && Match(node)) {
position++;
return true;
}
}
}
}
// Iterate over all element children irrespective of the localName and namespace
internal class DocumentXPathNodeIterator_AllElemChildren : DocumentXPathNodeIterator_ElemDescendants {
internal DocumentXPathNodeIterator_AllElemChildren( DocumentXPathNavigator nav ) : base( nav ) {
Debug.Assert( ((XmlNode)nav.UnderlyingObject).NodeType != XmlNodeType.Attribute );
}
internal DocumentXPathNodeIterator_AllElemChildren( DocumentXPathNodeIterator_AllElemChildren other ) : base( other ) {
}
public override XPathNodeIterator Clone() {
return new DocumentXPathNodeIterator_AllElemChildren( this );
}
protected override bool Match( XmlNode node ) {
Debug.Assert( node != null );
return ( node.NodeType == XmlNodeType.Element );
}
}
// Iterate over all element children irrespective of the localName and namespace, include the self node when testing for localName/ns
internal sealed class DocumentXPathNodeIterator_AllElemChildren_AndSelf : DocumentXPathNodeIterator_AllElemChildren {
internal DocumentXPathNodeIterator_AllElemChildren_AndSelf( DocumentXPathNavigator nav ) : base( nav ) {
}
internal DocumentXPathNodeIterator_AllElemChildren_AndSelf( DocumentXPathNodeIterator_AllElemChildren_AndSelf other ) : base( other ) {
}
public override XPathNodeIterator Clone() {
return new DocumentXPathNodeIterator_AllElemChildren_AndSelf( this );
}
public override bool MoveNext() {
if( CurrentPosition == 0 ) {
DocumentXPathNavigator nav = (DocumentXPathNavigator)this.Current;
XmlNode node = (XmlNode)nav.UnderlyingObject;
if ( node.NodeType == XmlNodeType.Element && Match( node ) ) {
SetPosition( 1 );
return true;
}
}
return base.MoveNext();
}
}
// Iterate over all element children that have a given namespace but irrespective of the localName
internal class DocumentXPathNodeIterator_ElemChildren_NoLocalName : DocumentXPathNodeIterator_ElemDescendants {
private string nsAtom;
internal DocumentXPathNodeIterator_ElemChildren_NoLocalName( DocumentXPathNavigator nav, string nsAtom ) : base( nav ) {
Debug.Assert( ((XmlNode)nav.UnderlyingObject).NodeType != XmlNodeType.Attribute );
Debug.Assert( Ref.Equal(nav.NameTable.Get( nsAtom ), nsAtom) );
this.nsAtom = nsAtom;
}
internal DocumentXPathNodeIterator_ElemChildren_NoLocalName( DocumentXPathNodeIterator_ElemChildren_NoLocalName other ) : base( other ) {
this.nsAtom = other.nsAtom;
}
public override XPathNodeIterator Clone() {
return new DocumentXPathNodeIterator_ElemChildren_NoLocalName( this );
}
protected override bool Match( XmlNode node ) {
Debug.Assert( node != null );
Debug.Assert( node.NodeType == XmlNodeType.Element );
return Ref.Equal(node.NamespaceURI, nsAtom);
}
}
// Iterate over all element children that have a given namespace but irrespective of the localName, include self node when checking for ns
internal sealed class DocumentXPathNodeIterator_ElemChildren_AndSelf_NoLocalName : DocumentXPathNodeIterator_ElemChildren_NoLocalName {
internal DocumentXPathNodeIterator_ElemChildren_AndSelf_NoLocalName( DocumentXPathNavigator nav, string nsAtom ) : base( nav, nsAtom ) {
}
internal DocumentXPathNodeIterator_ElemChildren_AndSelf_NoLocalName( DocumentXPathNodeIterator_ElemChildren_AndSelf_NoLocalName other ) : base( other ) {
}
public override XPathNodeIterator Clone() {
return new DocumentXPathNodeIterator_ElemChildren_AndSelf_NoLocalName( this );
}
public override bool MoveNext() {
if( CurrentPosition == 0 ) {
DocumentXPathNavigator nav = (DocumentXPathNavigator)this.Current;
XmlNode node = (XmlNode)nav.UnderlyingObject;
if ( node.NodeType == XmlNodeType.Element && Match( node ) ) {
SetPosition( 1 );
return true;
}
}
return base.MoveNext();
}
}
// Iterate over all element children that have a given name and namespace
internal class DocumentXPathNodeIterator_ElemChildren : DocumentXPathNodeIterator_ElemDescendants {
protected string localNameAtom;
protected string nsAtom;
internal DocumentXPathNodeIterator_ElemChildren( DocumentXPathNavigator nav, string localNameAtom, string nsAtom ) : base( nav ) {
Debug.Assert( ((XmlNode)nav.UnderlyingObject).NodeType != XmlNodeType.Attribute );
Debug.Assert( Ref.Equal(nav.NameTable.Get( localNameAtom ), localNameAtom) );
Debug.Assert( Ref.Equal(nav.NameTable.Get( nsAtom ), nsAtom) );
Debug.Assert( localNameAtom.Length > 0 ); // Use DocumentXPathNodeIterator_ElemChildren_NoLocalName class for special magic value of localNameAtom
this.localNameAtom = localNameAtom;
this.nsAtom = nsAtom;
}
internal DocumentXPathNodeIterator_ElemChildren( DocumentXPathNodeIterator_ElemChildren other ) : base( other ) {
this.localNameAtom = other.localNameAtom;
this.nsAtom = other.nsAtom;
}
public override XPathNodeIterator Clone() {
return new DocumentXPathNodeIterator_ElemChildren( this );
}
protected override bool Match( XmlNode node ) {
Debug.Assert( node != null );
Debug.Assert( node.NodeType == XmlNodeType.Element );
return Ref.Equal(node.LocalName, localNameAtom) && Ref.Equal(node.NamespaceURI, nsAtom);
}
}
// Iterate over all elem children and itself and check for the given localName (including the magic value "") and namespace
internal sealed class DocumentXPathNodeIterator_ElemChildren_AndSelf : DocumentXPathNodeIterator_ElemChildren {
internal DocumentXPathNodeIterator_ElemChildren_AndSelf( DocumentXPathNavigator nav, string localNameAtom, string nsAtom )
: base( nav, localNameAtom, nsAtom ) {
Debug.Assert( localNameAtom.Length > 0 ); // Use DocumentXPathNodeIterator_ElemChildren_AndSelf_NoLocalName if localName == String.Empty
}
internal DocumentXPathNodeIterator_ElemChildren_AndSelf( DocumentXPathNodeIterator_ElemChildren_AndSelf other ) : base( other ) {
}
public override XPathNodeIterator Clone() {
return new DocumentXPathNodeIterator_ElemChildren_AndSelf( this );
}
public override bool MoveNext() {
if( CurrentPosition == 0 ) {
DocumentXPathNavigator nav = (DocumentXPathNavigator)this.Current;
XmlNode node = (XmlNode)nav.UnderlyingObject;
if ( node.NodeType == XmlNodeType.Element && Match( node ) ) {
SetPosition( 1 );
return true;
}
}
return base.MoveNext();
}
}
}
|