XPathNavigator.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #endregion
  34. #region Methods
  35. public abstract XPathNavigator Clone ();
  36. [MonoTODO]
  37. public virtual XmlNodeOrder ComparePosition (XPathNavigator nav)
  38. {
  39. throw new NotImplementedException ();
  40. }
  41. public virtual XPathExpression Compile (string xpath)
  42. {
  43. Tokenizer tokenizer = new Tokenizer (xpath);
  44. XPathParser parser = new XPathParser ();
  45. Expression expr = (Expression) parser.yyparse (tokenizer);
  46. // Expression expr = (Expression) parser.yyparseDebug (tokenizer);
  47. return new CompiledExpression (expr);
  48. }
  49. public virtual object Evaluate (string xpath)
  50. {
  51. return Evaluate (Compile (xpath));
  52. }
  53. public virtual object Evaluate (XPathExpression expr)
  54. {
  55. return Evaluate (expr, null);
  56. }
  57. public virtual object Evaluate (XPathExpression expr, XPathNodeIterator context)
  58. {
  59. if (context == null)
  60. context = new NullIterator (this, new DefaultContext ());
  61. return ((CompiledExpression) expr).Evaluate ((BaseIterator) context);
  62. }
  63. public abstract string GetAttribute (string localName, string namespaceURI);
  64. public abstract string GetNamespace (string name);
  65. object ICloneable.Clone ()
  66. {
  67. return Clone ();
  68. }
  69. public virtual bool IsDescendant (XPathNavigator nav)
  70. {
  71. if (nav != null)
  72. {
  73. nav = nav.Clone ();
  74. while (nav.MoveToParent ())
  75. {
  76. if (IsSamePosition (nav))
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. public abstract bool IsSamePosition (XPathNavigator other);
  83. public virtual bool Matches (string xpath)
  84. {
  85. return Matches (Compile (xpath));
  86. }
  87. public virtual bool Matches (XPathExpression expr)
  88. {
  89. XPathNodeIterator nodes = Select (expr);
  90. while (nodes.MoveNext ()) {
  91. if (IsSamePosition (nodes.Current))
  92. return true;
  93. }
  94. XPathNavigator navigator = Clone ();
  95. while (navigator.MoveToParent ()) {
  96. nodes = navigator.Select (expr);
  97. while (nodes.MoveNext ()) {
  98. if (IsSamePosition (nodes.Current))
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. public abstract bool MoveTo (XPathNavigator other);
  105. public abstract bool MoveToAttribute (string localName, string namespaceURI);
  106. public abstract bool MoveToFirst ();
  107. public abstract bool MoveToFirstAttribute ();
  108. public abstract bool MoveToFirstChild ();
  109. public bool MoveToFirstNamespace ()
  110. {
  111. return MoveToFirstNamespace (XPathNamespaceScope.All);
  112. }
  113. public abstract bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope);
  114. public abstract bool MoveToId (string id);
  115. public abstract bool MoveToNamespace (string name);
  116. public abstract bool MoveToNext ();
  117. public abstract bool MoveToNextAttribute ();
  118. public bool MoveToNextNamespace ()
  119. {
  120. return MoveToNextNamespace (XPathNamespaceScope.All);
  121. }
  122. public abstract bool MoveToNextNamespace (XPathNamespaceScope namespaceScope);
  123. public abstract bool MoveToParent ();
  124. public abstract bool MoveToPrevious ();
  125. public abstract void MoveToRoot ();
  126. public virtual XPathNodeIterator Select (string xpath)
  127. {
  128. return Select (Compile (xpath));
  129. }
  130. public virtual XPathNodeIterator Select (XPathExpression expr)
  131. {
  132. BaseIterator iter = new NullIterator (this, new DefaultContext ());
  133. return ((CompiledExpression) expr).EvaluateNodeSet (iter);
  134. }
  135. public virtual XPathNodeIterator SelectAncestors (XPathNodeType type, bool matchSelf)
  136. {
  137. Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
  138. NodeTest test = new NodeTypeTest (axis, type);
  139. return SelectTest (test);
  140. }
  141. [MonoTODO]
  142. public virtual XPathNodeIterator SelectAncestors (string name, string namespaceURI, bool matchSelf)
  143. {
  144. if (namespaceURI != null && namespaceURI != "")
  145. throw new NotImplementedException ();
  146. Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
  147. QName qname = new QName ("", name);
  148. NodeTest test = new NodeNameTest (axis, qname);
  149. return SelectTest (test);
  150. }
  151. public virtual XPathNodeIterator SelectChildren (XPathNodeType type)
  152. {
  153. NodeTest test = new NodeTypeTest (Axes.Child, type);
  154. return SelectTest (test);
  155. }
  156. [MonoTODO]
  157. public virtual XPathNodeIterator SelectChildren (string name, string namespaceURI)
  158. {
  159. if (namespaceURI != null && namespaceURI != "")
  160. throw new NotImplementedException ();
  161. Axes axis = Axes.Child;
  162. QName qname = new QName ("", name);
  163. NodeTest test = new NodeNameTest (axis, qname);
  164. return SelectTest (test);
  165. }
  166. public virtual XPathNodeIterator SelectDescendants (XPathNodeType type, bool matchSelf)
  167. {
  168. Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
  169. NodeTest test = new NodeTypeTest (axis, type);
  170. return SelectTest (test);
  171. }
  172. [MonoTODO]
  173. public virtual XPathNodeIterator SelectDescendants (string name, string namespaceURI, bool matchSelf)
  174. {
  175. if (namespaceURI != null && namespaceURI != "")
  176. throw new NotImplementedException ();
  177. Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
  178. QName qname = new QName ("", name);
  179. NodeTest test = new NodeNameTest (axis, qname);
  180. return SelectTest (test);
  181. }
  182. internal XPathNodeIterator SelectTest (NodeTest test)
  183. {
  184. Expression expr = new ExprStep (test, null);
  185. BaseIterator iter = new NullIterator (this, new DefaultContext ());
  186. return expr.EvaluateNodeSet (iter);
  187. }
  188. public override string ToString ()
  189. {
  190. return Value;
  191. }
  192. #endregion
  193. }
  194. }