2
0

XPathNavigator.cs 12 KB

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