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