* System.dll.sources: Added Win32EventLog.cs.
[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 #if CONFIGURATION_DEP
36 extern alias PrebuiltSystem;
37 using NameValueCollection = PrebuiltSystem.System.Collections.Specialized.NameValueCollection;
38 #endif
39
40 using System;
41 using System.Collections;
42 using System.Collections.Specialized;
43 using System.IO;
44 using System.Runtime.CompilerServices;
45 using System.Security.Permissions;
46 #if (XML_DEP)
47 using System.Xml;
48 using System.Xml.XPath;
49 #endif
50
51 namespace System.Configuration
52 {
53         public sealed class ConfigurationSettings
54         {
55                 static IConfigurationSystem config = DefaultConfig.GetInstance ();
56                 static object lockobj = new object ();
57                 private ConfigurationSettings ()
58                 {
59                 }
60
61 #if NET_2_0
62                 [Obsolete ("This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.GetSection")]
63 #endif
64                 public static object GetConfig (string sectionName)
65                 {
66                         return config.GetConfig (sectionName);
67                 }
68
69 #if NET_2_0
70                 [Obsolete ("This property is obsolete.  Please use System.Configuration.ConfigurationManager.AppSettings")]
71 #endif
72                 public static NameValueCollection AppSettings
73                 {
74                         get {
75 #if NET_2_0 && CONFIGURATION_DEP
76                                 object appSettings = ConfigurationManager.GetSection ("appSettings");
77 #else
78                                 object appSettings = GetConfig ("appSettings");
79 #endif
80                                 if (appSettings == null)
81                                         appSettings = new NameValueCollection ();
82                                 return (NameValueCollection) appSettings;
83                         }
84                 }
85
86                 // Invoked from System.Web, disable warning
87                 internal static IConfigurationSystem ChangeConfigurationSystem (IConfigurationSystem newSystem)
88                 {
89                         if (newSystem == null)
90                                 throw new ArgumentNullException ("newSystem");
91
92                         lock (lockobj) {
93                                 IConfigurationSystem old = config;
94                                 config = newSystem;
95                                 return old;
96                         }
97                 }
98         }
99
100         //
101         // class DefaultConfig: read configuration from machine.config file and application
102         // config file if available.
103         //
104         class DefaultConfig : IConfigurationSystem
105         {
106                 static readonly DefaultConfig instance = new DefaultConfig ();
107                 ConfigurationData config;
108                 
109                 private DefaultConfig ()
110                 {
111                 }
112
113                 public static DefaultConfig GetInstance ()
114                 {
115                         return instance;
116                 }
117
118 #if NET_2_0
119                 [Obsolete ("This method is obsolete.  Please use System.Configuration.ConfigurationManager.GetConfig")]
120 #endif
121                 public object GetConfig (string sectionName)
122                 {
123                         Init ();
124                         return config.GetConfig (sectionName);
125                 }
126
127                 public void Init ()
128                 {
129                         lock (this) {
130                                 if (config != null)
131                                         return;
132
133                                 ConfigurationData data = new ConfigurationData ();
134                                 if (!data.Load (GetMachineConfigPath ()))
135                                         throw new ConfigurationException ("Cannot find " + GetMachineConfigPath ());
136
137                                 string appfile = GetAppConfigPath ();
138                                 if (appfile == null) {
139                                         config = data;
140                                         return;
141                                 }
142
143                                 ConfigurationData appData = new ConfigurationData (data);
144                                 if (appData.Load (appfile))
145                                         config = appData;
146                                 else
147                                         config = data;
148                         }
149                 }
150
151                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
152                 extern private static string get_machine_config_path ();
153
154                 internal static string GetMachineConfigPath ()
155                 {
156                         return get_machine_config_path ();
157                 }
158
159                 private static string GetAppConfigPath ()
160                 {
161                         AppDomainSetup currentInfo = AppDomain.CurrentDomain.SetupInformation;
162
163                         string configFile = currentInfo.ConfigurationFile;
164                         if (configFile == null || configFile.Length == 0)
165                                 return null;
166
167                         return configFile;
168
169                 }
170         }
171
172         enum AllowDefinition
173         {
174                 Everywhere,
175                 MachineOnly,
176                 MachineToApplication
177         }
178         
179         class SectionData
180         {
181                 public readonly string SectionName;
182                 public readonly string TypeName;
183                 public readonly bool AllowLocation;
184                 public readonly AllowDefinition AllowDefinition;
185                 public string FileName;
186
187                 public SectionData (string sectionName, string typeName,
188                                     bool allowLocation, AllowDefinition allowDefinition)
189                 {
190                         SectionName = sectionName;
191                         TypeName = typeName;
192                         AllowLocation = allowLocation;
193                         AllowDefinition = allowDefinition;
194                 }
195         }
196         
197
198         class ConfigurationData
199         {
200                 ConfigurationData parent;
201                 Hashtable factories;
202                 Hashtable pending;
203                 string fileName;
204                 static object removedMark = new object ();
205                 static object groupMark = new object ();
206                 static object emptyMark = new object ();
207                 Hashtable cache;
208
209                 Hashtable FileCache {
210                         get {
211                                 if (cache != null)
212                                         return cache;
213
214                                 cache = new Hashtable ();
215                                 return cache;
216                         }
217                 }
218
219                 public ConfigurationData () : this (null)
220                 {
221                 }
222
223                 public ConfigurationData (ConfigurationData parent)
224                 {
225                         this.parent = (parent == this) ? null : parent;
226                         factories = new Hashtable ();
227                 }
228
229                 // SECURITY-FIXME: limit this with an imperative assert for reading the specific file
230                 [FileIOPermission (SecurityAction.Assert, Unrestricted = true)]
231                 public bool Load (string fileName)
232                 {
233                         this.fileName = fileName;
234                         if (fileName == null || !File.Exists (fileName))
235                                 return false;
236 #if (XML_DEP)
237                         XmlTextReader reader = null;
238
239                         try {
240                                 FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
241                                 reader = new XmlTextReader (fs);
242                                 InitRead (reader);
243                                 ReadConfigFile (reader);
244                         } catch (ConfigurationException) {
245                                 throw;
246                         } catch (Exception e) {
247                                 throw new ConfigurationException ("Error reading " + fileName, e);
248                         } finally {
249                                 if (reader != null)
250                                         reader.Close();
251                         }
252 #endif
253                         return true;
254                 }
255
256                 object GetHandler (string sectionName)
257                 {
258                         lock (factories) {
259                                 object o = factories [sectionName];
260                                 if (o == null || o == removedMark) {
261                                         if (parent != null)
262                                                 return parent.GetHandler (sectionName);
263
264                                         return null;
265                                 }
266
267                                 if (o is IConfigurationSectionHandler)
268                                         return (IConfigurationSectionHandler) o;
269
270                                 o = CreateNewHandler (sectionName, (SectionData) o);
271                                 factories [sectionName] = o;
272                                 return o;
273                         }
274                 }
275
276                 object CreateNewHandler (string sectionName, SectionData section)
277                 {
278                         Type t = Type.GetType (section.TypeName);
279                         if (t == null)
280                                 throw new ConfigurationException ("Cannot get Type for " + section.TypeName);
281
282 #if false
283                         Type iconfig = typeof (IConfigurationSectionHandler);
284                         if (!iconfig.IsAssignableFrom (t))
285                                 throw new ConfigurationException (sectionName + " does not implement " + iconfig);
286 #endif
287                         
288                         object o = Activator.CreateInstance (t, true);
289                         if (o == null)
290                                 throw new ConfigurationException ("Cannot get instance for " + t);
291
292                         return o;
293                 }
294 #if (XML_DEP)
295                 XmlDocument GetInnerDoc (XmlDocument doc, int i, string [] sectionPath)
296                 {
297                         if (++i >= sectionPath.Length)
298                                 return doc;
299
300                         if (doc.DocumentElement == null)
301                                 return null;
302
303                         XmlNode node = doc.DocumentElement.FirstChild;
304                         while (node != null) {
305                                 if (node.Name == sectionPath [i]) {
306                                         ConfigXmlDocument result = new ConfigXmlDocument ();
307                                         result.Load (new StringReader (node.OuterXml));
308                                         return GetInnerDoc (result, i, sectionPath);
309                                 }
310                                 node = node.NextSibling;
311                         }
312
313                         return null;
314                 }
315
316                 XmlDocument GetDocumentForSection (string sectionName)
317                 {
318                         ConfigXmlDocument doc = new ConfigXmlDocument ();
319                         if (pending == null)
320                                 return doc;
321
322                         string [] sectionPath = sectionName.Split ('/');
323                         string outerxml = pending [sectionPath [0]] as string;
324                         if (outerxml == null)
325                                 return doc;
326
327                         StringReader reader = new StringReader (outerxml);
328                         XmlTextReader rd = new XmlTextReader (reader);
329                         rd.MoveToContent ();
330                         doc.LoadSingleElement (fileName, rd);
331
332                         return GetInnerDoc (doc, 0, sectionPath);
333                 }
334                 
335                 object GetConfigInternal (string sectionName)
336                 {
337                         object handler = GetHandler (sectionName);
338                         IConfigurationSectionHandler iconf = handler as IConfigurationSectionHandler;
339                         if (iconf == null)
340                                 return handler;
341
342                         object parentConfig = null;
343                         if (parent != null)
344                                 parentConfig = parent.GetConfig (sectionName);
345
346                         XmlDocument doc = GetDocumentForSection (sectionName);
347                         if (doc == null || doc.DocumentElement == null)
348                                 return parentConfig;
349                         
350                         return iconf.Create (parentConfig, fileName, doc.DocumentElement);
351                 }
352 #else
353                 object GetConfigInternal (string sectionName)
354                 {
355                     return null;
356                 }
357 #endif
358                 public object GetConfig (string sectionName)
359                 {
360                         object config;
361                         lock (this) {
362                                 config = this.FileCache [sectionName];
363                         }
364
365                         if (config == emptyMark)
366                                 return null;
367
368                         if (config != null)
369                                 return config;
370
371                         lock (this) {
372                                 config = GetConfigInternal (sectionName);
373                                 this.FileCache [sectionName] = (config == null) ? emptyMark : config;
374                         }
375
376                         return config;
377                 }
378
379                 private object LookForFactory (string key)
380                 {
381                         object o = factories [key];
382                         if (o != null)
383                                 return o;
384
385                         if (parent != null)
386                                 return parent.LookForFactory (key);
387
388                         return null;
389                 }
390 #if (XML_DEP)
391                 private void InitRead (XmlTextReader reader)
392                 {
393                         reader.MoveToContent ();
394                         if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
395                                 ThrowException ("Configuration file does not have a valid root element", reader);
396
397                         if (reader.HasAttributes)
398                                 ThrowException ("Unrecognized attribute in root element", reader);
399
400                         MoveToNextElement (reader);
401                 }
402
403                 private void MoveToNextElement (XmlTextReader reader)
404                 {
405                         while (reader.Read ()) {
406                                 XmlNodeType ntype = reader.NodeType;
407                                 if (ntype == XmlNodeType.Element)
408                                         return;
409
410                                 if (ntype != XmlNodeType.Whitespace &&
411                                     ntype != XmlNodeType.Comment &&
412                                     ntype != XmlNodeType.SignificantWhitespace &&
413                                     ntype != XmlNodeType.EndElement)
414                                         ThrowException ("Unrecognized element", reader);
415                         }
416                 }
417
418                 private void ReadSection (XmlTextReader reader, string sectionName)
419                 {
420                         string attName;
421                         string nameValue = null;
422                         string typeValue = null;
423                         string allowLoc = null, allowDef = null;
424                         bool allowLocation = true;
425                         AllowDefinition allowDefinition = AllowDefinition.Everywhere;
426
427                         while (reader.MoveToNextAttribute ()) {
428                                 attName = reader.Name;
429                                 if (attName == null)
430                                         continue;
431
432                                 if (attName == "allowLocation") {
433                                         if (allowLoc != null)
434                                                 ThrowException ("Duplicated allowLocation attribute.", reader);
435
436                                         allowLoc = reader.Value;
437                                         allowLocation = (allowLoc == "true");
438                                         if (!allowLocation && allowLoc != "false")
439                                                 ThrowException ("Invalid attribute value", reader);
440
441                                         continue;
442                                 }
443
444                                 if (attName == "allowDefinition") {
445                                         if (allowDef != null)
446                                                 ThrowException ("Duplicated allowDefinition attribute.", reader);
447
448                                         allowDef = reader.Value;
449                                         try {
450                                                 allowDefinition = (AllowDefinition) Enum.Parse (
451                                                                    typeof (AllowDefinition), allowDef);
452                                         } catch {
453                                                 ThrowException ("Invalid attribute value", reader);
454                                         }
455
456                                         continue;
457                                 }
458
459                                 if (attName == "type")  {
460                                         if (typeValue != null)
461                                                 ThrowException ("Duplicated type attribute.", reader);
462                                         typeValue = reader.Value;
463                                         continue;
464                                 }
465                                 
466                                 if (attName == "name")  {
467                                         if (nameValue != null)
468                                                 ThrowException ("Duplicated name attribute.", reader);
469                                         nameValue = reader.Value;
470                                         if (nameValue == "location")
471                                                 ThrowException ("location is a reserved section name", reader);
472                                         continue;
473                                 }
474
475                                 ThrowException ("Unrecognized attribute.", reader);
476                         }
477
478                         if (nameValue == null || typeValue == null)
479                                 ThrowException ("Required attribute missing", reader);
480
481                         if (sectionName != null)
482                                 nameValue = sectionName + '/' + nameValue;
483
484                         reader.MoveToElement();
485                         object o = LookForFactory (nameValue);
486                         if (o != null && o != removedMark)
487                                 ThrowException ("Already have a factory for " + nameValue, reader);
488                         SectionData section = new SectionData (nameValue, typeValue, allowLocation, allowDefinition);
489                         section.FileName = fileName;
490                         factories [nameValue] = section;
491
492                         MoveToNextElement (reader);
493                 }
494
495                 private void ReadRemoveSection (XmlTextReader reader, string sectionName)
496                 {
497                         if (!reader.MoveToNextAttribute () || reader.Name != "name")
498                                 ThrowException ("Unrecognized attribute.", reader);
499
500                         string removeValue = reader.Value;
501                         if (removeValue == null || removeValue.Length == 0)
502                                 ThrowException ("Empty name to remove", reader);
503
504                         reader.MoveToElement ();
505
506                         if (sectionName != null)
507                                 removeValue = sectionName + '/' + removeValue;
508
509                         object o = LookForFactory (removeValue);
510                         if (o != null && o == removedMark)
511                                 ThrowException ("No factory for " + removeValue, reader);
512
513                         factories [removeValue] = removedMark;
514                         MoveToNextElement (reader);
515                 }
516
517                 private void ReadSectionGroup (XmlTextReader reader, string configSection)
518                 {
519                         if (!reader.MoveToNextAttribute ())
520                                 ThrowException ("sectionGroup must have a 'name' attribute.", reader);
521
522                         string value = null;
523                         do {
524                                 if (reader.Name == "name") {
525                                         if (value != null)
526                                                 ThrowException ("Duplicate 'name' attribute.", reader);
527                                         value = reader.Value;
528                                 }
529                                 else 
530 #if NET_2_0 && CONFIGURATION_2_0
531                                 if (reader.Name != "type")
532 #endif
533                                         ThrowException ("Unrecognized attribute.", reader);
534                         } while (reader.MoveToNextAttribute ());
535
536                         if (value == null)
537                                 ThrowException ("No 'name' attribute.", reader);
538
539                         if (value == "location")
540                                 ThrowException ("location is a reserved section name", reader);
541                         
542                         if (configSection != null)
543                                 value = configSection + '/' + value;
544
545                         object o = LookForFactory (value);
546                         if (o != null && o != removedMark && o != groupMark)
547                                 ThrowException ("Already have a factory for " + value, reader);
548
549                         factories [value] = groupMark;
550                         MoveToNextElement (reader);
551                         ReadSections (reader, value);
552                 }
553
554                 private void ReadSections (XmlTextReader reader, string configSection)
555                 {
556                         int depth = reader.Depth;
557                         while (reader.Depth == depth) {
558                                 string name = reader.Name;
559                                 if (name == "section") {
560                                         ReadSection (reader, configSection);
561                                         continue;
562                                 } 
563                                 
564                                 if (name == "remove") {
565                                         ReadRemoveSection (reader, configSection);
566                                         continue;
567                                 }
568
569                                 if (name == "clear") {
570                                         if (reader.HasAttributes)
571                                                 ThrowException ("Unrecognized attribute.", reader);
572
573                                         factories.Clear ();
574                                         MoveToNextElement (reader);
575                                         continue;
576                                 }
577
578                                 if (name == "sectionGroup") {
579                                         ReadSectionGroup (reader, configSection);
580                                         continue;
581                                 }
582                                 
583
584                                 ThrowException ("Unrecognized element: " + reader.Name, reader);
585                         }
586                 }
587
588                 void StorePending (string name, XmlTextReader reader)
589                 {
590                         if (pending == null)
591                                 pending = new Hashtable ();
592
593                         pending [name] = reader.ReadOuterXml ();
594                 }
595
596                 private void ReadConfigFile (XmlTextReader reader)
597                 {
598                         int depth = reader.Depth;
599                         while (!reader.EOF && reader.Depth == depth) {
600                                 string name = reader.Name;
601                                 if (name == "configSections") {
602                                         if (reader.HasAttributes)
603                                                 ThrowException ("Unrecognized attribute in <configSections>.", reader);
604
605                                         MoveToNextElement (reader);
606                                         if (reader.Depth > depth)
607                                                 ReadSections (reader, null);
608                                 } else if (name != null && name != "") {
609                                         StorePending (name, reader);
610                                         MoveToNextElement (reader);
611                                 } else {
612                                         MoveToNextElement (reader);
613                                 }
614                         }
615                 }
616                                 
617                 private void ThrowException (string text, XmlTextReader reader)
618                 {
619                         throw new ConfigurationException (text, fileName, reader.LineNumber);
620                 }
621 #endif
622         }
623 }
624
625