2 // System.Xml.XPath.XPathNavigator
5 // Jason Diamond (jason@injektilo.org)
7 // (C) 2002 Jason Diamond http://injektilo.org/
13 namespace System.Xml.XPath
15 public abstract class XPathNavigator : ICloneable
19 protected XPathNavigator ()
27 public abstract string BaseURI { get; }
29 public abstract bool HasAttributes { get; }
31 public abstract bool HasChildren { get; }
33 public abstract bool IsEmptyElement { get; }
35 public abstract string LocalName { get; }
37 public abstract string Name { get; }
39 public abstract string NamespaceURI { get; }
41 public abstract XmlNameTable NameTable { get; }
43 public abstract XPathNodeType NodeType { get; }
45 public abstract string Prefix { get; }
47 public abstract string Value { get; }
49 public abstract string XmlLang { get; }
55 public abstract XPathNavigator Clone ();
58 public virtual XmlNodeOrder ComparePosition (XPathNavigator nav)
60 throw new NotImplementedException ();
63 public virtual XPathExpression Compile (string xpath)
65 Tokenizer tokenizer = new Tokenizer (xpath);
66 XPathParser parser = new XPathParser ();
67 Expression expr = (Expression) parser.yyparse (tokenizer);
68 // Expression expr = (Expression) parser.yyparseDebug (tokenizer);
69 return new CompiledExpression (expr);
72 public virtual object Evaluate (string xpath)
74 return Evaluate (Compile (xpath));
77 public virtual object Evaluate (XPathExpression expr)
79 return Evaluate (expr, null);
82 public virtual object Evaluate (XPathExpression expr, XPathNodeIterator context)
85 context = new SelfIterator (this, new DefaultContext ());
86 return ((CompiledExpression) expr).Evaluate ((BaseIterator) context);
89 public abstract string GetAttribute (string localName, string namespaceURI);
91 public abstract string GetNamespace (string name);
93 object ICloneable.Clone ()
98 public virtual bool IsDescendant (XPathNavigator nav)
103 while (nav.MoveToParent ())
105 if (IsSamePosition (nav))
112 public abstract bool IsSamePosition (XPathNavigator other);
114 public virtual bool Matches (string xpath)
116 return Matches (Compile (xpath));
119 public virtual bool Matches (XPathExpression expr)
121 if (expr.Expression.StartsWith ("/")) {
122 XPathNodeIterator nodes = Select (expr);
123 while (nodes.MoveNext ()) {
124 if (IsSamePosition (nodes.Current))
128 XPathNavigator navigator = Clone ();
130 while (navigator.MoveToParent ()) {
131 XPathNodeIterator nodes = navigator.Select (expr);
132 while (nodes.MoveNext ()) {
133 if (IsSamePosition (nodes.Current))
142 public abstract bool MoveTo (XPathNavigator other);
144 public abstract bool MoveToAttribute (string localName, string namespaceURI);
146 public abstract bool MoveToFirst ();
148 public abstract bool MoveToFirstAttribute ();
150 public abstract bool MoveToFirstChild ();
152 public bool MoveToFirstNamespace ()
154 return MoveToFirstNamespace (XPathNamespaceScope.All);
157 public abstract bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope);
159 public abstract bool MoveToId (string id);
161 public abstract bool MoveToNamespace (string name);
163 public abstract bool MoveToNext ();
165 public abstract bool MoveToNextAttribute ();
167 public bool MoveToNextNamespace ()
169 return MoveToNextNamespace (XPathNamespaceScope.All);
172 public abstract bool MoveToNextNamespace (XPathNamespaceScope namespaceScope);
174 public abstract bool MoveToParent ();
176 public abstract bool MoveToPrevious ();
178 public abstract void MoveToRoot ();
180 public virtual XPathNodeIterator Select (string xpath)
182 return Select (Compile (xpath));
185 public virtual XPathNodeIterator Select (XPathExpression expr)
187 BaseIterator iter = new SelfIterator (this, new DefaultContext ());
188 return ((CompiledExpression) expr).EvaluateNodeSet (iter);
191 public virtual XPathNodeIterator SelectAncestors (XPathNodeType type, bool matchSelf)
193 Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
194 NodeTest test = new NodeTypeTest (axis, type);
195 return SelectTest (test);
199 public virtual XPathNodeIterator SelectAncestors (string name, string namespaceURI, bool matchSelf)
201 if (namespaceURI != null && namespaceURI != "")
202 throw new NotImplementedException ();
204 Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
205 QName qname = new QName ("", name);
206 NodeTest test = new NodeNameTest (axis, qname);
207 return SelectTest (test);
210 public virtual XPathNodeIterator SelectChildren (XPathNodeType type)
212 NodeTest test = new NodeTypeTest (Axes.Child, type);
213 return SelectTest (test);
217 public virtual XPathNodeIterator SelectChildren (string name, string namespaceURI)
219 if (namespaceURI != null && namespaceURI != "")
220 throw new NotImplementedException ();
222 Axes axis = Axes.Child;
223 QName qname = new QName ("", name);
224 NodeTest test = new NodeNameTest (axis, qname);
225 return SelectTest (test);
228 public virtual XPathNodeIterator SelectDescendants (XPathNodeType type, bool matchSelf)
230 Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
231 NodeTest test = new NodeTypeTest (axis, type);
232 return SelectTest (test);
236 public virtual XPathNodeIterator SelectDescendants (string name, string namespaceURI, bool matchSelf)
238 if (namespaceURI != null && namespaceURI != "")
239 throw new NotImplementedException ();
241 Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
242 QName qname = new QName ("", name);
243 NodeTest test = new NodeNameTest (axis, qname);
244 return SelectTest (test);
247 internal XPathNodeIterator SelectTest (NodeTest test)
249 Expression expr = new ExprStep (test, null);
250 BaseIterator iter = new SelfIterator (this, new DefaultContext ());
251 return expr.EvaluateNodeSet (iter);
254 public override string ToString ()