Finished XmlNodeListChildren
[mono.git] / mcs / class / System.XML / System.Xml / XmlNodeListChildren.cs
1 //
2 // System.Xml.XmlNodeList
3 //
4 // Author:
5 //   Kral Ferch <kral_ferch@hotmail.com>
6 //
7 // (C) 2002 Kral Ferch
8 //
9
10 using System;
11 using System.Collections;
12
13 namespace System.Xml
14 {
15         public class XmlNodeListChildren : XmlNodeList
16         {
17                 #region Enumerator
18
19                 private class Enumerator : IEnumerator
20                 {
21                         XmlNode parent;
22                         XmlLinkedNode currentChild;
23                         bool passedLastNode;
24
25                         internal Enumerator (XmlNode parent)
26                         {
27                                 currentChild = null;
28                                 this.parent = parent;
29                                 passedLastNode = false;
30                         }
31
32                         public virtual object Current {
33                                 get {
34                                         if ((currentChild == null) ||
35                                                 (parent.LastLinkedChild == null) ||
36                                                 (passedLastNode == true))
37                                                 throw new InvalidOperationException();
38
39                                         return currentChild;
40                                 }
41                         }
42
43                         public virtual bool MoveNext()
44                         {
45                                 bool movedNext = true;
46
47                                 if (parent.LastLinkedChild == null) {
48                                         movedNext = false;
49                                 }
50                                 else if (currentChild == null) {
51                                         currentChild = parent.LastLinkedChild.NextLinkedSibling;
52                                 }
53                                 else {
54                                         if (Object.ReferenceEquals(currentChild, parent.LastLinkedChild)) {
55                                                 movedNext = false;
56                                                 passedLastNode = true;
57                                         }
58                                         else {
59                                                 currentChild = currentChild.NextLinkedSibling;
60                                         }
61                                 }
62
63                                 return movedNext;
64                         }
65
66                         public virtual void Reset()
67                         {
68                                 currentChild = null;
69                         }
70                 }
71
72                 #endregion
73
74                 #region Fields
75
76                 XmlNode parent;
77
78                 #endregion
79
80                 #region Constructors
81                 public XmlNodeListChildren(XmlNode parent)
82                 {
83                         this.parent = parent;
84                 }
85
86                 #endregion
87
88                 #region Properties
89
90                 public override int Count {
91                         get {
92                                 int count = 0;
93
94                                 if (parent.LastLinkedChild != null) {
95                                         XmlLinkedNode currentChild = parent.LastLinkedChild.NextLinkedSibling;
96                                         
97                                         count = 1;
98                                         while (!Object.ReferenceEquals(currentChild, parent.LastLinkedChild)) {
99                                                 currentChild = currentChild.NextLinkedSibling;
100                                                 count++;
101                                         }
102                                 }
103
104                                 return count;
105                         }
106                 }
107
108                 #endregion
109
110                 #region Methods
111
112                 public override IEnumerator GetEnumerator ()
113                 {
114                         return new Enumerator(parent);
115                 }
116
117                 public override XmlNode Item (int index)
118                 {
119                         XmlNode requestedNode = null;
120
121                         // Instead of checking for && index < Count which has to walk
122                         // the whole list to get a count, we'll just keep a count since
123                         // we have to walk the list anyways to get to index.
124                         if ((index >= 0) && (parent.LastLinkedChild != null)) {
125                                 XmlLinkedNode currentChild = parent.LastLinkedChild.NextLinkedSibling;
126                                 int count = 0;
127
128                                 while ((count < index) && !Object.ReferenceEquals(currentChild, parent.LastLinkedChild)) 
129                                 {
130                                         currentChild = currentChild.NextLinkedSibling;
131                                         count++;
132                                 }
133
134                                 if (count == index) {
135                                         requestedNode = currentChild;
136                                 }
137                         }
138
139                         return requestedNode;
140                 }
141
142                 #endregion
143         }
144
145         
146 }