XPathNavigator.cs 5.8 KB

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