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 ()
62 [Obsolete ("This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.GetSection")]
64 public static object GetConfig (string sectionName)
66 return config.GetConfig (sectionName);
70 [Obsolete ("This property is obsolete. Please use System.Configuration.ConfigurationManager.AppSettings")]
72 public static NameValueCollection AppSettings
75 #if NET_2_0 && CONFIGURATION_DEP
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 ()
119 [Obsolete ("This method is obsolete. Please use System.Configuration.ConfigurationManager.GetConfig")]
121 public object GetConfig (string sectionName)
124 return config.GetConfig (sectionName);
133 ConfigurationData data = new ConfigurationData ();
134 if (!data.Load (GetMachineConfigPath ()))
135 throw new ConfigurationException ("Cannot find " + GetMachineConfigPath ());
137 string appfile = GetAppConfigPath ();
138 if (appfile == null) {
143 ConfigurationData appData = new ConfigurationData (data);
144 if (appData.Load (appfile))
151 [MethodImplAttribute(MethodImplOptions.InternalCall)]
152 extern private static string get_machine_config_path ();
154 internal static string GetMachineConfigPath ()
156 return get_machine_config_path ();
159 private static string GetAppConfigPath ()
161 AppDomainSetup currentInfo = AppDomain.CurrentDomain.SetupInformation;
163 string configFile = currentInfo.ConfigurationFile;
164 if (configFile == null || configFile.Length == 0)
181 public readonly string SectionName;
182 public readonly string TypeName;
183 public readonly bool AllowLocation;
184 public readonly AllowDefinition AllowDefinition;
185 public string FileName;
187 public SectionData (string sectionName, string typeName,
188 bool allowLocation, AllowDefinition allowDefinition)
190 SectionName = sectionName;
192 AllowLocation = allowLocation;
193 AllowDefinition = allowDefinition;
198 class ConfigurationData
200 ConfigurationData parent;
204 static object removedMark = new object ();
205 static object groupMark = new object ();
206 static object emptyMark = new object ();
209 Hashtable FileCache {
214 cache = new Hashtable ();
219 public ConfigurationData () : this (null)
223 public ConfigurationData (ConfigurationData parent)
225 this.parent = (parent == this) ? null : parent;
226 factories = new Hashtable ();
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)
233 this.fileName = fileName;
234 if (fileName == null || !File.Exists (fileName))
237 XmlTextReader reader = null;
240 FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
241 reader = new XmlTextReader (fs);
243 ReadConfigFile (reader);
244 } catch (ConfigurationException) {
246 } catch (Exception e) {
247 throw new ConfigurationException ("Error reading " + fileName, e);
256 object GetHandler (string sectionName)
259 object o = factories [sectionName];
260 if (o == null || o == removedMark) {
262 return parent.GetHandler (sectionName);
267 if (o is IConfigurationSectionHandler)
268 return (IConfigurationSectionHandler) o;
270 o = CreateNewHandler (sectionName, (SectionData) o);
271 factories [sectionName] = o;
276 object CreateNewHandler (string sectionName, SectionData section)
278 Type t = Type.GetType (section.TypeName);
280 throw new ConfigurationException ("Cannot get Type for " + section.TypeName);
283 Type iconfig = typeof (IConfigurationSectionHandler);
284 if (!iconfig.IsAssignableFrom (t))
285 throw new ConfigurationException (sectionName + " does not implement " + iconfig);
288 object o = Activator.CreateInstance (t, true);
290 throw new ConfigurationException ("Cannot get instance for " + t);
295 XmlDocument GetInnerDoc (XmlDocument doc, int i, string [] sectionPath)
297 if (++i >= sectionPath.Length)
300 if (doc.DocumentElement == null)
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);
310 node = node.NextSibling;
316 XmlDocument GetDocumentForSection (string sectionName)
318 ConfigXmlDocument doc = new ConfigXmlDocument ();
322 string [] sectionPath = sectionName.Split ('/');
323 string outerxml = pending [sectionPath [0]] as string;
324 if (outerxml == null)
327 StringReader reader = new StringReader (outerxml);
328 XmlTextReader rd = new XmlTextReader (reader);
330 doc.LoadSingleElement (fileName, rd);
332 return GetInnerDoc (doc, 0, sectionPath);
335 object GetConfigInternal (string sectionName)
337 object handler = GetHandler (sectionName);
338 IConfigurationSectionHandler iconf = handler as IConfigurationSectionHandler;
342 object parentConfig = null;
344 parentConfig = parent.GetConfig (sectionName);
346 XmlDocument doc = GetDocumentForSection (sectionName);
347 if (doc == null || doc.DocumentElement == null)
350 return iconf.Create (parentConfig, fileName, doc.DocumentElement);
353 object GetConfigInternal (string sectionName)
358 public object GetConfig (string sectionName)
362 config = this.FileCache [sectionName];
365 if (config == emptyMark)
372 config = GetConfigInternal (sectionName);
373 this.FileCache [sectionName] = (config == null) ? emptyMark : config;
379 private object LookForFactory (string key)
381 object o = factories [key];
386 return parent.LookForFactory (key);
391 private void InitRead (XmlTextReader reader)
393 reader.MoveToContent ();
394 if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
395 ThrowException ("Configuration file does not have a valid root element", reader);
397 if (reader.HasAttributes)
398 ThrowException ("Unrecognized attribute in root element", reader);
400 MoveToNextElement (reader);
403 private void MoveToNextElement (XmlTextReader reader)
405 while (reader.Read ()) {
406 XmlNodeType ntype = reader.NodeType;
407 if (ntype == XmlNodeType.Element)
410 if (ntype != XmlNodeType.Whitespace &&
411 ntype != XmlNodeType.Comment &&
412 ntype != XmlNodeType.SignificantWhitespace &&
413 ntype != XmlNodeType.EndElement)
414 ThrowException ("Unrecognized element", reader);
418 private void ReadSection (XmlTextReader reader, string sectionName)
421 string nameValue = null;
422 string typeValue = null;
423 string allowLoc = null, allowDef = null;
424 bool allowLocation = true;
425 AllowDefinition allowDefinition = AllowDefinition.Everywhere;
427 while (reader.MoveToNextAttribute ()) {
428 attName = reader.Name;
432 if (attName == "allowLocation") {
433 if (allowLoc != null)
434 ThrowException ("Duplicated allowLocation attribute.", reader);
436 allowLoc = reader.Value;
437 allowLocation = (allowLoc == "true");
438 if (!allowLocation && allowLoc != "false")
439 ThrowException ("Invalid attribute value", reader);
444 if (attName == "allowDefinition") {
445 if (allowDef != null)
446 ThrowException ("Duplicated allowDefinition attribute.", reader);
448 allowDef = reader.Value;
450 allowDefinition = (AllowDefinition) Enum.Parse (
451 typeof (AllowDefinition), allowDef);
453 ThrowException ("Invalid attribute value", reader);
459 if (attName == "type") {
460 if (typeValue != null)
461 ThrowException ("Duplicated type attribute.", reader);
462 typeValue = reader.Value;
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);
475 ThrowException ("Unrecognized attribute.", reader);
478 if (nameValue == null || typeValue == null)
479 ThrowException ("Required attribute missing", reader);
481 if (sectionName != null)
482 nameValue = sectionName + '/' + nameValue;
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;
492 MoveToNextElement (reader);
495 private void ReadRemoveSection (XmlTextReader reader, string sectionName)
497 if (!reader.MoveToNextAttribute () || reader.Name != "name")
498 ThrowException ("Unrecognized attribute.", reader);
500 string removeValue = reader.Value;
501 if (removeValue == null || removeValue.Length == 0)
502 ThrowException ("Empty name to remove", reader);
504 reader.MoveToElement ();
506 if (sectionName != null)
507 removeValue = sectionName + '/' + removeValue;
509 object o = LookForFactory (removeValue);
510 if (o != null && o == removedMark)
511 ThrowException ("No factory for " + removeValue, reader);
513 factories [removeValue] = removedMark;
514 MoveToNextElement (reader);
517 private void ReadSectionGroup (XmlTextReader reader, string configSection)
519 if (!reader.MoveToNextAttribute ())
520 ThrowException ("sectionGroup must have a 'name' attribute.", reader);
524 if (reader.Name == "name") {
526 ThrowException ("Duplicate 'name' attribute.", reader);
527 value = reader.Value;
530 #if NET_2_0 && CONFIGURATION_2_0
531 if (reader.Name != "type")
533 ThrowException ("Unrecognized attribute.", reader);
534 } while (reader.MoveToNextAttribute ());
537 ThrowException ("No 'name' attribute.", reader);
539 if (value == "location")
540 ThrowException ("location is a reserved section name", reader);
542 if (configSection != null)
543 value = configSection + '/' + value;
545 object o = LookForFactory (value);
546 if (o != null && o != removedMark && o != groupMark)
547 ThrowException ("Already have a factory for " + value, reader);
549 factories [value] = groupMark;
550 MoveToNextElement (reader);
551 ReadSections (reader, value);
554 private void ReadSections (XmlTextReader reader, string configSection)
556 int depth = reader.Depth;
557 while (reader.Depth == depth) {
558 string name = reader.Name;
559 if (name == "section") {
560 ReadSection (reader, configSection);
564 if (name == "remove") {
565 ReadRemoveSection (reader, configSection);
569 if (name == "clear") {
570 if (reader.HasAttributes)
571 ThrowException ("Unrecognized attribute.", reader);
574 MoveToNextElement (reader);
578 if (name == "sectionGroup") {
579 ReadSectionGroup (reader, configSection);
584 ThrowException ("Unrecognized element: " + reader.Name, reader);
588 void StorePending (string name, XmlTextReader reader)
591 pending = new Hashtable ();
593 pending [name] = reader.ReadOuterXml ();
596 private void ReadConfigFile (XmlTextReader reader)
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);
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);
612 MoveToNextElement (reader);
617 private void ThrowException (string text, XmlTextReader reader)
619 throw new ConfigurationException (text, fileName, reader.LineNumber);