Merge pull request #952 from ermshiperete/bug-xamarin-2912
[mono.git] / mcs / class / System.Xml.Linq / System.Xml.Linq / Extensions.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 using System;
28 using System.Collections;
29 using System.Collections.Generic;
30 using System.Runtime.CompilerServices;
31
32 namespace System.Xml.Linq
33 {
34         public static class Extensions
35         {
36                 public static IEnumerable<XElement> Ancestors<T> (this IEnumerable<T> source) where T : XNode
37                 {
38                         foreach (T item in source)
39                                 for (XElement n = item.Parent as XElement; n != null; n = n.Parent as XElement)
40                                         yield return n;
41                 }
42
43                 public static IEnumerable<XElement> Ancestors<T> (this IEnumerable<T> source, XName name) where T : XNode
44                 {
45                         foreach (T item in source)
46                                 for (XElement n = item.Parent as XElement; n != null; n = n.Parent as XElement)
47                                         if (n.Name == name)
48                                                 yield return n;
49                 }
50
51                 public static IEnumerable<XElement> AncestorsAndSelf (this IEnumerable<XElement> source)
52                 {
53                         foreach (XElement item in source)
54                                 for (XElement n = item as XElement; n != null; n = n.Parent as XElement)
55                                         yield return n;
56                 }
57
58                 public static IEnumerable<XElement> AncestorsAndSelf (this IEnumerable<XElement> source, XName name)
59                 {
60                         foreach (XElement item in source)
61                                 for (XElement n = item as XElement; n != null; n = n.Parent as XElement)
62                                         if (n.Name == name)
63                                                 yield return n;
64                 }
65
66                 public static IEnumerable <XAttribute> Attributes (this IEnumerable <XElement> source)
67                 {
68                         foreach (XElement item in source)
69                                 foreach (XAttribute attr in item.Attributes ())
70                                         yield return attr;
71                 }
72
73                 public static IEnumerable <XAttribute> Attributes (this IEnumerable <XElement> source, XName name)
74                 {
75                         foreach (XElement item in source)
76                                 foreach (XAttribute attr in item.Attributes (name))
77                                         yield return attr;
78                 }
79
80                 public static IEnumerable<XNode> DescendantNodes<T> (
81                         this IEnumerable<T> source) where T : XContainer
82                 {
83                         foreach (XContainer item in source)
84                                 foreach (XNode n in item.DescendantNodes ())
85                                         yield return n;
86                 }
87
88                 public static IEnumerable<XNode> DescendantNodesAndSelf (
89                         this IEnumerable<XElement> source)
90                 {
91                         foreach (XElement item in source)
92                                 foreach (XNode n in item.DescendantNodesAndSelf ())
93                                         yield return n;
94                 }
95
96                 public static IEnumerable<XElement> Descendants<T> (
97                         this IEnumerable<T> source) where T : XContainer
98                 {
99                         foreach (XContainer item in source)
100                                 foreach (XElement n in item.Descendants ())
101                                         yield return n;
102                 }
103
104                 public static IEnumerable<XElement> Descendants<T> (
105                         this IEnumerable<T> source, XName name) where T : XContainer
106                 {
107                         foreach (XContainer item in source)
108                                 foreach (XElement n in item.Descendants (name))
109                                         yield return n;
110                 }
111
112                 public static IEnumerable<XElement> DescendantsAndSelf (
113                         this IEnumerable<XElement> source)
114                 {
115                         foreach (XElement item in source)
116                                 foreach (XElement n in item.DescendantsAndSelf ())
117                                         yield return n;
118                 }
119
120                 public static IEnumerable<XElement> DescendantsAndSelf (
121                         this IEnumerable<XElement> source, XName name)
122                 {
123                         foreach (XElement item in source)
124                                 foreach (XElement n in item.DescendantsAndSelf (name))
125                                         yield return n;
126                 }
127
128                 public static IEnumerable<XElement> Elements<T> (
129                         this IEnumerable<T> source) where T : XContainer
130                 {
131                         foreach (XContainer item in source)
132                                 foreach (XElement n in item.Elements ())
133                                         yield return n;
134                 }
135
136                 public static IEnumerable<XElement> Elements<T> (
137                         this IEnumerable<T> source, XName name) where T : XContainer
138                 {
139                         foreach (XContainer item in source)
140                                 foreach (XElement n in item.Elements (name))
141                                         yield return n;
142                 }
143
144                 public static IEnumerable<T> InDocumentOrder<T> (
145                         this IEnumerable<T> source) where T : XNode
146                 {
147                         List<XNode> list = new List<XNode> ();
148                         foreach (XNode n in source)
149                                 list.Add (n);
150                         list.Sort (XNode.DocumentOrderComparer);
151                         foreach (T n in list)
152                                 yield return n;
153                 }
154
155                 public static IEnumerable<XNode> Nodes<T> (
156                         this IEnumerable<T> source) where T : XContainer
157                 {
158                         foreach (XContainer item in source)
159                                 foreach (XNode n in item.Nodes ())
160                                         yield return n;
161                 }
162
163                 public static void Remove (this IEnumerable<XAttribute> source)
164                 {
165                         foreach (XAttribute item in source)
166                                 item.Remove ();
167                 }
168
169                 public static void Remove<T> (this IEnumerable<T> source) where T : XNode
170                 {
171                         var l = new List<T> (source);
172                         foreach (T item in l)
173                                 item.Remove ();
174                 }
175         }
176 }