ConfigurationSettings.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. //
  2. // System.Configuration.ConfigurationSettings.cs
  3. //
  4. // Author:
  5. // Christopher Podurgiel ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // C) Christopher Podurgiel
  9. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.Collections.Specialized;
  14. using System.IO;
  15. using System.Reflection;
  16. using System.Runtime.CompilerServices;
  17. using System.Xml;
  18. using System.Xml.XPath;
  19. namespace System.Configuration
  20. {
  21. public sealed class ConfigurationSettings
  22. {
  23. static IConfigurationSystem config;
  24. private ConfigurationSettings ()
  25. {
  26. }
  27. public static object GetConfig (string sectionName)
  28. {
  29. if (config == null)
  30. config = DefaultConfig.GetInstance ();
  31. return config.GetConfig (sectionName);
  32. }
  33. public static NameValueCollection AppSettings
  34. {
  35. get {
  36. object appSettings = GetConfig ("appSettings");
  37. if (appSettings == null)
  38. appSettings = new NameValueCollection ();
  39. return (NameValueCollection) appSettings;
  40. }
  41. }
  42. }
  43. //
  44. // class DefaultConfig: read configuration from machine.config file and application
  45. // config file if available.
  46. //
  47. class DefaultConfig : IConfigurationSystem
  48. {
  49. static string creatingInstance = "137213797382-asad";
  50. static string buildingData = "1797382-ladgasjkdg";
  51. static DefaultConfig instance;
  52. ConfigurationData config;
  53. private DefaultConfig ()
  54. {
  55. }
  56. public static DefaultConfig GetInstance ()
  57. {
  58. if (instance == null) {
  59. lock (creatingInstance) {
  60. if (instance == null) {
  61. instance = new DefaultConfig ();
  62. instance.Init ();
  63. }
  64. }
  65. }
  66. return instance;
  67. }
  68. public object GetConfig (string sectionName)
  69. {
  70. return config.GetConfig (sectionName);
  71. }
  72. public void Init ()
  73. {
  74. if (config == null){
  75. lock (buildingData) {
  76. if (config != null)
  77. return;
  78. ConfigurationData data = new ConfigurationData ();
  79. if (data.Load (GetMachineConfigPath ())) {
  80. ConfigurationData appData = new ConfigurationData (data);
  81. appData.Load (GetAppConfigPath ());
  82. config = appData;
  83. }
  84. }
  85. }
  86. }
  87. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  88. extern private static string get_machine_config_path ();
  89. private static string GetMachineConfigPath ()
  90. {
  91. return get_machine_config_path ();
  92. }
  93. private static string GetAppConfigPath ()
  94. {
  95. AppDomainSetup currentInfo = AppDomain.CurrentDomain.SetupInformation;
  96. string appBase = currentInfo.ApplicationBase;
  97. string configFile = currentInfo.ConfigurationFile;
  98. // FIXME: need to check out default domain configuration file name
  99. if (configFile == null || configFile.Length == 0)
  100. return null;
  101. return Path.Combine (appBase, configFile);
  102. }
  103. }
  104. class ConfigurationData
  105. {
  106. ConfigurationData parent;
  107. Hashtable factories;
  108. string fileName;
  109. object removedMark = new object ();
  110. object groupMark = new object ();
  111. public ConfigurationData () : this (null)
  112. {
  113. }
  114. public ConfigurationData (ConfigurationData parent)
  115. {
  116. this.parent = (parent == this) ? null : parent;
  117. factories = new Hashtable ();
  118. }
  119. public bool Load (string fileName)
  120. {
  121. if (fileName == null)
  122. return false;
  123. this.fileName = fileName;
  124. XmlTextReader reader = null;
  125. try {
  126. reader = new XmlTextReader (fileName);
  127. InitRead (reader);
  128. MoveToNextElement (reader);
  129. ReadSections (reader, null);
  130. } finally {
  131. if (reader != null)
  132. reader.Close();
  133. }
  134. return true;
  135. }
  136. object GetHandler (string sectionName)
  137. {
  138. object o = factories [sectionName];
  139. if (o == null || o == removedMark) {
  140. if (parent != null)
  141. return parent.GetConfig (sectionName);
  142. return null;
  143. }
  144. if (o is IConfigurationSectionHandler)
  145. return (IConfigurationSectionHandler) o;
  146. string [] typeInfo = ((string) o).Split (',');
  147. Type t;
  148. // Hack. Type.GetType should be enough
  149. if (typeInfo.Length > 1) {
  150. Assembly ass = Assembly.Load (typeInfo [1].Trim ());
  151. t = ass.GetType (typeInfo [0].Trim ());
  152. } else {
  153. t = Type.GetType (typeInfo [0]);
  154. }
  155. if (t == null)
  156. throw new ConfigurationException ("Cannot get Type for " + o);
  157. Type iconfig = typeof (IConfigurationSectionHandler);
  158. if (!iconfig.IsAssignableFrom (t))
  159. throw new ConfigurationException (sectionName + " does not implement " + iconfig);
  160. o = Activator.CreateInstance (t, true);
  161. if (o == null)
  162. throw new ConfigurationException ("Cannot get instance for " + t);
  163. factories [sectionName] = o;
  164. return o;
  165. }
  166. //TODO: Should use XPath when it works properly for this.
  167. XmlDocument GetDocumentForSection (string sectionName)
  168. {
  169. ConfigXmlDocument doc = new ConfigXmlDocument ();
  170. XmlTextReader reader = new XmlTextReader (fileName);
  171. InitRead (reader);
  172. string [] sectionPath = sectionName.Split ('/');
  173. int i = 0;
  174. if (!reader.EOF) {
  175. reader.Skip ();
  176. while (!reader.EOF) {
  177. if (reader.NodeType == XmlNodeType.Element &&
  178. reader.Name == sectionPath [i]) {
  179. if (++i == sectionPath.Length) {
  180. doc.LoadSingleElement (fileName, reader);
  181. break;
  182. }
  183. MoveToNextElement (reader);
  184. continue;
  185. }
  186. reader.Skip ();
  187. if (reader.NodeType != XmlNodeType.Element)
  188. MoveToNextElement (reader);
  189. }
  190. }
  191. reader.Close ();
  192. return doc;
  193. }
  194. public object GetConfig (string sectionName)
  195. {
  196. object handler = GetHandler (sectionName);
  197. if (!(handler is IConfigurationSectionHandler))
  198. return handler;
  199. XmlDocument doc = GetDocumentForSection (sectionName);
  200. if (doc == null)
  201. throw new ConfigurationException ("Section not found: " + sectionName);
  202. return ((IConfigurationSectionHandler) handler).Create (null, null, doc);
  203. }
  204. private object LookForFactory (string key)
  205. {
  206. object o = factories [key];
  207. if (o != null)
  208. return o;
  209. if (parent != null)
  210. return parent.LookForFactory (key);
  211. return null;
  212. }
  213. private void InitRead (XmlTextReader reader)
  214. {
  215. reader.MoveToContent ();
  216. if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
  217. ThrowException ("Configuration file does not have a valid root element", reader);
  218. if (reader.HasAttributes)
  219. ThrowException ("Unrecognized attribute in root element", reader);
  220. MoveToNextElement (reader);
  221. if (reader.Depth == 1 && reader.Name == "configSections") {
  222. if (reader.HasAttributes)
  223. ThrowException ("Unrecognized attribute in configSections element.",
  224. reader);
  225. }
  226. }
  227. private void MoveToNextElement (XmlTextReader reader)
  228. {
  229. while (reader.Read ()) {
  230. XmlNodeType ntype = reader.NodeType;
  231. if (ntype == XmlNodeType.Element)
  232. return;
  233. if (ntype != XmlNodeType.Whitespace &&
  234. ntype != XmlNodeType.Comment &&
  235. ntype != XmlNodeType.SignificantWhitespace &&
  236. ntype != XmlNodeType.EndElement)
  237. ThrowException ("Unrecognized element", reader);
  238. }
  239. }
  240. private void ReadSection (XmlTextReader reader, string sectionName)
  241. {
  242. string attName;
  243. string nameValue = null;
  244. string typeValue = null;
  245. while (reader.MoveToNextAttribute ()) {
  246. attName = reader.Name;
  247. if (attName == null)
  248. continue;
  249. if (attName == "allowLocation" || attName == "allowDefinition")
  250. continue;
  251. if (attName == "type") {
  252. if (typeValue != null)
  253. ThrowException ("Duplicated type attribute.", reader);
  254. typeValue = reader.Value;
  255. continue;
  256. }
  257. if (attName == "name") {
  258. if (nameValue != null)
  259. ThrowException ("Duplicated name attribute.", reader);
  260. nameValue = reader.Value;
  261. continue;
  262. }
  263. ThrowException ("Unrecognized attribute.", reader);
  264. }
  265. if (nameValue == null || typeValue == null)
  266. ThrowException ("Required attribute missing", reader);
  267. if (sectionName != null)
  268. nameValue = sectionName + '/' + nameValue;
  269. reader.MoveToElement();
  270. object o = LookForFactory (nameValue);
  271. if (o != null && o != removedMark)
  272. ThrowException ("Already have a factory for " + nameValue, reader);
  273. factories [nameValue] = typeValue;
  274. MoveToNextElement (reader);
  275. }
  276. private void ReadRemoveSection (XmlTextReader reader, string sectionName)
  277. {
  278. if (!reader.MoveToNextAttribute () || reader.Name != "name")
  279. ThrowException ("Unrecognized attribute.", reader);
  280. string removeValue = reader.Value;
  281. if (removeValue == null || removeValue.Length == 0)
  282. ThrowException ("Empty name to remove", reader);
  283. reader.MoveToElement ();
  284. if (sectionName != null)
  285. removeValue = sectionName + '/' + removeValue;
  286. object o = LookForFactory (removeValue);
  287. if (o != null && o != removedMark)
  288. ThrowException ("No factory for " + removeValue, reader);
  289. factories [removeValue] = removedMark;
  290. MoveToNextElement (reader);
  291. }
  292. private void ReadSectionGroup (XmlTextReader reader, string configSection)
  293. {
  294. if (!reader.MoveToNextAttribute ())
  295. ThrowException ("sectionGroup must have a 'name' attribute.", reader);
  296. if (reader.Name != "name")
  297. ThrowException ("Unrecognized attribute.", reader);
  298. if (reader.MoveToNextAttribute ())
  299. ThrowException ("Unrecognized attribute.", reader);
  300. string value = reader.Value;
  301. if (configSection != null)
  302. value = configSection + '/' + value;
  303. object o = LookForFactory (value);
  304. if (o != null && o != removedMark)
  305. ThrowException ("Already have a factory for " + value, reader);
  306. factories [value] = groupMark;
  307. MoveToNextElement (reader);
  308. ReadSections (reader, value);
  309. }
  310. private void ReadSections (XmlTextReader reader, string configSection)
  311. {
  312. int depth = reader.Depth;
  313. while (reader.Depth == depth) {
  314. string name = reader.Name;
  315. if (reader.Name == null)
  316. continue;
  317. if (name == "section") {
  318. ReadSection (reader, configSection);
  319. continue;
  320. }
  321. if (name == "remove") {
  322. ReadRemoveSection (reader, configSection);
  323. continue;
  324. }
  325. if (name == "clear") {
  326. if (reader.HasAttributes)
  327. ThrowException ("Unrecognized attribute.", reader);
  328. factories.Clear ();
  329. continue;
  330. }
  331. if (name == "sectionGroup") {
  332. ReadSectionGroup (reader, configSection);
  333. continue;
  334. }
  335. ThrowException ("Unrecognized element", reader);
  336. }
  337. }
  338. private void ThrowException (string text, XmlTextReader reader)
  339. {
  340. throw new ConfigurationException (text, fileName, reader.LineNumber);
  341. }
  342. }
  343. }