XPathNavigator.cs 6.2 KB

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