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 using System; 30 using System.Collections; 31 32 namespace System.Configuration 33 { 34 [Flags] 35 internal enum ConfigurationLockType 36 { 37 Attribute = 0x01, 38 Element = 0x02, 39 40 Exclude = 0x10 41 } 42 43 public sealed class ConfigurationLockCollection : ICollection, IEnumerable 44 { 45 ArrayList names; 46 ConfigurationElement element; 47 ConfigurationLockType lockType; 48 bool is_modified; 49 Hashtable valid_name_hash; 50 string valid_names; 51 ConfigurationLockCollection(ConfigurationElement element, ConfigurationLockType lockType)52 internal ConfigurationLockCollection (ConfigurationElement element, 53 ConfigurationLockType lockType) 54 { 55 names = new ArrayList (); 56 this.element = element; 57 this.lockType = lockType; 58 } 59 CheckName(string name)60 void CheckName (string name) 61 { 62 bool isAttribute = (lockType & ConfigurationLockType.Attribute) == ConfigurationLockType.Attribute; 63 64 if (valid_name_hash == null) { 65 valid_name_hash = new Hashtable (); 66 foreach (ConfigurationProperty prop in element.Properties) { 67 if (isAttribute == prop.IsElement) 68 continue; 69 valid_name_hash.Add (prop.Name, true); 70 } 71 72 /* add the add/remove/clear names of the 73 * default collection if there is one */ 74 if (!isAttribute) { 75 ConfigurationElementCollection c = element.GetDefaultCollection (); 76 valid_name_hash.Add (c.AddElementName, true); 77 valid_name_hash.Add (c.ClearElementName, true); 78 valid_name_hash.Add (c.RemoveElementName, true); 79 } 80 81 string[] valid_name_array = new string[valid_name_hash.Keys.Count]; 82 valid_name_hash.Keys.CopyTo (valid_name_array, 0); 83 84 valid_names = String.Join (",", valid_name_array); 85 } 86 87 if (valid_name_hash [name] == null) 88 throw new ConfigurationErrorsException ( 89 String.Format ("The {2} '{0}' is not valid in the locked list for this section. The following {3} can be locked: '{1}'", 90 name, valid_names, isAttribute ? "attribute" : "element", isAttribute ? "attributes" : "elements")); 91 } 92 Add(string name)93 public void Add (string name) 94 { 95 CheckName (name); 96 if (!names.Contains (name)) { 97 names.Add (name); 98 is_modified = true; 99 } 100 } 101 Clear()102 public void Clear () 103 { 104 names.Clear (); 105 is_modified = true; 106 } 107 Contains(string name)108 public bool Contains (string name) 109 { 110 return names.Contains (name); 111 } 112 CopyTo(string[] array, int index)113 public void CopyTo (string[] array, int index) 114 { 115 names.CopyTo (array, index); 116 } 117 GetEnumerator()118 public IEnumerator GetEnumerator() 119 { 120 return names.GetEnumerator (); 121 } 122 123 [MonoInternalNote ("we can't possibly *always* return false here...")] IsReadOnly(string name)124 public bool IsReadOnly (string name) 125 { 126 for (int i = 0; i < names.Count; i ++) { 127 if ((string)names[i] == name) { 128 /* this test used to switch off whether the collection was 'Exclude' or not 129 * (the LockAll*Except collections), but that doesn't seem to be the crux of 130 * it. maybe this returns true if the element/attribute is locked in a parent 131 * element's lock collections? */ 132 return false; 133 } 134 } 135 136 throw new ConfigurationErrorsException (String.Format ("The entry '{0}' is not in the collection.", name)); 137 } 138 Remove(string name)139 public void Remove (string name) 140 { 141 names.Remove (name); 142 is_modified = true; 143 } 144 SetFromList(string attributeList)145 public void SetFromList (string attributeList) 146 { 147 Clear (); 148 149 char [] split = {','}; 150 string [] attrs = attributeList.Split (split); 151 foreach (string a in attrs) { 152 Add (a.Trim ()); 153 } 154 } 155 ICollection.CopyTo(System.Array array, int index)156 void ICollection.CopyTo (System.Array array, int index) 157 { 158 names.CopyTo (array, index); 159 } 160 161 public string AttributeList { 162 get { 163 string[] name_arr = new string[names.Count]; 164 names.CopyTo (name_arr, 0); 165 return String.Join (",", name_arr); 166 } 167 } 168 169 public int Count { 170 get { return names.Count; } 171 } 172 173 [MonoTODO] 174 public bool HasParentElements { 175 get { return false; /* XXX */ } 176 } 177 178 [MonoTODO] 179 public bool IsModified { 180 get { return is_modified; } 181 internal set { is_modified = value; } 182 } 183 184 [MonoTODO] 185 public bool IsSynchronized { 186 get { return false; /* XXX */ } 187 } 188 189 [MonoTODO] 190 public object SyncRoot { 191 get { return this; /* XXX */ } 192 } 193 } 194 } 195 196