XPathNavigator.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. public virtual XmlNodeOrder ComparePosition (XPathNavigator nav)
  48. {
  49. if (IsSamePosition (nav))
  50. return XmlNodeOrder.Same;
  51. XPathNavigator nav1 = Clone ();
  52. XPathNavigator nav2 = nav.Clone ();
  53. int nDepth1 = nav1.Depth;
  54. int nDepth2 = nav2.Depth;
  55. if (nDepth1 > nDepth2)
  56. {
  57. while (nDepth1 > nDepth2)
  58. {
  59. nav1.MoveToParent ();
  60. nDepth1 --;
  61. }
  62. if (nav1.IsSamePosition (nav2))
  63. return XmlNodeOrder.After;
  64. }
  65. else if (nDepth1 < nDepth2)
  66. {
  67. while (nDepth1 < nDepth2)
  68. {
  69. nav2.MoveToParent ();
  70. nDepth2 --;
  71. }
  72. if (nav1.IsSamePosition (nav2))
  73. return XmlNodeOrder.Before;
  74. }
  75. XPathNavigator parent1 = nav1.Clone ();
  76. XPathNavigator parent2 = nav2.Clone ();
  77. while (parent1.MoveToParent () && parent2.MoveToParent ())
  78. {
  79. if (parent1.IsSamePosition (parent2))
  80. {
  81. // the ordering is namespace, attribute, children
  82. // assume that nav1 is before nav2, find counter-example
  83. if (nav1.NodeType == XPathNodeType.Namespace)
  84. {
  85. if (nav2.NodeType == XPathNodeType.Namespace)
  86. {
  87. // match namespaces
  88. while (nav2.MoveToNextNamespace ())
  89. if (nav2.IsSamePosition (nav1))
  90. return XmlNodeOrder.After;
  91. }
  92. }
  93. else if (nav1.NodeType == XPathNodeType.Attribute)
  94. {
  95. if (nav2.NodeType == XPathNodeType.Namespace)
  96. return XmlNodeOrder.After;
  97. else if (nav2.NodeType == XPathNodeType.Attribute)
  98. {
  99. // match attributes
  100. while (nav2.MoveToNextAttribute ())
  101. if (nav2.IsSamePosition (nav1))
  102. return XmlNodeOrder.After;
  103. }
  104. }
  105. else
  106. {
  107. // match children
  108. while (nav2.MoveToNext ())
  109. if (nav2.IsSamePosition (nav1))
  110. return XmlNodeOrder.After;
  111. }
  112. return XmlNodeOrder.Before;
  113. }
  114. nav1.MoveToParent ();
  115. nav2.MoveToParent ();
  116. }
  117. return XmlNodeOrder.Unknown;
  118. }
  119. public virtual XPathExpression Compile (string xpath)
  120. {
  121. XPathParser parser = new XPathParser ();
  122. return new CompiledExpression (parser.Compile (xpath));
  123. }
  124. internal virtual XPathExpression Compile (string xpath, System.Xml.Xsl.IStaticXsltContext ctx)
  125. {
  126. XPathParser parser = new XPathParser (ctx);
  127. return new CompiledExpression (parser.Compile (xpath));
  128. }
  129. public virtual object Evaluate (string xpath)
  130. {
  131. return Evaluate (Compile (xpath));
  132. }
  133. public virtual object Evaluate (XPathExpression expr)
  134. {
  135. return Evaluate (expr, null);
  136. }
  137. public virtual object Evaluate (XPathExpression expr, XPathNodeIterator context)
  138. {
  139. return Evaluate (expr, context, null);
  140. }
  141. internal virtual object Evaluate (XPathExpression expr, XPathNodeIterator context, XmlNamespaceManager ctx)
  142. {
  143. CompiledExpression cexpr = (CompiledExpression) expr;
  144. if (ctx == null)
  145. ctx = cexpr.NamespaceManager;
  146. if (context == null)
  147. context = new NullIterator (this, ctx);
  148. BaseIterator iterContext = (BaseIterator) context;
  149. iterContext.NamespaceManager = ctx;
  150. return cexpr.Evaluate (iterContext);
  151. }
  152. internal XPathNodeIterator EvaluateNodeSet (XPathExpression expr, XPathNodeIterator context, XmlNamespaceManager ctx)
  153. {
  154. CompiledExpression cexpr = (CompiledExpression) expr;
  155. if (ctx == null)
  156. ctx = cexpr.NamespaceManager;
  157. if (context == null)
  158. context = new NullIterator (this, cexpr.NamespaceManager);
  159. BaseIterator iterContext = (BaseIterator) context;
  160. iterContext.NamespaceManager = ctx;
  161. return cexpr.EvaluateNodeSet (iterContext);
  162. }
  163. internal string EvaluateString (XPathExpression expr, XPathNodeIterator context, XmlNamespaceManager ctx)
  164. {
  165. CompiledExpression cexpr = (CompiledExpression) expr;
  166. if (ctx == null)
  167. ctx = cexpr.NamespaceManager;
  168. if (context == null)
  169. context = new NullIterator (this, cexpr.NamespaceManager);
  170. BaseIterator iterContext = (BaseIterator) context;
  171. iterContext.NamespaceManager = ctx;
  172. return cexpr.EvaluateString (iterContext);
  173. }
  174. internal double EvaluateNumber (XPathExpression expr, XPathNodeIterator context, XmlNamespaceManager ctx)
  175. {
  176. CompiledExpression cexpr = (CompiledExpression) expr;
  177. if (ctx == null)
  178. ctx = cexpr.NamespaceManager;
  179. if (context == null)
  180. context = new NullIterator (this, cexpr.NamespaceManager);
  181. BaseIterator iterContext = (BaseIterator) context;
  182. iterContext.NamespaceManager = ctx;
  183. return cexpr.EvaluateNumber (iterContext);
  184. }
  185. internal bool EvaluateBoolean (XPathExpression expr, XPathNodeIterator context, XmlNamespaceManager ctx)
  186. {
  187. CompiledExpression cexpr = (CompiledExpression) expr;
  188. if (ctx == null)
  189. ctx = cexpr.NamespaceManager;
  190. if (context == null)
  191. context = new NullIterator (this, cexpr.NamespaceManager);
  192. BaseIterator iterContext = (BaseIterator) context;
  193. iterContext.NamespaceManager = ctx;
  194. return cexpr.EvaluateBoolean (iterContext);
  195. }
  196. public abstract string GetAttribute (string localName, string namespaceURI);
  197. public abstract string GetNamespace (string name);
  198. object ICloneable.Clone ()
  199. {
  200. return Clone ();
  201. }
  202. public virtual bool IsDescendant (XPathNavigator nav)
  203. {
  204. if (nav != null)
  205. {
  206. nav = nav.Clone ();
  207. while (nav.MoveToParent ())
  208. {
  209. if (IsSamePosition (nav))
  210. return true;
  211. }
  212. }
  213. return false;
  214. }
  215. public abstract bool IsSamePosition (XPathNavigator other);
  216. public virtual bool Matches (string xpath)
  217. {
  218. return Matches (Compile (xpath));
  219. }
  220. [MonoTODO] // optimize...
  221. public virtual bool Matches (XPathExpression expr)
  222. {
  223. Expression e = ((CompiledExpression)expr).ExpressionNode;
  224. if (e is NodeTest)
  225. return ((NodeTest)e).Match (((CompiledExpression)expr).NamespaceManager, this);
  226. if (e is ExprFilter) {
  227. do {
  228. e = ((ExprFilter)e).LeftHandSide;
  229. } while (e is ExprFilter);
  230. if (e is NodeTest && !((NodeTest)e).Match (((CompiledExpression)expr).NamespaceManager, this))
  231. return false;
  232. }
  233. //e = ((CompiledExpression)expr).ExpressionNode;
  234. //Console.WriteLine ("Didnt filter : " + e.GetType ().ToString () + " " + e.ToString ());
  235. XPathNodeIterator nodes = Select (expr);
  236. while (nodes.MoveNext ()) {
  237. if (IsSamePosition (nodes.Current))
  238. return true;
  239. }
  240. XPathNavigator navigator = Clone ();
  241. while (navigator.MoveToParent ()) {
  242. nodes = navigator.Select (expr);
  243. while (nodes.MoveNext ()) {
  244. if (IsSamePosition (nodes.Current))
  245. return true;
  246. }
  247. }
  248. return false;
  249. }
  250. public abstract bool MoveTo (XPathNavigator other);
  251. public abstract bool MoveToAttribute (string localName, string namespaceURI);
  252. public abstract bool MoveToFirst ();
  253. public abstract bool MoveToFirstAttribute ();
  254. public abstract bool MoveToFirstChild ();
  255. public bool MoveToFirstNamespace ()
  256. {
  257. return MoveToFirstNamespace (XPathNamespaceScope.All);
  258. }
  259. public abstract bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope);
  260. public abstract bool MoveToId (string id);
  261. public abstract bool MoveToNamespace (string name);
  262. public abstract bool MoveToNext ();
  263. public abstract bool MoveToNextAttribute ();
  264. public bool MoveToNextNamespace ()
  265. {
  266. return MoveToNextNamespace (XPathNamespaceScope.All);
  267. }
  268. public abstract bool MoveToNextNamespace (XPathNamespaceScope namespaceScope);
  269. public abstract bool MoveToParent ();
  270. public abstract bool MoveToPrevious ();
  271. public abstract void MoveToRoot ();
  272. public virtual XPathNodeIterator Select (string xpath)
  273. {
  274. return Select (Compile (xpath));
  275. }
  276. public virtual XPathNodeIterator Select (XPathExpression expr)
  277. {
  278. return Select (expr, null);
  279. }
  280. internal virtual XPathNodeIterator Select (XPathExpression expr, XmlNamespaceManager ctx)
  281. {
  282. CompiledExpression cexpr = (CompiledExpression) expr;
  283. if (ctx == null)
  284. ctx = cexpr.NamespaceManager;
  285. BaseIterator iter = new NullIterator (this, ctx);
  286. return cexpr.EvaluateNodeSet (iter);
  287. }
  288. public virtual XPathNodeIterator SelectAncestors (XPathNodeType type, bool matchSelf)
  289. {
  290. Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
  291. return SelectTest (new NodeTypeTest (axis, type));
  292. }
  293. public virtual XPathNodeIterator SelectAncestors (string name, string namespaceURI, bool matchSelf)
  294. {
  295. if (name == null)
  296. throw new ArgumentNullException ("name");
  297. if (namespaceURI == null)
  298. throw new ArgumentNullException ("namespaceURI");
  299. Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
  300. XmlQualifiedName qname = new XmlQualifiedName (name, namespaceURI);
  301. return SelectTest (new NodeNameTest (axis, qname, true));
  302. }
  303. public virtual XPathNodeIterator SelectChildren (XPathNodeType type)
  304. {
  305. return SelectTest (new NodeTypeTest (Axes.Child, type));
  306. }
  307. public virtual XPathNodeIterator SelectChildren (string name, string namespaceURI)
  308. {
  309. if (name == null)
  310. throw new ArgumentNullException ("name");
  311. if (namespaceURI == null)
  312. throw new ArgumentNullException ("namespaceURI");
  313. Axes axis = Axes.Child;
  314. XmlQualifiedName qname = new XmlQualifiedName (name, namespaceURI);
  315. return SelectTest (new NodeNameTest (axis, qname, true));
  316. }
  317. public virtual XPathNodeIterator SelectDescendants (XPathNodeType type, bool matchSelf)
  318. {
  319. Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
  320. return SelectTest (new NodeTypeTest (axis, type));
  321. }
  322. public virtual XPathNodeIterator SelectDescendants (string name, string namespaceURI, bool matchSelf)
  323. {
  324. if (name == null)
  325. throw new ArgumentNullException ("name");
  326. if (namespaceURI == null)
  327. throw new ArgumentNullException ("namespaceURI");
  328. Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
  329. XmlQualifiedName qname = new XmlQualifiedName (name, namespaceURI);
  330. return SelectTest (new NodeNameTest (axis, qname, true));
  331. }
  332. internal XPathNodeIterator SelectTest (NodeTest test)
  333. {
  334. return test.EvaluateNodeSet (new NullIterator (this));
  335. }
  336. public override string ToString ()
  337. {
  338. return Value;
  339. }
  340. #endregion
  341. }
  342. }