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