XmlDocumentNavigator.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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 = new ArrayList ();
  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. iteratedNsNames.Clear ();
  107. else
  108. iteratedNsNames.Add (value.Name);
  109. nsNode = value;
  110. }
  111. }
  112. public override string LocalName {
  113. get {
  114. XmlAttribute nsNode = NsNode;
  115. if (nsNode != null) {
  116. if (nsNode == nsNodeXml)
  117. return "xml";
  118. else
  119. return (nsNode.Name == "xmlns") ? String.Empty : nsNode.LocalName;
  120. }
  121. XPathNodeType nodeType = NodeType;
  122. bool canHaveName =
  123. nodeType == XPathNodeType.Element ||
  124. nodeType == XPathNodeType.Attribute ||
  125. nodeType == XPathNodeType.ProcessingInstruction ||
  126. nodeType == XPathNodeType.Namespace;
  127. return canHaveName ? node.LocalName : String.Empty;
  128. }
  129. }
  130. public override string Name {
  131. get {
  132. if (NsNode != null)
  133. return LocalName;
  134. XPathNodeType nodeType = NodeType;
  135. bool canHaveName =
  136. nodeType == XPathNodeType.Element ||
  137. nodeType == XPathNodeType.Attribute ||
  138. nodeType == XPathNodeType.ProcessingInstruction ||
  139. nodeType == XPathNodeType.Namespace;
  140. return canHaveName ? node.Name : String.Empty;
  141. }
  142. }
  143. public override string NamespaceURI {
  144. get { return (NsNode != null) ? String.Empty : node.NamespaceURI; }
  145. }
  146. public override XmlNameTable NameTable {
  147. get {
  148. return document.NameTable;
  149. }
  150. }
  151. public override XPathNodeType NodeType {
  152. get { return (NsNode != null) ? XPathNodeType.Namespace : node.XPathNodeType; }
  153. }
  154. public override string Prefix {
  155. get { return (NsNode != null) ? String.Empty : node.Prefix; }
  156. }
  157. public override string Value {
  158. get {
  159. switch (NodeType) {
  160. case XPathNodeType.Attribute:
  161. case XPathNodeType.Comment:
  162. case XPathNodeType.ProcessingInstruction:
  163. return node.Value;
  164. case XPathNodeType.Text:
  165. case XPathNodeType.Whitespace:
  166. case XPathNodeType.SignificantWhitespace:
  167. string value = node.Value;
  168. for (XmlNode n = node.NextSibling; n != null; n = n.NextSibling) {
  169. switch (n.XPathNodeType) {
  170. case XPathNodeType.Text:
  171. case XPathNodeType.Whitespace:
  172. case XPathNodeType.SignificantWhitespace:
  173. value += n.Value;
  174. continue;
  175. }
  176. break;
  177. }
  178. return value;
  179. case XPathNodeType.Element:
  180. case XPathNodeType.Root:
  181. return node.InnerText;
  182. case XPathNodeType.Namespace:
  183. return NsNode == nsNodeXml ? XmlnsXML : NsNode.Value;
  184. }
  185. return String.Empty;
  186. }
  187. }
  188. public override string XmlLang {
  189. get {
  190. return node.XmlLang;
  191. }
  192. }
  193. #endregion
  194. #region Methods
  195. private bool CheckNsNameAppearance (string name, string ns)
  196. {
  197. if (iteratedNsNames.Contains (name))
  198. return true;
  199. // default namespace erasure - just add name and never return this node
  200. if (ns == String.Empty) {
  201. iteratedNsNames.Add ("xmlns");
  202. return true;
  203. }
  204. return false;
  205. }
  206. public override XPathNavigator Clone ()
  207. {
  208. XmlDocumentNavigator clone = new XmlDocumentNavigator (node, nsNodeXml);
  209. clone.nsNode = nsNode;
  210. clone.iteratedNsNames = (ArrayList) iteratedNsNames.Clone ();
  211. return clone;
  212. }
  213. public override string GetAttribute (string localName, string namespaceURI)
  214. {
  215. if (HasAttributes) {
  216. XmlElement el = Node as XmlElement;
  217. return el != null ? el.GetAttribute (localName, namespaceURI) : String.Empty;
  218. }
  219. return String.Empty;
  220. }
  221. public override string GetNamespace (string name)
  222. {
  223. // MSDN says "String.Empty if a matching namespace
  224. // node is not found or if the navigator is not
  225. // positioned on an element node", but in fact it
  226. // returns actual namespace for the other nodes.
  227. return Node.GetNamespaceOfPrefix (name);
  228. }
  229. public override bool IsSamePosition (XPathNavigator other)
  230. {
  231. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  232. if (otherDocumentNavigator != null)
  233. return node == otherDocumentNavigator.node
  234. && NsNode == otherDocumentNavigator.NsNode;
  235. return false;
  236. }
  237. public override bool MoveTo (XPathNavigator other)
  238. {
  239. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  240. if (otherDocumentNavigator != null) {
  241. if (document == otherDocumentNavigator.document) {
  242. node = otherDocumentNavigator.node;
  243. NsNode = otherDocumentNavigator.NsNode;
  244. return true;
  245. }
  246. }
  247. return false;
  248. }
  249. public override bool MoveToAttribute (string localName, string namespaceURI)
  250. {
  251. if (node.Attributes != null) {
  252. for (int i = 0; i < node.Attributes.Count; i++) {
  253. XmlAttribute attr = node.Attributes [i];
  254. if (attr.LocalName == localName
  255. && attr.NamespaceURI == namespaceURI) {
  256. node = attr;
  257. NsNode = null;
  258. return true;
  259. }
  260. }
  261. }
  262. return false;
  263. }
  264. public override bool MoveToFirst ()
  265. {
  266. if (NsNode == null && node.NodeType != XmlNodeType.Attribute && node.ParentNode != null) {
  267. if (!MoveToParent ())
  268. return false;
  269. // Follow these 2 steps so that we can skip
  270. // some types of nodes .
  271. MoveToFirstChild ();
  272. return true;
  273. }
  274. return false;
  275. }
  276. public override bool MoveToFirstAttribute ()
  277. {
  278. if (node.Attributes == null)
  279. return false;
  280. if (NodeType == XPathNodeType.Element) {
  281. for (int i = 0; i < node.Attributes.Count; i++) {
  282. XmlAttribute attr = node.Attributes [i];
  283. if (attr.NamespaceURI != Xmlns) {
  284. node = attr;
  285. NsNode = null;
  286. return true;
  287. }
  288. }
  289. }
  290. return false;
  291. }
  292. public override bool MoveToFirstChild ()
  293. {
  294. if (HasChildren) {
  295. if (node == document) {
  296. XmlNode n = node.FirstChild;
  297. if (n == null)
  298. return false;
  299. bool loop = true;
  300. do {
  301. switch (n.NodeType) {
  302. case XmlNodeType.XmlDeclaration:
  303. case XmlNodeType.DocumentType:
  304. n = n.NextSibling;
  305. if (n == null)
  306. return false;
  307. break;
  308. default:
  309. loop = false;
  310. break;
  311. }
  312. } while (loop);
  313. node = n;
  314. } else {
  315. XmlNode n2 = null;
  316. do {
  317. n2 = node.FirstChild;
  318. if (node.NodeType != XmlNodeType.EntityReference)
  319. break;
  320. n2 = node.NextSibling;
  321. } while (n2 != null);
  322. if (n2 == null)
  323. return false;
  324. node = n2;
  325. }
  326. return true;
  327. }
  328. return false;
  329. }
  330. public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope)
  331. {
  332. if (NodeType != XPathNodeType.Element)
  333. return false;
  334. XmlElement el = node as XmlElement;
  335. if (node.Attributes != null) {
  336. do {
  337. for (int i = 0; i < el.Attributes.Count; i++) {
  338. XmlAttribute attr = el.Attributes [i];
  339. if (attr.NamespaceURI == Xmlns) {
  340. if (CheckNsNameAppearance (attr.Name, attr.Value))
  341. continue;
  342. NsNode = attr;
  343. return true;
  344. }
  345. }
  346. if (namespaceScope == XPathNamespaceScope.Local)
  347. return false;
  348. el = el.ParentNode as XmlElement;
  349. } while (el != null);
  350. }
  351. if (namespaceScope == XPathNamespaceScope.All) {
  352. if (CheckNsNameAppearance (nsNodeXml.Name, nsNodeXml.Value))
  353. return false;
  354. NsNode = nsNodeXml;
  355. return true;
  356. }
  357. else
  358. return false;
  359. }
  360. public override bool MoveToId (string id)
  361. {
  362. XmlElement eltNew = document.GetElementById (id);
  363. if (eltNew == null)
  364. return false;
  365. node = eltNew;
  366. return true;
  367. }
  368. public override bool MoveToNamespace (string name)
  369. {
  370. if (name == "xml") {
  371. NsNode = nsNodeXml;
  372. return true;
  373. }
  374. if (NodeType != XPathNodeType.Element)
  375. return false;
  376. XmlElement el = node as XmlElement;
  377. if (node.Attributes != null) {
  378. do {
  379. for (int i = 0; i < el.Attributes.Count; i++) {
  380. XmlAttribute attr = el.Attributes [i];
  381. if (attr.NamespaceURI == Xmlns && attr.Name == name) {
  382. NsNode = attr;
  383. return true;
  384. }
  385. }
  386. el = node.ParentNode as XmlElement;
  387. } while (el != null);
  388. }
  389. return false;
  390. }
  391. public override bool MoveToNext ()
  392. {
  393. if (NsNode != null)
  394. return false;
  395. XmlNode n = node;
  396. if (NodeType == XPathNodeType.Text) {
  397. do {
  398. n = n.NextSibling;
  399. if (n == null)
  400. return false;
  401. switch (n.NodeType) {
  402. case XmlNodeType.CDATA:
  403. case XmlNodeType.EntityReference:
  404. case XmlNodeType.SignificantWhitespace:
  405. case XmlNodeType.Text:
  406. case XmlNodeType.Whitespace:
  407. continue;
  408. default:
  409. break;
  410. }
  411. break;
  412. } while (true);
  413. } else {
  414. n = n.NextSibling;
  415. if (n == null)
  416. return false;
  417. }
  418. if (n.ParentNode != null && n.ParentNode.NodeType == XmlNodeType.Document) {
  419. while (n != null) {
  420. switch (n.NodeType) {
  421. case XmlNodeType.DocumentType:
  422. case XmlNodeType.XmlDeclaration:
  423. n = n.NextSibling;
  424. continue;
  425. }
  426. break;
  427. }
  428. if (n != null)
  429. node = n;
  430. else
  431. return false;
  432. } else {
  433. while (n != null) {
  434. if (n.NodeType != XmlNodeType.EntityReference)
  435. break;
  436. n = n.NextSibling;
  437. }
  438. if (n != null)
  439. node = n;
  440. else
  441. return false;
  442. }
  443. return true;
  444. }
  445. public override bool MoveToNextAttribute ()
  446. {
  447. if (node == null)
  448. return false;
  449. if (NodeType != XPathNodeType.Attribute)
  450. return false;
  451. // Find current attribute.
  452. int pos = 0;
  453. XmlElement owner = ((XmlAttribute) node).OwnerElement;
  454. if (owner == null)
  455. return false;
  456. int count = owner.Attributes.Count;
  457. for(; pos < count; pos++)
  458. if (owner.Attributes [pos] == node)
  459. break;
  460. if (pos == count)
  461. return false; // Where is current attribute? Maybe removed.
  462. // Find next attribute.
  463. for(pos++; pos < count; pos++) {
  464. if (owner.Attributes [pos].NamespaceURI != Xmlns) {
  465. node = owner.Attributes [pos];
  466. NsNode = null;
  467. return true;
  468. }
  469. }
  470. return false;
  471. }
  472. public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope)
  473. {
  474. if (NsNode == nsNodeXml)
  475. // Current namespace is "xml", so there should be no more namespace nodes.
  476. return false;
  477. if (NsNode == null)
  478. return false;
  479. // Get current attribute's position.
  480. int pos = 0;
  481. XmlElement owner = ((XmlAttribute) NsNode).OwnerElement;
  482. if (owner == null)
  483. return false;
  484. int count = owner.Attributes.Count;
  485. for(; pos < count; pos++)
  486. if (owner.Attributes [pos] == NsNode)
  487. break;
  488. if (pos == count)
  489. return false; // Where is current attribute? Maybe removed.
  490. // Find next namespace from the same element as current ns node.
  491. for(pos++; pos < count; pos++) {
  492. if (owner.Attributes [pos].NamespaceURI == Xmlns) {
  493. XmlAttribute a = owner.Attributes [pos];
  494. if (CheckNsNameAppearance (a.Name, a.Value))
  495. continue;
  496. NsNode = a;
  497. return true;
  498. }
  499. }
  500. // If not found more, then find from ancestors.
  501. // But if scope is Local, then it returns false here.
  502. if (namespaceScope == XPathNamespaceScope.Local)
  503. return false;
  504. owner = owner.ParentNode as XmlElement;
  505. while (owner != null) {
  506. for (int i = 0; i < owner.Attributes.Count; i++) {
  507. XmlAttribute attr = owner.Attributes [i];
  508. if (attr.NamespaceURI == Xmlns) {
  509. if (CheckNsNameAppearance (attr.Name, attr.Value))
  510. continue;
  511. NsNode = attr;
  512. return true;
  513. }
  514. }
  515. owner = owner.ParentNode as XmlElement;
  516. }
  517. if (namespaceScope == XPathNamespaceScope.All) {
  518. if (CheckNsNameAppearance (nsNodeXml.Name, nsNodeXml.Value))
  519. return false;
  520. NsNode = nsNodeXml;
  521. return true;
  522. }
  523. return false;
  524. }
  525. public override bool MoveToParent ()
  526. {
  527. if (NsNode != null) {
  528. NsNode = null;
  529. return true;
  530. }
  531. else if (node.NodeType == XmlNodeType.Attribute) {
  532. XmlElement ownerElement = ((XmlAttribute)node).OwnerElement;
  533. if (ownerElement != null) {
  534. node = ownerElement;
  535. NsNode = null;
  536. return true;
  537. }
  538. } else if (node.ParentNode != null) {
  539. node = node.ParentNode;
  540. NsNode = null;
  541. return true;
  542. }
  543. return false;
  544. }
  545. public override bool MoveToPrevious ()
  546. {
  547. if (NsNode != null)
  548. return false;
  549. if (node.PreviousSibling != null) {
  550. if (node.ParentNode != null && node.ParentNode.NodeType == XmlNodeType.Document) {
  551. XmlNode n = node.PreviousSibling;
  552. while (n != null) {
  553. switch (n.NodeType) {
  554. case XmlNodeType.DocumentType:
  555. case XmlNodeType.XmlDeclaration:
  556. n = n.PreviousSibling;
  557. continue;
  558. }
  559. break;
  560. }
  561. if (n != null)
  562. node = n;
  563. else
  564. return false;
  565. }
  566. else
  567. node = node.PreviousSibling;
  568. return true;
  569. }
  570. else
  571. return false;
  572. }
  573. public override void MoveToRoot ()
  574. {
  575. XmlAttribute attr = node as XmlAttribute;
  576. XmlNode tmp = attr != null ? attr.OwnerElement : node;
  577. while (tmp.ParentNode != null)
  578. tmp = tmp.ParentNode;
  579. node = tmp;
  580. NsNode = null;
  581. }
  582. internal XmlNode Node { get { return NsNode != null ? NsNode : node; } }
  583. XmlNode IHasXmlNode.GetNode ()
  584. {
  585. return node;
  586. }
  587. #endregion
  588. }
  589. }