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 using System.Collections;
37 using System.Collections.Specialized;
39 using System.Runtime.CompilerServices;
42 using System.Xml.XPath;
45 namespace System.Configuration
47 public sealed class ConfigurationSettings
49 static IConfigurationSystem config = DefaultConfig.GetInstance ();
50 static object lockobj = new object ();
51 private ConfigurationSettings ()
55 public static object GetConfig (string sectionName)
57 return config.GetConfig (sectionName);
61 [Obsolete ("This property is obsolete. Please use System.Configuration.ConfigurationManager.AppSettings")]
63 public static NameValueCollection AppSettings
66 object appSettings = GetConfig ("appSettings");
67 if (appSettings == null)
68 appSettings = new NameValueCollection ();
70 return (NameValueCollection) appSettings;
74 // Invoked from System.Web
75 static IConfigurationSystem ChangeConfigurationSystem (IConfigurationSystem newSystem)
77 if (newSystem == null)
78 throw new ArgumentNullException ("newSystem");
81 IConfigurationSystem old = config;
89 // class DefaultConfig: read configuration from machine.config file and application
90 // config file if available.
92 class DefaultConfig : IConfigurationSystem
94 static readonly DefaultConfig instance = new DefaultConfig ();
95 ConfigurationData config;
97 private DefaultConfig ()
101 public static DefaultConfig GetInstance ()
107 [Obsolete ("This method is obsolete. Please use System.Configuration.ConfigurationManager.GetConfig")]
109 public object GetConfig (string sectionName)
112 return config.GetConfig (sectionName);
121 ConfigurationData data = new ConfigurationData ();
122 if (!data.Load (GetMachineConfigPath ()))
123 throw new ConfigurationException ("Cannot find " + GetMachineConfigPath ());
125 string appfile = GetAppConfigPath ();
126 if (appfile == null) {
131 ConfigurationData appData = new ConfigurationData (data);
132 if (appData.Load (appfile))
139 [MethodImplAttribute(MethodImplOptions.InternalCall)]
140 extern private static string get_machine_config_path ();
142 internal static string GetMachineConfigPath ()
144 return get_machine_config_path ();
147 private static string GetAppConfigPath ()
149 AppDomainSetup currentInfo = AppDomain.CurrentDomain.SetupInformation;
151 string configFile = currentInfo.ConfigurationFile;
152 if (configFile == null || configFile.Length == 0)
169 public readonly string SectionName;
170 public readonly string TypeName;
171 public readonly bool AllowLocation;
172 public readonly AllowDefinition AllowDefinition;
173 public string FileName;
175 public SectionData (string sectionName, string typeName,
176 bool allowLocation, AllowDefinition allowDefinition)
178 SectionName = sectionName;
180 AllowLocation = allowLocation;
181 AllowDefinition = allowDefinition;
186 class ConfigurationData
188 ConfigurationData parent;
192 static object removedMark = new object ();
193 static object groupMark = new object ();
194 static object emptyMark = new object ();
197 Hashtable FileCache {
202 cache = new Hashtable ();
207 public ConfigurationData () : this (null)
211 public ConfigurationData (ConfigurationData parent)
213 this.parent = (parent == this) ? null : parent;
214 factories = new Hashtable ();
217 public bool Load (string fileName)
219 this.fileName = fileName;
220 if (fileName == null || !File.Exists (fileName))
223 XmlTextReader reader = null;
226 FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
227 reader = new XmlTextReader (fs);
229 ReadConfigFile (reader);
230 } catch (ConfigurationException) {
232 } catch (Exception e) {
233 throw new ConfigurationException ("Error reading " + fileName, e);
242 object GetHandler (string sectionName)
245 object o = factories [sectionName];
246 if (o == null || o == removedMark) {
248 return parent.GetHandler (sectionName);
253 if (o is IConfigurationSectionHandler)
254 return (IConfigurationSectionHandler) o;
256 o = CreateNewHandler (sectionName, (SectionData) o);
257 factories [sectionName] = o;
262 object CreateNewHandler (string sectionName, SectionData section)
264 Type t = Type.GetType (section.TypeName);
266 throw new ConfigurationException ("Cannot get Type for " + section.TypeName);
269 Type iconfig = typeof (IConfigurationSectionHandler);
270 if (!iconfig.IsAssignableFrom (t))
271 throw new ConfigurationException (sectionName + " does not implement " + iconfig);
274 object o = Activator.CreateInstance (t, true);
276 throw new ConfigurationException ("Cannot get instance for " + t);
281 XmlDocument GetInnerDoc (XmlDocument doc, int i, string [] sectionPath)
283 if (++i >= sectionPath.Length)
286 if (doc.DocumentElement == null)
289 XmlNode node = doc.DocumentElement.FirstChild;
290 while (node != null) {
291 if (node.Name == sectionPath [i]) {
292 ConfigXmlDocument result = new ConfigXmlDocument ();
293 result.Load (new StringReader (node.OuterXml));
294 return GetInnerDoc (result, i, sectionPath);
296 node = node.NextSibling;
302 XmlDocument GetDocumentForSection (string sectionName)
304 ConfigXmlDocument doc = new ConfigXmlDocument ();
308 string [] sectionPath = sectionName.Split ('/');
309 string outerxml = pending [sectionPath [0]] as string;
310 if (outerxml == null)
313 StringReader reader = new StringReader (outerxml);
314 XmlTextReader rd = new XmlTextReader (reader);
316 doc.LoadSingleElement (fileName, rd);
318 return GetInnerDoc (doc, 0, sectionPath);
321 object GetConfigInternal (string sectionName)
323 object handler = GetHandler (sectionName);
324 IConfigurationSectionHandler iconf = handler as IConfigurationSectionHandler;
328 object parentConfig = null;
330 parentConfig = parent.GetConfig (sectionName);
332 XmlDocument doc = GetDocumentForSection (sectionName);
333 if (doc == null || doc.DocumentElement == null)
336 return iconf.Create (parentConfig, fileName, doc.DocumentElement);
339 object GetConfigInternal (string sectionName)
344 public object GetConfig (string sectionName)
348 config = this.FileCache [sectionName];
351 if (config == emptyMark)
358 config = GetConfigInternal (sectionName);
359 this.FileCache [sectionName] = (config == null) ? emptyMark : config;
365 private object LookForFactory (string key)
367 object o = factories [key];
372 return parent.LookForFactory (key);
377 private void InitRead (XmlTextReader reader)
379 reader.MoveToContent ();
380 if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
381 ThrowException ("Configuration file does not have a valid root element", reader);
383 if (reader.HasAttributes)
384 ThrowException ("Unrecognized attribute in root element", reader);
386 MoveToNextElement (reader);
389 private void MoveToNextElement (XmlTextReader reader)
391 while (reader.Read ()) {
392 XmlNodeType ntype = reader.NodeType;
393 if (ntype == XmlNodeType.Element)
396 if (ntype != XmlNodeType.Whitespace &&
397 ntype != XmlNodeType.Comment &&
398 ntype != XmlNodeType.SignificantWhitespace &&
399 ntype != XmlNodeType.EndElement)
400 ThrowException ("Unrecognized element", reader);
404 private void ReadSection (XmlTextReader reader, string sectionName)
407 string nameValue = null;
408 string typeValue = null;
409 string allowLoc = null, allowDef = null;
410 bool allowLocation = true;
411 AllowDefinition allowDefinition = AllowDefinition.Everywhere;
413 while (reader.MoveToNextAttribute ()) {
414 attName = reader.Name;
418 if (attName == "allowLocation") {
419 if (allowLoc != null)
420 ThrowException ("Duplicated allowLocation attribute.", reader);
422 allowLoc = reader.Value;
423 allowLocation = (allowLoc == "true");
424 if (!allowLocation && allowLoc != "false")
425 ThrowException ("Invalid attribute value", reader);
430 if (attName == "allowDefinition") {
431 if (allowDef != null)
432 ThrowException ("Duplicated allowDefinition attribute.", reader);
434 allowDef = reader.Value;
436 allowDefinition = (AllowDefinition) Enum.Parse (
437 typeof (AllowDefinition), allowDef);
439 ThrowException ("Invalid attribute value", reader);
445 if (attName == "type") {
446 if (typeValue != null)
447 ThrowException ("Duplicated type attribute.", reader);
448 typeValue = reader.Value;
452 if (attName == "name") {
453 if (nameValue != null)
454 ThrowException ("Duplicated name attribute.", reader);
455 nameValue = reader.Value;
456 if (nameValue == "location")
457 ThrowException ("location is a reserved section name", reader);
461 ThrowException ("Unrecognized attribute.", reader);
464 if (nameValue == null || typeValue == null)
465 ThrowException ("Required attribute missing", reader);
467 if (sectionName != null)
468 nameValue = sectionName + '/' + nameValue;
470 reader.MoveToElement();
471 object o = LookForFactory (nameValue);
472 if (o != null && o != removedMark)
473 ThrowException ("Already have a factory for " + nameValue, reader);
474 SectionData section = new SectionData (nameValue, typeValue, allowLocation, allowDefinition);
475 section.FileName = fileName;
476 factories [nameValue] = section;
478 MoveToNextElement (reader);
481 private void ReadRemoveSection (XmlTextReader reader, string sectionName)
483 if (!reader.MoveToNextAttribute () || reader.Name != "name")
484 ThrowException ("Unrecognized attribute.", reader);
486 string removeValue = reader.Value;
487 if (removeValue == null || removeValue.Length == 0)
488 ThrowException ("Empty name to remove", reader);
490 reader.MoveToElement ();
492 if (sectionName != null)
493 removeValue = sectionName + '/' + removeValue;
495 object o = LookForFactory (removeValue);
496 if (o != null && o == removedMark)
497 ThrowException ("No factory for " + removeValue, reader);
499 factories [removeValue] = removedMark;
500 MoveToNextElement (reader);
503 private void ReadSectionGroup (XmlTextReader reader, string configSection)
505 if (!reader.MoveToNextAttribute ())
506 ThrowException ("sectionGroup must have a 'name' attribute.", reader);
510 if (reader.Name == "name") {
512 ThrowException ("Duplicate 'name' attribute.", reader);
513 value = reader.Value;
517 if (reader.Name != "type")
519 ThrowException ("Unrecognized attribute.", reader);
520 } while (reader.MoveToNextAttribute ());
523 ThrowException ("No 'name' attribute.", reader);
525 if (value == "location")
526 ThrowException ("location is a reserved section name", reader);
528 if (configSection != null)
529 value = configSection + '/' + value;
531 object o = LookForFactory (value);
532 if (o != null && o != removedMark && o != groupMark)
533 ThrowException ("Already have a factory for " + value, reader);
535 factories [value] = groupMark;
536 MoveToNextElement (reader);
537 ReadSections (reader, value);
540 private void ReadSections (XmlTextReader reader, string configSection)
542 int depth = reader.Depth;
543 while (reader.Depth == depth) {
544 string name = reader.Name;
545 if (name == "section") {
546 ReadSection (reader, configSection);
550 if (name == "remove") {
551 ReadRemoveSection (reader, configSection);
555 if (name == "clear") {
556 if (reader.HasAttributes)
557 ThrowException ("Unrecognized attribute.", reader);
560 MoveToNextElement (reader);
564 if (name == "sectionGroup") {
565 ReadSectionGroup (reader, configSection);
570 ThrowException ("Unrecognized element: " + reader.Name, reader);
574 void StorePending (string name, XmlTextReader reader)
577 pending = new Hashtable ();
579 pending [name] = reader.ReadOuterXml ();
582 private void ReadConfigFile (XmlTextReader reader)
584 int depth = reader.Depth;
585 while (!reader.EOF && reader.Depth == depth) {
586 string name = reader.Name;
587 if (name == "configSections") {
588 if (reader.HasAttributes)
589 ThrowException ("Unrecognized attribute in <configSections>.", reader);
591 MoveToNextElement (reader);
592 if (reader.Depth > depth)
593 ReadSections (reader, null);
594 } else if (name != null && name != "") {
595 StorePending (name, reader);
596 MoveToNextElement (reader);
598 MoveToNextElement (reader);
603 private void ThrowException (string text, XmlTextReader reader)
605 throw new ConfigurationException (text, fileName, reader.LineNumber);