Merge pull request #3800 from madewokherd/mingwbuild
[mono.git] / mcs / class / System / System.Configuration / ConfigHelper.cs
1 //
2 // System.Configuration.ConfigHelper
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.Collections;
32 using System.Collections.Specialized;
33 #if (XML_DEP)
34 using System.Xml;
35 #endif
36
37 #pragma warning disable 618
38
39 namespace System.Configuration
40 {
41         class ConfigHelper
42         {
43                 class CollectionWrapper
44                 {
45                         IDictionary dict;
46                         NameValueCollection collection;
47                         bool isDict;
48
49                         public CollectionWrapper (IDictionary dict)
50                         {
51                                 this.dict = dict;
52                                 isDict = true;
53                         }
54
55                         public CollectionWrapper (NameValueCollection collection)
56                         {
57                                 this.collection = collection;
58                                 isDict = false;
59                         }
60
61                         public void Remove (string s)
62                         {
63                                 if (isDict)
64                                         dict.Remove (s);
65                                 else
66                                         collection.Remove (s);
67                         }
68
69                         public void Clear ()
70                         {
71                                 if (isDict)
72                                         dict.Clear ();
73                                 else
74                                         collection.Clear ();
75                         }
76
77                         public string this [string key]
78                         {
79                                 set {
80                                         if (isDict)
81                                                 dict [key] = value;
82                                         else
83                                                 collection [key] = value;
84                                 }
85                         }
86
87                         public object UnWrap ()
88                         {
89                                 if (isDict)
90                                         return dict;
91                                 else
92                                         return collection;
93                         }
94                 }
95 #if (XML_DEP)
96                 internal static IDictionary GetDictionary (IDictionary prev,
97                                                            XmlNode region,
98                                                            string nameAtt,
99                                                            string valueAtt)
100                 {
101                         Hashtable hash;
102                         if (prev == null)
103                                 hash = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
104                                                       CaseInsensitiveComparer.Default);
105                         else {
106                                 Hashtable aux = (Hashtable) prev;
107                                 hash = (Hashtable) aux.Clone ();
108                         }
109
110                         CollectionWrapper result = new CollectionWrapper (hash);
111                         result = GoGetThem (result, region, nameAtt, valueAtt);
112                         if (result == null)
113                                 return null;
114
115                         return result.UnWrap () as IDictionary;
116                 }
117
118                 internal static ConfigNameValueCollection GetNameValueCollection (NameValueCollection prev,
119                                                                             XmlNode region,
120                                                                             string nameAtt,
121                                                                             string valueAtt)
122                 {
123                         ConfigNameValueCollection coll =
124                                         new ConfigNameValueCollection (CaseInsensitiveHashCodeProvider.Default,
125                                                                  CaseInsensitiveComparer.Default);
126
127                         if (prev != null)
128                                 coll.Add (prev);
129
130                         CollectionWrapper result = new CollectionWrapper (coll);
131                         result = GoGetThem (result, region, nameAtt, valueAtt);
132                         if (result == null)
133                                 return null;
134
135                         return result.UnWrap () as ConfigNameValueCollection;
136                 }
137
138                 private static CollectionWrapper GoGetThem (CollectionWrapper result,
139                                                             XmlNode region,
140                                                             string nameAtt,
141                                                             string valueAtt)
142                 {
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);
146                                 }
147                         }
148
149                         XmlNode keyNode;
150                         XmlNode valueNode;
151                         XmlNodeList childs = region.ChildNodes;
152                         foreach (XmlNode node in childs) {
153                                 XmlNodeType ntype = node.NodeType;
154                                 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
155                                         continue;
156
157                                 if (ntype != XmlNodeType.Element)
158                                         throw new ConfigurationException ("Only XmlElement allowed", node);
159                                         
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);
164
165                                         result.Clear ();
166                                 } else if (nodeName == "remove") {
167                                         keyNode = null;
168                                         if (node.Attributes != null)
169                                                 keyNode = node.Attributes.RemoveNamedItem (nameAtt);
170
171                                         if (keyNode == null)
172                                                 throw new ConfigurationException ("Required attribute not found",
173                                                                                   node);
174                                         if (keyNode.Value == String.Empty)
175                                                 throw new ConfigurationException ("Required attribute is empty",
176                                                                                   node);
177
178                                         if (node.Attributes.Count != 0)
179                                                 throw new ConfigurationException ("Unknown attribute", node);
180                                         
181                                         result.Remove (keyNode.Value);
182                                 } else if (nodeName == "add") {
183                                         keyNode = null;
184                                         if (node.Attributes != null)
185                                                 keyNode = node.Attributes.RemoveNamedItem (nameAtt);
186
187                                         if (keyNode == null)
188                                                 throw new ConfigurationException ("Required attribute not found",
189                                                                                   node);
190                                         if (keyNode.Value == String.Empty)
191                                                 throw new ConfigurationException ("Required attribute is empty",
192                                                                                   node);
193
194                                         valueNode = node.Attributes.RemoveNamedItem (valueAtt);
195                                         if (valueNode == null)
196                                                 throw new ConfigurationException ("Required attribute not found",
197                                                                                   node);
198
199                                         if (node.Attributes.Count != 0)
200                                                 throw new ConfigurationException ("Unknown attribute", node);
201
202                                         result [keyNode.Value] = valueNode.Value;
203                                 } else {
204                                         throw new ConfigurationException ("Unknown element", node);
205                                 }
206                         }
207
208                         return result;
209                 }
210 #endif
211         }
212         
213         internal class ConfigNameValueCollection: NameValueCollection
214         {
215                 bool modified;
216                 
217                 public ConfigNameValueCollection ()
218                 {
219                 }
220                 
221                 public ConfigNameValueCollection (ConfigNameValueCollection col)
222                 : base (col.Count, col)
223                 {
224                 }
225                 
226                 public ConfigNameValueCollection (IHashCodeProvider hashProvider, IComparer comparer)
227                         : base(hashProvider, comparer)
228                 {
229                 }
230                 
231                 public void ResetModified ()
232                 {
233                         modified = false;
234                 }
235                 
236                 public bool IsModified {
237                         get { return modified; }
238                 }
239                 
240                 public override void Set (string name, string value)
241                 {
242                         base.Set (name, value);
243                         modified = true;
244                 }
245         }
246
247 }