2009-06-05 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.Configuration_2.0 / ProfilePropertySettingsCollection.cs
1 //
2 // System.Web.Configuration.ProfilePropertySettingsCollection
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9
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 #if NET_2_0
32
33 using System;
34 using System.Configuration;
35 using System.Xml;
36
37 namespace System.Web.Configuration
38 {
39         [ConfigurationCollection (typeof (ProfilePropertySettings), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
40         public class ProfilePropertySettingsCollection : ConfigurationElementCollection
41         {
42                 static ConfigurationPropertyCollection properties;
43
44                 static ProfilePropertySettingsCollection ()
45                 {
46                         properties = new ConfigurationPropertyCollection ();
47                 }
48                 
49                 public void Add (ProfilePropertySettings propertySettings)
50                 {
51                         BaseAdd (propertySettings);
52                 }
53
54                 public void Clear ()
55                 {
56                         BaseClear ();
57                 }
58                 
59                 protected override ConfigurationElement CreateNewElement ()
60                 {
61                         return new ProfilePropertySettings ();
62                 }
63
64                 public ProfilePropertySettings Get (int index)
65                 {
66                         return (ProfilePropertySettings) BaseGet (index);
67                 }
68
69                 public ProfilePropertySettings Get (string name)
70                 {
71                         return (ProfilePropertySettings) BaseGet (name);
72                 }
73
74                 protected override object GetElementKey (ConfigurationElement element)
75                 {
76                         return ((ProfilePropertySettings)element).Name;
77                 }
78
79                 protected override bool OnDeserializeUnrecognizedElement (string elementName, XmlReader reader) 
80                 {
81                         /* Disabled: pending investigation
82                          *
83                         if (elementName == "clear" || elementName == "group") {
84                                 throw new ConfigurationErrorsException (String.Format ("{0} is not permitted here", elementName), reader);
85                         }
86                         */
87                         
88                         return base.OnDeserializeUnrecognizedElement (elementName, reader);
89                 }
90
91                 public string GetKey (int index)
92                 {
93                         ProfilePropertySettings s = Get (index);
94                         if (s == null)
95                                 return null;
96
97                         return s.Name;
98                 }
99
100                 public int IndexOf (ProfilePropertySettings propertySettings)
101                 {
102                         return BaseIndexOf (propertySettings);
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                 public void Set (ProfilePropertySettings propertySettings)
116                 {
117                         ProfilePropertySettings existing = Get (propertySettings.Name);
118
119                         if (existing == null) {
120                                 Add (propertySettings);
121                         }
122                         else {
123                                 int index = BaseIndexOf (existing);
124                                 RemoveAt (index);
125                                 BaseAdd (index, propertySettings);
126                         }
127                 }
128
129                 public string[ ] AllKeys {
130                         get {
131                                 string[] keys = new string[Count];
132                                 for (int i = 0; i < Count; i ++)
133                                         keys[i] = this[i].Name;
134                                 return keys;
135                         }
136                 }
137
138                 protected virtual bool AllowClear {
139                         get {
140                                 return false;
141                         }
142                 }
143
144                 public ProfilePropertySettings this[int index] {
145                         get { return Get (index); }
146                         set { if (Get (index) != null) BaseRemoveAt (index); BaseAdd (index, value); }
147                 }
148                 
149                 public new ProfilePropertySettings this [string name] {
150                         get { return (ProfilePropertySettings) base.BaseGet (name); }
151                 }
152                 
153                 protected override bool ThrowOnDuplicate {
154                         get {
155                                 return true;
156                         }
157                 }
158
159                 protected override ConfigurationPropertyCollection Properties {
160                         get { return properties; }
161                 }
162         }
163 }
164
165 #endif