Merge pull request #950 from ermshiperete/bug-xamarin-2787
[mono.git] / mcs / class / System.Xml.Linq / System.Xml.Linq / XIterators.cs
1 //
2 // Authors:
3 //   Atsushi Enomoto
4 //
5 // Copyright 2007 Novell (http://www.novell.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26
27 #if NET_2_0
28
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.IO;
33 using System.Text;
34 using System.Xml;
35
36 using XPI = System.Xml.Linq.XProcessingInstruction;
37
38
39 namespace System.Xml.Linq
40 {
41         /*
42         // Iterators
43         internal class XFilterIterator <T> : IEnumerable <T>
44         {
45                 IEnumerable <object> source;
46                 XName name;
47
48                 public XFilterIterator (IEnumerable <object> source, XName name)
49                 {
50                         this.source = source;
51                         this.name = name;
52                 }
53
54                 public IEnumerator <T> GetEnumerator ()
55                 {
56                         foreach (object o in source) {
57                                 if (! (o is T))
58                                         continue;
59                                 if (name != null) {
60                                         if (o is XElement) {
61                                                 if (((XElement) o).Name != name)
62                                                         continue;
63                                         }
64                                         else if (o is XAttribute) {
65                                                 if (((XAttribute) o).Name != name)
66                                                         continue;
67                                         }
68                                 }
69                                 yield return (T) o;
70                         }
71                 }
72
73                 IEnumerator IEnumerable.GetEnumerator ()
74                 {
75                         return GetEnumerator ();
76                 }
77         }
78
79         internal class XDescendantIterator <T> : IEnumerable <T>
80         {
81                 IEnumerable <T> source;
82
83                 public XDescendantIterator (IEnumerable <object> source)
84                 {
85                         this.source = new XFilterIterator <T> (source, null);
86                 }
87
88                 public IEnumerator <T> GetEnumerator ()
89                 {
90                         foreach (T t1 in source) {
91                                 yield return t1;
92                                 XContainer xc = t1 as XContainer;
93                                 if (xc == null || xc.FirstChild == null)
94                                         continue;
95                                 foreach (T t2 in new XDescendantIterator <T> (xc.Content ()))
96                                         yield return t2;
97                         }
98                 }
99
100                 IEnumerator IEnumerable.GetEnumerator ()
101                 {
102                         return GetEnumerator ();
103                 }
104         }
105         */
106
107 #if !LIST_BASED
108         internal class XChildrenIterator : IEnumerable <object>
109         {
110                 XContainer source;
111                 XNode n;
112
113                 public XChildrenIterator (XContainer source)
114                 {
115                         this.source = source;
116                 }
117
118                 public IEnumerator <object> GetEnumerator ()
119                 {
120                         if (n == null) {
121                                 n = source.FirstNode;
122                                 if (n == null)
123                                         yield break;
124                         }
125                         do {
126                                 yield return n;
127                                 n = n.NextNode;
128                         } while (n != source.LastNode);
129                 }
130
131                 IEnumerator IEnumerable.GetEnumerator ()
132                 {
133                         return GetEnumerator ();
134                 }
135         }
136 #endif
137 }
138
139 #endif