ConfigurationSettings.cs 11 KB

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