XPathNavigator.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. public abstract string GetAttribute (string localName, string namespaceURI);
  146. public abstract string GetNamespace (string name);
  147. object ICloneable.Clone ()
  148. {
  149. return Clone ();
  150. }
  151. public virtual bool IsDescendant (XPathNavigator nav)
  152. {
  153. if (nav != null)
  154. {
  155. nav = nav.Clone ();
  156. while (nav.MoveToParent ())
  157. {
  158. if (IsSamePosition (nav))
  159. return true;
  160. }
  161. }
  162. return false;
  163. }
  164. public abstract bool IsSamePosition (XPathNavigator other);
  165. public virtual bool Matches (string xpath)
  166. {
  167. return Matches (Compile (xpath));
  168. }
  169. public virtual bool Matches (XPathExpression expr)
  170. {
  171. XPathNodeIterator nodes = Select (expr);
  172. while (nodes.MoveNext ()) {
  173. if (IsSamePosition (nodes.Current))
  174. return true;
  175. }
  176. XPathNavigator navigator = Clone ();
  177. while (navigator.MoveToParent ()) {
  178. nodes = navigator.Select (expr);
  179. while (nodes.MoveNext ()) {
  180. if (IsSamePosition (nodes.Current))
  181. return true;
  182. }
  183. }
  184. return false;
  185. }
  186. public abstract bool MoveTo (XPathNavigator other);
  187. public abstract bool MoveToAttribute (string localName, string namespaceURI);
  188. public abstract bool MoveToFirst ();
  189. public abstract bool MoveToFirstAttribute ();
  190. public abstract bool MoveToFirstChild ();
  191. public bool MoveToFirstNamespace ()
  192. {
  193. return MoveToFirstNamespace (XPathNamespaceScope.All);
  194. }
  195. public abstract bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope);
  196. public abstract bool MoveToId (string id);
  197. public abstract bool MoveToNamespace (string name);
  198. public abstract bool MoveToNext ();
  199. public abstract bool MoveToNextAttribute ();
  200. public bool MoveToNextNamespace ()
  201. {
  202. return MoveToNextNamespace (XPathNamespaceScope.All);
  203. }
  204. public abstract bool MoveToNextNamespace (XPathNamespaceScope namespaceScope);
  205. public abstract bool MoveToParent ();
  206. public abstract bool MoveToPrevious ();
  207. public abstract void MoveToRoot ();
  208. public virtual XPathNodeIterator Select (string xpath)
  209. {
  210. return Select (Compile (xpath));
  211. }
  212. public virtual XPathNodeIterator Select (XPathExpression expr)
  213. {
  214. CompiledExpression cexpr = (CompiledExpression) expr;
  215. BaseIterator iter = new NullIterator (this, cexpr.NamespaceManager);
  216. return cexpr.EvaluateNodeSet (iter);
  217. }
  218. public virtual XPathNodeIterator SelectAncestors (XPathNodeType type, bool matchSelf)
  219. {
  220. Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
  221. NodeTest test = new NodeTypeTest (axis, type);
  222. return SelectTest (test);
  223. }
  224. [MonoTODO]
  225. public virtual XPathNodeIterator SelectAncestors (string name, string namespaceURI, bool matchSelf)
  226. {
  227. if (namespaceURI != null && namespaceURI != "")
  228. throw new NotImplementedException ();
  229. Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
  230. XmlQualifiedName qname = new XmlQualifiedName (name);
  231. NodeTest test = new NodeNameTest (axis, qname);
  232. return SelectTest (test);
  233. }
  234. public virtual XPathNodeIterator SelectChildren (XPathNodeType type)
  235. {
  236. NodeTest test = new NodeTypeTest (Axes.Child, type);
  237. return SelectTest (test);
  238. }
  239. [MonoTODO]
  240. public virtual XPathNodeIterator SelectChildren (string name, string namespaceURI)
  241. {
  242. if (namespaceURI != null && namespaceURI != "")
  243. throw new NotImplementedException ();
  244. Axes axis = Axes.Child;
  245. XmlQualifiedName qname = new XmlQualifiedName (name);
  246. NodeTest test = new NodeNameTest (axis, qname);
  247. return SelectTest (test);
  248. }
  249. public virtual XPathNodeIterator SelectDescendants (XPathNodeType type, bool matchSelf)
  250. {
  251. Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
  252. NodeTest test = new NodeTypeTest (axis, type);
  253. return SelectTest (test);
  254. }
  255. [MonoTODO]
  256. public virtual XPathNodeIterator SelectDescendants (string name, string namespaceURI, bool matchSelf)
  257. {
  258. if (namespaceURI != null && namespaceURI != "")
  259. throw new NotImplementedException ();
  260. Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
  261. XmlQualifiedName qname = new XmlQualifiedName (name);
  262. NodeTest test = new NodeNameTest (axis, qname);
  263. return SelectTest (test);
  264. }
  265. internal XPathNodeIterator SelectTest (NodeTest test)
  266. {
  267. Expression expr = new ExprStep (test, null);
  268. BaseIterator iter = new NullIterator (this, null);
  269. return expr.EvaluateNodeSet (iter);
  270. }
  271. public override string ToString ()
  272. {
  273. return Value;
  274. }
  275. #endregion
  276. }
  277. }