2 // System.Configuration.ConfigurationSettings.cs
5 // Christopher Podurgiel (cpodurgiel@msn.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Eric Lindvall (eric@5stops.com)
9 // (c) Christopher Podurgiel
10 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
11 // (c) 2003 Novell, Inc. (http://www.novell.com)
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
35 #if CONFIGURATION_DEP && !TARGET_JVM
36 extern alias PrebuiltSystem;
37 using NameValueCollection = PrebuiltSystem.System.Collections.Specialized.NameValueCollection;
41 using System.Collections;
42 using System.Collections.Specialized;
44 using System.Runtime.CompilerServices;
45 using System.Security.Permissions;
48 using System.Xml.XPath;
52 using vmw.@internal.io;
55 namespace System.Configuration
57 public sealed class ConfigurationSettings
60 static IConfigurationSystem config = DefaultConfig.GetInstance ();
62 static IConfigurationSystem config {
64 IConfigurationSystem conf = (IConfigurationSystem) AppDomain.CurrentDomain.GetData ("ConfigurationSettings.Config");
66 conf = DefaultConfig.GetInstance ();
67 AppDomain.CurrentDomain.SetData ("ConfigurationSettings.Config", conf);
72 AppDomain.CurrentDomain.SetData ("ConfigurationSettings.Config", value);
76 static object lockobj = new object ();
77 private ConfigurationSettings ()
82 [Obsolete ("This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.GetSection")]
84 public static object GetConfig (string sectionName)
86 return config.GetConfig (sectionName);
90 [Obsolete ("This property is obsolete. Please use System.Configuration.ConfigurationManager.AppSettings")]
92 public static NameValueCollection AppSettings
95 #if NET_2_0 && CONFIGURATION_DEP
96 object appSettings = ConfigurationManager.GetSection ("appSettings");
98 object appSettings = GetConfig ("appSettings");
100 if (appSettings == null)
101 appSettings = new NameValueCollection ();
102 return (NameValueCollection) appSettings;
106 // Invoked from System.Web, disable warning
107 internal static IConfigurationSystem ChangeConfigurationSystem (IConfigurationSystem newSystem)
109 if (newSystem == null)
110 throw new ArgumentNullException ("newSystem");
113 IConfigurationSystem old = config;
121 // class DefaultConfig: read configuration from machine.config file and application
122 // config file if available.
124 class DefaultConfig : IConfigurationSystem
127 static readonly DefaultConfig instance = new DefaultConfig ();
129 static DefaultConfig instance {
131 DefaultConfig conf = (DefaultConfig) AppDomain.CurrentDomain.GetData ("DefaultConfig.instance");
133 conf = new DefaultConfig ();
134 AppDomain.CurrentDomain.SetData ("DefaultConfig.instance", conf);
139 AppDomain.CurrentDomain.SetData ("DefaultConfig.instance", value);
143 ConfigurationData config;
145 private DefaultConfig ()
149 public static DefaultConfig GetInstance ()
155 [Obsolete ("This method is obsolete. Please use System.Configuration.ConfigurationManager.GetConfig")]
157 public object GetConfig (string sectionName)
160 return config.GetConfig (sectionName);
169 ConfigurationData data = new ConfigurationData ();
170 if (data.LoadString (GetBundledMachineConfig ())) {
173 if (!data.Load (GetMachineConfigPath ()))
174 throw new ConfigurationException ("Cannot find " + GetMachineConfigPath ());
177 string appfile = GetAppConfigPath ();
178 if (appfile == null) {
183 ConfigurationData appData = new ConfigurationData (data);
184 if (appData.Load (appfile))
191 [MethodImplAttribute(MethodImplOptions.InternalCall)]
192 extern private static string get_bundled_machine_config ();
193 internal static string GetBundledMachineConfig ()
195 return get_bundled_machine_config ();
198 internal static string GetMachineConfigPath ()
200 return "/machine.config";
203 [MethodImplAttribute(MethodImplOptions.InternalCall)]
204 extern private static string get_machine_config_path ();
205 internal static string GetMachineConfigPath ()
207 return get_machine_config_path ();
210 private static string GetAppConfigPath ()
212 AppDomainSetup currentInfo = AppDomain.CurrentDomain.SetupInformation;
214 string configFile = currentInfo.ConfigurationFile;
215 if (configFile == null || configFile.Length == 0)
232 public readonly string SectionName;
233 public readonly string TypeName;
234 public readonly bool AllowLocation;
235 public readonly AllowDefinition AllowDefinition;
236 public string FileName;
237 public readonly bool RequirePermission;
239 public SectionData (string sectionName, string typeName,
240 bool allowLocation, AllowDefinition allowDefinition, bool requirePermission)
242 SectionName = sectionName;
244 AllowLocation = allowLocation;
245 AllowDefinition = allowDefinition;
246 RequirePermission = requirePermission;
251 class ConfigurationData
253 ConfigurationData parent;
255 static object removedMark = new object ();
256 static object emptyMark = new object ();
260 static object groupMark = new object ();
264 Hashtable FileCache {
269 cache = new Hashtable ();
274 public ConfigurationData () : this (null)
278 public ConfigurationData (ConfigurationData parent)
280 this.parent = (parent == this) ? null : parent;
281 factories = new Hashtable ();
284 // SECURITY-FIXME: limit this with an imperative assert for reading the specific file
285 [FileIOPermission (SecurityAction.Assert, Unrestricted = true)]
286 public bool Load (string fileName)
289 this.fileName = fileName;
292 || !File.Exists (fileName)
297 XmlTextReader reader = null;
301 FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
303 Stream fs = (Stream) vmw.common.IOUtils.getStream (fileName);
305 //patch for machine.config
306 if (fs == null && fileName.EndsWith ("machine.config")) {
307 fs = (Stream) IOUtils.getStreamForGHConfigs (fileName);
314 reader = new XmlTextReader (fs);
315 if (InitRead (reader))
316 ReadConfigFile (reader);
317 } catch (ConfigurationException) {
319 } catch (Exception e) {
320 throw new ConfigurationException ("Error reading " + fileName, e);
329 public bool LoadString (string data)
334 XmlTextReader reader = null;
337 TextReader tr = new StringReader (data);
338 reader = new XmlTextReader (tr);
339 if (InitRead (reader))
340 ReadConfigFile (reader);
341 } catch (ConfigurationException) {
343 } catch (Exception e) {
344 throw new ConfigurationException ("Error reading " + fileName, e);
353 object GetHandler (string sectionName)
356 object o = factories [sectionName];
357 if (o == null || o == removedMark) {
359 return parent.GetHandler (sectionName);
364 if (o is IConfigurationSectionHandler)
365 return (IConfigurationSectionHandler) o;
367 o = CreateNewHandler (sectionName, (SectionData) o);
368 factories [sectionName] = o;
373 object CreateNewHandler (string sectionName, SectionData section)
375 Type t = Type.GetType (section.TypeName);
377 throw new ConfigurationException ("Cannot get Type for " + section.TypeName);
380 Type iconfig = typeof (IConfigurationSectionHandler);
381 if (!iconfig.IsAssignableFrom (t))
382 throw new ConfigurationException (sectionName + " does not implement " + iconfig);
385 object o = Activator.CreateInstance (t, true);
387 throw new ConfigurationException ("Cannot get instance for " + t);
392 XmlDocument GetInnerDoc (XmlDocument doc, int i, string [] sectionPath)
394 if (++i >= sectionPath.Length)
397 if (doc.DocumentElement == null)
400 XmlNode node = doc.DocumentElement.FirstChild;
401 while (node != null) {
402 if (node.Name == sectionPath [i]) {
403 ConfigXmlDocument result = new ConfigXmlDocument ();
404 result.Load (new StringReader (node.OuterXml));
405 return GetInnerDoc (result, i, sectionPath);
407 node = node.NextSibling;
413 XmlDocument GetDocumentForSection (string sectionName)
415 ConfigXmlDocument doc = new ConfigXmlDocument ();
419 string [] sectionPath = sectionName.Split ('/');
420 string outerxml = pending [sectionPath [0]] as string;
421 if (outerxml == null)
424 StringReader reader = new StringReader (outerxml);
425 XmlTextReader rd = new XmlTextReader (reader);
427 doc.LoadSingleElement (fileName, rd);
429 return GetInnerDoc (doc, 0, sectionPath);
432 object GetConfigInternal (string sectionName)
434 object handler = GetHandler (sectionName);
435 IConfigurationSectionHandler iconf = handler as IConfigurationSectionHandler;
439 object parentConfig = null;
441 parentConfig = parent.GetConfig (sectionName);
443 XmlDocument doc = GetDocumentForSection (sectionName);
444 if (doc == null || doc.DocumentElement == null)
447 return iconf.Create (parentConfig, fileName, doc.DocumentElement);
450 object GetConfigInternal (string sectionName)
455 public object GetConfig (string sectionName)
459 config = this.FileCache [sectionName];
462 if (config == emptyMark)
469 config = GetConfigInternal (sectionName);
470 this.FileCache [sectionName] = (config == null) ? emptyMark : config;
476 private object LookForFactory (string key)
478 object o = factories [key];
483 return parent.LookForFactory (key);
488 private bool InitRead (XmlTextReader reader)
490 reader.MoveToContent ();
491 if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
492 ThrowException ("Configuration file does not have a valid root element", reader);
494 if (reader.HasAttributes)
495 ThrowException ("Unrecognized attribute in root element", reader);
496 if (reader.IsEmptyElement) {
501 reader.MoveToContent ();
502 return reader.NodeType != XmlNodeType.EndElement;
505 // FIXME: this approach is not always safe and likely to cause bugs.
506 private void MoveToNextElement (XmlTextReader reader)
508 while (reader.Read ()) {
509 XmlNodeType ntype = reader.NodeType;
510 if (ntype == XmlNodeType.Element)
513 if (ntype != XmlNodeType.Whitespace &&
514 ntype != XmlNodeType.Comment &&
515 ntype != XmlNodeType.SignificantWhitespace &&
516 ntype != XmlNodeType.EndElement)
517 ThrowException ("Unrecognized element", reader);
521 private void ReadSection (XmlTextReader reader, string sectionName)
524 string nameValue = null;
525 string typeValue = null;
526 string allowLoc = null, allowDef = null;
527 bool requirePermission = false;
528 string requirePer = null;
529 bool allowLocation = true;
530 AllowDefinition allowDefinition = AllowDefinition.Everywhere;
532 while (reader.MoveToNextAttribute ()) {
533 attName = reader.Name;
537 if (attName == "allowLocation") {
538 if (allowLoc != null)
539 ThrowException ("Duplicated allowLocation attribute.", reader);
541 allowLoc = reader.Value;
542 allowLocation = (allowLoc == "true");
543 if (!allowLocation && allowLoc != "false")
544 ThrowException ("Invalid attribute value", reader);
549 if (attName == "requirePermission") {
550 if (requirePer != null)
551 ThrowException ("Duplicated requirePermission attribute.", reader);
552 requirePer = reader.Value;
553 requirePermission = (requirePer == "true");
554 if (!requirePermission && requirePer != "false")
555 ThrowException ("Invalid attribute value", reader);
559 if (attName == "allowDefinition") {
560 if (allowDef != null)
561 ThrowException ("Duplicated allowDefinition attribute.", reader);
563 allowDef = reader.Value;
565 allowDefinition = (AllowDefinition) Enum.Parse (
566 typeof (AllowDefinition), allowDef);
568 ThrowException ("Invalid attribute value", reader);
574 if (attName == "type") {
575 if (typeValue != null)
576 ThrowException ("Duplicated type attribute.", reader);
577 typeValue = reader.Value;
581 if (attName == "name") {
582 if (nameValue != null)
583 ThrowException ("Duplicated name attribute.", reader);
584 nameValue = reader.Value;
585 if (nameValue == "location")
586 ThrowException ("location is a reserved section name", reader);
590 ThrowException ("Unrecognized attribute.", reader);
593 if (nameValue == null || typeValue == null)
594 ThrowException ("Required attribute missing", reader);
596 if (sectionName != null)
597 nameValue = sectionName + '/' + nameValue;
599 reader.MoveToElement();
600 object o = LookForFactory (nameValue);
601 if (o != null && o != removedMark)
602 ThrowException ("Already have a factory for " + nameValue, reader);
603 SectionData section = new SectionData (nameValue, typeValue, allowLocation,
604 allowDefinition, requirePermission);
605 section.FileName = fileName;
606 factories [nameValue] = section;
608 if (reader.IsEmptyElement)
612 reader.MoveToContent ();
613 if (reader.NodeType != XmlNodeType.EndElement)
614 // sub-section inside a section
615 ReadSections (reader, nameValue);
616 reader.ReadEndElement ();
618 reader.MoveToContent ();
621 private void ReadRemoveSection (XmlTextReader reader, string sectionName)
623 if (!reader.MoveToNextAttribute () || reader.Name != "name")
624 ThrowException ("Unrecognized attribute.", reader);
626 string removeValue = reader.Value;
627 if (removeValue == null || removeValue.Length == 0)
628 ThrowException ("Empty name to remove", reader);
630 reader.MoveToElement ();
632 if (sectionName != null)
633 removeValue = sectionName + '/' + removeValue;
635 object o = LookForFactory (removeValue);
636 if (o != null && o == removedMark)
637 ThrowException ("No factory for " + removeValue, reader);
639 factories [removeValue] = removedMark;
640 MoveToNextElement (reader);
643 private void ReadSectionGroup (XmlTextReader reader, string configSection)
645 if (!reader.MoveToNextAttribute ())
646 ThrowException ("sectionGroup must have a 'name' attribute.", reader);
650 if (reader.Name == "name") {
652 ThrowException ("Duplicate 'name' attribute.", reader);
653 value = reader.Value;
657 if (reader.Name != "type")
659 ThrowException ("Unrecognized attribute.", reader);
660 } while (reader.MoveToNextAttribute ());
663 ThrowException ("No 'name' attribute.", reader);
665 if (value == "location")
666 ThrowException ("location is a reserved section name", reader);
668 if (configSection != null)
669 value = configSection + '/' + value;
671 object o = LookForFactory (value);
672 if (o != null && o != removedMark && o != groupMark)
673 ThrowException ("Already have a factory for " + value, reader);
675 factories [value] = groupMark;
677 if (reader.IsEmptyElement) {
679 reader.MoveToContent ();
682 reader.MoveToContent ();
683 if (reader.NodeType != XmlNodeType.EndElement)
684 ReadSections (reader, value);
685 reader.ReadEndElement ();
686 reader.MoveToContent ();
690 // It stops XmlReader consumption at where it found
691 // surrounding EndElement i.e. EndElement is not consumed here
692 private void ReadSections (XmlTextReader reader, string configSection)
694 int depth = reader.Depth;
695 for (reader.MoveToContent ();
696 reader.Depth == depth;
697 reader.MoveToContent ()) {
698 string name = reader.Name;
699 if (name == "section") {
700 ReadSection (reader, configSection);
704 if (name == "remove") {
705 ReadRemoveSection (reader, configSection);
709 if (name == "clear") {
710 if (reader.HasAttributes)
711 ThrowException ("Unrecognized attribute.", reader);
714 MoveToNextElement (reader);
718 if (name == "sectionGroup") {
719 ReadSectionGroup (reader, configSection);
724 ThrowException ("Unrecognized element: " + reader.Name, reader);
728 void StorePending (string name, XmlTextReader reader)
731 pending = new Hashtable ();
733 pending [name] = reader.ReadOuterXml ();
736 private void ReadConfigFile (XmlTextReader reader)
738 int depth = reader.Depth;
739 for (reader.MoveToContent ();
740 !reader.EOF && reader.NodeType != XmlNodeType.EndElement;
741 reader.MoveToContent ()) {
742 string name = reader.Name;
743 if (name == "configSections") {
744 if (reader.HasAttributes)
745 ThrowException ("Unrecognized attribute in <configSections>.", reader);
746 if (reader.IsEmptyElement)
750 reader.MoveToContent ();
751 if (reader.NodeType != XmlNodeType.EndElement)
752 ReadSections (reader, null);
753 reader.ReadEndElement ();
755 } else if (name != null && name != "") {
756 StorePending (name, reader);
757 MoveToNextElement (reader);
759 MoveToNextElement (reader);
764 private void ThrowException (string text, XmlTextReader reader)
766 throw new ConfigurationException (text, fileName, reader.LineNumber);