XPathNavigator.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 SelfIterator (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. [MonoTODO]
  88. public virtual bool Matches (XPathExpression expr)
  89. {
  90. throw new NotImplementedException ();
  91. }
  92. public abstract bool MoveTo (XPathNavigator other);
  93. public abstract bool MoveToAttribute (string localName, string namespaceURI);
  94. public abstract bool MoveToFirst ();
  95. public abstract bool MoveToFirstAttribute ();
  96. public abstract bool MoveToFirstChild ();
  97. public bool MoveToFirstNamespace ()
  98. {
  99. return MoveToFirstNamespace (XPathNamespaceScope.All);
  100. }
  101. public abstract bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope);
  102. public abstract bool MoveToId (string id);
  103. public abstract bool MoveToNamespace (string name);
  104. public abstract bool MoveToNext ();
  105. public abstract bool MoveToNextAttribute ();
  106. public bool MoveToNextNamespace ()
  107. {
  108. return MoveToNextNamespace (XPathNamespaceScope.All);
  109. }
  110. public abstract bool MoveToNextNamespace (XPathNamespaceScope namespaceScope);
  111. public abstract bool MoveToParent ();
  112. public abstract bool MoveToPrevious ();
  113. public abstract void MoveToRoot ();
  114. public virtual XPathNodeIterator Select (string xpath)
  115. {
  116. return Select (Compile (xpath));
  117. }
  118. public virtual XPathNodeIterator Select (XPathExpression expr)
  119. {
  120. BaseIterator iter = new SelfIterator (this, new DefaultContext ());
  121. return ((CompiledExpression) expr).EvaluateNodeSet (iter);
  122. }
  123. public virtual XPathNodeIterator SelectAncestors (XPathNodeType type, bool matchSelf)
  124. {
  125. Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
  126. NodeTest test = new NodeTypeTest (axis, type);
  127. return SelectTest (test);
  128. }
  129. [MonoTODO]
  130. public virtual XPathNodeIterator SelectAncestors (string name, string namespaceURI, bool matchSelf)
  131. {
  132. if (namespaceURI != null && namespaceURI != "")
  133. throw new NotImplementedException ();
  134. Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
  135. QName qname = new QName ("", name);
  136. NodeTest test = new NodeNameTest (axis, qname);
  137. return SelectTest (test);
  138. }
  139. public virtual XPathNodeIterator SelectChildren (XPathNodeType type)
  140. {
  141. NodeTest test = new NodeTypeTest (Axes.Child, type);
  142. return SelectTest (test);
  143. }
  144. [MonoTODO]
  145. public virtual XPathNodeIterator SelectChildren (string name, string namespaceURI)
  146. {
  147. if (namespaceURI != null && namespaceURI != "")
  148. throw new NotImplementedException ();
  149. Axes axis = Axes.Child;
  150. QName qname = new QName ("", name);
  151. NodeTest test = new NodeNameTest (axis, qname);
  152. return SelectTest (test);
  153. }
  154. public virtual XPathNodeIterator SelectDescendants (XPathNodeType type, bool matchSelf)
  155. {
  156. Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
  157. NodeTest test = new NodeTypeTest (axis, type);
  158. return SelectTest (test);
  159. }
  160. [MonoTODO]
  161. public virtual XPathNodeIterator SelectDescendants (string name, string namespaceURI, bool matchSelf)
  162. {
  163. if (namespaceURI != null && namespaceURI != "")
  164. throw new NotImplementedException ();
  165. Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
  166. QName qname = new QName ("", name);
  167. NodeTest test = new NodeNameTest (axis, qname);
  168. return SelectTest (test);
  169. }
  170. internal XPathNodeIterator SelectTest (NodeTest test)
  171. {
  172. Expression expr = new ExprStep (test, null);
  173. BaseIterator iter = new SelfIterator (this, new DefaultContext ());
  174. return expr.EvaluateNodeSet (iter);
  175. }
  176. public override string ToString ()
  177. {
  178. return Value;
  179. }
  180. #endregion
  181. }
  182. }