XPathNavigator.cs 6.4 KB

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