ConfigurationSettings.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. if (config == null)
  71. return null;
  72. return config.GetConfig (sectionName);
  73. }
  74. public void Init ()
  75. {
  76. if (config == null){
  77. lock (buildingData) {
  78. if (config != null)
  79. return;
  80. ConfigurationData data = new ConfigurationData ();
  81. if (data.Load (GetMachineConfigPath ())) {
  82. ConfigurationData appData = new ConfigurationData (data);
  83. appData.Load (GetAppConfigPath ());
  84. config = appData;
  85. } else {
  86. Console.WriteLine ("** Warning **: cannot find " + GetMachineConfigPath ());
  87. Console.WriteLine ("Trying to load app config file...");
  88. data.Load (GetAppConfigPath ());
  89. config = data;
  90. }
  91. }
  92. }
  93. }
  94. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  95. extern private static string get_machine_config_path ();
  96. private static string GetMachineConfigPath ()
  97. {
  98. return get_machine_config_path ();
  99. }
  100. private static string GetAppConfigPath ()
  101. {
  102. /* FIXME: Uncomment and use this stuff when the current domain is initialized with
  103. the appropiate information.
  104. AppDomainSetup currentInfo = AppDomain.CurrentDomain.SetupInformation;
  105. string appBase = currentInfo.ApplicationBase;
  106. string configFile = currentInfo.ConfigurationFile;
  107. // FIXME: need to check out default domain configuration file name
  108. if (configFile == null || configFile.Length == 0)
  109. return null;
  110. */
  111. // Remove when uncomment the previous comment
  112. string assemblyName = Assembly.GetEntryAssembly ().FullName;
  113. string appBase = Path.GetDirectoryName (assemblyName);
  114. string configFile = Path.GetFileName (assemblyName) + ".exe.config";
  115. // End remove
  116. return Path.Combine (appBase, configFile);
  117. }
  118. }
  119. class ConfigurationData
  120. {
  121. ConfigurationData parent;
  122. Hashtable factories;
  123. string fileName;
  124. object removedMark = new object ();
  125. object groupMark = new object ();
  126. public ConfigurationData () : this (null)
  127. {
  128. }
  129. public ConfigurationData (ConfigurationData parent)
  130. {
  131. this.parent = (parent == this) ? null : parent;
  132. factories = new Hashtable ();
  133. }
  134. public bool Load (string fileName)
  135. {
  136. if (fileName == null)
  137. return false;
  138. this.fileName = fileName;
  139. XmlTextReader reader = null;
  140. try {
  141. try {
  142. reader = new XmlTextReader (fileName);
  143. } catch {
  144. return false;
  145. }
  146. InitRead (reader);
  147. ReadConfigFile (reader);
  148. } finally {
  149. if (reader != null)
  150. reader.Close();
  151. }
  152. return true;
  153. }
  154. object GetHandler (string sectionName)
  155. {
  156. object o = factories [sectionName];
  157. if (o == null || o == removedMark) {
  158. if (parent != null)
  159. return parent.GetHandler (sectionName);
  160. return null;
  161. }
  162. if (o is IConfigurationSectionHandler)
  163. return (IConfigurationSectionHandler) o;
  164. string [] typeInfo = ((string) o).Split (',');
  165. Type t;
  166. // Hack. Type.GetType should be enough
  167. if (typeInfo.Length > 1) {
  168. Assembly ass = Assembly.Load (typeInfo [1].Trim ());
  169. t = ass.GetType (typeInfo [0].Trim ());
  170. } else {
  171. t = Type.GetType (typeInfo [0]);
  172. }
  173. if (t == null)
  174. throw new ConfigurationException ("Cannot get Type for " + o);
  175. Type iconfig = typeof (IConfigurationSectionHandler);
  176. if (!iconfig.IsAssignableFrom (t))
  177. throw new ConfigurationException (sectionName + " does not implement " + iconfig);
  178. o = Activator.CreateInstance (t, true);
  179. if (o == null)
  180. throw new ConfigurationException ("Cannot get instance for " + t);
  181. factories [sectionName] = o;
  182. return o;
  183. }
  184. //TODO: Should use XPath when it works properly for this.
  185. XmlDocument GetDocumentForSection (string sectionName)
  186. {
  187. ConfigXmlDocument doc = new ConfigXmlDocument ();
  188. XmlTextReader reader = null;
  189. try {
  190. reader = new XmlTextReader (fileName);
  191. } catch {
  192. return doc;
  193. }
  194. InitRead (reader);
  195. string [] sectionPath = sectionName.Split ('/');
  196. int i = 0;
  197. if (!reader.EOF) {
  198. if (reader.Name == "configSections")
  199. reader.Skip ();
  200. while (!reader.EOF) {
  201. if (reader.NodeType == XmlNodeType.Element &&
  202. reader.Name == sectionPath [i]) {
  203. if (++i == sectionPath.Length) {
  204. doc.LoadSingleElement (fileName, reader);
  205. break;
  206. }
  207. MoveToNextElement (reader);
  208. continue;
  209. }
  210. reader.Skip ();
  211. if (reader.NodeType != XmlNodeType.Element)
  212. MoveToNextElement (reader);
  213. }
  214. }
  215. reader.Close ();
  216. return doc;
  217. }
  218. public object GetConfig (string sectionName)
  219. {
  220. object handler = GetHandler (sectionName);
  221. if (handler == null)
  222. return null;
  223. if (!(handler is IConfigurationSectionHandler))
  224. return handler;
  225. object parentConfig = null;
  226. if (parent != null)
  227. parentConfig = parent.GetConfig (sectionName);
  228. XmlDocument doc = GetDocumentForSection (sectionName);
  229. if (doc == null) {
  230. if (parentConfig == null)
  231. return null;
  232. return parentConfig;
  233. }
  234. return ((IConfigurationSectionHandler) handler).Create (parentConfig, null, doc);
  235. }
  236. private object LookForFactory (string key)
  237. {
  238. object o = factories [key];
  239. if (o != null)
  240. return o;
  241. if (parent != null)
  242. return parent.LookForFactory (key);
  243. return null;
  244. }
  245. private void InitRead (XmlTextReader reader)
  246. {
  247. reader.MoveToContent ();
  248. if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
  249. ThrowException ("Configuration file does not have a valid root element", reader);
  250. if (reader.HasAttributes)
  251. ThrowException ("Unrecognized attribute in root element", reader);
  252. MoveToNextElement (reader);
  253. }
  254. private void MoveToNextElement (XmlTextReader reader)
  255. {
  256. while (reader.Read ()) {
  257. XmlNodeType ntype = reader.NodeType;
  258. if (ntype == XmlNodeType.Element)
  259. return;
  260. if (ntype != XmlNodeType.Whitespace &&
  261. ntype != XmlNodeType.Comment &&
  262. ntype != XmlNodeType.SignificantWhitespace &&
  263. ntype != XmlNodeType.EndElement)
  264. ThrowException ("Unrecognized element", reader);
  265. }
  266. }
  267. private void ReadSection (XmlTextReader reader, string sectionName)
  268. {
  269. string attName;
  270. string nameValue = null;
  271. string typeValue = null;
  272. while (reader.MoveToNextAttribute ()) {
  273. attName = reader.Name;
  274. if (attName == null)
  275. continue;
  276. if (attName == "allowLocation" || attName == "allowDefinition")
  277. continue;
  278. if (attName == "type") {
  279. if (typeValue != null)
  280. ThrowException ("Duplicated type attribute.", reader);
  281. typeValue = reader.Value;
  282. continue;
  283. }
  284. if (attName == "name") {
  285. if (nameValue != null)
  286. ThrowException ("Duplicated name attribute.", reader);
  287. nameValue = reader.Value;
  288. continue;
  289. }
  290. ThrowException ("Unrecognized attribute.", reader);
  291. }
  292. if (nameValue == null || typeValue == null)
  293. ThrowException ("Required attribute missing", reader);
  294. if (sectionName != null)
  295. nameValue = sectionName + '/' + nameValue;
  296. reader.MoveToElement();
  297. object o = LookForFactory (nameValue);
  298. if (o != null && o != removedMark)
  299. ThrowException ("Already have a factory for " + nameValue, reader);
  300. factories [nameValue] = typeValue;
  301. MoveToNextElement (reader);
  302. }
  303. private void ReadRemoveSection (XmlTextReader reader, string sectionName)
  304. {
  305. if (!reader.MoveToNextAttribute () || reader.Name != "name")
  306. ThrowException ("Unrecognized attribute.", reader);
  307. string removeValue = reader.Value;
  308. if (removeValue == null || removeValue.Length == 0)
  309. ThrowException ("Empty name to remove", reader);
  310. reader.MoveToElement ();
  311. if (sectionName != null)
  312. removeValue = sectionName + '/' + removeValue;
  313. object o = LookForFactory (removeValue);
  314. if (o != null && o != removedMark)
  315. ThrowException ("No factory for " + removeValue, reader);
  316. factories [removeValue] = removedMark;
  317. MoveToNextElement (reader);
  318. }
  319. private void ReadSectionGroup (XmlTextReader reader, string configSection)
  320. {
  321. if (!reader.MoveToNextAttribute ())
  322. ThrowException ("sectionGroup must have a 'name' attribute.", reader);
  323. if (reader.Name != "name")
  324. ThrowException ("Unrecognized attribute.", reader);
  325. if (reader.MoveToNextAttribute ())
  326. ThrowException ("Unrecognized attribute.", reader);
  327. string value = reader.Value;
  328. if (configSection != null)
  329. value = configSection + '/' + value;
  330. object o = LookForFactory (value);
  331. if (o != null && o != removedMark)
  332. ThrowException ("Already have a factory for " + value, reader);
  333. factories [value] = groupMark;
  334. MoveToNextElement (reader);
  335. ReadSections (reader, value);
  336. }
  337. private void ReadSections (XmlTextReader reader, string configSection)
  338. {
  339. int depth = reader.Depth;
  340. while (reader.Depth == depth) {
  341. string name = reader.Name;
  342. if (name == "section") {
  343. ReadSection (reader, configSection);
  344. continue;
  345. }
  346. if (name == "remove") {
  347. ReadRemoveSection (reader, configSection);
  348. continue;
  349. }
  350. if (name == "clear") {
  351. if (reader.HasAttributes)
  352. ThrowException ("Unrecognized attribute.", reader);
  353. factories.Clear ();
  354. MoveToNextElement (reader);
  355. continue;
  356. }
  357. if (name == "sectionGroup") {
  358. ReadSectionGroup (reader, configSection);
  359. continue;
  360. }
  361. ThrowException ("Unrecognized element: " + reader.Name, reader);
  362. }
  363. }
  364. private void ReadConfigFile (XmlTextReader reader)
  365. {
  366. int depth = reader.Depth;
  367. while (reader.Depth == depth) {
  368. string name = reader.Name;
  369. if (name == "configSections") {
  370. if (reader.HasAttributes)
  371. ThrowException ("Unrecognized attribute in configSections element.", reader);
  372. MoveToNextElement (reader);
  373. ReadSections (reader, null);
  374. return;
  375. }
  376. MoveToNextElement (reader);
  377. }
  378. }
  379. private void ThrowException (string text, XmlTextReader reader)
  380. {
  381. throw new ConfigurationException (text, fileName, reader.LineNumber);
  382. }
  383. }
  384. }