XmlDocumentNavigator.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. //
  2. // System.Xml.XmlDocumentNavigator
  3. //
  4. // Authors:
  5. // Jason Diamond <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. //
  8. // (C) 2002 Jason Diamond
  9. // (C) 2003 Atsushi Enomoto
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.Xml;
  14. using System.Xml.XPath;
  15. namespace System.Xml
  16. {
  17. internal class XmlDocumentNavigator : XPathNavigator, IHasXmlNode
  18. {
  19. #region Constructors
  20. internal XmlDocumentNavigator (XmlNode node)
  21. : this (node, null)
  22. {
  23. nsNodeXml = document.CreateAttribute ("xmlns", "xml", Xmlns);
  24. nsNodeXml.Value = XmlnsXML;
  25. if (node.NodeType == XmlNodeType.Attribute && node.NamespaceURI == XmlNamespaceManager.XmlnsXmlns) {
  26. nsNode = (XmlAttribute) node;
  27. node = nsNode.OwnerElement;
  28. }
  29. }
  30. private XmlDocumentNavigator (XmlNode node, XmlAttribute nsNodeXml)
  31. {
  32. this.node = node;
  33. this.document = node.NodeType == XmlNodeType.Document ?
  34. node as XmlDocument : node.OwnerDocument;
  35. this.nsNodeXml = nsNodeXml;
  36. }
  37. #endregion
  38. #region Fields
  39. private const string Xmlns = "http://www.w3.org/2000/xmlns/";
  40. private const string XmlnsXML = "http://www.w3.org/XML/1998/namespace";
  41. private XmlAttribute nsNodeXml;
  42. private XmlNode node;
  43. private XmlDocument document;
  44. // Current namespace node (ancestor's attribute of current node).
  45. private XmlAttribute nsNode;
  46. private ArrayList iteratedNsNames = new ArrayList ();
  47. #endregion
  48. #region Properties
  49. public override string BaseURI {
  50. get {
  51. return node.BaseURI;
  52. }
  53. }
  54. public override bool HasAttributes {
  55. get {
  56. if (NsNode != null)
  57. return false;
  58. if (node.Attributes != null)
  59. for (int i = 0; i < node.Attributes.Count; i++)
  60. if (node.Attributes [i].NamespaceURI != Xmlns)
  61. return true;
  62. return false;
  63. }
  64. }
  65. public override bool HasChildren {
  66. get {
  67. if (NsNode != null)
  68. return false;
  69. XPathNodeType nodeType = NodeType;
  70. bool canHaveChildren = nodeType == XPathNodeType.Root || nodeType == XPathNodeType.Element;
  71. return canHaveChildren && node.FirstChild != null;
  72. }
  73. }
  74. public override bool IsEmptyElement {
  75. get {
  76. if (NsNode != null)
  77. return false;
  78. return node.NodeType == XmlNodeType.Element
  79. && ((XmlElement) node).IsEmpty;
  80. }
  81. }
  82. public XmlAttribute NsNode {
  83. get { return nsNode; }
  84. set {
  85. if (value == null)
  86. iteratedNsNames.Clear ();
  87. else
  88. iteratedNsNames.Add (value.Name);
  89. nsNode = value;
  90. }
  91. }
  92. public override string LocalName {
  93. get {
  94. XmlAttribute nsNode = NsNode;
  95. if (nsNode != null) {
  96. if (nsNode == nsNodeXml)
  97. return "xml";
  98. else
  99. return (nsNode.Name == "xmlns") ? String.Empty : nsNode.LocalName;
  100. }
  101. XPathNodeType nodeType = NodeType;
  102. bool canHaveName =
  103. nodeType == XPathNodeType.Element ||
  104. nodeType == XPathNodeType.Attribute ||
  105. nodeType == XPathNodeType.ProcessingInstruction ||
  106. nodeType == XPathNodeType.Namespace;
  107. return canHaveName ? node.LocalName : String.Empty;
  108. }
  109. }
  110. public override string Name {
  111. get {
  112. if (NsNode != null)
  113. return LocalName;
  114. XPathNodeType nodeType = NodeType;
  115. bool canHaveName =
  116. nodeType == XPathNodeType.Element ||
  117. nodeType == XPathNodeType.Attribute ||
  118. nodeType == XPathNodeType.ProcessingInstruction ||
  119. nodeType == XPathNodeType.Namespace;
  120. return canHaveName ? node.Name : String.Empty;
  121. }
  122. }
  123. public override string NamespaceURI {
  124. get { return (NsNode != null) ? String.Empty : node.NamespaceURI; }
  125. }
  126. public override XmlNameTable NameTable {
  127. get {
  128. return document.NameTable;
  129. }
  130. }
  131. public override XPathNodeType NodeType {
  132. get { return (NsNode != null) ? XPathNodeType.Namespace : node.XPathNodeType; }
  133. }
  134. public override string Prefix {
  135. get { return (NsNode != null) ? String.Empty : node.Prefix; }
  136. }
  137. public override string Value {
  138. get {
  139. switch (NodeType) {
  140. case XPathNodeType.Attribute:
  141. case XPathNodeType.Comment:
  142. case XPathNodeType.ProcessingInstruction:
  143. return node.Value;
  144. case XPathNodeType.Text:
  145. case XPathNodeType.Whitespace:
  146. case XPathNodeType.SignificantWhitespace:
  147. string value = node.Value;
  148. for (XmlNode n = node.NextSibling; n != null; n = n.NextSibling) {
  149. switch (n.XPathNodeType) {
  150. case XPathNodeType.Text:
  151. case XPathNodeType.Whitespace:
  152. case XPathNodeType.SignificantWhitespace:
  153. value += n.Value;
  154. continue;
  155. }
  156. break;
  157. }
  158. return value;
  159. case XPathNodeType.Element:
  160. case XPathNodeType.Root:
  161. return node.InnerText;
  162. case XPathNodeType.Namespace:
  163. return NsNode == nsNodeXml ? XmlnsXML : NsNode.Value;
  164. }
  165. return String.Empty;
  166. }
  167. }
  168. public override string XmlLang {
  169. get {
  170. return node.XmlLang;
  171. }
  172. }
  173. #endregion
  174. #region Methods
  175. private bool CheckNsNameAppearance (string name, string ns)
  176. {
  177. if (iteratedNsNames.Contains (name))
  178. return true;
  179. // default namespace erasure - just add name and never return this node
  180. if (ns == String.Empty) {
  181. iteratedNsNames.Add ("xmlns");
  182. return true;
  183. }
  184. return false;
  185. }
  186. public override XPathNavigator Clone ()
  187. {
  188. XmlDocumentNavigator clone = new XmlDocumentNavigator (node, nsNodeXml);
  189. clone.nsNode = nsNode;
  190. clone.iteratedNsNames = (ArrayList) iteratedNsNames.Clone ();
  191. return clone;
  192. }
  193. public override string GetAttribute (string localName, string namespaceURI)
  194. {
  195. if (HasAttributes) {
  196. XmlElement el = Node as XmlElement;
  197. return el != null ? el.GetAttribute (localName, namespaceURI) : String.Empty;
  198. }
  199. return String.Empty;
  200. }
  201. public override string GetNamespace (string name)
  202. {
  203. // MSDN says "String.Empty if a matching namespace
  204. // node is not found or if the navigator is not
  205. // positioned on an element node", but in fact it
  206. // returns actual namespace for the other nodes.
  207. return Node.GetNamespaceOfPrefix (name);
  208. }
  209. public override bool IsSamePosition (XPathNavigator other)
  210. {
  211. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  212. if (otherDocumentNavigator != null)
  213. return node == otherDocumentNavigator.node
  214. && NsNode == otherDocumentNavigator.NsNode;
  215. return false;
  216. }
  217. public override bool MoveTo (XPathNavigator other)
  218. {
  219. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  220. if (otherDocumentNavigator != null) {
  221. if (document == otherDocumentNavigator.document) {
  222. node = otherDocumentNavigator.node;
  223. NsNode = otherDocumentNavigator.NsNode;
  224. return true;
  225. }
  226. }
  227. return false;
  228. }
  229. public override bool MoveToAttribute (string localName, string namespaceURI)
  230. {
  231. if (node.Attributes != null) {
  232. for (int i = 0; i < node.Attributes.Count; i++) {
  233. XmlAttribute attr = node.Attributes [i];
  234. if (attr.LocalName == localName
  235. && attr.NamespaceURI == namespaceURI) {
  236. node = attr;
  237. NsNode = null;
  238. return true;
  239. }
  240. }
  241. }
  242. return false;
  243. }
  244. public override bool MoveToFirst ()
  245. {
  246. if (NsNode == null && node.NodeType != XmlNodeType.Attribute && node.ParentNode != null) {
  247. if (!MoveToParent ())
  248. return false;
  249. // Follow these 2 steps so that we can skip
  250. // some types of nodes .
  251. MoveToFirstChild ();
  252. return true;
  253. }
  254. return false;
  255. }
  256. public override bool MoveToFirstAttribute ()
  257. {
  258. if (node.Attributes == null)
  259. return false;
  260. if (NodeType == XPathNodeType.Element) {
  261. for (int i = 0; i < node.Attributes.Count; i++) {
  262. XmlAttribute attr = node.Attributes [i];
  263. if (attr.NamespaceURI != Xmlns) {
  264. node = attr;
  265. NsNode = null;
  266. return true;
  267. }
  268. }
  269. }
  270. return false;
  271. }
  272. public override bool MoveToFirstChild ()
  273. {
  274. if (HasChildren) {
  275. if (node == document) {
  276. XmlNode n = node.FirstChild;
  277. if (n == null)
  278. return false;
  279. bool loop = true;
  280. do {
  281. switch (n.NodeType) {
  282. case XmlNodeType.XmlDeclaration:
  283. case XmlNodeType.DocumentType:
  284. n = n.NextSibling;
  285. if (n == null)
  286. return false;
  287. break;
  288. default:
  289. loop = false;
  290. break;
  291. }
  292. } while (loop);
  293. node = n;
  294. } else {
  295. do {
  296. node = node.FirstChild;
  297. if (node.NodeType != XmlNodeType.EntityReference)
  298. break;
  299. node = node.NextSibling;
  300. } while (node != null);
  301. if (node == null)
  302. return false;
  303. }
  304. return true;
  305. }
  306. return false;
  307. }
  308. public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope)
  309. {
  310. if (NodeType != XPathNodeType.Element)
  311. return false;
  312. XmlElement el = node as XmlElement;
  313. if (node.Attributes != null) {
  314. do {
  315. for (int i = 0; i < el.Attributes.Count; i++) {
  316. XmlAttribute attr = el.Attributes [i];
  317. if (attr.NamespaceURI == Xmlns) {
  318. if (CheckNsNameAppearance (attr.Name, attr.Value))
  319. continue;
  320. NsNode = attr;
  321. return true;
  322. }
  323. }
  324. if (namespaceScope == XPathNamespaceScope.Local)
  325. return false;
  326. el = el.ParentNode as XmlElement;
  327. } while (el != null);
  328. }
  329. if (namespaceScope == XPathNamespaceScope.All) {
  330. if (CheckNsNameAppearance (nsNodeXml.Name, nsNodeXml.Value))
  331. return false;
  332. NsNode = nsNodeXml;
  333. return true;
  334. }
  335. else
  336. return false;
  337. }
  338. public override bool MoveToId (string id)
  339. {
  340. XmlElement eltNew = document.GetElementById (id);
  341. if (eltNew == null)
  342. return false;
  343. node = eltNew;
  344. return true;
  345. }
  346. public override bool MoveToNamespace (string name)
  347. {
  348. if (name == "xml") {
  349. NsNode = nsNodeXml;
  350. return true;
  351. }
  352. if (NodeType != XPathNodeType.Element)
  353. return false;
  354. XmlElement el = node as XmlElement;
  355. if (node.Attributes != null) {
  356. do {
  357. for (int i = 0; i < el.Attributes.Count; i++) {
  358. XmlAttribute attr = el.Attributes [i];
  359. if (attr.NamespaceURI == Xmlns && attr.Name == name) {
  360. NsNode = attr;
  361. return true;
  362. }
  363. }
  364. el = node.ParentNode as XmlElement;
  365. } while (el != null);
  366. }
  367. return false;
  368. }
  369. public override bool MoveToNext ()
  370. {
  371. if (NsNode != null)
  372. return false;
  373. if (node.NextSibling != null) {
  374. XmlNode n = node.NextSibling;
  375. if (node.ParentNode != null && node.ParentNode.NodeType == XmlNodeType.Document) {
  376. while (n != null) {
  377. switch (n.NodeType) {
  378. case XmlNodeType.DocumentType:
  379. case XmlNodeType.XmlDeclaration:
  380. n = n.NextSibling;
  381. continue;
  382. }
  383. break;
  384. }
  385. if (n != null)
  386. node = n;
  387. else
  388. return false;
  389. } else {
  390. while (n != null) {
  391. if (n.NodeType != XmlNodeType.EntityReference)
  392. break;
  393. n = n.NextSibling;
  394. }
  395. if (n != null)
  396. node = n;
  397. else
  398. return false;
  399. }
  400. return true;
  401. }
  402. else
  403. return false;
  404. }
  405. public override bool MoveToNextAttribute ()
  406. {
  407. if (node == null)
  408. return false;
  409. if (NodeType != XPathNodeType.Attribute)
  410. return false;
  411. // Find current attribute.
  412. int pos = 0;
  413. XmlElement owner = ((XmlAttribute) node).OwnerElement;
  414. if (owner == null)
  415. return false;
  416. int count = owner.Attributes.Count;
  417. for(; pos < count; pos++)
  418. if (owner.Attributes [pos] == node)
  419. break;
  420. if (pos == count)
  421. return false; // Where is current attribute? Maybe removed.
  422. // Find next attribute.
  423. for(pos++; pos < count; pos++) {
  424. if (owner.Attributes [pos].NamespaceURI != Xmlns) {
  425. node = owner.Attributes [pos];
  426. NsNode = null;
  427. return true;
  428. }
  429. }
  430. return false;
  431. }
  432. public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope)
  433. {
  434. if (NsNode == nsNodeXml)
  435. // Current namespace is "xml", so there should be no more namespace nodes.
  436. return false;
  437. if (NsNode == null)
  438. return false;
  439. // Get current attribute's position.
  440. int pos = 0;
  441. XmlElement owner = ((XmlAttribute) NsNode).OwnerElement;
  442. if (owner == null)
  443. return false;
  444. int count = owner.Attributes.Count;
  445. for(; pos < count; pos++)
  446. if (owner.Attributes [pos] == NsNode)
  447. break;
  448. if (pos == count)
  449. return false; // Where is current attribute? Maybe removed.
  450. // Find next namespace from the same element as current ns node.
  451. for(pos++; pos < count; pos++) {
  452. if (owner.Attributes [pos].NamespaceURI == Xmlns) {
  453. XmlAttribute a = owner.Attributes [pos];
  454. if (CheckNsNameAppearance (a.Name, a.Value))
  455. continue;
  456. NsNode = a;
  457. return true;
  458. }
  459. }
  460. // If not found more, then find from ancestors.
  461. // But if scope is Local, then it returns false here.
  462. if (namespaceScope == XPathNamespaceScope.Local)
  463. return false;
  464. owner = owner.ParentNode as XmlElement;
  465. while (owner != null) {
  466. for (int i = 0; i < owner.Attributes.Count; i++) {
  467. XmlAttribute attr = owner.Attributes [i];
  468. if (attr.NamespaceURI == Xmlns) {
  469. if (CheckNsNameAppearance (attr.Name, attr.Value))
  470. continue;
  471. NsNode = attr;
  472. return true;
  473. }
  474. }
  475. owner = owner.ParentNode as XmlElement;
  476. }
  477. if (namespaceScope == XPathNamespaceScope.All) {
  478. if (CheckNsNameAppearance (nsNodeXml.Name, nsNodeXml.Value))
  479. return false;
  480. NsNode = nsNodeXml;
  481. return true;
  482. }
  483. return false;
  484. }
  485. public override bool MoveToParent ()
  486. {
  487. if (NsNode != null) {
  488. NsNode = null;
  489. return true;
  490. }
  491. else if (node.NodeType == XmlNodeType.Attribute) {
  492. XmlElement ownerElement = ((XmlAttribute)node).OwnerElement;
  493. if (ownerElement != null) {
  494. node = ownerElement;
  495. NsNode = null;
  496. return true;
  497. }
  498. } else if (node.ParentNode != null) {
  499. node = node.ParentNode;
  500. NsNode = null;
  501. return true;
  502. }
  503. return false;
  504. }
  505. public override bool MoveToPrevious ()
  506. {
  507. if (NsNode != null)
  508. return false;
  509. if (node.PreviousSibling != null) {
  510. if (node.ParentNode != null && node.ParentNode.NodeType == XmlNodeType.Document) {
  511. XmlNode n = node.PreviousSibling;
  512. while (n != null) {
  513. switch (n.NodeType) {
  514. case XmlNodeType.DocumentType:
  515. case XmlNodeType.XmlDeclaration:
  516. n = n.PreviousSibling;
  517. continue;
  518. }
  519. break;
  520. }
  521. if (n != null)
  522. node = n;
  523. else
  524. return false;
  525. }
  526. else
  527. node = node.PreviousSibling;
  528. return true;
  529. }
  530. else
  531. return false;
  532. }
  533. public override void MoveToRoot ()
  534. {
  535. XmlAttribute attr = node as XmlAttribute;
  536. XmlNode tmp = attr != null ? attr.OwnerElement : node;
  537. while (tmp.ParentNode != null)
  538. tmp = tmp.ParentNode;
  539. node = tmp;
  540. NsNode = null;
  541. }
  542. internal XmlNode Node { get { return NsNode != null ? NsNode : node; } }
  543. XmlNode IHasXmlNode.GetNode ()
  544. {
  545. return node;
  546. }
  547. #endregion
  548. }
  549. }