DTMXPathNavigator.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. //
  2. // Mono.Xml.XPath.DTMXPathNavigator
  3. //
  4. // Author:
  5. // Atsushi Enomoto ([email protected])
  6. //
  7. // (C) 2003 Atsushi Enomoto
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Text;
  32. using System.Xml;
  33. using System.Xml.Schema;
  34. using System.Xml.XPath;
  35. namespace Mono.Xml.XPath
  36. {
  37. #if OUTSIDE_SYSTEM_XML
  38. public
  39. #else
  40. internal
  41. #endif
  42. class DTMXPathNavigator : XPathNavigator, IXmlLineInfo
  43. {
  44. #region Copy of XPathDocument
  45. public DTMXPathNavigator (DTMXPathDocument document,
  46. XmlNameTable nameTable,
  47. DTMXPathLinkedNode [] nodes,
  48. DTMXPathAttributeNode [] attributes,
  49. DTMXPathNamespaceNode [] namespaces,
  50. Hashtable idTable)
  51. {
  52. this.nodes = nodes;
  53. this.attributes = attributes;
  54. this.namespaces = namespaces;
  55. this.idTable = idTable;
  56. this.nameTable = nameTable;
  57. this.MoveToRoot ();
  58. this.document = document;
  59. }
  60. // Copy constructor including position informations.
  61. public DTMXPathNavigator (DTMXPathNavigator org)
  62. : this (org.document, org.nameTable,
  63. org.nodes, org.attributes, org.namespaces,
  64. org.idTable)
  65. {
  66. currentIsNode = org.currentIsNode;
  67. currentIsAttr = org.currentIsAttr;
  68. currentNode = org.currentNode;
  69. currentAttr = org.currentAttr;
  70. currentNs = org.currentNs;
  71. }
  72. XmlNameTable nameTable;
  73. // Created XPathDocument. This is used to identify the origin of the navigator.
  74. DTMXPathDocument document;
  75. DTMXPathLinkedNode [] nodes;// = new DTMXPathLinkedNode [0];
  76. DTMXPathAttributeNode [] attributes;// = new DTMXPathAttributeNode [0];
  77. DTMXPathNamespaceNode [] namespaces;// = new DTMXPathNamespaceNode [0];
  78. // ID table
  79. Hashtable idTable;
  80. // // Key table (considered xsd:keyref for XPath 2.0)
  81. // Hashtable keyRefTable; // [string key-name] -> idTable
  82. // // idTable [string value] -> int nodeId
  83. #endregion
  84. bool currentIsNode;
  85. bool currentIsAttr;
  86. int currentNode;
  87. int currentAttr;
  88. int currentNs;
  89. StringBuilder valueBuilder;
  90. #region Ctor
  91. internal DTMXPathNavigator (XmlNameTable nt)
  92. {
  93. this.nameTable = nt;
  94. }
  95. #endregion
  96. #region Properties
  97. public override string BaseURI {
  98. get { return nodes [currentNode].BaseURI; }
  99. }
  100. public override bool HasAttributes {
  101. get { return currentIsNode ? nodes [currentNode].FirstAttribute != 0 : false; }
  102. }
  103. public override bool HasChildren {
  104. get { return currentIsNode ? nodes [currentNode].FirstChild != 0 : false; }
  105. }
  106. public override bool IsEmptyElement {
  107. get { return currentIsNode ? nodes [currentNode].IsEmptyElement : false; }
  108. }
  109. int IXmlLineInfo.LineNumber {
  110. get {
  111. return currentIsAttr ? attributes [currentAttr].LineNumber :
  112. nodes [currentNode].LineNumber;
  113. }
  114. }
  115. int IXmlLineInfo.LinePosition {
  116. get {
  117. return currentIsAttr ? attributes [currentAttr].LinePosition :
  118. nodes [currentNode].LinePosition;
  119. }
  120. }
  121. public override string LocalName {
  122. get {
  123. if (currentIsNode)
  124. return nodes [currentNode].LocalName;
  125. else if (currentIsAttr)
  126. return attributes [currentAttr].LocalName;
  127. else
  128. return namespaces [currentNs].Name;
  129. }
  130. }
  131. // It maybe scarcely used, so I decided to compute it always.
  132. public override string Name {
  133. get {
  134. string prefix;
  135. string localName;
  136. if (currentIsNode) {
  137. prefix = nodes [currentNode].Prefix;
  138. localName = nodes [currentNode].LocalName;
  139. } else if (currentIsAttr) {
  140. prefix = attributes [currentAttr].Prefix;
  141. localName = attributes [currentAttr].LocalName;
  142. } else
  143. return namespaces [currentNs].Name;
  144. if (prefix != "")
  145. return prefix + ':' + localName;
  146. else
  147. return localName;
  148. }
  149. }
  150. public override string NamespaceURI {
  151. get {
  152. if (currentIsNode)
  153. return nodes [currentNode].NamespaceURI;
  154. if (currentIsAttr)
  155. return attributes [currentAttr].NamespaceURI;
  156. return String.Empty;
  157. }
  158. }
  159. public override XmlNameTable NameTable {
  160. get { return nameTable; }
  161. }
  162. public override XPathNodeType NodeType {
  163. get {
  164. if (currentIsNode)
  165. return nodes [currentNode].NodeType;
  166. else if (currentIsAttr)
  167. return XPathNodeType.Attribute;
  168. else
  169. return XPathNodeType.Namespace;
  170. }
  171. }
  172. public override string Prefix {
  173. get {
  174. if (currentIsNode)
  175. return nodes [currentNode].Prefix;
  176. else if (currentIsAttr)
  177. return attributes [currentAttr].Prefix;
  178. return String.Empty;
  179. }
  180. }
  181. public override string Value {
  182. get {
  183. if (currentIsAttr)
  184. return attributes [currentAttr].Value;
  185. else if (!currentIsNode)
  186. return namespaces [currentNs].Namespace;
  187. switch (nodes [currentNode].NodeType) {
  188. case XPathNodeType.Comment:
  189. case XPathNodeType.ProcessingInstruction:
  190. case XPathNodeType.Text:
  191. case XPathNodeType.Whitespace:
  192. case XPathNodeType.SignificantWhitespace:
  193. return nodes [currentNode].Value;
  194. }
  195. // Element
  196. if (valueBuilder == null)
  197. valueBuilder = new StringBuilder ();
  198. else
  199. valueBuilder.Length = 0;
  200. int iter = nodes [currentNode].FirstChild;
  201. int depth = nodes [currentNode].Depth;
  202. while (iter < nodes.Length && nodes [iter].Depth > depth) {
  203. switch (nodes [iter].NodeType) {
  204. case XPathNodeType.Comment:
  205. case XPathNodeType.ProcessingInstruction:
  206. break;
  207. default:
  208. valueBuilder.Append (nodes [iter].Value);
  209. break;
  210. }
  211. iter++;
  212. }
  213. return valueBuilder.ToString ();
  214. }
  215. }
  216. public override string XmlLang {
  217. get { return nodes [currentNode].XmlLang; }
  218. }
  219. #endregion
  220. #region Methods
  221. public override XPathNavigator Clone ()
  222. {
  223. return new DTMXPathNavigator (this);
  224. }
  225. public override XmlNodeOrder ComparePosition (XPathNavigator nav)
  226. {
  227. DTMXPathNavigator another = nav as DTMXPathNavigator;
  228. if (another == null || another.document != this.document)
  229. return XmlNodeOrder.Unknown;
  230. if (currentNode > another.currentNode)
  231. return XmlNodeOrder.After;
  232. else if (currentNode < another.currentNode)
  233. return XmlNodeOrder.Before;
  234. // another may attr or ns,
  235. // and this may be also attr or ns.
  236. if (another.currentIsAttr) {
  237. if (this.currentIsAttr) {
  238. if (currentAttr > another.currentAttr)
  239. return XmlNodeOrder.After;
  240. else if (currentAttr < another.currentAttr)
  241. return XmlNodeOrder.Before;
  242. else
  243. return XmlNodeOrder.Same;
  244. } else
  245. return XmlNodeOrder.Before;
  246. } else if (!another.currentIsNode) {
  247. if (!this.currentIsNode) {
  248. if (currentNs > another.currentNs)
  249. return XmlNodeOrder.After;
  250. else if (currentNs < another.currentNs)
  251. return XmlNodeOrder.Before;
  252. else
  253. return XmlNodeOrder.Same;
  254. } else
  255. return XmlNodeOrder.Before;
  256. } else
  257. return !another.currentIsNode ? XmlNodeOrder.Before : XmlNodeOrder.Same;
  258. }
  259. private int findAttribute (string localName, string namespaceURI)
  260. {
  261. if (currentIsNode && nodes [currentNode].NodeType == XPathNodeType.Element) {
  262. int cur = nodes [currentNode].FirstAttribute;
  263. while (cur != 0) {
  264. if (attributes [cur].LocalName == localName && attributes [cur].NamespaceURI == namespaceURI)
  265. return cur;
  266. cur = attributes [cur].NextAttribute;
  267. }
  268. }
  269. return 0;
  270. }
  271. public override string GetAttribute (string localName,
  272. string namespaceURI)
  273. {
  274. int attr = findAttribute (localName, namespaceURI);
  275. return (attr != 0) ? attributes [attr].Value : String.Empty;
  276. }
  277. public override string GetNamespace (string name)
  278. {
  279. if (currentIsNode && nodes [currentNode].NodeType == XPathNodeType.Element) {
  280. int nsNode = nodes [currentNode].FirstNamespace;
  281. while (nsNode != 0) {
  282. if (namespaces [nsNode].Name == name)
  283. return namespaces [nsNode].Namespace;
  284. nsNode = namespaces [nsNode].NextNamespace;
  285. }
  286. }
  287. return String.Empty;
  288. }
  289. bool IXmlLineInfo.HasLineInfo ()
  290. {
  291. return true;
  292. }
  293. public override bool IsDescendant (XPathNavigator nav)
  294. {
  295. DTMXPathNavigator another = nav as DTMXPathNavigator;
  296. if (another == null || another.document != this.document)
  297. return false;
  298. // Maybe we can improve here more efficiently by
  299. // comparing node indices.
  300. if (another.currentNode == currentNode)
  301. return !another.currentIsNode;
  302. int tmp = nodes [another.currentNode].Parent;
  303. while (tmp != 0) {
  304. if (tmp == currentNode)
  305. return true;
  306. tmp = nodes [tmp].Parent;
  307. }
  308. return false;
  309. }
  310. public override bool IsSamePosition (XPathNavigator other)
  311. {
  312. DTMXPathNavigator another = other as DTMXPathNavigator;
  313. if (another == null || another.document != this.document)
  314. return false;
  315. if (this.currentNode != another.currentNode ||
  316. this.currentIsAttr != another.currentIsAttr ||
  317. this.currentIsNode != another.currentIsNode)
  318. return false;
  319. if (currentIsAttr)
  320. return this.currentAttr == another.currentAttr;
  321. else if (!currentIsNode)
  322. return this.currentNs == another.currentNs;
  323. return true;
  324. }
  325. public override bool MoveTo (XPathNavigator other)
  326. {
  327. DTMXPathNavigator another = other as DTMXPathNavigator;
  328. if (another == null || another.document != this.document)
  329. return false;
  330. this.currentNode = another.currentNode;
  331. this.currentAttr = another.currentAttr;
  332. this.currentNs = another.currentNs;
  333. this.currentIsNode = another.currentIsNode;
  334. this.currentIsAttr = another.currentIsAttr;
  335. return true;
  336. }
  337. public override bool MoveToAttribute (string localName,
  338. string namespaceURI)
  339. {
  340. int attr = findAttribute (localName, namespaceURI);
  341. if (attr == 0)
  342. return false;
  343. currentAttr = attr;
  344. currentIsAttr = true;
  345. currentIsNode = false;
  346. return true;
  347. }
  348. public override bool MoveToFirst ()
  349. {
  350. if (currentIsAttr)
  351. return false;
  352. int cur = nodes [currentNode].PreviousSibling;
  353. if (cur == 0)
  354. return false;
  355. int next = cur;
  356. while (next != 0) {
  357. cur = next;
  358. next = nodes [cur].PreviousSibling;
  359. }
  360. currentNode = cur;
  361. currentIsNode = true;
  362. return true;
  363. }
  364. public override bool MoveToFirstAttribute ()
  365. {
  366. if (!currentIsNode)
  367. return false;
  368. int first = nodes [currentNode].FirstAttribute;
  369. if (first == 0)
  370. return false;
  371. currentAttr = first;
  372. currentIsAttr = true;
  373. currentIsNode = false;
  374. return true;
  375. }
  376. public override bool MoveToFirstChild ()
  377. {
  378. if (!currentIsNode)
  379. return false;
  380. int first = nodes [currentNode].FirstChild;
  381. if (first == 0)
  382. return false;
  383. currentNode = first;
  384. return true;
  385. }
  386. private bool moveToSpecifiedNamespace (int cur,
  387. XPathNamespaceScope namespaceScope)
  388. {
  389. if (cur == 0)
  390. return false;
  391. if (namespaceScope == XPathNamespaceScope.Local &&
  392. namespaces [cur].DeclaredElement != currentNode)
  393. return false;
  394. if (namespaceScope != XPathNamespaceScope.All
  395. && namespaces [cur].Namespace == XmlNamespaces.XML)
  396. return false;
  397. if (cur != 0) {
  398. moveToNamespace (cur);
  399. return true;
  400. }
  401. else
  402. return false;
  403. }
  404. public override bool MoveToFirstNamespace (
  405. XPathNamespaceScope namespaceScope)
  406. {
  407. if (!currentIsNode)
  408. return false;
  409. int cur = nodes [currentNode].FirstNamespace;
  410. return moveToSpecifiedNamespace (cur, namespaceScope);
  411. }
  412. // Note that this support is extension to XPathDocument.
  413. // XPathDocument does not support ID reference.
  414. public override bool MoveToId (string id)
  415. {
  416. if (idTable.ContainsKey (id)) {
  417. currentNode = (int) idTable [id];
  418. currentIsNode = true;
  419. currentIsAttr = false;
  420. return true;
  421. }
  422. else
  423. return false;
  424. }
  425. private void moveToNamespace (int nsNode)
  426. {
  427. currentIsNode = currentIsAttr = false;
  428. currentNs = nsNode;
  429. }
  430. public override bool MoveToNamespace (string name)
  431. {
  432. int cur = nodes [currentNode].FirstNamespace;
  433. if (cur == 0)
  434. return false;
  435. while (cur != 0) {
  436. if (namespaces [cur].Name == name) {
  437. moveToNamespace (cur);
  438. return true;
  439. }
  440. cur = namespaces [cur].NextNamespace;
  441. }
  442. return false;
  443. }
  444. public override bool MoveToNext ()
  445. {
  446. if (currentIsAttr)
  447. return false;
  448. int next = nodes [currentNode].NextSibling;
  449. if (next == 0)
  450. return false;
  451. currentNode = next;
  452. currentIsNode = true;
  453. return true;
  454. }
  455. public override bool MoveToNextAttribute ()
  456. {
  457. if (!currentIsAttr)
  458. return false;
  459. int next = attributes [currentAttr].NextAttribute;
  460. if (next == 0)
  461. return false;
  462. currentAttr = next;
  463. return true;
  464. }
  465. public override bool MoveToNextNamespace (
  466. XPathNamespaceScope namespaceScope)
  467. {
  468. if (currentIsAttr || currentIsNode)
  469. return false;
  470. int cur = namespaces [currentNs].NextNamespace;
  471. return moveToSpecifiedNamespace (cur, namespaceScope);
  472. }
  473. public override bool MoveToParent ()
  474. {
  475. if (!currentIsNode) {
  476. currentIsNode = true;
  477. currentIsAttr = false;
  478. return true;
  479. }
  480. int parent = nodes [currentNode].Parent;
  481. if (parent == 0) // It is root itself.
  482. return false;
  483. currentNode = parent;
  484. return true;
  485. }
  486. public override bool MoveToPrevious ()
  487. {
  488. if (currentIsAttr)
  489. return false;
  490. int previous = nodes [currentNode].PreviousSibling;
  491. if (previous == 0)
  492. return false;
  493. currentNode = previous;
  494. currentIsNode = true;
  495. return true;
  496. }
  497. public override void MoveToRoot ()
  498. {
  499. currentNode = 1; // root is 1.
  500. currentIsNode = true;
  501. currentIsAttr = false;
  502. }
  503. #endregion
  504. /*
  505. public string DebugDump {
  506. get {
  507. StringBuilder sb = new StringBuilder ();
  508. for (int i = 0; i < namespaces.Length; i++) {
  509. DTMXPathNamespaceNode n = namespaces [i];
  510. sb.AppendFormat ("{0}: {1},{2} {3}/{4}\n", i,
  511. n.DeclaredElement, n.NextNamespace,
  512. n.Name, n.Namespace);
  513. }
  514. for (int i=0; i<this.nodes.Length; i++) {
  515. DTMXPathLinkedNode n = nodes [i];
  516. sb.AppendFormat ("{0}: {1}:{2} {3} {4} {5} {6} {7}\n", new object [] {i, n.Prefix, n.LocalName, n.NamespaceURI, n.FirstNamespace, n.FirstAttribute, n.FirstChild, n.Parent});
  517. }
  518. for (int i=0; i<this.attributes.Length; i++) {
  519. DTMXPathAttributeNode n = attributes [i];
  520. sb.AppendFormat ("{0}: {1}:{2} {3} {4}\n", i, n.Prefix, n.LocalName, n.NamespaceURI, n.NextAttribute);
  521. }
  522. return sb.ToString ();
  523. }
  524. }
  525. */
  526. }
  527. internal class XmlNamespaces
  528. {
  529. public const string XML = "http://www.w3.org/XML/1998/namespace";
  530. public const string XMLNS = "http://www.w3.org/2000/xmlns/";
  531. }
  532. }