XPathDocument2Navigator.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. //
  2. // Mono.Xml.XPath.XPathDocument2Navigator.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C)2004 Novell Inc.
  8. //
  9. // read-only XPathNavigator for editable XPathDocument.
  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. #if NET_2_0
  32. using System;
  33. using System.Collections;
  34. using System.Collections.Specialized;
  35. using System.IO;
  36. using System.Text;
  37. using System.Xml;
  38. using System.Xml.XPath;
  39. namespace Mono.Xml.XPath
  40. {
  41. public class XomNavigator : XPathNavigator, IHasXomNode
  42. {
  43. XomRoot root;
  44. XomNode current;
  45. XomNamespace currentNS;
  46. public XomNavigator (XomRoot root)
  47. {
  48. this.root = root;
  49. current = root;
  50. }
  51. public override string BaseURI {
  52. get { return current.BaseURI; }
  53. }
  54. public override bool IsEmptyElement {
  55. get { return currentNS == null && current.IsEmptyElement; }
  56. }
  57. public override bool HasChildren {
  58. get { return currentNS == null && current.ChildCount != 0; }
  59. }
  60. public override bool HasAttributes {
  61. get {
  62. if (currentNS != null || current.NodeType != XPathNodeType.Element)
  63. return false;
  64. XomElement el = current as XomElement;
  65. return el.AttributeCount != 0;
  66. }
  67. }
  68. XomNode IHasXomNode.GetNode ()
  69. {
  70. return currentNS != null ? currentNS : current;
  71. }
  72. public override string GetAttribute (string name, string ns)
  73. {
  74. if (currentNS != null || current.NodeType != XPathNodeType.Element)
  75. return String.Empty;
  76. XomElement el = current as XomElement;
  77. XomAttribute attr = el.GetAttribute (name, ns);
  78. return attr != null ? attr.Value : String.Empty;
  79. }
  80. public override XmlNameTable NameTable {
  81. get { return current.Root.NameTable; }
  82. }
  83. public override string LocalName {
  84. get { return current.LocalName; }
  85. }
  86. public override string Name {
  87. get { return current.PrefixedName; }
  88. }
  89. public override string NamespaceURI {
  90. get { return current.Namespace; }
  91. }
  92. public override string Prefix {
  93. get { return current.Prefix; }
  94. }
  95. public override string Value {
  96. get {
  97. switch (NodeType) {
  98. case XPathNodeType.Attribute:
  99. case XPathNodeType.Comment:
  100. case XPathNodeType.ProcessingInstruction:
  101. return current.Value;
  102. case XPathNodeType.Text:
  103. case XPathNodeType.Whitespace:
  104. case XPathNodeType.SignificantWhitespace:
  105. string value = current.Value;
  106. for (XomNode n = current.NextSibling; n != null; n = n.NextSibling) {
  107. switch (n.NodeType) {
  108. case XPathNodeType.Text:
  109. case XPathNodeType.Whitespace:
  110. case XPathNodeType.SignificantWhitespace:
  111. value += n.Value;
  112. continue;
  113. }
  114. break;
  115. }
  116. return value;
  117. case XPathNodeType.Element:
  118. case XPathNodeType.Root:
  119. return current.Value;
  120. case XPathNodeType.Namespace:
  121. return currentNS.Value;
  122. }
  123. return String.Empty;
  124. }
  125. }
  126. public override string XmlLang {
  127. get { return current.XmlLang; }
  128. }
  129. public override XPathNodeType NodeType {
  130. get { return current.NodeType; }
  131. }
  132. public override XPathNavigator Clone ()
  133. {
  134. XomNavigator n = new XomNavigator (root);
  135. n.current = current;
  136. n.currentNS = currentNS;
  137. return n;
  138. }
  139. public override void MoveToRoot ()
  140. {
  141. current = root;
  142. currentNS = null;
  143. }
  144. public override bool MoveToParent ()
  145. {
  146. if (currentNS != null) {
  147. currentNS = null;
  148. return true;
  149. }
  150. XomParentNode parent = current.Parent;
  151. if (parent == null)
  152. return false;
  153. current = parent;
  154. return true;
  155. }
  156. public override bool MoveToFirstChild ()
  157. {
  158. if (currentNS != null || current.NodeType == XPathNodeType.Attribute || current.ChildCount == 0)
  159. return false;
  160. current = current.FirstChild;
  161. return true;
  162. }
  163. public override bool MoveToFirst ()
  164. {
  165. if (currentNS != null || current.NodeType == XPathNodeType.Attribute)
  166. return false;
  167. XomParentNode pn = current.Parent;
  168. if (pn == null)
  169. return false;
  170. XomNode n = pn.FirstChild;
  171. if (n == current)
  172. return false;
  173. current = n;
  174. return true;
  175. }
  176. public override bool MoveToFirstAttribute ()
  177. {
  178. if (currentNS != null)
  179. return false;
  180. XomElement el = current as XomElement;
  181. if (el == null)
  182. return false;
  183. if (el.AttributeCount == 0)
  184. return false;
  185. current = el.GetAttribute (0);
  186. return true;
  187. }
  188. public override bool MoveToNextAttribute ()
  189. {
  190. XomAttribute attr = current as XomAttribute;
  191. if (attr == null)
  192. return false;
  193. XomElement owner = attr.Parent as XomElement;
  194. if (owner == null)
  195. return false;
  196. XomAttribute next = owner.GetNextAttribute (attr);
  197. if (next == null)
  198. return false;
  199. current = next;
  200. return true;
  201. }
  202. public override bool MoveToAttribute (string name, string ns)
  203. {
  204. if (currentNS != null || current.NodeType != XPathNodeType.Element)
  205. return false;
  206. XomElement el = current as XomElement;
  207. XomAttribute attr = el.GetAttribute (name, ns);
  208. if (attr == null)
  209. return false;
  210. current = attr;
  211. return true;
  212. }
  213. public override string GetNamespace (string prefix)
  214. {
  215. if (currentNS != null)
  216. return String.Empty;
  217. XomNamespace n = GetXomNamespace (prefix);
  218. if (n == null)
  219. return String.Empty;
  220. else
  221. return n.Namespace;
  222. }
  223. private XomNamespace GetXomNamespace (string prefix)
  224. {
  225. if (current.NodeType != XPathNodeType.Element)
  226. return null;
  227. if (prefix == "xml")
  228. return XomNamespace.Xml;
  229. XomElement el = current as XomElement;
  230. do {
  231. XomNamespace n = el.GetLocalNamespace (prefix);
  232. if (n != null)
  233. return n;
  234. el = el.Parent as XomElement;
  235. } while (el != null);
  236. return null;
  237. }
  238. public override bool MoveToNamespace (string prefix)
  239. {
  240. if (currentNS != null)
  241. return false;
  242. XomNamespace n = GetXomNamespace (prefix);
  243. if (n == null)
  244. return false;
  245. currentNS = n;
  246. return true;
  247. }
  248. public override bool MoveToFirstNamespace (XPathNamespaceScope scope)
  249. {
  250. if (currentNS != null || current.NodeType != XPathNodeType.Element)
  251. return false;
  252. XomElement el = current as XomElement;
  253. XomNamespace n = null;
  254. switch (scope) {
  255. case XPathNamespaceScope.Local:
  256. if (el.NamespaceCount > 0)
  257. n = el.GetLocalNamespace (0);
  258. break;
  259. default:
  260. do {
  261. if (el.NamespaceCount > 0) {
  262. n = el.GetLocalNamespace (0);
  263. break;
  264. }
  265. el = el.Parent as XomElement;
  266. } while (el != null);
  267. break;
  268. }
  269. if (n != null) {
  270. currentNS = n;
  271. return true;
  272. }
  273. else if (scope == XPathNamespaceScope.All) {
  274. currentNS = XomNamespace.Xml;
  275. return true;
  276. }
  277. return false;
  278. }
  279. public override bool MoveToNextNamespace (XPathNamespaceScope scope)
  280. {
  281. if (currentNS == null || currentNS == XomNamespace.Xml)
  282. return false;
  283. XomElement el = currentNS.Parent as XomElement;
  284. XomNamespace n = el.GetNextLocalNamespace (currentNS);
  285. if (n != null) {
  286. currentNS = n;
  287. return true;
  288. }
  289. switch (scope) {
  290. case XPathNamespaceScope.Local:
  291. return false;
  292. default:
  293. el = el.Parent as XomElement;
  294. while (el != null) {
  295. if (el.NamespaceCount > 0) {
  296. n = el.GetLocalNamespace (0);
  297. break;
  298. }
  299. el = el.Parent as XomElement;
  300. }
  301. break;
  302. }
  303. if (n != null) {
  304. currentNS = n;
  305. return true;
  306. }
  307. else if (scope == XPathNamespaceScope.All) {
  308. currentNS = XomNamespace.Xml;
  309. return true;
  310. }
  311. return false;
  312. }
  313. public override bool MoveToNext ()
  314. {
  315. if (currentNS != null)
  316. return false;
  317. switch (NodeType) {
  318. case XPathNodeType.Text:
  319. case XPathNodeType.SignificantWhitespace:
  320. case XPathNodeType.Whitespace:
  321. for (XomNode t = current.NextSibling; t != null; t = t.NextSibling) {
  322. switch (t.NodeType) {
  323. case XPathNodeType.Text:
  324. case XPathNodeType.SignificantWhitespace:
  325. case XPathNodeType.Whitespace:
  326. continue;
  327. }
  328. current = t;
  329. return true;
  330. }
  331. return false;
  332. default:
  333. XomNode n = current.NextSibling;
  334. if (n == null)
  335. return false;
  336. current = n;
  337. return true;
  338. }
  339. }
  340. public override bool MoveToPrevious ()
  341. {
  342. if (currentNS != null)
  343. return false;
  344. XomNode n = current.PreviousSibling;
  345. if (n == null)
  346. return false;
  347. current = n;
  348. switch (NodeType) {
  349. case XPathNodeType.Text:
  350. case XPathNodeType.SignificantWhitespace:
  351. case XPathNodeType.Whitespace:
  352. for (XomNode t = current.PreviousSibling; t != null; t = t.NextSibling) {
  353. switch (t.NodeType) {
  354. case XPathNodeType.Text:
  355. case XPathNodeType.SignificantWhitespace:
  356. case XPathNodeType.Whitespace:
  357. current = t;
  358. continue;
  359. }
  360. break;
  361. }
  362. break;
  363. }
  364. return true;
  365. }
  366. public override bool IsSamePosition (XPathNavigator other)
  367. {
  368. XomNavigator xom = other as XomNavigator;
  369. if (xom == null || root != xom.root)
  370. return false;
  371. return current == xom.current && currentNS == xom.currentNS;
  372. }
  373. public override bool MoveTo (XPathNavigator other)
  374. {
  375. XomNavigator xom = other as XomNavigator;
  376. if (xom == null)
  377. return false;
  378. current = xom.current;
  379. currentNS = xom.currentNS;
  380. return true;
  381. }
  382. public override bool MoveToId (string id)
  383. {
  384. XomElement el = root.GetIdenticalNode (id);
  385. if (el == null)
  386. return false;
  387. current = el;
  388. return true;
  389. }
  390. }
  391. }
  392. #endif