XPathNavigator.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // System.Xml.XPath.XPathNavigator
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. //
  7. // (C) 2002 Jason Diamond http://injektilo.org/
  8. //
  9. using System;
  10. using Mono.Xml.XPath;
  11. namespace System.Xml.XPath
  12. {
  13. public abstract class XPathNavigator : ICloneable
  14. {
  15. #region Constructor
  16. protected XPathNavigator ()
  17. {
  18. }
  19. #endregion
  20. #region Properties
  21. public abstract string BaseURI { get; }
  22. public abstract bool HasAttributes { get; }
  23. public abstract bool HasChildren { get; }
  24. public abstract bool IsEmptyElement { get; }
  25. public abstract string LocalName { get; }
  26. public abstract string Name { get; }
  27. public abstract string NamespaceURI { get; }
  28. public abstract XmlNameTable NameTable { get; }
  29. public abstract XPathNodeType NodeType { get; }
  30. public abstract string Prefix { get; }
  31. public abstract string Value { get; }
  32. public abstract string XmlLang { get; }
  33. int Depth
  34. {
  35. get
  36. {
  37. int cLevels = 0;
  38. XPathNavigator nav = Clone ();
  39. while (nav.MoveToParent ())
  40. cLevels ++;
  41. return cLevels;
  42. }
  43. }
  44. #endregion
  45. #region Methods
  46. public abstract XPathNavigator Clone ();
  47. [MonoTODO]
  48. public virtual XmlNodeOrder ComparePosition (XPathNavigator nav)
  49. {
  50. if (IsSamePosition (nav))
  51. return XmlNodeOrder.Same;
  52. XPathNavigator nav1 = Clone ();
  53. XPathNavigator nav2 = nav.Clone ();
  54. int nDepth1 = nav1.Depth;
  55. int nDepth2 = nav2.Depth;
  56. if (nDepth1 > nDepth2)
  57. {
  58. while (nDepth1 > nDepth2)
  59. {
  60. nav1.MoveToParent ();
  61. nDepth1 --;
  62. }
  63. if (nav1.IsSamePosition (nav2))
  64. return XmlNodeOrder.After;
  65. }
  66. else if (nDepth1 < nDepth2)
  67. {
  68. while (nDepth1 < nDepth2)
  69. {
  70. nav2.MoveToParent ();
  71. nDepth2 --;
  72. }
  73. if (nav1.IsSamePosition (nav2))
  74. return XmlNodeOrder.Before;
  75. }
  76. XPathNavigator parent1 = nav1.Clone ();
  77. XPathNavigator parent2 = nav2.Clone ();
  78. while (parent1.MoveToParent () && parent2.MoveToParent ())
  79. {
  80. if (parent1.IsSamePosition (parent2))
  81. {
  82. // the ordering is namespace, attribute, children
  83. // assume that nav1 is before nav2, find counter-example
  84. if (nav1.NodeType == XPathNodeType.Namespace)
  85. {
  86. if (nav2.NodeType == XPathNodeType.Namespace)
  87. {
  88. // match namespaces
  89. while (nav2.MoveToNextNamespace ())
  90. if (nav2.IsSamePosition (nav1))
  91. return XmlNodeOrder.After;
  92. }
  93. }
  94. else if (nav1.NodeType == XPathNodeType.Attribute)
  95. {
  96. if (nav2.NodeType == XPathNodeType.Namespace)
  97. return XmlNodeOrder.After;
  98. else if (nav2.NodeType == XPathNodeType.Attribute)
  99. {
  100. // match attributes
  101. while (nav2.MoveToNextAttribute ())
  102. if (nav2.IsSamePosition (nav1))
  103. return XmlNodeOrder.After;
  104. }
  105. }
  106. else
  107. {
  108. // match children
  109. while (nav2.MoveToNext ())
  110. if (nav2.IsSamePosition (nav1))
  111. return XmlNodeOrder.After;
  112. }
  113. return XmlNodeOrder.Before;
  114. }
  115. nav1.MoveToParent ();
  116. nav2.MoveToParent ();
  117. }
  118. return XmlNodeOrder.Unknown;
  119. }
  120. public virtual XPathExpression Compile (string xpath)
  121. {
  122. Tokenizer tokenizer = new Tokenizer (xpath);
  123. XPathParser parser = new XPathParser ();
  124. Expression expr = (Expression) parser.yyparseSafe (tokenizer);
  125. // Expression expr = (Expression) parser.yyparseDebug (tokenizer);
  126. return new CompiledExpression (expr);
  127. }
  128. public virtual object Evaluate (string xpath)
  129. {
  130. return Evaluate (Compile (xpath));
  131. }
  132. public virtual object Evaluate (XPathExpression expr)
  133. {
  134. return Evaluate (expr, null);
  135. }
  136. public virtual object Evaluate (XPathExpression expr, XPathNodeIterator context)
  137. {
  138. CompiledExpression cexpr = (CompiledExpression) expr;
  139. if (context == null)
  140. context = new NullIterator (this, cexpr.NamespaceManager);
  141. BaseIterator iterContext = (BaseIterator) context;
  142. iterContext.NamespaceManager = cexpr.NamespaceManager;
  143. return cexpr.Evaluate (iterContext);
  144. }
  145. internal XPathNodeIterator EvaluateNodeSet (XPathExpression expr, XPathNodeIterator context)
  146. {
  147. CompiledExpression cexpr = (CompiledExpression) expr;
  148. if (context == null)
  149. context = new NullIterator (this, cexpr.NamespaceManager);
  150. BaseIterator iterContext = (BaseIterator) context;
  151. iterContext.NamespaceManager = cexpr.NamespaceManager;
  152. return cexpr.EvaluateNodeSet (iterContext);
  153. }
  154. internal string EvaluateString (XPathExpression expr, XPathNodeIterator context)
  155. {
  156. CompiledExpression cexpr = (CompiledExpression) expr;
  157. if (context == null)
  158. context = new NullIterator (this, cexpr.NamespaceManager);
  159. BaseIterator iterContext = (BaseIterator) context;
  160. iterContext.NamespaceManager = cexpr.NamespaceManager;
  161. return cexpr.EvaluateString (iterContext);
  162. }
  163. internal double EvaluateNumber (XPathExpression expr, XPathNodeIterator context)
  164. {
  165. CompiledExpression cexpr = (CompiledExpression) expr;
  166. if (context == null)
  167. context = new NullIterator (this, cexpr.NamespaceManager);
  168. BaseIterator iterContext = (BaseIterator) context;
  169. iterContext.NamespaceManager = cexpr.NamespaceManager;
  170. return cexpr.EvaluateNumber (iterContext);
  171. }
  172. internal bool EvaluateBoolean (XPathExpression expr, XPathNodeIterator context)
  173. {
  174. CompiledExpression cexpr = (CompiledExpression) expr;
  175. if (context == null)
  176. context = new NullIterator (this, cexpr.NamespaceManager);
  177. BaseIterator iterContext = (BaseIterator) context;
  178. iterContext.NamespaceManager = cexpr.NamespaceManager;
  179. return cexpr.EvaluateBoolean (iterContext);
  180. }
  181. public abstract string GetAttribute (string localName, string namespaceURI);
  182. public abstract string GetNamespace (string name);
  183. object ICloneable.Clone ()
  184. {
  185. return Clone ();
  186. }
  187. public virtual bool IsDescendant (XPathNavigator nav)
  188. {
  189. if (nav != null)
  190. {
  191. nav = nav.Clone ();
  192. while (nav.MoveToParent ())
  193. {
  194. if (IsSamePosition (nav))
  195. return true;
  196. }
  197. }
  198. return false;
  199. }
  200. public abstract bool IsSamePosition (XPathNavigator other);
  201. public virtual bool Matches (string xpath)
  202. {
  203. return Matches (Compile (xpath));
  204. }
  205. public virtual bool Matches (XPathExpression expr)
  206. {
  207. XPathNodeIterator nodes = Select (expr);
  208. while (nodes.MoveNext ()) {
  209. if (IsSamePosition (nodes.Current))
  210. return true;
  211. }
  212. XPathNavigator navigator = Clone ();
  213. while (navigator.MoveToParent ()) {
  214. nodes = navigator.Select (expr);
  215. while (nodes.MoveNext ()) {
  216. if (IsSamePosition (nodes.Current))
  217. return true;
  218. }
  219. }
  220. return false;
  221. }
  222. public abstract bool MoveTo (XPathNavigator other);
  223. public abstract bool MoveToAttribute (string localName, string namespaceURI);
  224. public abstract bool MoveToFirst ();
  225. public abstract bool MoveToFirstAttribute ();
  226. public abstract bool MoveToFirstChild ();
  227. public bool MoveToFirstNamespace ()
  228. {
  229. return MoveToFirstNamespace (XPathNamespaceScope.All);
  230. }
  231. public abstract bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope);
  232. public abstract bool MoveToId (string id);
  233. public abstract bool MoveToNamespace (string name);
  234. public abstract bool MoveToNext ();
  235. public abstract bool MoveToNextAttribute ();
  236. public bool MoveToNextNamespace ()
  237. {
  238. return MoveToNextNamespace (XPathNamespaceScope.All);
  239. }
  240. public abstract bool MoveToNextNamespace (XPathNamespaceScope namespaceScope);
  241. public abstract bool MoveToParent ();
  242. public abstract bool MoveToPrevious ();
  243. public abstract void MoveToRoot ();
  244. public virtual XPathNodeIterator Select (string xpath)
  245. {
  246. return Select (Compile (xpath));
  247. }
  248. public virtual XPathNodeIterator Select (XPathExpression expr)
  249. {
  250. CompiledExpression cexpr = (CompiledExpression) expr;
  251. BaseIterator iter = new NullIterator (this, cexpr.NamespaceManager);
  252. return cexpr.EvaluateNodeSet (iter);
  253. }
  254. public virtual XPathNodeIterator SelectAncestors (XPathNodeType type, bool matchSelf)
  255. {
  256. Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
  257. return SelectTest (new NodeTypeTest (axis, type));
  258. }
  259. [MonoTODO]
  260. public virtual XPathNodeIterator SelectAncestors (string name, string namespaceURI, bool matchSelf)
  261. {
  262. if (namespaceURI != null && namespaceURI != "")
  263. throw new NotImplementedException ();
  264. Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
  265. XmlQualifiedName qname = new XmlQualifiedName (name);
  266. return SelectTest (new NodeNameTest (axis, qname));
  267. }
  268. public virtual XPathNodeIterator SelectChildren (XPathNodeType type)
  269. {
  270. return SelectTest (new NodeTypeTest (Axes.Child, type));
  271. }
  272. [MonoTODO]
  273. public virtual XPathNodeIterator SelectChildren (string name, string namespaceURI)
  274. {
  275. if (namespaceURI != null && namespaceURI != "")
  276. throw new NotImplementedException ();
  277. Axes axis = Axes.Child;
  278. XmlQualifiedName qname = new XmlQualifiedName (name);
  279. return SelectTest (new NodeNameTest (axis, qname));
  280. }
  281. public virtual XPathNodeIterator SelectDescendants (XPathNodeType type, bool matchSelf)
  282. {
  283. Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
  284. return SelectTest (new NodeTypeTest (axis, type));
  285. }
  286. [MonoTODO]
  287. public virtual XPathNodeIterator SelectDescendants (string name, string namespaceURI, bool matchSelf)
  288. {
  289. if (namespaceURI != null && namespaceURI != "")
  290. throw new NotImplementedException ();
  291. Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
  292. XmlQualifiedName qname = new XmlQualifiedName (name);
  293. return SelectTest (new NodeNameTest (axis, qname));
  294. }
  295. internal XPathNodeIterator SelectTest (NodeTest test)
  296. {
  297. return test.EvaluateNodeSet (new NullIterator (this));
  298. }
  299. public override string ToString ()
  300. {
  301. return Value;
  302. }
  303. #endregion
  304. }
  305. }