TARGET_JVM: refer nunit from mono tree
[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                 public virtual bool MoveToNext (string localName, string namespaceURI)
725                 {
726                         XPathNavigator nav = Clone ();
727                         while (nav.MoveToNext ()) {
728                                 if (nav.LocalName == localName &&
729                                         nav.NamespaceURI == namespaceURI) {
730                                         MoveTo (nav);
731                                         return true;
732                                 }
733                         }
734                         return false;
735                 }
736
737                 public virtual bool MoveToNext (XPathNodeType type)
738                 {
739                         XPathNavigator nav = Clone ();
740                         while (nav.MoveToNext ()) {
741                                 if (nav.NodeType == type) {
742                                         MoveTo (nav);
743                                         return true;
744                                 }
745                         }
746                         return false;
747                 }
748
749                 public virtual bool MoveToFollowing (string localName,
750                         string namespaceURI)
751                 {
752                         return MoveToFollowing (localName, namespaceURI, null);
753                 }
754
755                 public virtual bool MoveToFollowing (string localName,
756                         string namespaceURI, XPathNavigator end)
757                 {
758                         if (localName == null)
759                                 throw new ArgumentNullException ("localName");
760                         if (namespaceURI == null)
761                                 throw new ArgumentNullException ("namespaceURI");
762                         localName = NameTable.Get (localName);
763                         if (localName == null)
764                                 return false;
765                         namespaceURI = NameTable.Get (namespaceURI);
766                         if (namespaceURI == null)
767                                 return false;
768
769                         XPathNavigator nav = Clone ();
770                         switch (nav.NodeType) {
771                         case XPathNodeType.Attribute:
772                         case XPathNodeType.Namespace:
773                                 nav.MoveToParent ();
774                                 break;
775                         }
776                         do {
777                                 if (!nav.MoveToFirstChild ()) {
778                                         do {
779                                                 if (!nav.MoveToNext ()) {
780                                                         if (!nav.MoveToParent ())
781                                                                 return false;
782                                                 }
783                                                 else
784                                                         break;
785                                         } while (true);
786                                 }
787                                 if (end != null && end.IsSamePosition (nav))
788                                         return false;
789                                 if (object.ReferenceEquals (localName, nav.LocalName) &&
790                                         object.ReferenceEquals (namespaceURI, nav.NamespaceURI)) {
791                                         MoveTo (nav);
792                                         return true;
793                                 }
794                         } while (true);
795                 }
796
797                 public virtual bool MoveToFollowing (XPathNodeType type)
798                 {
799                         return MoveToFollowing (type, null);
800                 }
801
802                 public virtual bool MoveToFollowing (XPathNodeType type,
803                         XPathNavigator end)
804                 {
805                         if (type == XPathNodeType.Root)
806                                 return false; // will never match
807                         XPathNavigator nav = Clone ();
808                         switch (nav.NodeType) {
809                         case XPathNodeType.Attribute:
810                         case XPathNodeType.Namespace:
811                                 nav.MoveToParent ();
812                                 break;
813                         }
814                         do {
815                                 if (!nav.MoveToFirstChild ()) {
816                                         do {
817                                                 if (!nav.MoveToNext ()) {
818                                                         if (!nav.MoveToParent ())
819                                                                 return false;
820                                                 }
821                                                 else
822                                                         break;
823                                         } while (true);
824                                 }
825                                 if (end != null && end.IsSamePosition (nav))
826                                         return false;
827                                 if (nav.NodeType == type) {
828                                         MoveTo (nav);
829                                         return true;
830                                 }
831                         } while (true);
832                 }
833
834                 public virtual XmlReader ReadSubtree ()
835                 {
836                         return new XPathNavigatorReader (this);
837                 }
838
839                 public virtual XPathNodeIterator Select (string xpath, IXmlNamespaceResolver nsResolver)
840                 {
841                         return Select (Compile (xpath), nsResolver);
842                 }
843
844                 public virtual XPathNavigator SelectSingleNode (string xpath)
845                 {
846                         return SelectSingleNode (xpath, null);
847                 }
848
849                 public virtual XPathNavigator SelectSingleNode (string xpath, IXmlNamespaceResolver nsResolver)
850                 {
851                         XPathExpression expr = Compile (xpath);
852                         expr.SetContext (nsResolver);
853                         return SelectSingleNode (expr);
854                 }
855
856                 public virtual XPathNavigator SelectSingleNode (XPathExpression expression)
857                 {
858                         XPathNodeIterator iter = Select (expression);
859                         if (iter.MoveNext ())
860                                 return iter.Current;
861                         else
862                                 return null;
863                 }
864
865                 [MonoTODO]
866                 public override object ValueAs (Type type, IXmlNamespaceResolver nsResolver)
867                 {
868                         throw new NotImplementedException ();
869                 }
870
871                 public virtual void WriteSubtree (XmlWriter writer)
872                 {
873                         writer.WriteNode (this, 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                 public override sealed bool IsNode {
904                         get { return true; }
905                 }
906
907                 [MonoTODO]
908                 public virtual string OuterXml {
909                         get {
910                                 StringWriter sw = new StringWriter ();
911                                 XmlTextWriter xtw = new XmlTextWriter (sw);
912                                 WriteSubtree (xtw);
913                                 xtw.Close ();
914                                 return sw.ToString ();
915                         }
916                         set {
917                                 switch (NodeType) {
918                                 case XPathNodeType.Root:
919                                 case XPathNodeType.Attribute:
920                                 case XPathNodeType.Namespace:
921                                         throw new XmlException ("Setting OuterXml Root, Attribute and Namespace is not supported.");
922                                 }
923
924                                 DeleteSelf ();
925                                 AppendChild (value);
926                                 MoveToFirstChild ();
927                         }
928                 }
929
930                 [MonoTODO]
931                 public virtual IXmlSchemaInfo SchemaInfo {
932                         get {
933                                 return null;
934                         }
935                 }
936
937                 [MonoTODO]
938                 public override object TypedValue {
939                         get {
940                                 switch (NodeType) {
941                                 case XPathNodeType.Element:
942                                 case XPathNodeType.Attribute:
943                                         if (XmlType == null)
944                                                 break;
945                                         XmlSchemaDatatype dt = XmlType.Datatype;
946                                         if (dt == null)
947                                                 break;
948                                         return dt.ParseValue (Value, NameTable, this as IXmlNamespaceResolver);
949                                 }
950                                 return Value;
951                         }
952                 }
953
954                 public virtual object UnderlyingObject {
955                         get { return null; }
956                 }
957
958                 public override bool ValueAsBoolean {
959                         get { return XQueryConvert.StringToBoolean (Value); }
960                 }
961
962                 public override DateTime ValueAsDateTime {
963                         get { return XmlConvert.ToDateTime (Value); }
964                 }
965
966                 public override double ValueAsDouble {
967                         get { return XQueryConvert.StringToDouble (Value); }
968                 }
969
970                 public override int ValueAsInt {
971                         get { return XQueryConvert.StringToInt (Value); }
972                 }
973
974                 public override long ValueAsLong {
975                         get { return XQueryConvert.StringToInteger (Value); }
976                 }
977
978                 public override Type ValueType {
979                         get {
980                                 return SchemaInfo != null &&
981                                         SchemaInfo.SchemaType != null &&
982                                         SchemaInfo.SchemaType.Datatype != null ?
983                                         SchemaInfo.SchemaType.Datatype.ValueType
984                                         : null;
985                         }
986                 }
987
988                 [MonoTODO]
989                 public override XmlSchemaType XmlType {
990                         get {
991                                 if (SchemaInfo != null)
992                                         return SchemaInfo.SchemaType;
993                                 return null;
994                         }
995                 }
996
997                 private XmlReader CreateFragmentReader (string fragment)
998                 {
999                         XmlReaderSettings settings = new XmlReaderSettings ();
1000                         settings.ConformanceLevel = ConformanceLevel.Fragment;
1001                         XmlNamespaceManager nsmgr = new XmlNamespaceManager (NameTable);
1002                         foreach (KeyValuePair<string,string> nss in GetNamespacesInScope (XmlNamespaceScope.All))
1003                                 nsmgr.AddNamespace (nss.Key, nss.Value);
1004                         return XmlReader.Create (
1005                                 new StringReader (fragment),
1006                                 settings,
1007                                 new XmlParserContext (NameTable, nsmgr, null, XmlSpace.None));
1008                 }
1009
1010                 // must override it.
1011                 public virtual XmlWriter AppendChild ()
1012                 {
1013                         throw new NotSupportedException ();
1014                 }
1015
1016                 public virtual void AppendChild (
1017                         string xmlFragments)
1018                 {
1019                         AppendChild (CreateFragmentReader (xmlFragments));
1020                 }
1021
1022                 public virtual void AppendChild (
1023                         XmlReader reader)
1024                 {
1025                         XmlWriter w = AppendChild ();
1026                         while (!reader.EOF)
1027                                 w.WriteNode (reader, false);
1028                         w.Close ();
1029                 }
1030
1031                 [MonoTODO]
1032                 public virtual void AppendChild (
1033                         XPathNavigator nav)
1034                 {
1035                         AppendChild (new XPathNavigatorReader (nav));
1036                 }
1037
1038                 public virtual void AppendChildElement (string prefix, string name, string ns, string value)
1039                 {
1040                         XmlWriter xw = AppendChild ();
1041                         xw.WriteStartElement (prefix, name, ns);
1042                         xw.WriteString (value);
1043                         xw.WriteEndElement ();
1044                         xw.Close ();
1045                 }
1046
1047                 public virtual void CreateAttribute (string prefix, string localName, string namespaceURI, string value)
1048                 {
1049                         using (XmlWriter w = CreateAttributes ()) {
1050                                 w.WriteAttributeString (prefix, localName, namespaceURI, value);
1051                         }
1052                 }
1053
1054                 // must override it.
1055                 [MonoTODO ("needs tests")]
1056                 public virtual XmlWriter CreateAttributes ()
1057                 {
1058                         throw new NotSupportedException ();
1059                 }
1060
1061                 // must override it.
1062                 public virtual void DeleteSelf ()
1063                 {
1064                         throw new NotSupportedException ();
1065                 }
1066
1067                 // must override it.
1068                 public virtual void DeleteRange (XPathNavigator nav)
1069                 {
1070                         throw new NotSupportedException ();
1071                 }
1072
1073                 public virtual XmlWriter ReplaceRange (XPathNavigator nav)
1074                 {
1075                         throw new NotSupportedException ();
1076                 }
1077         
1078                 public virtual XmlWriter InsertAfter ()
1079                 {
1080                         switch (NodeType) {
1081                         case XPathNodeType.Root:
1082                         case XPathNodeType.Attribute:
1083                         case XPathNodeType.Namespace:
1084                                 throw new InvalidOperationException (String.Format ("Insertion after {0} is not allowed.", NodeType));
1085                         }
1086                         XPathNavigator nav = Clone ();
1087                         if (nav.MoveToNext ())
1088                                 return nav.InsertBefore ();
1089                         else if (nav.MoveToParent ())
1090                                 return nav.AppendChild ();
1091                         else
1092                                 throw new InvalidOperationException ("Could not move to parent to insert sibling node");
1093                 }
1094
1095                 public virtual void InsertAfter (string xmlFragments)
1096                 {
1097                         InsertAfter (CreateFragmentReader (xmlFragments));
1098                 }
1099
1100                 public virtual void InsertAfter (XmlReader reader)
1101                 {
1102                         using (XmlWriter w = InsertAfter ()) {
1103                                 w.WriteNode (reader, false);
1104                         }
1105                 }
1106
1107                 [MonoTODO]
1108                 public virtual void InsertAfter (XPathNavigator nav)
1109                 {
1110                         InsertAfter (new XPathNavigatorReader (nav));
1111                 }
1112
1113                 public virtual XmlWriter InsertBefore ()
1114                 {
1115                         throw new NotSupportedException ();
1116                 }
1117
1118                 public virtual void InsertBefore (string xmlFragments)
1119                 {
1120                         InsertBefore (CreateFragmentReader (xmlFragments));
1121                 }
1122
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 AppendChild ();
1159                 }
1160
1161                 public virtual void PrependChild (string xmlFragments)
1162                 {
1163                         PrependChild (CreateFragmentReader (xmlFragments));
1164                 }
1165
1166                 public virtual void PrependChild (XmlReader reader)
1167                 {
1168                         using (XmlWriter w = PrependChild ()) {
1169                                 w.WriteNode (reader, false);
1170                         }
1171                 }
1172
1173                 [MonoTODO]
1174                 public virtual void PrependChild (XPathNavigator nav)
1175                 {
1176                         PrependChild (new XPathNavigatorReader (nav));
1177                 }
1178
1179                 public virtual void PrependChildElement (string prefix, 
1180                         string localName, string namespaceURI, string value)
1181                 {
1182                         using (XmlWriter w = PrependChild ()) {
1183                                 w.WriteElementString (prefix, localName, namespaceURI, value);
1184                         }
1185                 }
1186
1187                 public virtual void ReplaceSelf (string xmlFragment)
1188                 {
1189                         ReplaceSelf (CreateFragmentReader (xmlFragment));
1190                 }
1191
1192                 // must override it.
1193                 public virtual void ReplaceSelf (XmlReader reader)
1194                 {
1195                         throw new NotSupportedException ();
1196                 }
1197
1198                 [MonoTODO]
1199                 public virtual void ReplaceSelf (XPathNavigator navigator)
1200                 {
1201                         ReplaceSelf (new XPathNavigatorReader (navigator));
1202                 }
1203
1204                 // Dunno the exact purpose, but maybe internal editor use
1205                 [MonoTODO]
1206                 public virtual void SetTypedValue (object value)
1207                 {
1208                         throw new NotSupportedException ();
1209                 }
1210
1211                 public virtual void SetValue (string value)
1212                 {
1213                         throw new NotSupportedException ();
1214                 }
1215
1216                 [MonoTODO]
1217                 private void DeleteChildren ()
1218                 {
1219                         switch (NodeType) {
1220                         case XPathNodeType.Namespace:
1221                                 throw new InvalidOperationException ("Removing namespace node content is not supported.");
1222                         case XPathNodeType.Attribute:
1223                                 return;
1224                         case XPathNodeType.Text:
1225                         case XPathNodeType.SignificantWhitespace:
1226                         case XPathNodeType.Whitespace:
1227                         case XPathNodeType.ProcessingInstruction:
1228                         case XPathNodeType.Comment:
1229                                 DeleteSelf ();
1230                                 return;
1231                         }
1232                         if (!HasChildren)
1233                                 return;
1234                         XPathNavigator nav = Clone ();
1235                         nav.MoveToFirstChild ();
1236                         while (!nav.IsSamePosition (this))
1237                                 nav.DeleteSelf ();
1238                 }
1239 #endif
1240         }
1241 }