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 namespace System.Configuration
41 class CollectionWrapper
44 NameValueCollection collection;
47 public CollectionWrapper (IDictionary dict)
53 public CollectionWrapper (NameValueCollection collection)
55 this.collection = collection;
59 public void Remove (string s)
64 collection.Remove (s);
75 public string this [string key]
81 collection [key] = value;
85 public object UnWrap ()
94 internal static IDictionary GetDictionary (IDictionary prev,
101 hash = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
102 CaseInsensitiveComparer.Default);
104 Hashtable aux = (Hashtable) prev;
105 hash = (Hashtable) aux.Clone ();
108 CollectionWrapper result = new CollectionWrapper (hash);
109 result = GoGetThem (result, region, nameAtt, valueAtt);
113 return result.UnWrap () as IDictionary;
116 internal static ConfigNameValueCollection GetNameValueCollection (NameValueCollection prev,
121 ConfigNameValueCollection coll =
122 new ConfigNameValueCollection (CaseInsensitiveHashCodeProvider.Default,
123 CaseInsensitiveComparer.Default);
128 CollectionWrapper result = new CollectionWrapper (coll);
129 result = GoGetThem (result, region, nameAtt, valueAtt);
133 return result.UnWrap () as ConfigNameValueCollection;
136 private static CollectionWrapper GoGetThem (CollectionWrapper result,
141 if (region.Attributes != null && region.Attributes.Count != 0) {
142 if (region.Attributes.Count != 1 || region.Attributes[0].Name != "xmlns") {
143 throw new ConfigurationException ("Unknown attribute", region);
149 XmlNodeList childs = region.ChildNodes;
150 foreach (XmlNode node in childs) {
151 XmlNodeType ntype = node.NodeType;
152 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
155 if (ntype != XmlNodeType.Element)
156 throw new ConfigurationException ("Only XmlElement allowed", node);
158 string nodeName = node.Name;
159 if (nodeName == "clear") {
160 if (node.Attributes != null && node.Attributes.Count != 0)
161 throw new ConfigurationException ("Unknown attribute", node);
164 } else if (nodeName == "remove") {
166 if (node.Attributes != null)
167 keyNode = node.Attributes.RemoveNamedItem (nameAtt);
170 throw new ConfigurationException ("Required attribute not found",
172 if (keyNode.Value == String.Empty)
173 throw new ConfigurationException ("Required attribute is empty",
176 if (node.Attributes.Count != 0)
177 throw new ConfigurationException ("Unknown attribute", node);
179 result.Remove (keyNode.Value);
180 } else if (nodeName == "add") {
182 if (node.Attributes != null)
183 keyNode = node.Attributes.RemoveNamedItem (nameAtt);
186 throw new ConfigurationException ("Required attribute not found",
188 if (keyNode.Value == String.Empty)
189 throw new ConfigurationException ("Required attribute is empty",
192 valueNode = node.Attributes.RemoveNamedItem (valueAtt);
193 if (valueNode == null)
194 throw new ConfigurationException ("Required attribute not found",
197 if (node.Attributes.Count != 0)
198 throw new ConfigurationException ("Unknown attribute", node);
200 result [keyNode.Value] = valueNode.Value;
202 throw new ConfigurationException ("Unknown element", node);
211 internal class ConfigNameValueCollection: NameValueCollection
215 public ConfigNameValueCollection ()
219 public ConfigNameValueCollection (ConfigNameValueCollection col)
220 : base (col.Count, col)
224 public ConfigNameValueCollection (IHashCodeProvider hashProvider, IComparer comparer)
225 : base(hashProvider, comparer)
229 public void ResetModified ()
234 public bool IsModified {
235 get { return modified; }
238 public override void Set (string name, string value)
240 base.Set (name, value);