XPathNavigator.cs 12 KB

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