This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 namespace System.Configuration
38 {
39         class ConfigHelper
40         {
41                 class CollectionWrapper
42                 {
43                         IDictionary dict;
44                         NameValueCollection collection;
45                         bool isDict;
46
47                         public CollectionWrapper (IDictionary dict)
48                         {
49                                 this.dict = dict;
50                                 isDict = true;
51                         }
52
53                         public CollectionWrapper (NameValueCollection collection)
54                         {
55                                 this.collection = collection;
56                                 isDict = false;
57                         }
58
59                         public void Remove (string s)
60                         {
61                                 if (isDict)
62                                         dict.Remove (s);
63                                 else
64                                         collection.Remove (s);
65                         }
66
67                         public void Clear ()
68                         {
69                                 if (isDict)
70                                         dict.Clear ();
71                                 else
72                                         collection.Clear ();
73                         }
74
75                         public string this [string key]
76                         {
77                                 set {
78                                         if (isDict)
79                                                 dict [key] = value;
80                                         else
81                                                 collection [key] = value;
82                                 }
83                         }
84
85                         public object UnWrap ()
86                         {
87                                 if (isDict)
88                                         return dict;
89                                 else
90                                         return collection;
91                         }
92                 }
93 #if (XML_DEP)
94                 internal static IDictionary GetDictionary (IDictionary prev,
95                                                            XmlNode region,
96                                                            string nameAtt,
97                                                            string valueAtt)
98                 {
99                         Hashtable hash;
100                         if (prev == null)
101                                 hash = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
102                                                       CaseInsensitiveComparer.Default);
103                         else {
104                                 Hashtable aux = (Hashtable) prev;
105                                 hash = (Hashtable) aux.Clone ();
106                         }
107
108                         CollectionWrapper result = new CollectionWrapper (hash);
109                         result = GoGetThem (result, region, nameAtt, valueAtt);
110                         if (result == null)
111                                 return null;
112
113                         return result.UnWrap () as IDictionary;
114                 }
115
116                 internal static NameValueCollection GetNameValueCollection (NameValueCollection prev,
117                                                                             XmlNode region,
118                                                                             string nameAtt,
119                                                                             string valueAtt)
120                 {
121                         NameValueCollection coll =
122                                         new NameValueCollection (CaseInsensitiveHashCodeProvider.Default,
123                                                                  CaseInsensitiveComparer.Default);
124
125                         if (prev != null)
126                                 coll.Add (prev);
127
128                         CollectionWrapper result = new CollectionWrapper (coll);
129                         result = GoGetThem (result, region, nameAtt, valueAtt);
130                         if (result == null)
131                                 return null;
132
133                         return result.UnWrap () as NameValueCollection;
134                 }
135
136                 private static CollectionWrapper GoGetThem (CollectionWrapper result,
137                                                             XmlNode region,
138                                                             string nameAtt,
139                                                             string valueAtt)
140                 {
141                         if (region.Attributes != null && region.Attributes.Count != 0)
142                                 throw new ConfigurationException ("Unknown attribute", region);
143
144                         XmlNode keyNode;
145                         XmlNode valueNode;
146                         XmlNodeList childs = region.ChildNodes;
147                         foreach (XmlNode node in childs) {
148                                 XmlNodeType ntype = node.NodeType;
149                                 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
150                                         continue;
151
152                                 if (ntype != XmlNodeType.Element)
153                                         throw new ConfigurationException ("Only XmlElement allowed", node);
154                                         
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);
159
160                                         result.Clear ();
161                                 } else if (nodeName == "remove") {
162                                         keyNode = null;
163                                         if (node.Attributes != null)
164                                                 keyNode = node.Attributes.RemoveNamedItem (nameAtt);
165
166                                         if (keyNode == null)
167                                                 throw new ConfigurationException ("Required attribute not found",
168                                                                                   node);
169                                         if (keyNode.Value == String.Empty)
170                                                 throw new ConfigurationException ("Required attribute is empty",
171                                                                                   node);
172
173                                         if (node.Attributes.Count != 0)
174                                                 throw new ConfigurationException ("Unknown attribute", node);
175                                         
176                                         result.Remove (keyNode.Value);
177                                 } else if (nodeName == "add") {
178                                         keyNode = null;
179                                         if (node.Attributes != null)
180                                                 keyNode = node.Attributes.RemoveNamedItem (nameAtt);
181
182                                         if (keyNode == null)
183                                                 throw new ConfigurationException ("Required attribute not found",
184                                                                                   node);
185                                         if (keyNode.Value == String.Empty)
186                                                 throw new ConfigurationException ("Required attribute is empty",
187                                                                                   node);
188
189                                         valueNode = node.Attributes.RemoveNamedItem (valueAtt);
190                                         if (valueNode == null)
191                                                 throw new ConfigurationException ("Required attribute not found",
192                                                                                   node);
193
194                                         if (node.Attributes.Count != 0)
195                                                 throw new ConfigurationException ("Unknown attribute", node);
196
197                                         result [keyNode.Value] = valueNode.Value;
198                                 } else {
199                                         throw new ConfigurationException ("Unknown element", node);
200                                 }
201                         }
202
203                         return result;
204                 }
205 #endif
206         }
207
208 }