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 throw new ConfigurationException ("Unknown attribute", region);
146 XmlNodeList childs = region.ChildNodes;
147 foreach (XmlNode node in childs) {
148 XmlNodeType ntype = node.NodeType;
149 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
152 if (ntype != XmlNodeType.Element)
153 throw new ConfigurationException ("Only XmlElement allowed", node);
155 string nodeName = node.Name;
156 if (nodeName == "clear") {
157 if (node.Attributes != null && node.Attributes.Count != 0)
158 throw new ConfigurationException ("Unknown attribute", node);
161 } else if (nodeName == "remove") {
163 if (node.Attributes != null)
164 keyNode = node.Attributes.RemoveNamedItem (nameAtt);
167 throw new ConfigurationException ("Required attribute not found",
169 if (keyNode.Value == String.Empty)
170 throw new ConfigurationException ("Required attribute is empty",
173 if (node.Attributes.Count != 0)
174 throw new ConfigurationException ("Unknown attribute", node);
176 result.Remove (keyNode.Value);
177 } else if (nodeName == "add") {
179 if (node.Attributes != null)
180 keyNode = node.Attributes.RemoveNamedItem (nameAtt);
183 throw new ConfigurationException ("Required attribute not found",
185 if (keyNode.Value == String.Empty)
186 throw new ConfigurationException ("Required attribute is empty",
189 valueNode = node.Attributes.RemoveNamedItem (valueAtt);
190 if (valueNode == null)
191 throw new ConfigurationException ("Required attribute not found",
194 if (node.Attributes.Count != 0)
195 throw new ConfigurationException ("Unknown attribute", node);
197 result [keyNode.Value] = valueNode.Value;
199 throw new ConfigurationException ("Unknown element", node);
208 internal class ConfigNameValueCollection: NameValueCollection
212 public ConfigNameValueCollection ()
216 public ConfigNameValueCollection (ConfigNameValueCollection col)
217 : base (col.Count, col)
221 public ConfigNameValueCollection (IHashCodeProvider hashProvider, IComparer comparer)
222 : base(hashProvider, comparer)
226 public void ResetModified ()
231 public bool IsModified {
232 get { return modified; }
235 public override void Set (string name, string value)
237 base.Set (name, value);