ConfigurationSettings.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // System.Configuration.ConfigurationSettings.cs
  3. //
  4. // Author:
  5. // Christopher Podurgiel ([email protected])
  6. //
  7. // C) Christopher Podurgiel
  8. //
  9. using System;
  10. using System.Collections.Specialized;
  11. using System.Reflection;
  12. using System.Xml;
  13. using System.Xml.XPath;
  14. namespace System.Configuration
  15. {
  16. /// <summary>
  17. /// Component class.
  18. /// </summary>
  19. /// <remarks>
  20. /// Longer description
  21. /// </remarks>
  22. public sealed class ConfigurationSettings
  23. {
  24. private static string applicationConfigFileName;
  25. /// <summary>
  26. /// ConfigurationSettings Constructor.
  27. /// </summary>
  28. private ConfigurationSettings ()
  29. {
  30. }
  31. /// <summary>
  32. /// Returns configuration settings for a user-defined configuration section.
  33. /// </summary>
  34. /// <param name="sectionName">The name of the configuration section that configuration settings are read from.</param>
  35. /// <returns></returns>
  36. public static object GetConfig(string sectionName)
  37. {
  38. //Create an instance of an XML Document.
  39. XmlDocument ConfigurationDocument = new XmlDocument();
  40. /*
  41. * LAMESPEC: The .config file that needs to be parsed is the name of the application, plus ".config"
  42. * ie. "Myapplication.exe.config"
  43. * The only way I could find to get the name of the application is through System.Forms.Application.ExecutablePath, this
  44. * may be an incorrect way to get this information. It works properly on a windows machine when building an executable,
  45. * however, I'm not sure how this would work under other platforms.
  46. */
  47. //Get the full path to the Applicaton Configuration File.
  48. applicationConfigFileName = "FIXME:ConfigurationSettings" + ".config";
  49. //Try to load the XML Document.
  50. try
  51. {
  52. ConfigurationDocument.Load(applicationConfigFileName);
  53. }
  54. catch(XmlException e)
  55. {
  56. //Error loading the XML Document. Throw a ConfigurationException.
  57. throw(new ConfigurationException(e.Message, applicationConfigFileName, e.LineNumber));
  58. }
  59. string sectionHandlerName = GetSectionHanderType(ConfigurationDocument, sectionName);
  60. XmlNode sectionNode = ConfigurationDocument.SelectSingleNode("/configuration/" + sectionName);
  61. //If the specified sectionName is not found, then sectionNode will be null. When calling objNVSHandler.Create(),
  62. //sectionNode cannot be null.
  63. if(sectionNode == null)
  64. {
  65. return null;
  66. }
  67. //Create a new SectionHandler
  68. //According to the Docs provided by Microsoft, the user can create their own configuration sections, and create a custom
  69. //handler class for it. The user would specify the class and its assebly in the <configSections> section. These would be
  70. //seperated by a comma.
  71. string sectionHandlerClassName = sectionHandlerName;
  72. string sectionHandlerAssemblyName = "System";
  73. //Split the SectionHandler Class Name from the Assembly Name (if provided).
  74. string[] sectionHandlerArray = sectionHandlerName.Split(new char[]{','}, 2);
  75. if(sectionHandlerArray.Length == 2)
  76. {
  77. sectionHandlerClassName = sectionHandlerArray[0];
  78. sectionHandlerAssemblyName = sectionHandlerArray[1];
  79. }
  80. // Load the assembly to use.
  81. Assembly assem = Assembly.Load(sectionHandlerAssemblyName);
  82. //Get the class type.
  83. Type handlerObjectType = assem.GetType(sectionHandlerClassName);
  84. //Get a reference to the method "Create"
  85. MethodInfo createMethod = handlerObjectType.GetMethod("Create");
  86. //Create an Instance of this SectionHandler.
  87. Object objSectionHandler = Activator.CreateInstance(handlerObjectType);
  88. //define the arguments to be passed to the "Create" Method.
  89. Object[] args = new Object[3];
  90. args[0] = null;
  91. args[1] = null;
  92. args[2] = sectionNode;
  93. object sectionHandlerCollection = createMethod.Invoke(objSectionHandler, args);
  94. //Return the collection
  95. return sectionHandlerCollection;
  96. }
  97. /// <summary>
  98. /// Gets the name of the SectionHander Class that will handle this section.
  99. /// </summary>
  100. /// <param name="xmlDoc">An xml Configuration Document.</param>
  101. /// <param name="sectionName">The name of the configuration section that configuration settings are read from.</param>
  102. /// <returns>The name of the Handler Object for this configuration section, including the name if its Assembly.</returns>
  103. [MonoTODO]
  104. private static string GetSectionHanderType(XmlDocument xmlDoc, string sectionName)
  105. {
  106. //TODO: This method does not account for sectionGroups yet.
  107. string handlerName = null;
  108. //<appSettings> is a predefined configuration section. It does not have a definition
  109. // in the <configSections> section, and will always be handled by the NameValueSectionHandler.
  110. if(sectionName == "appSettings")
  111. {
  112. handlerName = "System.Configuration.NameValueSectionHandler";
  113. }
  114. else
  115. {
  116. string[] sectionPathArray = sectionName.Split(new char[]{'/'});
  117. //Build an XPath statement.
  118. string xpathStatement = "/configuration/configSections";
  119. for (int i=0; i < sectionPathArray.Length; i++)
  120. {
  121. if(i < sectionPathArray.Length - 1)
  122. {
  123. xpathStatement = xpathStatement + "/sectionGroup[@name='" + sectionPathArray[i] + "']";
  124. }
  125. else
  126. {
  127. xpathStatement = xpathStatement + "/section[@name='" + sectionPathArray[i] + "']";
  128. }
  129. }
  130. //Get all of the <section> node using the xpath statement.
  131. XmlNode sectionNode = xmlDoc.SelectSingleNode(xpathStatement);
  132. // if this section isn't found, then there was something wrong with the config document,
  133. // or the sectionName didn't have a proper definition.
  134. if(sectionNode == null)
  135. {
  136. throw (new ConfigurationException("Unrecognized element."));
  137. }
  138. handlerName = sectionNode.Attributes["type"].Value;
  139. }
  140. //Return the name of the handler.
  141. return handlerName;
  142. }
  143. /// <summary>
  144. /// Get the Application Configuration Settings.
  145. /// </summary>
  146. public static NameValueCollection AppSettings
  147. {
  148. get
  149. {
  150. //Get the Configuration Settings for the "appSettings" section.
  151. NameValueCollection appSettings = (NameValueCollection)GetConfig("appSettings");;
  152. return appSettings;
  153. }
  154. }
  155. }
  156. }