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.yyparseSafe (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)
84 CompiledExpression cexpr = (CompiledExpression) expr;
86 context = new NullIterator (this, cexpr.NamespaceManager);
87 return cexpr.Evaluate ((BaseIterator) context);
90 public abstract string GetAttribute (string localName, string namespaceURI);
92 public abstract string GetNamespace (string name);
94 object ICloneable.Clone ()
99 public virtual bool IsDescendant (XPathNavigator nav)
104 while (nav.MoveToParent ())
106 if (IsSamePosition (nav))
113 public abstract bool IsSamePosition (XPathNavigator other);
115 public virtual bool Matches (string xpath)
117 return Matches (Compile (xpath));
120 public virtual bool Matches (XPathExpression expr)
122 XPathNodeIterator nodes = Select (expr);
124 while (nodes.MoveNext ()) {
125 if (IsSamePosition (nodes.Current))
129 XPathNavigator navigator = Clone ();
131 while (navigator.MoveToParent ()) {
132 nodes = navigator.Select (expr);
134 while (nodes.MoveNext ()) {
135 if (IsSamePosition (nodes.Current))
143 public abstract bool MoveTo (XPathNavigator other);
145 public abstract bool MoveToAttribute (string localName, string namespaceURI);
147 public abstract bool MoveToFirst ();
149 public abstract bool MoveToFirstAttribute ();
151 public abstract bool MoveToFirstChild ();
153 public bool MoveToFirstNamespace ()
155 return MoveToFirstNamespace (XPathNamespaceScope.All);
158 public abstract bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope);
160 public abstract bool MoveToId (string id);
162 public abstract bool MoveToNamespace (string name);
164 public abstract bool MoveToNext ();
166 public abstract bool MoveToNextAttribute ();
168 public bool MoveToNextNamespace ()
170 return MoveToNextNamespace (XPathNamespaceScope.All);
173 public abstract bool MoveToNextNamespace (XPathNamespaceScope namespaceScope);
175 public abstract bool MoveToParent ();
177 public abstract bool MoveToPrevious ();
179 public abstract void MoveToRoot ();
181 public virtual XPathNodeIterator Select (string xpath)
183 return Select (Compile (xpath));
186 public virtual XPathNodeIterator Select (XPathExpression expr)
188 CompiledExpression cexpr = (CompiledExpression) expr;
189 BaseIterator iter = new NullIterator (this, cexpr.NamespaceManager);
190 return cexpr.EvaluateNodeSet (iter);
193 public virtual XPathNodeIterator SelectAncestors (XPathNodeType type, bool matchSelf)
195 Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
196 NodeTest test = new NodeTypeTest (axis, type);
197 return SelectTest (test);
201 public virtual XPathNodeIterator SelectAncestors (string name, string namespaceURI, bool matchSelf)
203 if (namespaceURI != null && namespaceURI != "")
204 throw new NotImplementedException ();
206 Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
207 QName qname = new QName ("", name);
208 NodeTest test = new NodeNameTest (axis, qname);
209 return SelectTest (test);
212 public virtual XPathNodeIterator SelectChildren (XPathNodeType type)
214 NodeTest test = new NodeTypeTest (Axes.Child, type);
215 return SelectTest (test);
219 public virtual XPathNodeIterator SelectChildren (string name, string namespaceURI)
221 if (namespaceURI != null && namespaceURI != "")
222 throw new NotImplementedException ();
224 Axes axis = Axes.Child;
225 QName qname = new QName ("", name);
226 NodeTest test = new NodeNameTest (axis, qname);
227 return SelectTest (test);
230 public virtual XPathNodeIterator SelectDescendants (XPathNodeType type, bool matchSelf)
232 Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
233 NodeTest test = new NodeTypeTest (axis, type);
234 return SelectTest (test);
238 public virtual XPathNodeIterator SelectDescendants (string name, string namespaceURI, bool matchSelf)
240 if (namespaceURI != null && namespaceURI != "")
241 throw new NotImplementedException ();
243 Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
244 QName qname = new QName ("", name);
245 NodeTest test = new NodeNameTest (axis, qname);
246 return SelectTest (test);
249 internal XPathNodeIterator SelectTest (NodeTest test)
251 Expression expr = new ExprStep (test, null);
252 BaseIterator iter = new NullIterator (this, null);
253 return expr.EvaluateNodeSet (iter);
256 public override string ToString ()