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);
60 public static NameValueCollection AppSettings
63 object appSettings = GetConfig ("appSettings");
64 if (appSettings == null)
65 appSettings = new NameValueCollection ();
67 return (NameValueCollection) appSettings;
73 public static ConnectionStringSettingsCollection ConnectionStrings
76 ConnectionStringsSection connSection = (ConnectionStringsSection) GetConfig ("connectionStrings");
77 ConnectionStringSettingsCollection connectionStrings = null;
79 if (connSection != null)
80 connectionStrings = connSection.ConnectionStrings;
82 connectionStrings = new ConnectionStringSettingsCollection ();
84 return connectionStrings;
90 // Invoked from System.Web
91 static IConfigurationSystem ChangeConfigurationSystem (IConfigurationSystem newSystem)
93 if (newSystem == null)
94 throw new ArgumentNullException ("newSystem");
97 IConfigurationSystem old = config;
105 // class DefaultConfig: read configuration from machine.config file and application
106 // config file if available.
108 class DefaultConfig : IConfigurationSystem
110 static readonly DefaultConfig instance = new DefaultConfig ();
111 ConfigurationData config;
113 private DefaultConfig ()
117 public static DefaultConfig GetInstance ()
122 public object GetConfig (string sectionName)
125 return config.GetConfig (sectionName);
134 ConfigurationData data = new ConfigurationData ();
135 if (!data.Load (GetMachineConfigPath ()))
136 throw new ConfigurationException ("Cannot find " + GetMachineConfigPath ());
138 string appfile = GetAppConfigPath ();
139 if (appfile == null) {
144 ConfigurationData appData = new ConfigurationData (data);
145 if (appData.Load (appfile))
152 [MethodImplAttribute(MethodImplOptions.InternalCall)]
153 extern private static string get_machine_config_path ();
155 internal static string GetMachineConfigPath ()
157 return get_machine_config_path ();
160 private static string GetAppConfigPath ()
162 AppDomainSetup currentInfo = AppDomain.CurrentDomain.SetupInformation;
164 string configFile = currentInfo.ConfigurationFile;
165 if (configFile == null || configFile.Length == 0)
182 public readonly string SectionName;
183 public readonly string TypeName;
184 public readonly bool AllowLocation;
185 public readonly AllowDefinition AllowDefinition;
186 public string FileName;
188 public SectionData (string sectionName, string typeName,
189 bool allowLocation, AllowDefinition allowDefinition)
191 SectionName = sectionName;
193 AllowLocation = allowLocation;
194 AllowDefinition = allowDefinition;
199 class ConfigurationData
201 ConfigurationData parent;
205 static object removedMark = new object ();
206 static object groupMark = new object ();
207 static object emptyMark = new object ();
210 Hashtable FileCache {
215 cache = new Hashtable ();
220 public ConfigurationData () : this (null)
224 public ConfigurationData (ConfigurationData parent)
226 this.parent = (parent == this) ? null : parent;
227 factories = new Hashtable ();
230 public bool Load (string fileName)
232 this.fileName = fileName;
233 if (fileName == null || !File.Exists (fileName))
236 XmlTextReader reader = null;
239 FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
240 reader = new XmlTextReader (fs);
242 ReadConfigFile (reader);
243 } catch (ConfigurationException) {
245 } catch (Exception e) {
246 throw new ConfigurationException ("Error reading " + fileName, e);
255 object GetHandler (string sectionName)
258 object o = factories [sectionName];
259 if (o == null || o == removedMark) {
261 return parent.GetHandler (sectionName);
266 if (o is IConfigurationSectionHandler)
267 return (IConfigurationSectionHandler) o;
269 o = CreateNewHandler (sectionName, (SectionData) o);
270 factories [sectionName] = o;
275 object CreateNewHandler (string sectionName, SectionData section)
277 Type t = Type.GetType (section.TypeName);
279 throw new ConfigurationException ("Cannot get Type for " + section.TypeName);
281 Type iconfig = typeof (IConfigurationSectionHandler);
282 if (!iconfig.IsAssignableFrom (t))
283 throw new ConfigurationException (sectionName + " does not implement " + iconfig);
285 object o = Activator.CreateInstance (t, true);
287 throw new ConfigurationException ("Cannot get instance for " + t);
292 XmlDocument GetInnerDoc (XmlDocument doc, int i, string [] sectionPath)
294 if (++i >= sectionPath.Length)
297 if (doc.DocumentElement == null)
300 XmlNode node = doc.DocumentElement.FirstChild;
301 while (node != null) {
302 if (node.Name == sectionPath [i]) {
303 ConfigXmlDocument result = new ConfigXmlDocument ();
304 result.Load (new StringReader (node.OuterXml));
305 return GetInnerDoc (result, i, sectionPath);
307 node = node.NextSibling;
313 XmlDocument GetDocumentForSection (string sectionName)
315 ConfigXmlDocument doc = new ConfigXmlDocument ();
319 string [] sectionPath = sectionName.Split ('/');
320 string outerxml = pending [sectionPath [0]] as string;
321 if (outerxml == null)
324 StringReader reader = new StringReader (outerxml);
325 XmlTextReader rd = new XmlTextReader (reader);
327 doc.LoadSingleElement (fileName, rd);
329 return GetInnerDoc (doc, 0, sectionPath);
332 object GetConfigInternal (string sectionName)
334 object handler = GetHandler (sectionName);
335 IConfigurationSectionHandler iconf = handler as IConfigurationSectionHandler;
339 object parentConfig = null;
341 parentConfig = parent.GetConfig (sectionName);
343 XmlDocument doc = GetDocumentForSection (sectionName);
344 if (doc == null || doc.DocumentElement == null)
347 return iconf.Create (parentConfig, fileName, doc.DocumentElement);
350 object GetConfigInternal (string sectionName)
355 public object GetConfig (string sectionName)
359 config = this.FileCache [sectionName];
362 if (config == emptyMark)
369 config = GetConfigInternal (sectionName);
370 this.FileCache [sectionName] = (config == null) ? emptyMark : config;
376 private object LookForFactory (string key)
378 object o = factories [key];
383 return parent.LookForFactory (key);
388 private void InitRead (XmlTextReader reader)
390 reader.MoveToContent ();
391 if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
392 ThrowException ("Configuration file does not have a valid root element", reader);
394 if (reader.HasAttributes)
395 ThrowException ("Unrecognized attribute in root element", reader);
397 MoveToNextElement (reader);
400 private void MoveToNextElement (XmlTextReader reader)
402 while (reader.Read ()) {
403 XmlNodeType ntype = reader.NodeType;
404 if (ntype == XmlNodeType.Element)
407 if (ntype != XmlNodeType.Whitespace &&
408 ntype != XmlNodeType.Comment &&
409 ntype != XmlNodeType.SignificantWhitespace &&
410 ntype != XmlNodeType.EndElement)
411 ThrowException ("Unrecognized element", reader);
415 private void ReadSection (XmlTextReader reader, string sectionName)
418 string nameValue = null;
419 string typeValue = null;
420 string allowLoc = null, allowDef = null;
421 bool allowLocation = true;
422 AllowDefinition allowDefinition = AllowDefinition.Everywhere;
424 while (reader.MoveToNextAttribute ()) {
425 attName = reader.Name;
429 if (attName == "allowLocation") {
430 if (allowLoc != null)
431 ThrowException ("Duplicated allowLocation attribute.", reader);
433 allowLoc = reader.Value;
434 allowLocation = (allowLoc == "true");
435 if (!allowLocation && allowLoc != "false")
436 ThrowException ("Invalid attribute value", reader);
441 if (attName == "allowDefinition") {
442 if (allowDef != null)
443 ThrowException ("Duplicated allowDefinition attribute.", reader);
445 allowDef = reader.Value;
447 allowDefinition = (AllowDefinition) Enum.Parse (
448 typeof (AllowDefinition), allowDef);
450 ThrowException ("Invalid attribute value", reader);
456 if (attName == "type") {
457 if (typeValue != null)
458 ThrowException ("Duplicated type attribute.", reader);
459 typeValue = reader.Value;
463 if (attName == "name") {
464 if (nameValue != null)
465 ThrowException ("Duplicated name attribute.", reader);
466 nameValue = reader.Value;
467 if (nameValue == "location")
468 ThrowException ("location is a reserved section name", reader);
472 ThrowException ("Unrecognized attribute.", reader);
475 if (nameValue == null || typeValue == null)
476 ThrowException ("Required attribute missing", reader);
478 if (sectionName != null)
479 nameValue = sectionName + '/' + nameValue;
481 reader.MoveToElement();
482 object o = LookForFactory (nameValue);
483 if (o != null && o != removedMark)
484 ThrowException ("Already have a factory for " + nameValue, reader);
485 SectionData section = new SectionData (nameValue, typeValue, allowLocation, allowDefinition);
486 section.FileName = fileName;
487 factories [nameValue] = section;
489 MoveToNextElement (reader);
492 private void ReadRemoveSection (XmlTextReader reader, string sectionName)
494 if (!reader.MoveToNextAttribute () || reader.Name != "name")
495 ThrowException ("Unrecognized attribute.", reader);
497 string removeValue = reader.Value;
498 if (removeValue == null || removeValue.Length == 0)
499 ThrowException ("Empty name to remove", reader);
501 reader.MoveToElement ();
503 if (sectionName != null)
504 removeValue = sectionName + '/' + removeValue;
506 object o = LookForFactory (removeValue);
507 if (o != null && o == removedMark)
508 ThrowException ("No factory for " + removeValue, reader);
510 factories [removeValue] = removedMark;
511 MoveToNextElement (reader);
514 private void ReadSectionGroup (XmlTextReader reader, string configSection)
516 if (!reader.MoveToNextAttribute ())
517 ThrowException ("sectionGroup must have a 'name' attribute.", reader);
519 if (reader.Name != "name")
520 ThrowException ("Unrecognized attribute.", reader);
522 if (reader.MoveToNextAttribute ())
523 ThrowException ("Unrecognized attribute.", reader);
525 string value = reader.Value;
526 if (value == "location")
527 ThrowException ("location is a reserved section name", reader);
529 if (configSection != null)
530 value = configSection + '/' + value;
532 object o = LookForFactory (value);
533 if (o != null && o != removedMark && o != groupMark)
534 ThrowException ("Already have a factory for " + value, reader);
536 factories [value] = groupMark;
537 MoveToNextElement (reader);
538 ReadSections (reader, value);
541 private void ReadSections (XmlTextReader reader, string configSection)
543 int depth = reader.Depth;
544 while (reader.Depth == depth) {
545 string name = reader.Name;
546 if (name == "section") {
547 ReadSection (reader, configSection);
551 if (name == "remove") {
552 ReadRemoveSection (reader, configSection);
556 if (name == "clear") {
557 if (reader.HasAttributes)
558 ThrowException ("Unrecognized attribute.", reader);
561 MoveToNextElement (reader);
565 if (name == "sectionGroup") {
566 ReadSectionGroup (reader, configSection);
571 ThrowException ("Unrecognized element: " + reader.Name, reader);
575 void StorePending (string name, XmlTextReader reader)
578 pending = new Hashtable ();
580 pending [name] = reader.ReadOuterXml ();
583 private void ReadConfigFile (XmlTextReader reader)
585 int depth = reader.Depth;
586 while (!reader.EOF && reader.Depth == depth) {
587 string name = reader.Name;
588 if (name == "configSections") {
589 if (reader.HasAttributes)
590 ThrowException ("Unrecognized attribute in <configSections>.", reader);
592 MoveToNextElement (reader);
593 if (reader.Depth > depth)
594 ReadSections (reader, null);
595 } else if (name != null && name != "") {
596 StorePending (name, reader);
597 MoveToNextElement (reader);
599 MoveToNextElement (reader);
604 private void ThrowException (string text, XmlTextReader reader)
606 throw new ConfigurationException (text, fileName, reader.LineNumber);