Merge pull request #799 from kebby/master
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConfigurationSectionGroupCollection.cs
1 //
2 // System.Configuration.ConfigurationSectionGroupCollection.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //  Lluis Sanchez Gual (lluis@novell.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
28 //
29
30 using System;
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Runtime.Serialization;
34
35 namespace System.Configuration {
36
37         [Serializable]
38         public sealed class ConfigurationSectionGroupCollection : NameObjectCollectionBase
39         {
40                 SectionGroupInfo group;
41                 Configuration config;
42                 
43                 internal ConfigurationSectionGroupCollection (Configuration config, SectionGroupInfo group)
44                         : base (StringComparer.Ordinal)
45                 {
46                         this.config = config;
47                         this.group = group;
48                 }
49                 
50                 public override NameObjectCollectionBase.KeysCollection Keys {
51                         get { return group.Groups.Keys; }
52                 }
53
54                 public override int Count {
55                         get { return group.Groups.Count; }
56                 }
57
58                 public ConfigurationSectionGroup this [string name] {
59                         get {
60                                 ConfigurationSectionGroup sec = BaseGet (name) as ConfigurationSectionGroup;
61                                 if (sec == null) {
62                                         SectionGroupInfo secData = group.Groups [name] as SectionGroupInfo;
63                                         if (secData == null) return null;
64                                         sec = config.GetSectionGroupInstance (secData);
65                                         BaseSet (name, sec);
66                                 }
67                                 return sec;
68                         }
69                 }
70
71                 public ConfigurationSectionGroup this [int index] {
72                         get { return this [GetKey (index)]; }
73                 }
74
75                 public void Add (string name, ConfigurationSectionGroup sectionGroup)
76                 {
77                         config.CreateSectionGroup (group, name, sectionGroup);
78                 }
79
80                 public void Clear ()
81                 {
82                         if (group.Groups != null) {
83                                 foreach (ConfigInfo data in group.Groups)
84                                         config.RemoveConfigInfo (data);
85                         }
86                 }
87
88                 public void CopyTo (ConfigurationSectionGroup [] array, int index)
89                 {
90                         for (int n=0; n<group.Groups.Count; n++)
91                                 array [n + index] = this [n];
92                 }
93
94                 public ConfigurationSectionGroup Get (int index)
95                 {
96                         return this [index];
97                 }
98
99                 public ConfigurationSectionGroup Get (string name)
100                 {
101                         return this [name];
102                 }
103
104                 public override IEnumerator GetEnumerator ()
105                 {
106                         return group.Groups.AllKeys.GetEnumerator ();
107                 }
108
109                 public string GetKey (int index)
110                 {
111                         return group.Groups.GetKey (index);
112                 }
113
114                 public void Remove (string name)
115                 {
116                         SectionGroupInfo secData = group.Groups [name] as SectionGroupInfo;
117                         if (secData != null)
118                                 config.RemoveConfigInfo (secData);
119                 }
120                 
121                 public void RemoveAt (int index)
122                 {
123                         SectionGroupInfo secData = group.Groups [index] as SectionGroupInfo;
124                         config.RemoveConfigInfo (secData);
125                 }
126
127                 [MonoTODO]
128                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
129                 {
130                         throw new NotImplementedException ();
131                 }
132         }
133 }
134