2003-02-16 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[mono.git] / mcs / class / System.XML / System.Xml.XPath / XPathNavigator.cs
1 //
2 // System.Xml.XPath.XPathNavigator
3 //
4 // Author:
5 //   Jason Diamond (jason@injektilo.org)
6 //
7 // (C) 2002 Jason Diamond  http://injektilo.org/
8 //
9
10 using System;
11 using Mono.Xml.XPath;
12
13 namespace System.Xml.XPath
14 {
15         public abstract class XPathNavigator : ICloneable
16         {
17                 #region Constructor
18
19                 protected XPathNavigator ()
20                 {
21                 }
22
23                 #endregion
24
25                 #region Properties
26
27                 public abstract string BaseURI { get; }
28
29                 public abstract bool HasAttributes { get; }
30
31                 public abstract bool HasChildren { get; }
32
33                 public abstract bool IsEmptyElement { get; }
34
35                 public abstract string LocalName { get; }
36
37                 public abstract string Name { get; }
38
39                 public abstract string NamespaceURI { get; }
40
41                 public abstract XmlNameTable NameTable { get; }
42
43                 public abstract XPathNodeType NodeType { get; }
44
45                 public abstract string Prefix { get; }
46
47                 public abstract string Value { get; }
48
49                 public abstract string XmlLang { get; }
50
51                 #endregion
52
53                 #region Methods
54
55                 public abstract XPathNavigator Clone ();
56
57                 [MonoTODO]
58                 public virtual XmlNodeOrder ComparePosition (XPathNavigator nav)
59                 {
60                         throw new NotImplementedException ();
61                 }
62
63                 public virtual XPathExpression Compile (string xpath)
64                 {
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);
70                 }
71
72                 public virtual object Evaluate (string xpath)
73                 {
74                         return Evaluate (Compile (xpath));
75                 }
76
77                 public virtual object Evaluate (XPathExpression expr)
78                 {
79                         return Evaluate (expr, null);
80                 }
81
82                 public virtual object Evaluate (XPathExpression expr, XPathNodeIterator context)
83                 {
84                         CompiledExpression cexpr = (CompiledExpression) expr;
85                         if (context == null)
86                                 context = new NullIterator (this, cexpr.NamespaceManager);
87                         return cexpr.Evaluate ((BaseIterator) context);
88                 }
89
90                 public abstract string GetAttribute (string localName, string namespaceURI);
91
92                 public abstract string GetNamespace (string name);
93                 
94                 object ICloneable.Clone ()
95                 {
96                         return Clone ();
97                 }
98
99                 public virtual bool IsDescendant (XPathNavigator nav)
100                 {
101                         if (nav != null)
102                         {
103                                 nav = nav.Clone ();
104                                 while (nav.MoveToParent ())
105                                 {
106                                         if (IsSamePosition (nav))
107                                                 return true;
108                                 }
109                         }
110                         return false;
111                 }
112
113                 public abstract bool IsSamePosition (XPathNavigator other);
114
115                 public virtual bool Matches (string xpath)
116                 {
117                         return Matches (Compile (xpath));
118                 }
119
120                 public virtual bool Matches (XPathExpression expr)
121                 {
122                         XPathNodeIterator nodes = Select (expr);
123
124                         while (nodes.MoveNext ()) {
125                                 if (IsSamePosition (nodes.Current))
126                                         return true;
127                         }
128
129                         XPathNavigator navigator = Clone ();
130
131                         while (navigator.MoveToParent ()) {
132                                 nodes = navigator.Select (expr);
133
134                                 while (nodes.MoveNext ()) {
135                                         if (IsSamePosition (nodes.Current))
136                                                 return true;
137                                 }
138                         }
139
140                         return false;
141                 }
142
143                 public abstract bool MoveTo (XPathNavigator other);
144
145                 public abstract bool MoveToAttribute (string localName, string namespaceURI);
146
147                 public abstract bool MoveToFirst ();
148
149                 public abstract bool MoveToFirstAttribute ();
150
151                 public abstract bool MoveToFirstChild ();
152
153                 public bool MoveToFirstNamespace ()
154                 {
155                         return MoveToFirstNamespace (XPathNamespaceScope.All);
156                 }
157
158                 public abstract bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope);
159
160                 public abstract bool MoveToId (string id);
161
162                 public abstract bool MoveToNamespace (string name);
163
164                 public abstract bool MoveToNext ();
165
166                 public abstract bool MoveToNextAttribute ();
167
168                 public bool MoveToNextNamespace ()
169                 {
170                         return MoveToNextNamespace (XPathNamespaceScope.All);
171                 }
172
173                 public abstract bool MoveToNextNamespace (XPathNamespaceScope namespaceScope);
174
175                 public abstract bool MoveToParent ();
176
177                 public abstract bool MoveToPrevious ();
178
179                 public abstract void MoveToRoot ();
180
181                 public virtual XPathNodeIterator Select (string xpath)
182                 {
183                         return Select (Compile (xpath));
184                 }
185
186                 public virtual XPathNodeIterator Select (XPathExpression expr)
187                 {
188                         CompiledExpression cexpr = (CompiledExpression) expr;
189                         BaseIterator iter = new NullIterator (this, cexpr.NamespaceManager);
190                         return cexpr.EvaluateNodeSet (iter);
191                 }
192
193                 public virtual XPathNodeIterator SelectAncestors (XPathNodeType type, bool matchSelf)
194                 {
195                         Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
196                         NodeTest test = new NodeTypeTest (axis, type);
197                         return SelectTest (test);
198                 }
199
200                 [MonoTODO]
201                 public virtual XPathNodeIterator SelectAncestors (string name, string namespaceURI, bool matchSelf)
202                 {
203                         if (namespaceURI != null && namespaceURI != "")
204                                 throw new NotImplementedException ();
205
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);
210                 }
211
212                 public virtual XPathNodeIterator SelectChildren (XPathNodeType type)
213                 {
214                         NodeTest test = new NodeTypeTest (Axes.Child, type);
215                         return SelectTest (test);
216                 }
217
218                 [MonoTODO]
219                 public virtual XPathNodeIterator SelectChildren (string name, string namespaceURI)
220                 {
221                         if (namespaceURI != null && namespaceURI != "")
222                                 throw new NotImplementedException ();
223
224                         Axes axis = Axes.Child;
225                         QName qname = new QName ("", name);
226                         NodeTest test = new NodeNameTest (axis, qname);
227                         return SelectTest (test);
228                 }
229
230                 public virtual XPathNodeIterator SelectDescendants (XPathNodeType type, bool matchSelf)
231                 {
232                         Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
233                         NodeTest test = new NodeTypeTest (axis, type);
234                         return SelectTest (test);
235                 }
236
237                 [MonoTODO]
238                 public virtual XPathNodeIterator SelectDescendants (string name, string namespaceURI, bool matchSelf)
239                 {
240                         if (namespaceURI != null && namespaceURI != "")
241                                 throw new NotImplementedException ();
242
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);
247                 }
248
249                 internal XPathNodeIterator SelectTest (NodeTest test)
250                 {
251                         Expression expr = new ExprStep (test, null);
252                         BaseIterator iter = new NullIterator (this, null);
253                         return expr.EvaluateNodeSet (iter);
254                 }
255
256                 public override string ToString ()
257                 {
258                         return Value;
259                 }
260
261                 #endregion
262         }
263 }