Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data / System / NewXml / TreeIterator.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="TreeIterator.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 //------------------------------------------------------------------------------
8 #pragma warning disable 618 // ignore obsolete warning about XmlDataDocument
9 namespace System.Xml {
10     using System.Diagnostics;
11
12     // Iterates over non-attribute nodes
13     internal sealed class TreeIterator : BaseTreeIterator {
14         private XmlNode         nodeTop;
15         private XmlNode         currentNode;
16
17         internal TreeIterator( XmlNode nodeTop ) : base( ((XmlDataDocument)(nodeTop.OwnerDocument)).Mapper ) {
18             Debug.Assert( nodeTop != null );
19             this.nodeTop     = nodeTop;
20             this.currentNode = nodeTop;
21         }
22
23         internal override void Reset() {
24             currentNode = nodeTop;
25         }
26
27         internal override XmlNode CurrentNode {
28             get {
29                 return currentNode;
30             }
31         }
32
33         internal override bool Next() {
34             XmlNode nextNode;
35
36             // Try to move to the first child
37             nextNode = currentNode.FirstChild;
38
39             // No children, try next sibling
40             if ( nextNode != null ) {
41                 currentNode = nextNode;
42                 return true;
43             }
44             return NextRight();
45         }
46
47         internal override bool NextRight() {
48             // Make sure we do not get past the nodeTop if we call NextRight on a just initialized iterator and nodeTop has no children
49             if ( currentNode == nodeTop ) {
50                 currentNode = null;
51                 return false;
52             }
53
54             XmlNode nextNode = currentNode.NextSibling;
55
56             if ( nextNode != null ) {
57                 currentNode = nextNode;
58                 return true;
59             }
60
61             // No next sibling, try the first sibling of from the parent chain
62             nextNode = currentNode;
63             while ( nextNode != nodeTop && nextNode.NextSibling == null )
64                 nextNode = nextNode.ParentNode;
65
66             if ( nextNode == nodeTop ) {
67                 currentNode = null;
68                 return false;
69             }
70
71             currentNode = nextNode.NextSibling;
72             Debug.Assert( currentNode != null );
73             return true;
74         }
75     }
76 }
77