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.
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;
51 namespace System.Configuration
53 public sealed class ConfigurationSettings
55 static IConfigurationSystem config = DefaultConfig.GetInstance ();
56 static object lockobj = new object ();
57 private ConfigurationSettings ()
61 [Obsolete ("This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.GetSection")]
62 public static object GetConfig (string sectionName)
65 return ConfigurationManager.GetSection (sectionName);
67 return config.GetConfig (sectionName);
71 [Obsolete ("This property is obsolete. Please use System.Configuration.ConfigurationManager.AppSettings")]
72 public static NameValueCollection AppSettings
76 object appSettings = ConfigurationManager.GetSection ("appSettings");
78 object appSettings = GetConfig ("appSettings");
80 if (appSettings == null)
81 appSettings = new NameValueCollection ();
82 return (NameValueCollection) appSettings;
86 // Invoked from System.Web, disable warning
87 internal static IConfigurationSystem ChangeConfigurationSystem (IConfigurationSystem newSystem)
89 if (newSystem == null)
90 throw new ArgumentNullException ("newSystem");
93 IConfigurationSystem old = config;
101 // class DefaultConfig: read configuration from machine.config file and application
102 // config file if available.
104 class DefaultConfig : IConfigurationSystem
106 static readonly DefaultConfig instance = new DefaultConfig ();
107 ConfigurationData config;
109 private DefaultConfig ()
113 public static DefaultConfig GetInstance ()
118 [Obsolete ("This method is obsolete. Please use System.Configuration.ConfigurationManager.GetConfig")]
119 public object GetConfig (string sectionName)
122 return config.GetConfig (sectionName);
131 ConfigurationData data = new ConfigurationData ();
132 if (data.LoadString (GetBundledMachineConfig ())) {
135 if (!data.Load (GetMachineConfigPath ()))
136 throw new ConfigurationException ("Cannot find " + GetMachineConfigPath ());
139 string appfile = GetAppConfigPath ();
140 if (appfile == null) {
145 ConfigurationData appData = new ConfigurationData (data);
146 if (appData.Load (appfile))
152 [MethodImplAttribute(MethodImplOptions.InternalCall)]
153 extern private static string get_bundled_machine_config ();
154 internal static string GetBundledMachineConfig ()
156 return get_bundled_machine_config ();
158 [MethodImplAttribute(MethodImplOptions.InternalCall)]
159 extern private static string get_machine_config_path ();
160 internal static string GetMachineConfigPath ()
162 return get_machine_config_path ();
164 private static string GetAppConfigPath ()
166 AppDomainSetup currentInfo = AppDomain.CurrentDomain.SetupInformation;
168 string configFile = currentInfo.ConfigurationFile;
169 if (configFile == null || configFile.Length == 0)
186 public readonly string SectionName;
187 public readonly string TypeName;
188 public readonly bool AllowLocation;
189 public readonly AllowDefinition AllowDefinition;
191 public string FileName;
193 public readonly bool RequirePermission;
195 public SectionData (string sectionName, string typeName,
196 bool allowLocation, AllowDefinition allowDefinition, bool requirePermission)
198 SectionName = sectionName;
200 AllowLocation = allowLocation;
201 AllowDefinition = allowDefinition;
202 RequirePermission = requirePermission;
207 class ConfigurationData
209 ConfigurationData parent;
211 static object removedMark = new object ();
212 static object emptyMark = new object ();
216 static object groupMark = new object ();
220 Hashtable FileCache {
225 cache = new Hashtable ();
230 public ConfigurationData () : this (null)
234 public ConfigurationData (ConfigurationData parent)
236 this.parent = (parent == this) ? null : parent;
237 factories = new Hashtable ();
240 // SECURITY-FIXME: limit this with an imperative assert for reading the specific file
241 [FileIOPermission (SecurityAction.Assert, Unrestricted = true)]
242 public bool Load (string fileName)
245 this.fileName = fileName;
247 || !File.Exists (fileName)
251 XmlTextReader reader = null;
254 FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
255 reader = new XmlTextReader (fs);
256 if (InitRead (reader))
257 ReadConfigFile (reader);
258 } catch (ConfigurationException) {
260 } catch (Exception e) {
261 throw new ConfigurationException ("Error reading " + fileName, e);
270 public bool LoadString (string data)
275 XmlTextReader reader = null;
278 TextReader tr = new StringReader (data);
279 reader = new XmlTextReader (tr);
280 if (InitRead (reader))
281 ReadConfigFile (reader);
282 } catch (ConfigurationException) {
284 } catch (Exception e) {
285 throw new ConfigurationException ("Error reading " + fileName, e);
294 object GetHandler (string sectionName)
297 object o = factories [sectionName];
298 if (o == null || o == removedMark) {
300 return parent.GetHandler (sectionName);
305 if (o is IConfigurationSectionHandler)
306 return (IConfigurationSectionHandler) o;
308 o = CreateNewHandler (sectionName, (SectionData) o);
309 factories [sectionName] = o;
314 object CreateNewHandler (string sectionName, SectionData section)
316 Type t = Type.GetType (section.TypeName);
318 throw new ConfigurationException ("Cannot get Type for " + section.TypeName);
321 Type iconfig = typeof (IConfigurationSectionHandler);
322 if (!iconfig.IsAssignableFrom (t))
323 throw new ConfigurationException (sectionName + " does not implement " + iconfig);
326 object o = Activator.CreateInstance (t, true);
328 throw new ConfigurationException ("Cannot get instance for " + t);
333 XmlDocument GetInnerDoc (XmlDocument doc, int i, string [] sectionPath)
335 if (++i >= sectionPath.Length)
338 if (doc.DocumentElement == null)
341 XmlNode node = doc.DocumentElement.FirstChild;
342 while (node != null) {
343 if (node.Name == sectionPath [i]) {
344 ConfigXmlDocument result = new ConfigXmlDocument ();
345 result.Load (new StringReader (node.OuterXml));
346 return GetInnerDoc (result, i, sectionPath);
348 node = node.NextSibling;
354 XmlDocument GetDocumentForSection (string sectionName)
356 ConfigXmlDocument doc = new ConfigXmlDocument ();
360 string [] sectionPath = sectionName.Split ('/');
361 string outerxml = pending [sectionPath [0]] as string;
362 if (outerxml == null)
365 StringReader reader = new StringReader (outerxml);
366 XmlTextReader rd = new XmlTextReader (reader);
368 doc.LoadSingleElement (fileName, rd);
370 return GetInnerDoc (doc, 0, sectionPath);
373 object GetConfigInternal (string sectionName)
375 object handler = GetHandler (sectionName);
376 IConfigurationSectionHandler iconf = handler as IConfigurationSectionHandler;
380 object parentConfig = null;
382 parentConfig = parent.GetConfig (sectionName);
384 XmlDocument doc = GetDocumentForSection (sectionName);
385 if (doc == null || doc.DocumentElement == null)
388 return iconf.Create (parentConfig, fileName, doc.DocumentElement);
391 object GetConfigInternal (string sectionName)
396 public object GetConfig (string sectionName)
400 config = this.FileCache [sectionName];
403 if (config == emptyMark)
410 config = GetConfigInternal (sectionName);
411 this.FileCache [sectionName] = (config == null) ? emptyMark : config;
417 private object LookForFactory (string key)
419 object o = factories [key];
424 return parent.LookForFactory (key);
429 private bool InitRead (XmlTextReader reader)
431 reader.MoveToContent ();
432 if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
433 ThrowException ("Configuration file does not have a valid root element", reader);
435 if (reader.HasAttributes)
436 ThrowException ("Unrecognized attribute in root element", reader);
437 if (reader.IsEmptyElement) {
442 reader.MoveToContent ();
443 return reader.NodeType != XmlNodeType.EndElement;
446 // FIXME: this approach is not always safe and likely to cause bugs.
447 private void MoveToNextElement (XmlTextReader reader)
449 while (reader.Read ()) {
450 XmlNodeType ntype = reader.NodeType;
451 if (ntype == XmlNodeType.Element)
454 if (ntype != XmlNodeType.Whitespace &&
455 ntype != XmlNodeType.Comment &&
456 ntype != XmlNodeType.SignificantWhitespace &&
457 ntype != XmlNodeType.EndElement)
458 ThrowException ("Unrecognized element", reader);
462 private void ReadSection (XmlTextReader reader, string sectionName)
465 string nameValue = null;
466 string typeValue = null;
467 string allowLoc = null, allowDef = null;
468 bool requirePermission = false;
469 string requirePer = null;
470 bool allowLocation = true;
471 AllowDefinition allowDefinition = AllowDefinition.Everywhere;
473 while (reader.MoveToNextAttribute ()) {
474 attName = reader.Name;
478 if (attName == "allowLocation") {
479 if (allowLoc != null)
480 ThrowException ("Duplicated allowLocation attribute.", reader);
482 allowLoc = reader.Value;
483 allowLocation = (allowLoc == "true");
484 if (!allowLocation && allowLoc != "false")
485 ThrowException ("Invalid attribute value", reader);
490 if (attName == "requirePermission") {
491 if (requirePer != null)
492 ThrowException ("Duplicated requirePermission attribute.", reader);
493 requirePer = reader.Value;
494 requirePermission = (requirePer == "true");
495 if (!requirePermission && requirePer != "false")
496 ThrowException ("Invalid attribute value", reader);
500 if (attName == "allowDefinition") {
501 if (allowDef != null)
502 ThrowException ("Duplicated allowDefinition attribute.", reader);
504 allowDef = reader.Value;
506 allowDefinition = (AllowDefinition) Enum.Parse (
507 typeof (AllowDefinition), allowDef);
509 ThrowException ("Invalid attribute value", reader);
515 if (attName == "type") {
516 if (typeValue != null)
517 ThrowException ("Duplicated type attribute.", reader);
518 typeValue = reader.Value;
522 if (attName == "name") {
523 if (nameValue != null)
524 ThrowException ("Duplicated name attribute.", reader);
525 nameValue = reader.Value;
526 if (nameValue == "location")
527 ThrowException ("location is a reserved section name", reader);
531 ThrowException ("Unrecognized attribute.", reader);
534 if (nameValue == null || typeValue == null)
535 ThrowException ("Required attribute missing", reader);
537 if (sectionName != null)
538 nameValue = sectionName + '/' + nameValue;
540 reader.MoveToElement();
541 object o = LookForFactory (nameValue);
542 if (o != null && o != removedMark)
543 ThrowException ("Already have a factory for " + nameValue, reader);
544 SectionData section = new SectionData (nameValue, typeValue, allowLocation,
545 allowDefinition, requirePermission);
546 section.FileName = fileName;
547 factories [nameValue] = section;
549 if (reader.IsEmptyElement)
553 reader.MoveToContent ();
554 if (reader.NodeType != XmlNodeType.EndElement)
555 // sub-section inside a section
556 ReadSections (reader, nameValue);
557 reader.ReadEndElement ();
559 reader.MoveToContent ();
562 private void ReadRemoveSection (XmlTextReader reader, string sectionName)
564 if (!reader.MoveToNextAttribute () || reader.Name != "name")
565 ThrowException ("Unrecognized attribute.", reader);
567 string removeValue = reader.Value;
568 if (removeValue == null || removeValue.Length == 0)
569 ThrowException ("Empty name to remove", reader);
571 reader.MoveToElement ();
573 if (sectionName != null)
574 removeValue = sectionName + '/' + removeValue;
576 object o = LookForFactory (removeValue);
577 if (o != null && o == removedMark)
578 ThrowException ("No factory for " + removeValue, reader);
580 factories [removeValue] = removedMark;
581 MoveToNextElement (reader);
584 private void ReadSectionGroup (XmlTextReader reader, string configSection)
586 if (!reader.MoveToNextAttribute ())
587 ThrowException ("sectionGroup must have a 'name' attribute.", reader);
591 if (reader.Name == "name") {
593 ThrowException ("Duplicate 'name' attribute.", reader);
594 value = reader.Value;
597 if (reader.Name != "type")
598 ThrowException ("Unrecognized attribute.", reader);
599 } while (reader.MoveToNextAttribute ());
602 ThrowException ("No 'name' attribute.", reader);
604 if (value == "location")
605 ThrowException ("location is a reserved section name", reader);
607 if (configSection != null)
608 value = configSection + '/' + value;
610 object o = LookForFactory (value);
611 if (o != null && o != removedMark && o != groupMark)
612 ThrowException ("Already have a factory for " + value, reader);
614 factories [value] = groupMark;
616 if (reader.IsEmptyElement) {
618 reader.MoveToContent ();
621 reader.MoveToContent ();
622 if (reader.NodeType != XmlNodeType.EndElement)
623 ReadSections (reader, value);
624 reader.ReadEndElement ();
625 reader.MoveToContent ();
629 // It stops XmlReader consumption at where it found
630 // surrounding EndElement i.e. EndElement is not consumed here
631 private void ReadSections (XmlTextReader reader, string configSection)
633 int depth = reader.Depth;
634 for (reader.MoveToContent ();
635 reader.Depth == depth;
636 reader.MoveToContent ()) {
637 string name = reader.Name;
638 if (name == "section") {
639 ReadSection (reader, configSection);
643 if (name == "remove") {
644 ReadRemoveSection (reader, configSection);
648 if (name == "clear") {
649 if (reader.HasAttributes)
650 ThrowException ("Unrecognized attribute.", reader);
653 MoveToNextElement (reader);
657 if (name == "sectionGroup") {
658 ReadSectionGroup (reader, configSection);
663 ThrowException ("Unrecognized element: " + reader.Name, reader);
667 void StorePending (string name, XmlTextReader reader)
670 pending = new Hashtable ();
672 pending [name] = reader.ReadOuterXml ();
675 private void ReadConfigFile (XmlTextReader reader)
677 //int depth = reader.Depth;
678 for (reader.MoveToContent ();
679 !reader.EOF && reader.NodeType != XmlNodeType.EndElement;
680 reader.MoveToContent ()) {
681 string name = reader.Name;
682 if (name == "configSections") {
683 if (reader.HasAttributes)
684 ThrowException ("Unrecognized attribute in <configSections>.", reader);
685 if (reader.IsEmptyElement)
689 reader.MoveToContent ();
690 if (reader.NodeType != XmlNodeType.EndElement)
691 ReadSections (reader, null);
692 reader.ReadEndElement ();
694 } else if (name != null && name != "") {
695 StorePending (name, reader);
696 MoveToNextElement (reader);
698 MoveToNextElement (reader);
703 private void ThrowException (string text, XmlTextReader reader)
705 throw new ConfigurationException (text, fileName, reader.LineNumber);