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;
47 using System.Xml.XPath;
50 namespace System.Configuration
52 public sealed class ConfigurationSettings
54 static IConfigurationSystem config = DefaultConfig.GetInstance ();
55 static object lockobj = new object ();
56 private ConfigurationSettings ()
60 public static object GetConfig (string sectionName)
62 return config.GetConfig (sectionName);
66 [Obsolete ("This property is obsolete. Please use System.Configuration.ConfigurationManager.AppSettings")]
68 public static NameValueCollection AppSettings
71 #if NET_2_0 && CONFIGURATION_2_0
73 return ConfigurationManager.AppSettings;
78 object appSettings = GetConfig ("appSettings");
79 if (appSettings == null)
80 appSettings = new NameValueCollection ();
82 return (NameValueCollection) appSettings;
87 // Invoked from System.Web
88 static IConfigurationSystem ChangeConfigurationSystem (IConfigurationSystem newSystem)
90 if (newSystem == null)
91 throw new ArgumentNullException ("newSystem");
94 IConfigurationSystem old = config;
102 // class DefaultConfig: read configuration from machine.config file and application
103 // config file if available.
105 class DefaultConfig : IConfigurationSystem
107 static readonly DefaultConfig instance = new DefaultConfig ();
108 ConfigurationData config;
110 private DefaultConfig ()
114 public static DefaultConfig GetInstance ()
120 [Obsolete ("This method is obsolete. Please use System.Configuration.ConfigurationManager.GetConfig")]
122 public object GetConfig (string sectionName)
124 #if NET_2_0 && CONFIGURATION_2_0
125 #if CONFIGURATION_DEP
127 return ConfigurationManager.GetSection (sectionName);
134 return config.GetConfig (sectionName);
144 ConfigurationData data = new ConfigurationData ();
145 if (!data.Load (GetMachineConfigPath ()))
146 throw new ConfigurationException ("Cannot find " + GetMachineConfigPath ());
148 string appfile = GetAppConfigPath ();
149 if (appfile == null) {
154 ConfigurationData appData = new ConfigurationData (data);
155 if (appData.Load (appfile))
162 [MethodImplAttribute(MethodImplOptions.InternalCall)]
163 extern private static string get_machine_config_path ();
165 internal static string GetMachineConfigPath ()
167 return get_machine_config_path ();
170 private static string GetAppConfigPath ()
172 AppDomainSetup currentInfo = AppDomain.CurrentDomain.SetupInformation;
174 string configFile = currentInfo.ConfigurationFile;
175 if (configFile == null || configFile.Length == 0)
192 public readonly string SectionName;
193 public readonly string TypeName;
194 public readonly bool AllowLocation;
195 public readonly AllowDefinition AllowDefinition;
196 public string FileName;
198 public SectionData (string sectionName, string typeName,
199 bool allowLocation, AllowDefinition allowDefinition)
201 SectionName = sectionName;
203 AllowLocation = allowLocation;
204 AllowDefinition = allowDefinition;
209 class ConfigurationData
211 ConfigurationData parent;
215 static object removedMark = new object ();
216 static object groupMark = new object ();
217 static object emptyMark = 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 public bool Load (string fileName)
242 this.fileName = fileName;
243 if (fileName == null || !File.Exists (fileName))
246 XmlTextReader reader = null;
249 FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
250 reader = new XmlTextReader (fs);
252 ReadConfigFile (reader);
253 } catch (ConfigurationException) {
255 } catch (Exception e) {
256 throw new ConfigurationException ("Error reading " + fileName, e);
265 object GetHandler (string sectionName)
268 object o = factories [sectionName];
269 if (o == null || o == removedMark) {
271 return parent.GetHandler (sectionName);
276 if (o is IConfigurationSectionHandler)
277 return (IConfigurationSectionHandler) o;
279 o = CreateNewHandler (sectionName, (SectionData) o);
280 factories [sectionName] = o;
285 object CreateNewHandler (string sectionName, SectionData section)
287 Type t = Type.GetType (section.TypeName);
289 throw new ConfigurationException ("Cannot get Type for " + section.TypeName);
292 Type iconfig = typeof (IConfigurationSectionHandler);
293 if (!iconfig.IsAssignableFrom (t))
294 throw new ConfigurationException (sectionName + " does not implement " + iconfig);
297 object o = Activator.CreateInstance (t, true);
299 throw new ConfigurationException ("Cannot get instance for " + t);
304 XmlDocument GetInnerDoc (XmlDocument doc, int i, string [] sectionPath)
306 if (++i >= sectionPath.Length)
309 if (doc.DocumentElement == null)
312 XmlNode node = doc.DocumentElement.FirstChild;
313 while (node != null) {
314 if (node.Name == sectionPath [i]) {
315 ConfigXmlDocument result = new ConfigXmlDocument ();
316 result.Load (new StringReader (node.OuterXml));
317 return GetInnerDoc (result, i, sectionPath);
319 node = node.NextSibling;
325 XmlDocument GetDocumentForSection (string sectionName)
327 ConfigXmlDocument doc = new ConfigXmlDocument ();
331 string [] sectionPath = sectionName.Split ('/');
332 string outerxml = pending [sectionPath [0]] as string;
333 if (outerxml == null)
336 StringReader reader = new StringReader (outerxml);
337 XmlTextReader rd = new XmlTextReader (reader);
339 doc.LoadSingleElement (fileName, rd);
341 return GetInnerDoc (doc, 0, sectionPath);
344 object GetConfigInternal (string sectionName)
346 object handler = GetHandler (sectionName);
347 IConfigurationSectionHandler iconf = handler as IConfigurationSectionHandler;
351 object parentConfig = null;
353 parentConfig = parent.GetConfig (sectionName);
355 XmlDocument doc = GetDocumentForSection (sectionName);
356 if (doc == null || doc.DocumentElement == null)
359 return iconf.Create (parentConfig, fileName, doc.DocumentElement);
362 object GetConfigInternal (string sectionName)
367 public object GetConfig (string sectionName)
371 config = this.FileCache [sectionName];
374 if (config == emptyMark)
381 config = GetConfigInternal (sectionName);
382 this.FileCache [sectionName] = (config == null) ? emptyMark : config;
388 private object LookForFactory (string key)
390 object o = factories [key];
395 return parent.LookForFactory (key);
400 private void InitRead (XmlTextReader reader)
402 reader.MoveToContent ();
403 if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
404 ThrowException ("Configuration file does not have a valid root element", reader);
406 if (reader.HasAttributes)
407 ThrowException ("Unrecognized attribute in root element", reader);
409 MoveToNextElement (reader);
412 private void MoveToNextElement (XmlTextReader reader)
414 while (reader.Read ()) {
415 XmlNodeType ntype = reader.NodeType;
416 if (ntype == XmlNodeType.Element)
419 if (ntype != XmlNodeType.Whitespace &&
420 ntype != XmlNodeType.Comment &&
421 ntype != XmlNodeType.SignificantWhitespace &&
422 ntype != XmlNodeType.EndElement)
423 ThrowException ("Unrecognized element", reader);
427 private void ReadSection (XmlTextReader reader, string sectionName)
430 string nameValue = null;
431 string typeValue = null;
432 string allowLoc = null, allowDef = null;
433 bool allowLocation = true;
434 AllowDefinition allowDefinition = AllowDefinition.Everywhere;
436 while (reader.MoveToNextAttribute ()) {
437 attName = reader.Name;
441 if (attName == "allowLocation") {
442 if (allowLoc != null)
443 ThrowException ("Duplicated allowLocation attribute.", reader);
445 allowLoc = reader.Value;
446 allowLocation = (allowLoc == "true");
447 if (!allowLocation && allowLoc != "false")
448 ThrowException ("Invalid attribute value", reader);
453 if (attName == "allowDefinition") {
454 if (allowDef != null)
455 ThrowException ("Duplicated allowDefinition attribute.", reader);
457 allowDef = reader.Value;
459 allowDefinition = (AllowDefinition) Enum.Parse (
460 typeof (AllowDefinition), allowDef);
462 ThrowException ("Invalid attribute value", reader);
468 if (attName == "type") {
469 if (typeValue != null)
470 ThrowException ("Duplicated type attribute.", reader);
471 typeValue = reader.Value;
475 if (attName == "name") {
476 if (nameValue != null)
477 ThrowException ("Duplicated name attribute.", reader);
478 nameValue = reader.Value;
479 if (nameValue == "location")
480 ThrowException ("location is a reserved section name", reader);
484 ThrowException ("Unrecognized attribute.", reader);
487 if (nameValue == null || typeValue == null)
488 ThrowException ("Required attribute missing", reader);
490 if (sectionName != null)
491 nameValue = sectionName + '/' + nameValue;
493 reader.MoveToElement();
494 object o = LookForFactory (nameValue);
495 if (o != null && o != removedMark)
496 ThrowException ("Already have a factory for " + nameValue, reader);
497 SectionData section = new SectionData (nameValue, typeValue, allowLocation, allowDefinition);
498 section.FileName = fileName;
499 factories [nameValue] = section;
501 MoveToNextElement (reader);
504 private void ReadRemoveSection (XmlTextReader reader, string sectionName)
506 if (!reader.MoveToNextAttribute () || reader.Name != "name")
507 ThrowException ("Unrecognized attribute.", reader);
509 string removeValue = reader.Value;
510 if (removeValue == null || removeValue.Length == 0)
511 ThrowException ("Empty name to remove", reader);
513 reader.MoveToElement ();
515 if (sectionName != null)
516 removeValue = sectionName + '/' + removeValue;
518 object o = LookForFactory (removeValue);
519 if (o != null && o == removedMark)
520 ThrowException ("No factory for " + removeValue, reader);
522 factories [removeValue] = removedMark;
523 MoveToNextElement (reader);
526 private void ReadSectionGroup (XmlTextReader reader, string configSection)
528 if (!reader.MoveToNextAttribute ())
529 ThrowException ("sectionGroup must have a 'name' attribute.", reader);
533 if (reader.Name == "name") {
535 ThrowException ("Duplicate 'name' attribute.", reader);
536 value = reader.Value;
539 #if NET_2_0 && CONFIGURATION_2_0
540 if (reader.Name != "type")
542 ThrowException ("Unrecognized attribute.", reader);
543 } while (reader.MoveToNextAttribute ());
546 ThrowException ("No 'name' attribute.", reader);
548 if (value == "location")
549 ThrowException ("location is a reserved section name", reader);
551 if (configSection != null)
552 value = configSection + '/' + value;
554 object o = LookForFactory (value);
555 if (o != null && o != removedMark && o != groupMark)
556 ThrowException ("Already have a factory for " + value, reader);
558 factories [value] = groupMark;
559 MoveToNextElement (reader);
560 ReadSections (reader, value);
563 private void ReadSections (XmlTextReader reader, string configSection)
565 int depth = reader.Depth;
566 while (reader.Depth == depth) {
567 string name = reader.Name;
568 if (name == "section") {
569 ReadSection (reader, configSection);
573 if (name == "remove") {
574 ReadRemoveSection (reader, configSection);
578 if (name == "clear") {
579 if (reader.HasAttributes)
580 ThrowException ("Unrecognized attribute.", reader);
583 MoveToNextElement (reader);
587 if (name == "sectionGroup") {
588 ReadSectionGroup (reader, configSection);
593 ThrowException ("Unrecognized element: " + reader.Name, reader);
597 void StorePending (string name, XmlTextReader reader)
600 pending = new Hashtable ();
602 pending [name] = reader.ReadOuterXml ();
605 private void ReadConfigFile (XmlTextReader reader)
607 int depth = reader.Depth;
608 while (!reader.EOF && reader.Depth == depth) {
609 string name = reader.Name;
610 if (name == "configSections") {
611 if (reader.HasAttributes)
612 ThrowException ("Unrecognized attribute in <configSections>.", reader);
614 MoveToNextElement (reader);
615 if (reader.Depth > depth)
616 ReadSections (reader, null);
617 } else if (name != null && name != "") {
618 StorePending (name, reader);
619 MoveToNextElement (reader);
621 MoveToNextElement (reader);
626 private void ThrowException (string text, XmlTextReader reader)
628 throw new ConfigurationException (text, fileName, reader.LineNumber);