ConfigurationSettings.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
  132. reader = new XmlTextReader (fs);
  133. } catch (Exception ex) {
  134. return false;
  135. }
  136. InitRead (reader);
  137. ReadConfigFile (reader);
  138. } finally {
  139. if (reader != null)
  140. reader.Close();
  141. }
  142. return true;
  143. }
  144. object GetHandler (string sectionName)
  145. {
  146. object o = factories [sectionName];
  147. if (o == null || o == removedMark) {
  148. if (parent != null)
  149. return parent.GetHandler (sectionName);
  150. return null;
  151. }
  152. if (o is IConfigurationSectionHandler)
  153. return (IConfigurationSectionHandler) o;
  154. Type t = Type.GetType ((string) o);
  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. XmlTextReader reader = null;
  170. try {
  171. FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
  172. reader = new XmlTextReader (fs);
  173. } catch {
  174. return null;
  175. }
  176. ConfigXmlDocument doc = new ConfigXmlDocument ();
  177. InitRead (reader);
  178. string [] sectionPath = sectionName.Split ('/');
  179. int i = 0;
  180. if (!reader.EOF) {
  181. if (reader.Name == "configSections")
  182. reader.Skip ();
  183. while (!reader.EOF) {
  184. if (reader.NodeType == XmlNodeType.Element &&
  185. reader.Name == sectionPath [i]) {
  186. if (++i == sectionPath.Length) {
  187. doc.LoadSingleElement (fileName, reader);
  188. break;
  189. }
  190. MoveToNextElement (reader);
  191. continue;
  192. }
  193. reader.Skip ();
  194. if (reader.NodeType != XmlNodeType.Element)
  195. MoveToNextElement (reader);
  196. }
  197. }
  198. reader.Close ();
  199. return doc;
  200. }
  201. public object GetConfig (string sectionName)
  202. {
  203. object handler = GetHandler (sectionName);
  204. if (handler == null)
  205. return null;
  206. if (!(handler is IConfigurationSectionHandler))
  207. return handler;
  208. object parentConfig = null;
  209. if (parent != null)
  210. parentConfig = parent.GetConfig (sectionName);
  211. XmlDocument doc = GetDocumentForSection (sectionName);
  212. if (doc == null || doc.DocumentElement == null) {
  213. if (parentConfig == null)
  214. return null;
  215. return parentConfig;
  216. }
  217. return ((IConfigurationSectionHandler) handler).Create (parentConfig, null, doc.DocumentElement);
  218. }
  219. private object LookForFactory (string key)
  220. {
  221. object o = factories [key];
  222. if (o != null)
  223. return o;
  224. if (parent != null)
  225. return parent.LookForFactory (key);
  226. return null;
  227. }
  228. private void InitRead (XmlTextReader reader)
  229. {
  230. reader.MoveToContent ();
  231. if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
  232. ThrowException ("Configuration file does not have a valid root element", reader);
  233. if (reader.HasAttributes)
  234. ThrowException ("Unrecognized attribute in root element", reader);
  235. MoveToNextElement (reader);
  236. }
  237. private void MoveToNextElement (XmlTextReader reader)
  238. {
  239. while (reader.Read ()) {
  240. XmlNodeType ntype = reader.NodeType;
  241. if (ntype == XmlNodeType.Element)
  242. return;
  243. if (ntype != XmlNodeType.Whitespace &&
  244. ntype != XmlNodeType.Comment &&
  245. ntype != XmlNodeType.SignificantWhitespace &&
  246. ntype != XmlNodeType.EndElement)
  247. ThrowException ("Unrecognized element", reader);
  248. }
  249. }
  250. private void ReadSection (XmlTextReader reader, string sectionName)
  251. {
  252. string attName;
  253. string nameValue = null;
  254. string typeValue = null;
  255. while (reader.MoveToNextAttribute ()) {
  256. attName = reader.Name;
  257. if (attName == null)
  258. continue;
  259. if (attName == "allowLocation" || attName == "allowDefinition")
  260. continue;
  261. if (attName == "type") {
  262. if (typeValue != null)
  263. ThrowException ("Duplicated type attribute.", reader);
  264. typeValue = reader.Value;
  265. continue;
  266. }
  267. if (attName == "name") {
  268. if (nameValue != null)
  269. ThrowException ("Duplicated name attribute.", reader);
  270. nameValue = reader.Value;
  271. continue;
  272. }
  273. ThrowException ("Unrecognized attribute.", reader);
  274. }
  275. if (nameValue == null || typeValue == null)
  276. ThrowException ("Required attribute missing", reader);
  277. if (sectionName != null)
  278. nameValue = sectionName + '/' + nameValue;
  279. reader.MoveToElement();
  280. object o = LookForFactory (nameValue);
  281. if (o != null && o != removedMark)
  282. ThrowException ("Already have a factory for " + nameValue, reader);
  283. factories [nameValue] = typeValue;
  284. MoveToNextElement (reader);
  285. }
  286. private void ReadRemoveSection (XmlTextReader reader, string sectionName)
  287. {
  288. if (!reader.MoveToNextAttribute () || reader.Name != "name")
  289. ThrowException ("Unrecognized attribute.", reader);
  290. string removeValue = reader.Value;
  291. if (removeValue == null || removeValue.Length == 0)
  292. ThrowException ("Empty name to remove", reader);
  293. reader.MoveToElement ();
  294. if (sectionName != null)
  295. removeValue = sectionName + '/' + removeValue;
  296. object o = LookForFactory (removeValue);
  297. if (o != null && o == removedMark)
  298. ThrowException ("No factory for " + removeValue, reader);
  299. factories [removeValue] = removedMark;
  300. MoveToNextElement (reader);
  301. }
  302. private void ReadSectionGroup (XmlTextReader reader, string configSection)
  303. {
  304. if (!reader.MoveToNextAttribute ())
  305. ThrowException ("sectionGroup must have a 'name' attribute.", reader);
  306. if (reader.Name != "name")
  307. ThrowException ("Unrecognized attribute.", reader);
  308. if (reader.MoveToNextAttribute ())
  309. ThrowException ("Unrecognized attribute.", reader);
  310. string value = reader.Value;
  311. if (configSection != null)
  312. value = configSection + '/' + value;
  313. object o = LookForFactory (value);
  314. if (o != null && o != removedMark)
  315. ThrowException ("Already have a factory for " + value, reader);
  316. factories [value] = groupMark;
  317. MoveToNextElement (reader);
  318. ReadSections (reader, value);
  319. }
  320. private void ReadSections (XmlTextReader reader, string configSection)
  321. {
  322. int depth = reader.Depth;
  323. while (reader.Depth == depth) {
  324. string name = reader.Name;
  325. if (name == "section") {
  326. ReadSection (reader, configSection);
  327. continue;
  328. }
  329. if (name == "remove") {
  330. ReadRemoveSection (reader, configSection);
  331. continue;
  332. }
  333. if (name == "clear") {
  334. if (reader.HasAttributes)
  335. ThrowException ("Unrecognized attribute.", reader);
  336. factories.Clear ();
  337. MoveToNextElement (reader);
  338. continue;
  339. }
  340. if (name == "sectionGroup") {
  341. ReadSectionGroup (reader, configSection);
  342. continue;
  343. }
  344. ThrowException ("Unrecognized element: " + reader.Name, reader);
  345. }
  346. }
  347. private void ReadConfigFile (XmlTextReader reader)
  348. {
  349. int depth = reader.Depth;
  350. while (reader.Depth == depth) {
  351. string name = reader.Name;
  352. if (name == "configSections") {
  353. if (reader.HasAttributes)
  354. ThrowException ("Unrecognized attribute in configSections element.", reader);
  355. MoveToNextElement (reader);
  356. ReadSections (reader, null);
  357. return;
  358. }
  359. MoveToNextElement (reader);
  360. }
  361. }
  362. private void ThrowException (string text, XmlTextReader reader)
  363. {
  364. throw new ConfigurationException (text, fileName, reader.LineNumber);
  365. }
  366. }
  367. }