XmlDocumentNavigator.cs 16 KB

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