Merge pull request #2916 from ludovic-henry/fix-40306
[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
32 using System;
33 using System.Configuration;
34 using System.Xml;
35
36 namespace System.Web.Configuration
37 {
38         [ConfigurationCollection (typeof (ProfilePropertySettings), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
39         public class ProfilePropertySettingsCollection : ConfigurationElementCollection
40         {
41                 static ConfigurationPropertyCollection properties;
42
43                 static ProfilePropertySettingsCollection ()
44                 {
45                         properties = new ConfigurationPropertyCollection ();
46                 }
47                 
48                 public void Add (ProfilePropertySettings propertySettings)
49                 {
50                         BaseAdd (propertySettings);
51                 }
52
53                 public void Clear ()
54                 {
55                         BaseClear ();
56                 }
57                 
58                 protected override ConfigurationElement CreateNewElement ()
59                 {
60                         return new ProfilePropertySettings ();
61                 }
62
63                 public ProfilePropertySettings Get (int index)
64                 {
65                         return (ProfilePropertySettings) BaseGet (index);
66                 }
67
68                 public ProfilePropertySettings Get (string name)
69                 {
70                         return (ProfilePropertySettings) BaseGet (name);
71                 }
72
73                 protected override object GetElementKey (ConfigurationElement element)
74                 {
75                         return ((ProfilePropertySettings)element).Name;
76                 }
77
78                 protected override bool OnDeserializeUnrecognizedElement (string elementName, XmlReader reader) 
79                 {
80                         /* Disabled: pending investigation
81                          *
82                         if (elementName == "clear" || elementName == "group") {
83                                 throw new ConfigurationErrorsException (String.Format ("{0} is not permitted here", elementName), reader);
84                         }
85                         */
86                         
87                         return base.OnDeserializeUnrecognizedElement (elementName, reader);
88                 }
89
90                 public string GetKey (int index)
91                 {
92                         ProfilePropertySettings s = Get (index);
93                         if (s == null)
94                                 return null;
95
96                         return s.Name;
97                 }
98
99                 public int IndexOf (ProfilePropertySettings propertySettings)
100                 {
101                         return BaseIndexOf (propertySettings);
102                 }
103
104                 public void Remove (string name)
105                 {
106                         BaseRemove (name);
107                 }
108
109                 public void RemoveAt (int index)
110                 {
111                         BaseRemoveAt (index);
112                 }
113
114                 public void Set (ProfilePropertySettings propertySettings)
115                 {
116                         ProfilePropertySettings existing = Get (propertySettings.Name);
117
118                         if (existing == null) {
119                                 Add (propertySettings);
120                         }
121                         else {
122                                 int index = BaseIndexOf (existing);
123                                 RemoveAt (index);
124                                 BaseAdd (index, propertySettings);
125                         }
126                 }
127
128                 public string[ ] AllKeys {
129                         get {
130                                 string[] keys = new string[Count];
131                                 for (int i = 0; i < Count; i ++)
132                                         keys[i] = this[i].Name;
133                                 return keys;
134                         }
135                 }
136
137                 protected virtual bool AllowClear {
138                         get {
139                                 return false;
140                         }
141                 }
142
143                 public ProfilePropertySettings this[int index] {
144                         get { return Get (index); }
145                         set { if (Get (index) != null) BaseRemoveAt (index); BaseAdd (index, value); }
146                 }
147                 
148                 public new ProfilePropertySettings this [string name] {
149                         get { return (ProfilePropertySettings) base.BaseGet (name); }
150                 }
151                 
152                 protected override bool ThrowOnDuplicate {
153                         get {
154                                 return true;
155                         }
156                 }
157
158                 protected internal override ConfigurationPropertyCollection Properties {
159                         get { return properties; }
160                 }
161         }
162 }
163