**** Merged from MCS ****
[mono-project.git] / mcs / class / System.XML / System.Xml / XmlNodeListChildren.cs
blob16a67c7994a52f7712102f31ca6e435a3e205df0
1 //
2 // System.Xml.XmlNodeList
3 //
4 // Author:
5 // Kral Ferch <kral_ferch@hotmail.com>
6 //
7 // (C) 2002 Kral Ferch
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System;
32 using System.Collections;
34 namespace System.Xml
36 internal class XmlNodeListChildren : XmlNodeList
38 #region Enumerator
40 private class Enumerator : IEnumerator
42 XmlNode parent;
43 XmlLinkedNode currentChild;
44 bool passedLastNode;
46 internal Enumerator (XmlNode parent)
48 currentChild = null;
49 this.parent = parent;
50 passedLastNode = false;
53 public virtual object Current {
54 get {
55 if ((currentChild == null) ||
56 (parent.LastLinkedChild == null) ||
57 (passedLastNode == true))
58 throw new InvalidOperationException();
60 return currentChild;
64 public virtual bool MoveNext()
66 bool movedNext = true;
68 if (parent.LastLinkedChild == null) {
69 movedNext = false;
71 else if (currentChild == null) {
72 currentChild = parent.LastLinkedChild.NextLinkedSibling;
74 else {
75 if (Object.ReferenceEquals(currentChild, parent.LastLinkedChild)) {
76 movedNext = false;
77 passedLastNode = true;
79 else {
80 currentChild = currentChild.NextLinkedSibling;
84 return movedNext;
87 public virtual void Reset()
89 currentChild = null;
93 #endregion
95 #region Fields
97 XmlNode parent;
99 #endregion
101 #region Constructors
102 public XmlNodeListChildren(XmlNode parent)
104 this.parent = parent;
107 #endregion
109 #region Properties
111 public override int Count {
112 get {
113 int count = 0;
115 if (parent.LastLinkedChild != null) {
116 XmlLinkedNode currentChild = parent.LastLinkedChild.NextLinkedSibling;
118 count = 1;
119 while (!Object.ReferenceEquals(currentChild, parent.LastLinkedChild)) {
120 currentChild = currentChild.NextLinkedSibling;
121 count++;
125 return count;
129 #endregion
131 #region Methods
133 public override IEnumerator GetEnumerator ()
135 return new Enumerator(parent);
138 public override XmlNode Item (int index)
140 XmlNode requestedNode = null;
142 // Return null if index is out of range. by DOM design.
143 if (Count <= index)
144 return null;
146 // Instead of checking for && index < Count which has to walk
147 // the whole list to get a count, we'll just keep a count since
148 // we have to walk the list anyways to get to index.
149 if ((index >= 0) && (parent.LastLinkedChild != null)) {
150 XmlLinkedNode currentChild = parent.LastLinkedChild.NextLinkedSibling;
151 int count = 0;
153 while ((count < index) && !Object.ReferenceEquals(currentChild, parent.LastLinkedChild))
155 currentChild = currentChild.NextLinkedSibling;
156 count++;
159 if (count == index) {
160 requestedNode = currentChild;
164 return requestedNode;
167 #endregion