merge -r 53370:58178
[mono.git] / mcs / class / System / System.Configuration / ConfigurationSettings.cs
1 //
2 // System.Configuration.ConfigurationSettings.cs
3 //
4 // Author:
5 //   Christopher Podurgiel (cpodurgiel@msn.com)
6 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //   Eric Lindvall (eric@5stops.com)
8 //
9 // (c) Christopher Podurgiel
10 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
11 // (c) 2003 Novell, Inc. (http://www.novell.com)
12 //
13
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Collections;
37 using System.Collections.Specialized;
38 using System.IO;
39 using System.Runtime.CompilerServices;
40 #if (XML_DEP)
41 using System.Xml;
42 using System.Xml.XPath;
43 #endif
44
45 namespace System.Configuration
46 {
47         public sealed class ConfigurationSettings
48         {
49                 static IConfigurationSystem config = DefaultConfig.GetInstance ();
50                 static object lockobj = new object ();
51                 private ConfigurationSettings ()
52                 {
53                 }
54
55                 public static object GetConfig (string sectionName)
56                 {
57                         return config.GetConfig (sectionName);
58                 }
59
60 #if NET_2_0
61                 [Obsolete ("This property is obsolete.  Please use System.Configuration.ConfigurationManager.AppSettings")]
62 #endif
63                 public static NameValueCollection AppSettings
64                 {
65                         get {
66                                 object appSettings = GetConfig ("appSettings");
67                                 if (appSettings == null)
68                                         appSettings = new NameValueCollection ();
69
70                                 return (NameValueCollection) appSettings;
71                         }
72                 }
73
74                 // Invoked from System.Web
75                 static IConfigurationSystem ChangeConfigurationSystem (IConfigurationSystem newSystem)
76                 {
77                         if (newSystem == null)
78                                 throw new ArgumentNullException ("newSystem");
79
80                         lock (lockobj) {
81                                 IConfigurationSystem old = config;
82                                 config = newSystem;
83                                 return old;
84                         }
85                 }
86         }
87
88         //
89         // class DefaultConfig: read configuration from machine.config file and application
90         // config file if available.
91         //
92         class DefaultConfig : IConfigurationSystem
93         {
94                 static readonly DefaultConfig instance = new DefaultConfig ();
95                 ConfigurationData config;
96                 
97                 private DefaultConfig ()
98                 {
99                 }
100
101                 public static DefaultConfig GetInstance ()
102                 {
103                         return instance;
104                 }
105
106 #if NET_2_0
107                 [Obsolete ("This method is obsolete.  Please use System.Configuration.ConfigurationManager.GetConfig")]
108 #endif
109                 public object GetConfig (string sectionName)
110                 {
111                         Init ();
112                         return config.GetConfig (sectionName);
113                 }
114
115                 public void Init ()
116                 {
117                         lock (this) {
118                                 if (config != null)
119                                         return;
120
121                                 ConfigurationData data = new ConfigurationData ();
122                                 if (!data.Load (GetMachineConfigPath ()))
123                                         throw new ConfigurationException ("Cannot find " + GetMachineConfigPath ());
124
125                                 string appfile = GetAppConfigPath ();
126                                 if (appfile == null) {
127                                         config = data;
128                                         return;
129                                 }
130
131                                 ConfigurationData appData = new ConfigurationData (data);
132                                 if (appData.Load (appfile))
133                                         config = appData;
134                                 else
135                                         config = data;
136                         }
137                 }
138
139                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
140                 extern private static string get_machine_config_path ();
141
142                 internal static string GetMachineConfigPath ()
143                 {
144                         return get_machine_config_path ();
145                 }
146
147                 private static string GetAppConfigPath ()
148                 {
149                         AppDomainSetup currentInfo = AppDomain.CurrentDomain.SetupInformation;
150
151                         string configFile = currentInfo.ConfigurationFile;
152                         if (configFile == null || configFile.Length == 0)
153                                 return null;
154
155                         return configFile;
156
157                 }
158         }
159
160         enum AllowDefinition
161         {
162                 Everywhere,
163                 MachineOnly,
164                 MachineToApplication
165         }
166         
167         class SectionData
168         {
169                 public readonly string SectionName;
170                 public readonly string TypeName;
171                 public readonly bool AllowLocation;
172                 public readonly AllowDefinition AllowDefinition;
173                 public string FileName;
174
175                 public SectionData (string sectionName, string typeName,
176                                     bool allowLocation, AllowDefinition allowDefinition)
177                 {
178                         SectionName = sectionName;
179                         TypeName = typeName;
180                         AllowLocation = allowLocation;
181                         AllowDefinition = allowDefinition;
182                 }
183         }
184         
185
186         class ConfigurationData
187         {
188                 ConfigurationData parent;
189                 Hashtable factories;
190                 Hashtable pending;
191                 string fileName;
192                 static object removedMark = new object ();
193                 static object groupMark = new object ();
194                 static object emptyMark = new object ();
195                 Hashtable cache;
196
197                 Hashtable FileCache {
198                         get {
199                                 if (cache != null)
200                                         return cache;
201
202                                 cache = new Hashtable ();
203                                 return cache;
204                         }
205                 }
206
207                 public ConfigurationData () : this (null)
208                 {
209                 }
210
211                 public ConfigurationData (ConfigurationData parent)
212                 {
213                         this.parent = (parent == this) ? null : parent;
214                         factories = new Hashtable ();
215                 }
216
217                 public bool Load (string fileName)
218                 {
219                         this.fileName = fileName;
220                         if (fileName == null || !File.Exists (fileName))
221                                 return false;
222 #if (XML_DEP)
223                         XmlTextReader reader = null;
224
225                         try {
226                                 FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
227                                 reader = new XmlTextReader (fs);
228                                 InitRead (reader);
229                                 ReadConfigFile (reader);
230                         } catch (ConfigurationException) {
231                                 throw;
232                         } catch (Exception e) {
233                                 throw new ConfigurationException ("Error reading " + fileName, e);
234                         } finally {
235                                 if (reader != null)
236                                         reader.Close();
237                         }
238 #endif
239                         return true;
240                 }
241
242                 object GetHandler (string sectionName)
243                 {
244                         lock (factories) {
245                                 object o = factories [sectionName];
246                                 if (o == null || o == removedMark) {
247                                         if (parent != null)
248                                                 return parent.GetHandler (sectionName);
249
250                                         return null;
251                                 }
252
253                                 if (o is IConfigurationSectionHandler)
254                                         return (IConfigurationSectionHandler) o;
255
256                                 o = CreateNewHandler (sectionName, (SectionData) o);
257                                 factories [sectionName] = o;
258                                 return o;
259                         }
260                 }
261
262                 object CreateNewHandler (string sectionName, SectionData section)
263                 {
264                         Type t = Type.GetType (section.TypeName);
265                         if (t == null)
266                                 throw new ConfigurationException ("Cannot get Type for " + section.TypeName);
267
268 #if false
269                         Type iconfig = typeof (IConfigurationSectionHandler);
270                         if (!iconfig.IsAssignableFrom (t))
271                                 throw new ConfigurationException (sectionName + " does not implement " + iconfig);
272 #endif
273                         
274                         object o = Activator.CreateInstance (t, true);
275                         if (o == null)
276                                 throw new ConfigurationException ("Cannot get instance for " + t);
277
278                         return o;
279                 }
280 #if (XML_DEP)
281                 XmlDocument GetInnerDoc (XmlDocument doc, int i, string [] sectionPath)
282                 {
283                         if (++i >= sectionPath.Length)
284                                 return doc;
285
286                         if (doc.DocumentElement == null)
287                                 return null;
288
289                         XmlNode node = doc.DocumentElement.FirstChild;
290                         while (node != null) {
291                                 if (node.Name == sectionPath [i]) {
292                                         ConfigXmlDocument result = new ConfigXmlDocument ();
293                                         result.Load (new StringReader (node.OuterXml));
294                                         return GetInnerDoc (result, i, sectionPath);
295                                 }
296                                 node = node.NextSibling;
297                         }
298
299                         return null;
300                 }
301
302                 XmlDocument GetDocumentForSection (string sectionName)
303                 {
304                         ConfigXmlDocument doc = new ConfigXmlDocument ();
305                         if (pending == null)
306                                 return doc;
307
308                         string [] sectionPath = sectionName.Split ('/');
309                         string outerxml = pending [sectionPath [0]] as string;
310                         if (outerxml == null)
311                                 return doc;
312
313                         StringReader reader = new StringReader (outerxml);
314                         XmlTextReader rd = new XmlTextReader (reader);
315                         rd.MoveToContent ();
316                         doc.LoadSingleElement (fileName, rd);
317
318                         return GetInnerDoc (doc, 0, sectionPath);
319                 }
320                 
321                 object GetConfigInternal (string sectionName)
322                 {
323                         object handler = GetHandler (sectionName);
324                         IConfigurationSectionHandler iconf = handler as IConfigurationSectionHandler;
325                         if (iconf == null)
326                                 return handler;
327
328                         object parentConfig = null;
329                         if (parent != null)
330                                 parentConfig = parent.GetConfig (sectionName);
331
332                         XmlDocument doc = GetDocumentForSection (sectionName);
333                         if (doc == null || doc.DocumentElement == null)
334                                 return parentConfig;
335                         
336                         return iconf.Create (parentConfig, fileName, doc.DocumentElement);
337                 }
338 #else
339                 object GetConfigInternal (string sectionName)
340                 {
341                     return null;
342                 }
343 #endif
344                 public object GetConfig (string sectionName)
345                 {
346                         object config;
347                         lock (this) {
348                                 config = this.FileCache [sectionName];
349                         }
350
351                         if (config == emptyMark)
352                                 return null;
353
354                         if (config != null)
355                                 return config;
356
357                         lock (this) {
358                                 config = GetConfigInternal (sectionName);
359                                 this.FileCache [sectionName] = (config == null) ? emptyMark : config;
360                         }
361
362                         return config;
363                 }
364
365                 private object LookForFactory (string key)
366                 {
367                         object o = factories [key];
368                         if (o != null)
369                                 return o;
370
371                         if (parent != null)
372                                 return parent.LookForFactory (key);
373
374                         return null;
375                 }
376 #if (XML_DEP)
377                 private void InitRead (XmlTextReader reader)
378                 {
379                         reader.MoveToContent ();
380                         if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
381                                 ThrowException ("Configuration file does not have a valid root element", reader);
382
383                         if (reader.HasAttributes)
384                                 ThrowException ("Unrecognized attribute in root element", reader);
385
386                         MoveToNextElement (reader);
387                 }
388
389                 private void MoveToNextElement (XmlTextReader reader)
390                 {
391                         while (reader.Read ()) {
392                                 XmlNodeType ntype = reader.NodeType;
393                                 if (ntype == XmlNodeType.Element)
394                                         return;
395
396                                 if (ntype != XmlNodeType.Whitespace &&
397                                     ntype != XmlNodeType.Comment &&
398                                     ntype != XmlNodeType.SignificantWhitespace &&
399                                     ntype != XmlNodeType.EndElement)
400                                         ThrowException ("Unrecognized element", reader);
401                         }
402                 }
403
404                 private void ReadSection (XmlTextReader reader, string sectionName)
405                 {
406                         string attName;
407                         string nameValue = null;
408                         string typeValue = null;
409                         string allowLoc = null, allowDef = null;
410                         bool allowLocation = true;
411                         AllowDefinition allowDefinition = AllowDefinition.Everywhere;
412
413                         while (reader.MoveToNextAttribute ()) {
414                                 attName = reader.Name;
415                                 if (attName == null)
416                                         continue;
417
418                                 if (attName == "allowLocation") {
419                                         if (allowLoc != null)
420                                                 ThrowException ("Duplicated allowLocation attribute.", reader);
421
422                                         allowLoc = reader.Value;
423                                         allowLocation = (allowLoc == "true");
424                                         if (!allowLocation && allowLoc != "false")
425                                                 ThrowException ("Invalid attribute value", reader);
426
427                                         continue;
428                                 }
429
430                                 if (attName == "allowDefinition") {
431                                         if (allowDef != null)
432                                                 ThrowException ("Duplicated allowDefinition attribute.", reader);
433
434                                         allowDef = reader.Value;
435                                         try {
436                                                 allowDefinition = (AllowDefinition) Enum.Parse (
437                                                                    typeof (AllowDefinition), allowDef);
438                                         } catch {
439                                                 ThrowException ("Invalid attribute value", reader);
440                                         }
441
442                                         continue;
443                                 }
444
445                                 if (attName == "type")  {
446                                         if (typeValue != null)
447                                                 ThrowException ("Duplicated type attribute.", reader);
448                                         typeValue = reader.Value;
449                                         continue;
450                                 }
451                                 
452                                 if (attName == "name")  {
453                                         if (nameValue != null)
454                                                 ThrowException ("Duplicated name attribute.", reader);
455                                         nameValue = reader.Value;
456                                         if (nameValue == "location")
457                                                 ThrowException ("location is a reserved section name", reader);
458                                         continue;
459                                 }
460
461                                 ThrowException ("Unrecognized attribute.", reader);
462                         }
463
464                         if (nameValue == null || typeValue == null)
465                                 ThrowException ("Required attribute missing", reader);
466
467                         if (sectionName != null)
468                                 nameValue = sectionName + '/' + nameValue;
469
470                         reader.MoveToElement();
471                         object o = LookForFactory (nameValue);
472                         if (o != null && o != removedMark)
473                                 ThrowException ("Already have a factory for " + nameValue, reader);
474                         SectionData section = new SectionData (nameValue, typeValue, allowLocation, allowDefinition);
475                         section.FileName = fileName;
476                         factories [nameValue] = section;
477
478                         MoveToNextElement (reader);
479                 }
480
481                 private void ReadRemoveSection (XmlTextReader reader, string sectionName)
482                 {
483                         if (!reader.MoveToNextAttribute () || reader.Name != "name")
484                                 ThrowException ("Unrecognized attribute.", reader);
485
486                         string removeValue = reader.Value;
487                         if (removeValue == null || removeValue.Length == 0)
488                                 ThrowException ("Empty name to remove", reader);
489
490                         reader.MoveToElement ();
491
492                         if (sectionName != null)
493                                 removeValue = sectionName + '/' + removeValue;
494
495                         object o = LookForFactory (removeValue);
496                         if (o != null && o == removedMark)
497                                 ThrowException ("No factory for " + removeValue, reader);
498
499                         factories [removeValue] = removedMark;
500                         MoveToNextElement (reader);
501                 }
502
503                 private void ReadSectionGroup (XmlTextReader reader, string configSection)
504                 {
505                         if (!reader.MoveToNextAttribute ())
506                                 ThrowException ("sectionGroup must have a 'name' attribute.", reader);
507
508                         string value = null;
509                         do {
510                                 if (reader.Name == "name") {
511                                         if (value != null)
512                                                 ThrowException ("Duplicate 'name' attribute.", reader);
513                                         value = reader.Value;
514                                 }
515                                 else 
516 #if NET_2_0
517                                 if (reader.Name != "type")
518 #endif
519                                         ThrowException ("Unrecognized attribute.", reader);
520                         } while (reader.MoveToNextAttribute ());
521
522                         if (value == null)
523                                 ThrowException ("No 'name' attribute.", reader);
524
525                         if (value == "location")
526                                 ThrowException ("location is a reserved section name", reader);
527                         
528                         if (configSection != null)
529                                 value = configSection + '/' + value;
530
531                         object o = LookForFactory (value);
532                         if (o != null && o != removedMark && o != groupMark)
533                                 ThrowException ("Already have a factory for " + value, reader);
534
535                         factories [value] = groupMark;
536                         MoveToNextElement (reader);
537                         ReadSections (reader, value);
538                 }
539
540                 private void ReadSections (XmlTextReader reader, string configSection)
541                 {
542                         int depth = reader.Depth;
543                         while (reader.Depth == depth) {
544                                 string name = reader.Name;
545                                 if (name == "section") {
546                                         ReadSection (reader, configSection);
547                                         continue;
548                                 } 
549                                 
550                                 if (name == "remove") {
551                                         ReadRemoveSection (reader, configSection);
552                                         continue;
553                                 }
554
555                                 if (name == "clear") {
556                                         if (reader.HasAttributes)
557                                                 ThrowException ("Unrecognized attribute.", reader);
558
559                                         factories.Clear ();
560                                         MoveToNextElement (reader);
561                                         continue;
562                                 }
563
564                                 if (name == "sectionGroup") {
565                                         ReadSectionGroup (reader, configSection);
566                                         continue;
567                                 }
568                                 
569
570                                 ThrowException ("Unrecognized element: " + reader.Name, reader);
571                         }
572                 }
573
574                 void StorePending (string name, XmlTextReader reader)
575                 {
576                         if (pending == null)
577                                 pending = new Hashtable ();
578
579                         pending [name] = reader.ReadOuterXml ();
580                 }
581
582                 private void ReadConfigFile (XmlTextReader reader)
583                 {
584                         int depth = reader.Depth;
585                         while (!reader.EOF && reader.Depth == depth) {
586                                 string name = reader.Name;
587                                 if (name == "configSections") {
588                                         if (reader.HasAttributes)
589                                                 ThrowException ("Unrecognized attribute in <configSections>.", reader);
590
591                                         MoveToNextElement (reader);
592                                         if (reader.Depth > depth)
593                                                 ReadSections (reader, null);
594                                 } else if (name != null && name != "") {
595                                         StorePending (name, reader);
596                                         MoveToNextElement (reader);
597                                 } else {
598                                         MoveToNextElement (reader);
599                                 }
600                         }
601                 }
602                                 
603                 private void ThrowException (string text, XmlTextReader reader)
604                 {
605                         throw new ConfigurationException (text, fileName, reader.LineNumber);
606                 }
607 #endif
608         }
609 }
610
611