* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConfigurationLockCollection.cs
1 //
2 // System.Configuration.ConfigurationLockCollection.cs
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
27 //
28
29 #if NET_2_0
30
31 using System;
32 using System.Collections;
33
34 namespace System.Configuration 
35 {
36         [Flags]
37         internal enum ConfigurationLockType
38         {
39                 Attribute = 0x01,
40                 Element = 0x02,
41
42                 Exclude = 0x10
43         }
44
45         public sealed class ConfigurationLockCollection : ICollection, IEnumerable
46         {
47                 ArrayList names;
48                 ConfigurationElement element;
49                 ConfigurationLockType lockType;
50                 bool is_modified;
51                 Hashtable valid_name_hash;
52                 string valid_names;
53
54                 internal ConfigurationLockCollection (ConfigurationElement element,
55                                                       ConfigurationLockType lockType)
56                 {
57                         names = new ArrayList ();
58                         this.element = element;
59                         this.lockType = lockType;
60                 }
61
62                 void CheckName (string name)
63                 {
64                         bool isAttribute = (lockType & ConfigurationLockType.Attribute) == ConfigurationLockType.Attribute;
65
66                         if (valid_name_hash == null) {
67                                 valid_name_hash = new Hashtable ();
68                                 foreach (ConfigurationProperty prop in element.Properties) {
69                                         if (isAttribute == prop.IsElement)
70                                                 continue;
71                                         valid_name_hash.Add (prop.Name, true);
72                                 }
73
74                                 /* add the add/remove/clear names of the
75                                  * default collection if there is one */
76                                 if (!isAttribute) {
77                                         ConfigurationElementCollection c = element.GetDefaultCollection ();
78                                         valid_name_hash.Add (c.AddElementName, true);
79                                         valid_name_hash.Add (c.ClearElementName, true);
80                                         valid_name_hash.Add (c.RemoveElementName, true);
81                                 }
82
83                                 string[] valid_name_array = new string[valid_name_hash.Keys.Count];
84                                 valid_name_hash.Keys.CopyTo (valid_name_array, 0);
85                                 
86                                 valid_names = String.Join (",", valid_name_array);
87                         }
88
89                         if (valid_name_hash [name] == null)
90                                 throw new ConfigurationErrorsException (
91                                                 String.Format ("The {2} '{0}' is not valid in the locked list for this section.  The following {3} can be locked: '{1}'",
92                                                                name, valid_names, isAttribute ? "attribute" : "element", isAttribute ? "attributes" : "elements"));
93                 }
94
95                 public void Add (string name)
96                 {
97                         CheckName (name);
98                         if (!names.Contains (name)) {
99                                 names.Add (name);
100                                 is_modified = true;
101                         }
102                 }
103
104                 public void Clear ()
105                 {
106                         names.Clear ();
107                         is_modified = true;
108                 }
109
110                 public bool Contains (string name)
111                 {
112                         return names.Contains (name);
113                 }
114
115                 public void CopyTo (string[] array, int index)
116                 {
117                         names.CopyTo (array, index);
118                 }
119
120                 public IEnumerator GetEnumerator()
121                 {
122                         return names.GetEnumerator ();
123                 }
124
125                 [MonoTODO ("we can't possibly *always* return false here...")]
126                 public bool IsReadOnly (string name)
127                 {
128                         for (int i = 0; i < names.Count; i ++) {
129                                 if ((string)names[i] == name) {
130                                         /* this test used to switch off whether the collection was 'Exclude' or not
131                                          * (the LockAll*Except collections), but that doesn't seem to be the crux of
132                                          * it.  maybe this returns true if the element/attribute is locked in a parent
133                                          * element's lock collections? */
134                                         return false;
135                                 }
136                         }
137
138                         throw new ConfigurationErrorsException (String.Format ("The entry '{0}' is not in the collection.", name));
139                 }
140
141                 public void Remove (string name)
142                 {
143                         names.Remove (name);
144                         is_modified = true;
145                 }
146
147                 public void SetFromList (string attributeList)
148                 {
149                         Clear ();
150
151                         char [] split = {','};
152                         string [] attrs = attributeList.Split (split);
153                         foreach (string a in attrs) {
154                                 Add (a.Trim ());
155                         }
156                 }
157
158                 void ICollection.CopyTo (System.Array array, int index)
159                 {
160                         names.CopyTo (array, index);
161                 }
162
163                 public string AttributeList {
164                         get {
165                                 string[] name_arr = new string[names.Count];
166                                 names.CopyTo (name_arr, 0);
167                                 return String.Join (",", name_arr);
168                         }
169                 }
170
171                 public int Count {
172                         get { return names.Count; }
173                 }
174
175                 [MonoTODO]
176                 public bool HasParentElements {
177                         get { return false; /* XXX */ }
178                 }
179
180                 [MonoTODO]
181                 public bool IsModified {
182                         get { return is_modified; }
183                         internal set { is_modified = value; }
184                 }
185
186                 [MonoTODO]
187                 public bool IsSynchronized {
188                         get { return false; /* XXX */ }
189                 }
190
191                 [MonoTODO]
192                 public object SyncRoot {
193                         get { return this; /* XXX */ }
194                 }
195         }
196 }
197
198 #endif