run-W3C-tests.bat: skip nunit build if already done
[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 //   Atsushi Enomoto (atsushi@ximian.com)
7 //
8 // (C) 2002 Jason Diamond  http://injektilo.org/
9 // (C) 2004 Novell Inc.
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 #if NET_2_0
35 using System.Collections;
36 using System.Collections.Generic;
37 using System.Diagnostics;
38 using System.Text;
39 #endif
40 using System.IO;
41 using System.Xml;
42 using System.Xml.Schema;
43 using Mono.Xml.XPath;
44
45 #if NET_2_0
46 using NSResolver = System.Xml.IXmlNamespaceResolver;
47 #else
48 using NSResolver = System.Xml.XmlNamespaceManager;
49 #endif
50
51 namespace System.Xml.XPath
52 {
53 #if NET_2_0
54         public abstract class XPathNavigator : XPathItem,
55                 ICloneable, IXPathNavigable, IXmlNamespaceResolver
56 #else
57         public abstract class XPathNavigator : ICloneable
58 #endif
59         {
60                 #region Static members
61 #if NET_2_0
62                 public static IEqualityComparer NavigatorComparer {
63                         get { return XPathNavigatorComparer.Instance; }
64                 }
65 #endif
66                 #endregion
67
68                 #region Constructor
69
70                 protected XPathNavigator ()
71                 {
72                 }
73
74                 #endregion
75
76                 #region Properties
77
78                 public abstract string BaseURI { get; }
79
80 #if NET_2_0
81                 public virtual bool CanEdit {
82                         get { return false; }
83                 }
84
85                 public virtual bool HasAttributes {
86                         get {
87                                 if (!MoveToFirstAttribute ())
88                                         return false;
89                                 MoveToParent ();
90                                 return true;
91                         }
92                 }
93
94                 public virtual bool HasChildren {
95                         get {
96                                 if (!MoveToFirstChild ())
97                                         return false;
98                                 MoveToParent ();
99                                 return true;
100                         }
101                 }
102 #else
103                 public abstract bool HasAttributes { get; }
104
105                 public abstract bool HasChildren { get; }
106 #endif
107
108                 public abstract bool IsEmptyElement { get; }
109
110                 public abstract string LocalName { get; }
111
112                 public abstract string Name { get; }
113
114                 public abstract string NamespaceURI { get; }
115
116                 public abstract XmlNameTable NameTable { get; }
117
118                 public abstract XPathNodeType NodeType { get; }
119
120                 public abstract string Prefix { get; }
121
122 #if NET_2_0
123                 public virtual string XmlLang {
124                         get {
125                                 XPathNavigator nav = Clone ();
126                                 switch (nav.NodeType) {
127                                 case XPathNodeType.Attribute:
128                                 case XPathNodeType.Namespace:
129                                         nav.MoveToParent ();
130                                         break;
131                                 }
132                                 do {
133                                         if (nav.MoveToAttribute ("lang", "http://www.w3.org/XML/1998/namespace"))
134                                                 return nav.Value;
135                                 } while (nav.MoveToParent ());
136                                 return String.Empty;
137                         }
138                 }
139 #else
140                 public abstract string Value { get; }
141
142                 public abstract string XmlLang { get; }
143 #endif
144
145                 #endregion
146
147                 #region Methods
148
149                 public abstract XPathNavigator Clone ();
150
151                 public virtual XmlNodeOrder ComparePosition (XPathNavigator nav)
152                 {
153                         if (IsSamePosition (nav))
154                                 return XmlNodeOrder.Same;
155
156                         // quick check for direct descendant
157                         if (IsDescendant (nav))
158                                 return XmlNodeOrder.Before;
159
160                         // quick check for direct ancestor
161                         if (nav.IsDescendant (this))
162                                 return XmlNodeOrder.After;
163
164                         XPathNavigator nav1 = Clone ();
165                         XPathNavigator nav2 = nav.Clone ();
166
167                         // check if document instance is the same.
168                         nav1.MoveToRoot ();
169                         nav2.MoveToRoot ();
170                         if (!nav1.IsSamePosition (nav2))
171                                 return XmlNodeOrder.Unknown;
172                         nav1.MoveTo (this);
173                         nav2.MoveTo (nav);
174
175                         int depth1 = 0;
176                         while (nav1.MoveToParent ())
177                                 depth1++;
178                         nav1.MoveTo (this);
179                         int depth2 = 0;
180                         while (nav2.MoveToParent ())
181                                 depth2++;
182                         nav2.MoveTo (nav);
183
184                         // find common parent depth
185                         int common = depth1;
186                         for (;common > depth2; common--)
187                                 nav1.MoveToParent ();
188                         for (int i = depth2; i > common; i--)
189                                 nav2.MoveToParent ();
190                         while (!nav1.IsSamePosition (nav2)) {
191                                 nav1.MoveToParent ();
192                                 nav2.MoveToParent ();
193                                 common--;
194                         }
195
196                         // For each this and target, move to the node that is 
197                         // ancestor of the node and child of the common parent.
198                         nav1.MoveTo (this);
199                         for (int i = depth1; i > common + 1; i--)
200                                 nav1.MoveToParent ();
201                         nav2.MoveTo (nav);
202                         for (int i = depth2; i > common + 1; i--)
203                                 nav2.MoveToParent ();
204
205                         // Those children of common parent are comparable.
206                         // namespace nodes precede to attributes, and they
207                         // precede to other nodes.
208                         if (nav1.NodeType == XPathNodeType.Namespace) {
209                                 if (nav2.NodeType != XPathNodeType.Namespace)
210                                         return XmlNodeOrder.Before;
211                                 while (nav1.MoveToNextNamespace ())
212                                         if (nav1.IsSamePosition (nav2))
213                                                 return XmlNodeOrder.Before;
214                                 return XmlNodeOrder.After;
215                         }
216                         if (nav2.NodeType == XPathNodeType.Namespace)
217                                 return XmlNodeOrder.After;
218                         if (nav1.NodeType == XPathNodeType.Attribute) {
219                                 if (nav2.NodeType != XPathNodeType.Attribute)
220                                         return XmlNodeOrder.Before;
221                                 while (nav1.MoveToNextAttribute ())
222                                         if (nav1.IsSamePosition (nav2))
223                                                 return XmlNodeOrder.Before;
224                                 return XmlNodeOrder.After;
225                         }
226                         while (nav1.MoveToNext ())
227                                 if (nav1.IsSamePosition (nav2))
228                                         return XmlNodeOrder.Before;
229                         return XmlNodeOrder.After;
230                 }
231
232                 public virtual XPathExpression Compile (string xpath)
233                 {
234                         return XPathExpression.Compile (xpath);
235                 }
236                 
237                 internal virtual XPathExpression Compile (string xpath, System.Xml.Xsl.IStaticXsltContext ctx)
238                 {
239                         return XPathExpression.Compile (xpath, null, ctx);
240                 }
241
242                 public virtual object Evaluate (string xpath)
243                 {
244                         return Evaluate (Compile (xpath));
245                 }
246
247                 public virtual object Evaluate (XPathExpression expr)
248                 {
249                         return Evaluate (expr, null);
250                 }
251
252                 public virtual object Evaluate (XPathExpression expr, XPathNodeIterator context)
253                 {
254                         return Evaluate (expr, context, null);
255                 }
256                 
257                 internal virtual object Evaluate (XPathExpression expr, XPathNodeIterator context, NSResolver ctx)
258                 {
259                         CompiledExpression cexpr = (CompiledExpression) expr;
260                         if (ctx == null)
261                                 ctx = cexpr.NamespaceManager;
262                         
263                         if (context == null)
264                                 context = new NullIterator (this, ctx);
265                         BaseIterator iterContext = (BaseIterator) context;
266                         iterContext.NamespaceManager = ctx;
267                         return cexpr.Evaluate (iterContext);
268                 }
269
270                 internal XPathNodeIterator EvaluateNodeSet (XPathExpression expr, XPathNodeIterator context, NSResolver ctx)
271                 {
272                         CompiledExpression cexpr = (CompiledExpression) expr;
273                         if (ctx == null)
274                                 ctx = cexpr.NamespaceManager;
275                         
276                         if (context == null)
277                                 context = new NullIterator (this, cexpr.NamespaceManager);
278                         BaseIterator iterContext = (BaseIterator) context;
279                         iterContext.NamespaceManager = ctx;
280                         return cexpr.EvaluateNodeSet (iterContext);
281                 }
282
283                 internal string EvaluateString (XPathExpression expr, XPathNodeIterator context, NSResolver ctx)
284                 {
285                         CompiledExpression cexpr = (CompiledExpression) expr;
286                         if (ctx == null)
287                                 ctx = cexpr.NamespaceManager;
288                         
289                         if (context == null)
290                                 context = new NullIterator (this, cexpr.NamespaceManager);
291                         BaseIterator iterContext = (BaseIterator) context;
292                         iterContext.NamespaceManager = ctx;
293                         return cexpr.EvaluateString (iterContext);
294                 }
295
296                 internal double EvaluateNumber (XPathExpression expr, XPathNodeIterator context, NSResolver ctx)
297                 {
298                         CompiledExpression cexpr = (CompiledExpression) expr;
299                         if (ctx == null)
300                                 ctx = cexpr.NamespaceManager;
301                         
302                         if (context == null)
303                                 context = new NullIterator (this, cexpr.NamespaceManager);
304                         BaseIterator iterContext = (BaseIterator) context;
305                         iterContext.NamespaceManager = ctx;
306                         return cexpr.EvaluateNumber (iterContext);
307                 }
308
309                 internal bool EvaluateBoolean (XPathExpression expr, XPathNodeIterator context, NSResolver ctx)
310                 {
311                         CompiledExpression cexpr = (CompiledExpression) expr;
312                         if (ctx == null)
313                                 ctx = cexpr.NamespaceManager;
314                         
315                         if (context == null)
316                                 context = new NullIterator (this, cexpr.NamespaceManager);
317                         BaseIterator iterContext = (BaseIterator) context;
318                         iterContext.NamespaceManager = ctx;
319                         return cexpr.EvaluateBoolean (iterContext);
320                 }
321
322 #if NET_2_0
323                 public virtual string GetAttribute (string localName, string namespaceURI)
324                 {
325                         if (!MoveToAttribute (localName, namespaceURI))
326                                 return String.Empty;
327                         string value = Value;
328                         MoveToParent ();
329                         return value;
330                 }
331
332                 public virtual string GetNamespace (string name)
333                 {
334                         if (!MoveToNamespace (name))
335                                 return String.Empty;
336                         string value = Value;
337                         MoveToParent ();
338                         return value;
339                 }
340
341 #else
342                 public abstract string GetAttribute (string localName, string namespaceURI);
343
344                 public abstract string GetNamespace (string name);
345 #endif
346                 
347                 object ICloneable.Clone ()
348                 {
349                         return Clone ();
350                 }
351
352                 public virtual bool IsDescendant (XPathNavigator nav)
353                 {
354                         if (nav != null)
355                         {
356                                 nav = nav.Clone ();
357                                 while (nav.MoveToParent ())
358                                 {
359                                         if (IsSamePosition (nav))
360                                                 return true;
361                                 }
362                         }
363                         return false;
364                 }
365
366                 public abstract bool IsSamePosition (XPathNavigator other);
367
368                 public virtual bool Matches (string xpath)
369                 {
370                         return Matches (Compile (xpath));
371                 }
372
373                 public virtual bool Matches (XPathExpression expr)
374                 {
375                         Expression e = ((CompiledExpression) expr).ExpressionNode;
376                         if (e is ExprRoot)
377                                 return NodeType == XPathNodeType.Root;
378                         
379                         NodeTest nt = e as NodeTest;
380                         if (nt != null) {
381                                 switch (nt.Axis.Axis) {
382                                 case Axes.Child:
383                                 case Axes.Attribute:
384                                         break;
385                                 default:
386                                         throw new XPathException ("Only child and attribute pattern are allowed for a pattern.");
387                                 }
388                                 return nt.Match (((CompiledExpression)expr).NamespaceManager, this);
389                         }
390                         if (e is ExprFilter) {
391                                 do {
392                                         e = ((ExprFilter) e).LeftHandSide;
393                                 } while (e is ExprFilter);
394                                 
395                                 if (e is NodeTest && !((NodeTest) e).Match (((CompiledExpression) expr).NamespaceManager, this))
396                                         return false;
397                         }
398
399                         XPathResultType resultType = e.ReturnType;
400                         switch (resultType) {
401                         case XPathResultType.Any:
402                         case XPathResultType.NodeSet:
403                                 break;
404                         default:
405                                 return false;
406                         }
407
408                         switch (e.EvaluatedNodeType) {
409                         case XPathNodeType.Attribute:
410                         case XPathNodeType.Namespace:
411                                 if (NodeType != e.EvaluatedNodeType)
412                                         return false;
413                                 break;
414                         }
415
416                         XPathNodeIterator nodes;
417                         nodes = this.Select (expr);
418                         while (nodes.MoveNext ()) {
419                                 if (IsSamePosition (nodes.Current))
420                                         return true;
421                         }
422
423                         // ancestors might select this node.
424
425                         XPathNavigator navigator = Clone ();
426
427                         while (navigator.MoveToParent ()) {
428                                 nodes = navigator.Select (expr);
429
430                                 while (nodes.MoveNext ()) {
431                                         if (IsSamePosition (nodes.Current))
432                                                 return true;
433                                 }
434                         }
435
436                         return false;
437                 }
438
439                 public abstract bool MoveTo (XPathNavigator other);
440
441 #if NET_2_0
442                 public virtual bool MoveToAttribute (string localName, string namespaceURI)
443                 {
444                         if (MoveToFirstAttribute ()) {
445                                 do {
446                                         if (LocalName == localName && NamespaceURI == namespaceURI)
447                                                 return true;
448                                 } while (MoveToNextAttribute ());
449                                 MoveToParent ();
450                         }
451                         return false;
452                 }
453
454                 public virtual bool MoveToNamespace (string name)
455                 {
456                         if (MoveToFirstNamespace ()) {
457                                 do {
458                                         if (LocalName == name)
459                                                 return true;
460                                 } while (MoveToNextNamespace ());
461                                 MoveToParent ();
462                         }
463                         return false;
464                 }
465
466                 /*
467                 public virtual bool MoveToFirst ()
468                 {
469                         if (MoveToPrevious ()) {
470                                 // It would be able to invoke MoveToPrevious() until the end, but this way would be much faster
471                                 MoveToParent ();
472                                 MoveToFirstChild ();
473                                 return true;
474                         }
475                         return false;
476                 }
477                 */
478
479                 public virtual bool MoveToFirst ()
480                 {
481                         return MoveToFirstImpl ();
482                 }
483
484                 public virtual void MoveToRoot ()
485                 {
486                         while (MoveToParent ())
487                                 ;
488                 }
489 #else
490                 public abstract bool MoveToAttribute (string localName, string namespaceURI);
491
492                 public abstract bool MoveToNamespace (string name);
493
494                 public abstract bool MoveToFirst ();
495
496                 public abstract void MoveToRoot ();
497 #endif
498
499                 internal bool MoveToFirstImpl ()
500                 {
501                         switch (NodeType) {
502                         case XPathNodeType.Attribute:
503                         case XPathNodeType.Namespace:
504                                 return false;
505                         default:
506                                 if (!MoveToParent ())
507                                         return false;
508                                 // Follow these 2 steps so that we can skip 
509                                 // some types of nodes .
510                                 MoveToFirstChild ();
511                                 return true;
512                         }
513                 }
514
515                 public abstract bool MoveToFirstAttribute ();
516
517                 public abstract bool MoveToFirstChild ();
518
519                 public bool MoveToFirstNamespace ()
520                 {
521                         return MoveToFirstNamespace (XPathNamespaceScope.All);
522                 }
523
524                 public abstract bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope);
525
526                 public abstract bool MoveToId (string id);
527
528                 public abstract bool MoveToNext ();
529
530                 public abstract bool MoveToNextAttribute ();
531
532                 public bool MoveToNextNamespace ()
533                 {
534                         return MoveToNextNamespace (XPathNamespaceScope.All);
535                 }
536
537                 public abstract bool MoveToNextNamespace (XPathNamespaceScope namespaceScope);
538
539                 public abstract bool MoveToParent ();
540
541                 public abstract bool MoveToPrevious ();
542
543                 public virtual XPathNodeIterator Select (string xpath)
544                 {
545                         return Select (Compile (xpath));
546                 }
547
548                 public virtual XPathNodeIterator Select (XPathExpression expr)
549                 {
550                         return Select (expr, null);
551                 }
552                 
553                 internal virtual XPathNodeIterator Select (XPathExpression expr, NSResolver ctx)
554                 {
555                         CompiledExpression cexpr = (CompiledExpression) expr;
556                         if (ctx == null)
557                                 ctx = cexpr.NamespaceManager;
558                         
559                         BaseIterator iter = new NullIterator (this, ctx);
560                         return cexpr.EvaluateNodeSet (iter);
561                 }
562
563                 public virtual XPathNodeIterator SelectAncestors (XPathNodeType type, bool matchSelf)
564                 {
565                         Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
566                         return SelectTest (new NodeTypeTest (axis, type));
567                 }
568
569                 public virtual XPathNodeIterator SelectAncestors (string name, string namespaceURI, bool matchSelf)
570                 {
571                         if (name == null)
572                                 throw new ArgumentNullException ("name");
573                         if (namespaceURI == null)
574                                 throw new ArgumentNullException ("namespaceURI");
575
576                         Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
577                         XmlQualifiedName qname = new XmlQualifiedName (name, namespaceURI);
578                         return SelectTest (new NodeNameTest (axis, qname, true));
579                 }
580
581                 public virtual XPathNodeIterator SelectChildren (XPathNodeType type)
582                 {
583                         return SelectTest (new NodeTypeTest (Axes.Child, type));
584                 }
585
586                 public virtual XPathNodeIterator SelectChildren (string name, string namespaceURI)
587                 {
588                         if (name == null)
589                                 throw new ArgumentNullException ("name");
590                         if (namespaceURI == null)
591                                 throw new ArgumentNullException ("namespaceURI");
592
593                         Axes axis = Axes.Child;
594                         XmlQualifiedName qname = new XmlQualifiedName (name, namespaceURI);
595                         return SelectTest (new NodeNameTest (axis, qname, true));
596                 }
597
598                 public virtual XPathNodeIterator SelectDescendants (XPathNodeType type, bool matchSelf)
599                 {
600                         Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
601                         return SelectTest (new NodeTypeTest (axis, type));
602                 }
603
604                 public virtual XPathNodeIterator SelectDescendants (string name, string namespaceURI, bool matchSelf)
605                 {
606                         if (name == null)
607                                 throw new ArgumentNullException ("name");
608                         if (namespaceURI == null)
609                                 throw new ArgumentNullException ("namespaceURI");
610
611
612                         Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
613                         XmlQualifiedName qname = new XmlQualifiedName (name, namespaceURI);
614                         return SelectTest (new NodeNameTest (axis, qname, true));
615                 }
616
617                 internal XPathNodeIterator SelectTest (NodeTest test)
618                 {
619                         return test.EvaluateNodeSet (new NullIterator (this));
620                 }
621
622                 public override string ToString ()
623                 {
624                         return Value;
625                 }
626
627                 #endregion
628
629 #if NET_2_0
630
631                 public virtual bool CheckValidity (XmlSchemaSet schemas, ValidationEventHandler handler)
632                 {
633                         XmlReaderSettings settings = new XmlReaderSettings ();
634                         settings.NameTable = NameTable;
635                         settings.SetSchemas (schemas);
636                         settings.ValidationEventHandler += handler;
637                         settings.ValidationType = ValidationType.Schema;
638                         try {
639                                 XmlReader r = XmlReader.Create (
640                                         ReadSubtree (), settings);
641                                 while (!r.EOF)
642                                         r.Read ();
643                         } catch (XmlSchemaValidationException) {
644                                 return false;
645                         }
646                         return true;
647                 }
648
649                 public virtual XPathNavigator CreateNavigator ()
650                 {
651                         return Clone ();
652                 }
653
654                 [MonoTODO]
655                 public virtual object Evaluate (string xpath, IXmlNamespaceResolver nsResolver)
656                 {
657                         return Evaluate (Compile (xpath), null, nsResolver);
658                 }
659
660                 [MonoTODO]
661                 public virtual IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
662                 {
663                         IDictionary<string, string> table = new Dictionary<string, string> ();
664                         XPathNamespaceScope xpscope =
665                                 scope == XmlNamespaceScope.Local ?
666                                         XPathNamespaceScope.Local :
667                                 scope == XmlNamespaceScope.ExcludeXml ?
668                                         XPathNamespaceScope.ExcludeXml :
669                                 XPathNamespaceScope.All;
670                         XPathNavigator nav = Clone ();
671                         if (nav.NodeType != XPathNodeType.Element)
672                                 nav.MoveToParent ();
673                         if (!nav.MoveToFirstNamespace (xpscope))
674                                 return table;
675                         do {
676                                 table.Add (nav.Name, nav.Value);
677                         } while (nav.MoveToNextNamespace (xpscope));
678                         return table;
679                 }
680
681                 public virtual string LookupNamespace (string prefix)
682                 {
683                         XPathNavigator nav = Clone ();
684                         if (nav.NodeType != XPathNodeType.Element)
685                                 nav.MoveToParent ();
686                         if (nav.MoveToNamespace (prefix))
687                                 return nav.Value;
688                         return null;
689                 }
690
691                 public virtual string LookupPrefix (string namespaceUri)
692                 {
693                         XPathNavigator nav = Clone ();
694                         if (nav.NodeType != XPathNodeType.Element)
695                                 nav.MoveToParent ();
696                         if (!nav.MoveToFirstNamespace ())
697                                 return null;
698                         do {
699                                 if (nav.Value == namespaceUri)
700                                         return nav.Name;
701                         } while (nav.MoveToNextNamespace ());
702                         return null;
703                 }
704
705                 private bool MoveTo (XPathNodeIterator iter)
706                 {
707                         if (iter.MoveNext ()) {
708                                 MoveTo (iter.Current);
709                                 return true;
710                         }
711                         else
712                                 return false;
713                 }
714
715                 public virtual bool MoveToChild (XPathNodeType type)
716                 {
717                         return MoveTo (SelectChildren (type));
718                 }
719
720                 public virtual bool MoveToChild (string localName, string namespaceURI)
721                 {
722                         return MoveTo (SelectChildren (localName, namespaceURI));
723                 }
724
725                 public virtual bool MoveToNext (string localName, string namespaceURI)
726                 {
727                         XPathNavigator nav = Clone ();
728                         while (nav.MoveToNext ()) {
729                                 if (nav.LocalName == localName &&
730                                         nav.NamespaceURI == namespaceURI) {
731                                         MoveTo (nav);
732                                         return true;
733                                 }
734                         }
735                         return false;
736                 }
737
738                 public virtual bool MoveToNext (XPathNodeType type)
739                 {
740                         XPathNavigator nav = Clone ();
741                         while (nav.MoveToNext ()) {
742                                 if (nav.NodeType == type) {
743                                         MoveTo (nav);
744                                         return true;
745                                 }
746                         }
747                         return false;
748                 }
749
750                 public virtual bool MoveToFollowing (string localName,
751                         string namespaceURI)
752                 {
753                         return MoveToFollowing (localName, namespaceURI, null);
754                 }
755
756                 public virtual bool MoveToFollowing (string localName,
757                         string namespaceURI, XPathNavigator end)
758                 {
759                         if (localName == null)
760                                 throw new ArgumentNullException ("localName");
761                         if (namespaceURI == null)
762                                 throw new ArgumentNullException ("namespaceURI");
763                         localName = NameTable.Get (localName);
764                         if (localName == null)
765                                 return false;
766                         namespaceURI = NameTable.Get (namespaceURI);
767                         if (namespaceURI == null)
768                                 return false;
769
770                         XPathNavigator nav = Clone ();
771                         switch (nav.NodeType) {
772                         case XPathNodeType.Attribute:
773                         case XPathNodeType.Namespace:
774                                 nav.MoveToParent ();
775                                 break;
776                         }
777                         do {
778                                 if (!nav.MoveToFirstChild ()) {
779                                         do {
780                                                 if (!nav.MoveToNext ()) {
781                                                         if (!nav.MoveToParent ())
782                                                                 return false;
783                                                 }
784                                                 else
785                                                         break;
786                                         } while (true);
787                                 }
788                                 if (end != null && end.IsSamePosition (nav))
789                                         return false;
790                                 if (object.ReferenceEquals (localName, nav.LocalName) &&
791                                         object.ReferenceEquals (namespaceURI, nav.NamespaceURI)) {
792                                         MoveTo (nav);
793                                         return true;
794                                 }
795                         } while (true);
796                 }
797
798                 public virtual bool MoveToFollowing (XPathNodeType type)
799                 {
800                         return MoveToFollowing (type, null);
801                 }
802
803                 public virtual bool MoveToFollowing (XPathNodeType type,
804                         XPathNavigator end)
805                 {
806                         if (type == XPathNodeType.Root)
807                                 return false; // will never match
808                         XPathNavigator nav = Clone ();
809                         switch (nav.NodeType) {
810                         case XPathNodeType.Attribute:
811                         case XPathNodeType.Namespace:
812                                 nav.MoveToParent ();
813                                 break;
814                         }
815                         do {
816                                 if (!nav.MoveToFirstChild ()) {
817                                         do {
818                                                 if (!nav.MoveToNext ()) {
819                                                         if (!nav.MoveToParent ())
820                                                                 return false;
821                                                 }
822                                                 else
823                                                         break;
824                                         } while (true);
825                                 }
826                                 if (end != null && end.IsSamePosition (nav))
827                                         return false;
828                                 if (nav.NodeType == type) {
829                                         MoveTo (nav);
830                                         return true;
831                                 }
832                         } while (true);
833                 }
834
835                 public virtual XmlReader ReadSubtree ()
836                 {
837                         return new XPathNavigatorReader (this);
838                 }
839
840                 public virtual XPathNodeIterator Select (string xpath, IXmlNamespaceResolver nsResolver)
841                 {
842                         return Select (Compile (xpath), nsResolver);
843                 }
844
845                 public virtual XPathNavigator SelectSingleNode (string xpath)
846                 {
847                         return SelectSingleNode (xpath, null);
848                 }
849
850                 public virtual XPathNavigator SelectSingleNode (string xpath, IXmlNamespaceResolver nsResolver)
851                 {
852                         XPathExpression expr = Compile (xpath);
853                         expr.SetContext (nsResolver);
854                         return SelectSingleNode (expr);
855                 }
856
857                 public virtual XPathNavigator SelectSingleNode (XPathExpression expression)
858                 {
859                         XPathNodeIterator iter = Select (expression);
860                         if (iter.MoveNext ())
861                                 return iter.Current;
862                         else
863                                 return null;
864                 }
865
866                 [MonoTODO]
867                 public override object ValueAs (Type type, IXmlNamespaceResolver nsResolver)
868                 {
869                         throw new NotImplementedException ();
870                 }
871
872                 public virtual void WriteSubtree (XmlWriter writer)
873                 {
874                         writer.WriteNode (this, false);
875                 }
876
877                 [MonoTODO]
878                 public virtual string InnerXml {
879                         get {
880                                 XmlReader r = ReadSubtree ();
881                                 r.Read (); // start
882                                 // skip the element itself (or will reach to 
883                                 // EOF if other than element) unless writing
884                                 // doc itself
885                                 int depth = r.Depth;
886                                 if (NodeType != XPathNodeType.Root)
887                                         r.Read ();
888                                 StringWriter sw = new StringWriter ();
889                                 XmlWriter xtw = XmlWriter.Create (sw);
890                                 while (!r.EOF && r.Depth > depth)
891                                         xtw.WriteNode (r, false);
892                                 return sw.ToString ();
893                         }
894                         set {
895                                 DeleteChildren ();
896                                 if (NodeType == XPathNodeType.Attribute) {
897                                         SetValue (value);
898                                         return;
899                                 }
900                                 AppendChild (value);
901                         }
902                 }
903
904                 public override sealed bool IsNode {
905                         get { return true; }
906                 }
907
908                 public virtual string OuterXml {
909                         get {
910                                 XmlWriterSettings s = new XmlWriterSettings ();
911                                 s.Indent = true;
912                                 s.OmitXmlDeclaration = true;
913                                 StringBuilder sb = new StringBuilder ();
914                                 using (XmlWriter w = XmlWriter.Create (sb, s)) {
915                                         WriteSubtree (w);
916                                 }
917                                 return sb.ToString ();
918                         }
919                         [MonoTODO]
920                         set {
921                                 switch (NodeType) {
922                                 case XPathNodeType.Root:
923                                 case XPathNodeType.Attribute:
924                                 case XPathNodeType.Namespace:
925                                         throw new XmlException ("Setting OuterXml Root, Attribute and Namespace is not supported.");
926                                 }
927
928                                 DeleteSelf ();
929                                 AppendChild (value);
930                                 MoveToFirstChild ();
931                         }
932                 }
933
934                 [MonoTODO]
935                 public virtual IXmlSchemaInfo SchemaInfo {
936                         get {
937                                 return null;
938                         }
939                 }
940
941                 [MonoTODO]
942                 public override object TypedValue {
943                         get {
944                                 switch (NodeType) {
945                                 case XPathNodeType.Element:
946                                 case XPathNodeType.Attribute:
947                                         if (XmlType == null)
948                                                 break;
949                                         XmlSchemaDatatype dt = XmlType.Datatype;
950                                         if (dt == null)
951                                                 break;
952                                         return dt.ParseValue (Value, NameTable, this as IXmlNamespaceResolver);
953                                 }
954                                 return Value;
955                         }
956                 }
957
958                 public virtual object UnderlyingObject {
959                         get { return null; }
960                 }
961
962                 public override bool ValueAsBoolean {
963                         get { return XQueryConvert.StringToBoolean (Value); }
964                 }
965
966                 public override DateTime ValueAsDateTime {
967                         get { return XmlConvert.ToDateTime (Value); }
968                 }
969
970                 public override double ValueAsDouble {
971                         get { return XQueryConvert.StringToDouble (Value); }
972                 }
973
974                 public override int ValueAsInt {
975                         get { return XQueryConvert.StringToInt (Value); }
976                 }
977
978                 public override long ValueAsLong {
979                         get { return XQueryConvert.StringToInteger (Value); }
980                 }
981
982                 public override Type ValueType {
983                         get {
984                                 return SchemaInfo != null &&
985                                         SchemaInfo.SchemaType != null &&
986                                         SchemaInfo.SchemaType.Datatype != null ?
987                                         SchemaInfo.SchemaType.Datatype.ValueType
988                                         : null;
989                         }
990                 }
991
992                 [MonoTODO]
993                 public override XmlSchemaType XmlType {
994                         get {
995                                 if (SchemaInfo != null)
996                                         return SchemaInfo.SchemaType;
997                                 return null;
998                         }
999                 }
1000
1001                 private XmlReader CreateFragmentReader (string fragment)
1002                 {
1003                         XmlReaderSettings settings = new XmlReaderSettings ();
1004                         settings.ConformanceLevel = ConformanceLevel.Fragment;
1005                         XmlNamespaceManager nsmgr = new XmlNamespaceManager (NameTable);
1006                         foreach (KeyValuePair<string,string> nss in GetNamespacesInScope (XmlNamespaceScope.All))
1007                                 nsmgr.AddNamespace (nss.Key, nss.Value);
1008                         return XmlReader.Create (
1009                                 new StringReader (fragment),
1010                                 settings,
1011                                 new XmlParserContext (NameTable, nsmgr, null, XmlSpace.None));
1012                 }
1013
1014                 // must override it.
1015                 public virtual XmlWriter AppendChild ()
1016                 {
1017                         throw new NotSupportedException ();
1018                 }
1019
1020                 public virtual void AppendChild (
1021                         string xmlFragments)
1022                 {
1023                         AppendChild (CreateFragmentReader (xmlFragments));
1024                 }
1025
1026                 public virtual void AppendChild (
1027                         XmlReader reader)
1028                 {
1029                         XmlWriter w = AppendChild ();
1030                         while (!reader.EOF)
1031                                 w.WriteNode (reader, false);
1032                         w.Close ();
1033                 }
1034
1035                 [MonoTODO]
1036                 public virtual void AppendChild (
1037                         XPathNavigator nav)
1038                 {
1039                         AppendChild (new XPathNavigatorReader (nav));
1040                 }
1041
1042                 public virtual void AppendChildElement (string prefix, string name, string ns, string value)
1043                 {
1044                         XmlWriter xw = AppendChild ();
1045                         xw.WriteStartElement (prefix, name, ns);
1046                         xw.WriteString (value);
1047                         xw.WriteEndElement ();
1048                         xw.Close ();
1049                 }
1050
1051                 public virtual void CreateAttribute (string prefix, string localName, string namespaceURI, string value)
1052                 {
1053                         using (XmlWriter w = CreateAttributes ()) {
1054                                 w.WriteAttributeString (prefix, localName, namespaceURI, value);
1055                         }
1056                 }
1057
1058                 // must override it.
1059                 [MonoTODO ("needs tests")]
1060                 public virtual XmlWriter CreateAttributes ()
1061                 {
1062                         throw new NotSupportedException ();
1063                 }
1064
1065                 // must override it.
1066                 public virtual void DeleteSelf ()
1067                 {
1068                         throw new NotSupportedException ();
1069                 }
1070
1071                 // must override it.
1072                 public virtual void DeleteRange (XPathNavigator nav)
1073                 {
1074                         throw new NotSupportedException ();
1075                 }
1076
1077                 public virtual XmlWriter ReplaceRange (XPathNavigator nav)
1078                 {
1079                         throw new NotSupportedException ();
1080                 }
1081         
1082                 public virtual XmlWriter InsertAfter ()
1083                 {
1084                         switch (NodeType) {
1085                         case XPathNodeType.Root:
1086                         case XPathNodeType.Attribute:
1087                         case XPathNodeType.Namespace:
1088                                 throw new InvalidOperationException (String.Format ("Insertion after {0} is not allowed.", NodeType));
1089                         }
1090                         XPathNavigator nav = Clone ();
1091                         if (nav.MoveToNext ())
1092                                 return nav.InsertBefore ();
1093                         else if (nav.MoveToParent ())
1094                                 return nav.AppendChild ();
1095                         else
1096                                 throw new InvalidOperationException ("Could not move to parent to insert sibling node");
1097                 }
1098
1099                 public virtual void InsertAfter (string xmlFragments)
1100                 {
1101                         InsertAfter (CreateFragmentReader (xmlFragments));
1102                 }
1103
1104                 public virtual void InsertAfter (XmlReader reader)
1105                 {
1106                         using (XmlWriter w = InsertAfter ()) {
1107                                 w.WriteNode (reader, false);
1108                         }
1109                 }
1110
1111                 [MonoTODO]
1112                 public virtual void InsertAfter (XPathNavigator nav)
1113                 {
1114                         InsertAfter (new XPathNavigatorReader (nav));
1115                 }
1116
1117                 public virtual XmlWriter InsertBefore ()
1118                 {
1119                         throw new NotSupportedException ();
1120                 }
1121
1122                 public virtual void InsertBefore (string xmlFragments)
1123                 {
1124                         InsertBefore (CreateFragmentReader (xmlFragments));
1125                 }
1126
1127                 public virtual void InsertBefore (XmlReader reader)
1128                 {
1129                         using (XmlWriter w = InsertBefore ()) {
1130                                 w.WriteNode (reader, false);
1131                         }
1132                 }
1133
1134                 [MonoTODO]
1135                 public virtual void InsertBefore (XPathNavigator nav)
1136                 {
1137                         InsertBefore (new XPathNavigatorReader (nav));
1138                 }
1139
1140                 public virtual void InsertElementAfter (string prefix, 
1141                         string localName, string namespaceURI, string value)
1142                 {
1143                         using (XmlWriter w = InsertAfter ()) {
1144                                 w.WriteElementString (prefix, localName, namespaceURI, value);
1145                         }
1146                 }
1147
1148                 public virtual void InsertElementBefore (string prefix, 
1149                         string localName, string namespaceURI, string value)
1150                 {
1151                         using (XmlWriter w = InsertBefore ()) {
1152                                 w.WriteElementString (prefix, localName, namespaceURI, value);
1153                         }
1154                 }
1155
1156                 public virtual XmlWriter PrependChild ()
1157                 {
1158                         XPathNavigator nav = Clone ();
1159                         if (nav.MoveToFirstChild ())
1160                                 return nav.InsertBefore ();
1161                         else
1162                                 return AppendChild ();
1163                 }
1164
1165                 public virtual void PrependChild (string xmlFragments)
1166                 {
1167                         PrependChild (CreateFragmentReader (xmlFragments));
1168                 }
1169
1170                 public virtual void PrependChild (XmlReader reader)
1171                 {
1172                         using (XmlWriter w = PrependChild ()) {
1173                                 w.WriteNode (reader, false);
1174                         }
1175                 }
1176
1177                 [MonoTODO]
1178                 public virtual void PrependChild (XPathNavigator nav)
1179                 {
1180                         PrependChild (new XPathNavigatorReader (nav));
1181                 }
1182
1183                 public virtual void PrependChildElement (string prefix, 
1184                         string localName, string namespaceURI, string value)
1185                 {
1186                         using (XmlWriter w = PrependChild ()) {
1187                                 w.WriteElementString (prefix, localName, namespaceURI, value);
1188                         }
1189                 }
1190
1191                 public virtual void ReplaceSelf (string xmlFragment)
1192                 {
1193                         ReplaceSelf (CreateFragmentReader (xmlFragment));
1194                 }
1195
1196                 // must override it.
1197                 public virtual void ReplaceSelf (XmlReader reader)
1198                 {
1199                         throw new NotSupportedException ();
1200                 }
1201
1202                 [MonoTODO]
1203                 public virtual void ReplaceSelf (XPathNavigator navigator)
1204                 {
1205                         ReplaceSelf (new XPathNavigatorReader (navigator));
1206                 }
1207
1208                 // Dunno the exact purpose, but maybe internal editor use
1209                 [MonoTODO]
1210                 public virtual void SetTypedValue (object value)
1211                 {
1212                         throw new NotSupportedException ();
1213                 }
1214
1215                 public virtual void SetValue (string value)
1216                 {
1217                         throw new NotSupportedException ();
1218                 }
1219
1220                 [MonoTODO]
1221                 private void DeleteChildren ()
1222                 {
1223                         switch (NodeType) {
1224                         case XPathNodeType.Namespace:
1225                                 throw new InvalidOperationException ("Removing namespace node content is not supported.");
1226                         case XPathNodeType.Attribute:
1227                                 return;
1228                         case XPathNodeType.Text:
1229                         case XPathNodeType.SignificantWhitespace:
1230                         case XPathNodeType.Whitespace:
1231                         case XPathNodeType.ProcessingInstruction:
1232                         case XPathNodeType.Comment:
1233                                 DeleteSelf ();
1234                                 return;
1235                         }
1236                         if (!HasChildren)
1237                                 return;
1238                         XPathNavigator nav = Clone ();
1239                         nav.MoveToFirstChild ();
1240                         while (!nav.IsSamePosition (this))
1241                                 nav.DeleteSelf ();
1242                 }
1243 #endif
1244         }
1245 }