2008-05-18 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConfigurationSectionCollection.cs
1 //
2 // System.Configuration.ConfigurationSectionCollection.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 #if NET_2_0
31 using System;
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Runtime.Serialization;
35
36 namespace System.Configuration
37 {
38         [Serializable]
39         public sealed class ConfigurationSectionCollection : NameObjectCollectionBase
40         {
41                 SectionGroupInfo group;
42                 Configuration config;
43                 
44                 internal ConfigurationSectionCollection (Configuration config, SectionGroupInfo group)
45                         : base (StringComparer.Ordinal)
46                 {
47                         this.config = config;
48                         this.group = group;
49                 }
50                 
51                 public override NameObjectCollectionBase.KeysCollection Keys
52                 {
53                         get { return group.Sections.Keys; }
54                 }
55                 
56                 public override int Count 
57                 {
58                         get { return group.Sections.Count; }
59                 }
60         
61                 public ConfigurationSection this [string name]
62                 {
63                         get {
64                                 ConfigurationSection sec = BaseGet (name) as ConfigurationSection;
65                                 if (sec == null) {
66                                         SectionInfo secData = group.Sections [name] as SectionInfo;
67                                         if (secData == null) return null;
68                                         sec = config.GetSectionInstance (secData, true);
69                                         if (sec == null) return null;
70                                         BaseSet (name, sec);
71                                 }
72                                 return sec;
73                         }
74                 }
75         
76                 public ConfigurationSection this [int index]
77                 {
78                         get { return this [GetKey (index)]; }
79                 }
80                 
81                 public void Add (string name, ConfigurationSection section)
82                 {
83                         config.CreateSection (group, name, section);
84                 }
85                 
86                 public void Clear ()
87                 {
88                         if (group.Sections != null) {
89                                 foreach (ConfigInfo data in group.Sections)
90                                         config.RemoveConfigInfo (data);
91                         }
92                 }
93                 
94                 public void CopyTo (ConfigurationSection [] array, int index)
95                 {
96                         for (int n=0; n<group.Sections.Count; n++)
97                                 array [n + index] = this [n];
98                 }
99                 
100                 public ConfigurationSection Get (int index)
101                 {
102                         return this [index];
103                 }
104                 
105                 public ConfigurationSection Get (string name)
106                 {
107                         return this [name];
108                 }
109                 
110                 public override IEnumerator GetEnumerator()
111                 {
112                         foreach (string key in group.Sections.AllKeys)
113                                 yield return this [key];
114                 }
115                 
116                 public string GetKey (int index)
117                 {
118                         return group.Sections.GetKey (index);
119                 }
120                 
121                 public void Remove (string name)
122                 {
123                         SectionInfo secData = group.Sections [name] as SectionInfo;
124                         if (secData != null)
125                                 config.RemoveConfigInfo (secData);
126                 }
127                 
128                 public void RemoveAt (int index)
129                 {
130                         SectionInfo secData = group.Sections [index] as SectionInfo;
131                         config.RemoveConfigInfo (secData);
132                 }
133
134                 [MonoTODO]
135                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
136                 {
137                         throw new NotImplementedException ();
138                 }
139         }
140 }
141 #endif