Merge pull request #600 from tr8dr/master
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConnectionStringSettingsCollection.cs
1 //
2 // System.Configuration.ConnectionStringSettingsCollection.cs
3 //
4 // Authors:
5 //   Sureshkumar T <tsureshkumar@novell.com>
6 //   Chris Toshok <toshok@ximian.com>
7 //
8 //
9 // Copyright (C) 2004,2005 Novell, Inc (http://www.novell.com)
10 //
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.
29 //
30
31 using System;
32
33 namespace System.Configuration
34 {
35         [ConfigurationCollection (typeof (ConnectionStringSettings),
36                                   CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
37         public sealed class ConnectionStringSettingsCollection : ConfigurationElementCollection
38         {
39
40                 public ConnectionStringSettingsCollection () : base ()
41                 {
42                 }
43
44                 public new ConnectionStringSettings this [string Name]
45                 {
46                         get {
47                                 foreach (ConfigurationElement c in this) {
48                                         if (!(c is ConnectionStringSettings))
49                                                 continue;
50                                         if (string.Compare(((ConnectionStringSettings) c).Name, Name, true, 
51                                                 System.Globalization.CultureInfo.InvariantCulture) == 0)
52                                                 return c as ConnectionStringSettings;
53
54                                 }
55                                 return null;
56                         }
57                 }
58
59                 public ConnectionStringSettings this [int index]
60                 {
61                         get { return (ConnectionStringSettings) BaseGet (index); }
62                         set {
63                                 if (BaseGet (index) != null)
64                                         BaseRemoveAt (index);
65                                 BaseAdd (index, value);
66                         }
67                 }
68
69                 [MonoTODO]
70                 protected internal override ConfigurationPropertyCollection Properties {
71                         get { return base.Properties; }
72                 }
73
74
75                 protected override ConfigurationElement CreateNewElement ()
76                 {
77                         return new ConnectionStringSettings ();
78                 }
79
80                 protected override object GetElementKey (ConfigurationElement element)
81                 {
82                         return ((ConnectionStringSettings) element).Name;
83                 }
84
85                 public void Add (ConnectionStringSettings settings)
86                 {
87                         BaseAdd ((ConfigurationElement) settings);
88                 }
89
90                 public void Clear ()
91                 {
92                         BaseClear ();
93                 }
94
95                 public int IndexOf (ConnectionStringSettings settings)
96                 {
97                         return BaseIndexOf (settings);
98                 }
99
100                 public void Remove (ConnectionStringSettings settings)
101                 {
102                         BaseRemove (settings.Name);
103                 }
104
105                 public void Remove (string name)
106                 {
107                         BaseRemove (name);
108                 }
109
110                 public void RemoveAt (int index)
111                 {
112                         BaseRemoveAt (index);
113                 }
114
115                 protected override void BaseAdd (int index, ConfigurationElement element)
116                 {
117                         if (!(element is ConnectionStringSettings))
118                                 base.BaseAdd (element);
119                         if (IndexOf ((ConnectionStringSettings) element) >= 0)
120                                 throw new ConfigurationErrorsException (String.Format ("The element {0} already exist!",
121                                                                                  ((ConnectionStringSettings) element).Name));
122                         this [index] = (ConnectionStringSettings) element;
123                 }
124         }
125
126 }
127