2 // System.Configuration.ConfigHelper
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
32 using System.Collections.Specialized;
37 #pragma warning disable 618
39 namespace System.Configuration
43 class CollectionWrapper
46 NameValueCollection collection;
49 public CollectionWrapper (IDictionary dict)
55 public CollectionWrapper (NameValueCollection collection)
57 this.collection = collection;
61 public void Remove (string s)
66 collection.Remove (s);
77 public string this [string key]
83 collection [key] = value;
87 public object UnWrap ()
96 internal static IDictionary GetDictionary (IDictionary prev,
103 hash = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
104 CaseInsensitiveComparer.Default);
106 Hashtable aux = (Hashtable) prev;
107 hash = (Hashtable) aux.Clone ();
110 CollectionWrapper result = new CollectionWrapper (hash);
111 result = GoGetThem (result, region, nameAtt, valueAtt);
115 return result.UnWrap () as IDictionary;
118 internal static ConfigNameValueCollection GetNameValueCollection (NameValueCollection prev,
123 ConfigNameValueCollection coll =
124 new ConfigNameValueCollection (CaseInsensitiveHashCodeProvider.Default,
125 CaseInsensitiveComparer.Default);
130 CollectionWrapper result = new CollectionWrapper (coll);
131 result = GoGetThem (result, region, nameAtt, valueAtt);
135 return result.UnWrap () as ConfigNameValueCollection;
138 private static CollectionWrapper GoGetThem (CollectionWrapper result,
143 if (region.Attributes != null && region.Attributes.Count != 0) {
144 if (region.Attributes.Count != 1 || region.Attributes[0].Name != "xmlns") {
145 throw new ConfigurationException ("Unknown attribute", region);
151 XmlNodeList childs = region.ChildNodes;
152 foreach (XmlNode node in childs) {
153 XmlNodeType ntype = node.NodeType;
154 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
157 if (ntype != XmlNodeType.Element)
158 throw new ConfigurationException ("Only XmlElement allowed", node);
160 string nodeName = node.Name;
161 if (nodeName == "clear") {
162 if (node.Attributes != null && node.Attributes.Count != 0)
163 throw new ConfigurationException ("Unknown attribute", node);
166 } else if (nodeName == "remove") {
168 if (node.Attributes != null)
169 keyNode = node.Attributes.RemoveNamedItem (nameAtt);
172 throw new ConfigurationException ("Required attribute not found",
174 if (keyNode.Value == String.Empty)
175 throw new ConfigurationException ("Required attribute is empty",
178 if (node.Attributes.Count != 0)
179 throw new ConfigurationException ("Unknown attribute", node);
181 result.Remove (keyNode.Value);
182 } else if (nodeName == "add") {
184 if (node.Attributes != null)
185 keyNode = node.Attributes.RemoveNamedItem (nameAtt);
188 throw new ConfigurationException ("Required attribute not found",
190 if (keyNode.Value == String.Empty)
191 throw new ConfigurationException ("Required attribute is empty",
194 valueNode = node.Attributes.RemoveNamedItem (valueAtt);
195 if (valueNode == null)
196 throw new ConfigurationException ("Required attribute not found",
199 if (node.Attributes.Count != 0)
200 throw new ConfigurationException ("Unknown attribute", node);
202 result [keyNode.Value] = valueNode.Value;
204 throw new ConfigurationException ("Unknown element", node);
213 internal class ConfigNameValueCollection: NameValueCollection
217 public ConfigNameValueCollection ()
221 public ConfigNameValueCollection (ConfigNameValueCollection col)
222 : base (col.Count, col)
226 public ConfigNameValueCollection (IHashCodeProvider hashProvider, IComparer comparer)
227 : base(hashProvider, comparer)
231 public void ResetModified ()
236 public bool IsModified {
237 get { return modified; }
240 public override void Set (string name, string value)
242 base.Set (name, value);