Configuration.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. //
  2. // System.Configuration.ConfigurationLocation.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  28. //
  29. #if NET_2_0 && XML_DEP
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Specialized;
  33. using System.Xml;
  34. using System.IO;
  35. namespace System.Configuration {
  36. public sealed class Configuration
  37. {
  38. Configuration parent;
  39. Hashtable elementData = new Hashtable ();
  40. string fileName;
  41. ConfigurationSectionGroup rootSectionGroup;
  42. ConfigurationLocationCollection locations;
  43. SectionGroupInfo rootGroup;
  44. internal Configuration (): this (null, null)
  45. {
  46. }
  47. internal Configuration (string file): this (file, null)
  48. {
  49. }
  50. internal Configuration (Configuration parent): this (null, parent)
  51. {
  52. }
  53. internal Configuration (string file, Configuration parent)
  54. {
  55. fileName = file;
  56. this.parent = parent;
  57. if (parent != null)
  58. rootGroup = parent.rootGroup;
  59. else {
  60. rootGroup = new SectionGroupInfo ();
  61. rootGroup.FileName = file;
  62. }
  63. if (file != null) Load (file);
  64. }
  65. internal Configuration Parent {
  66. get { return parent; }
  67. }
  68. internal string FileName {
  69. get { return fileName; }
  70. }
  71. [MonoTODO]
  72. public AppSettingsSection AppSettings {
  73. get { throw new NotImplementedException (); }
  74. }
  75. [MonoTODO]
  76. public PathLevel ConfigurationPathLevel {
  77. get { throw new NotImplementedException (); }
  78. }
  79. [MonoTODO]
  80. public ConnectionStringsSection ConnectionStrings {
  81. get { throw new NotImplementedException (); }
  82. }
  83. [MonoTODO]
  84. public string FilePath {
  85. get { throw new NotImplementedException (); }
  86. }
  87. [MonoTODO]
  88. public bool HasFile {
  89. get { throw new NotImplementedException (); }
  90. }
  91. public ConfigurationLocationCollection Locations {
  92. get {
  93. if (locations == null) locations = new ConfigurationLocationCollection ();
  94. return locations;
  95. }
  96. }
  97. [MonoTODO]
  98. public string Path {
  99. get { throw new NotImplementedException (); }
  100. }
  101. public ConfigurationSectionGroup RootSectionGroup {
  102. get {
  103. if (rootSectionGroup == null) {
  104. rootSectionGroup = new ConfigurationSectionGroup ();
  105. rootSectionGroup.Initialize (this, rootGroup);
  106. }
  107. return rootSectionGroup;
  108. }
  109. }
  110. public ConfigurationSectionGroupCollection SectionGroups {
  111. get { return RootSectionGroup.SectionGroups; }
  112. }
  113. public ConfigurationSectionCollection Sections {
  114. get { return RootSectionGroup.Sections; }
  115. }
  116. public static Configuration GetExeConfiguration (string path, ConfigurationUserLevel level)
  117. {
  118. return new Configuration (path + ".config", GetMachineConfiguration ());
  119. }
  120. public static Configuration GetMachineConfiguration ()
  121. {
  122. return GetMachineConfiguration (System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile);
  123. }
  124. public static Configuration GetMachineConfiguration (string path)
  125. {
  126. return new Configuration (path);
  127. }
  128. [MonoTODO]
  129. public static Configuration GetMachineConfiguration (string path, string server)
  130. {
  131. throw new NotImplementedException ();
  132. }
  133. [MonoTODO]
  134. public static Configuration GetMachineConfiguration (
  135. string path, string server, IntPtr user_token)
  136. {
  137. throw new NotImplementedException ();
  138. }
  139. [MonoTODO]
  140. public static Configuration GetMachineConfiguration (
  141. string path, string server, string username, string password)
  142. {
  143. throw new NotImplementedException ();
  144. }
  145. [MonoTODO]
  146. public static Configuration GetWebConfiguration ()
  147. {
  148. throw new NotImplementedException ();
  149. }
  150. [MonoTODO]
  151. public static Configuration GetWebConfiguration (string path)
  152. {
  153. return new Configuration (path, GetMachineConfiguration ());
  154. }
  155. [MonoTODO]
  156. public static Configuration GetWebConfiguration (string path, string site)
  157. {
  158. throw new NotImplementedException ();
  159. }
  160. [MonoTODO]
  161. public static Configuration GetWebConfiguration (string path, string site, string subpath)
  162. {
  163. throw new NotImplementedException ();
  164. }
  165. [MonoTODO]
  166. public static Configuration GetWebConfiguration (
  167. string path, string site, string subpath, string server)
  168. {
  169. throw new NotImplementedException ();
  170. }
  171. [MonoTODO]
  172. public static Configuration GetWebConfiguration (
  173. string path, string site, string subpath, string server, IntPtr user_token)
  174. {
  175. throw new NotImplementedException ();
  176. }
  177. [MonoTODO]
  178. public static Configuration GetWebConfiguration (
  179. string path, string site, string subpath, string server, string username, string password)
  180. {
  181. throw new NotImplementedException ();
  182. }
  183. internal ConfigurationSection GetSectionInstance (SectionInfo config, bool createDefaultInstance)
  184. {
  185. object data = elementData [config];
  186. ConfigurationSection sec = data as ConfigurationSection;
  187. if (sec != null || !createDefaultInstance) return sec;
  188. object secObj = config.CreateInstance () as ConfigurationSection;
  189. if (!(secObj is ConfigurationSection))
  190. sec = new RuntimeOnlySection ();
  191. else
  192. sec = (ConfigurationSection) secObj;
  193. ConfigurationSection parentSection = parent != null ? parent.GetSectionInstance (config, true) : null;
  194. sec.RawXml = data as string;
  195. sec.Reset (parentSection, this);
  196. if (data != null) {
  197. XmlTextReader r = new XmlTextReader (new StringReader (data as string));
  198. sec.ReadXml (r, this);
  199. r.Close ();
  200. }
  201. elementData [config] = sec;
  202. return sec;
  203. }
  204. internal ConfigurationSectionGroup GetSectionGroupInstance (SectionGroupInfo group)
  205. {
  206. ConfigurationSectionGroup gr = group.CreateInstance () as ConfigurationSectionGroup;
  207. if (gr != null) gr.Initialize (this, group);
  208. return gr;
  209. }
  210. internal void SetConfigurationSection (SectionInfo config, ConfigurationSection sec)
  211. {
  212. elementData [config] = sec;
  213. }
  214. internal void SetSectionData (SectionInfo config, string data)
  215. {
  216. elementData [config] = data;
  217. }
  218. internal void CreateSection (SectionGroupInfo group, string name, ConfigurationSection sec)
  219. {
  220. if (group.HasChild (name)) throw new ConfigurationException ("Cannot add a ConfigurationSection. A section or section group already exists with the name '" + name + "'");
  221. if (sec.TypeName == null) sec.TypeName = sec.GetType ().AssemblyQualifiedName;
  222. sec.SetName (name);
  223. SectionInfo section = new SectionInfo (name, sec.TypeName, sec.AllowLocation, sec.AllowDefinition);
  224. section.FileName = FileName;
  225. group.AddChild (section);
  226. elementData [section] = sec;
  227. }
  228. internal void CreateSectionGroup (SectionGroupInfo parentGroup, string name, ConfigurationSectionGroup sec)
  229. {
  230. if (parentGroup.HasChild (name)) throw new ConfigurationException ("Cannot add a ConfigurationSectionGroup. A section or section group already exists with the name '" + name + "'");
  231. if (sec.TypeName == null) sec.TypeName = sec.GetType ().AssemblyQualifiedName;
  232. sec.SetName (name);
  233. SectionGroupInfo section = new SectionGroupInfo (name, sec.TypeName);
  234. section.FileName = FileName;
  235. parentGroup.AddChild (section);
  236. elementData [section] = sec;
  237. }
  238. internal void RemoveConfigInfo (ConfigInfo config)
  239. {
  240. elementData.Remove (config);
  241. }
  242. public void Update ()
  243. {
  244. Update (ConfigurationUpdateMode.Full, true);
  245. }
  246. public void Update (ConfigurationUpdateMode mode)
  247. {
  248. Update (mode, false);
  249. }
  250. [MonoTODO]
  251. public void Update (ConfigurationUpdateMode mode, bool forceUpdateAll)
  252. {
  253. XmlTextWriter tw = new XmlTextWriter (new StreamWriter (fileName));
  254. tw.Formatting = Formatting.Indented;
  255. try {
  256. tw.WriteStartElement ("configuration");
  257. if (rootGroup.HasConfigContent (this)) {
  258. rootGroup.WriteConfig (this, tw, mode);
  259. }
  260. rootGroup.WriteRootData (tw, this, mode);
  261. tw.WriteEndElement ();
  262. }
  263. finally {
  264. tw.Close ();
  265. }
  266. }
  267. internal bool Load (string fileName)
  268. {
  269. this.fileName = fileName;
  270. if (!File.Exists (fileName))
  271. throw new ConfigurationException ("File '" + fileName + "' not found");
  272. #if (XML_DEP)
  273. XmlTextReader reader = null;
  274. try {
  275. FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
  276. reader = new XmlTextReader (fs);
  277. ReadConfigFile (reader, fileName);
  278. /* } catch (ConfigurationException) {
  279. throw;
  280. } catch (Exception e) {
  281. throw new ConfigurationException ("Error reading " + fileName, e);
  282. */ } finally {
  283. if (reader != null)
  284. reader.Close();
  285. }
  286. #endif
  287. return true;
  288. }
  289. #if (XML_DEP)
  290. internal void ReadConfigFile (XmlTextReader reader, string fileName)
  291. {
  292. reader.MoveToContent ();
  293. if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
  294. ThrowException ("Configuration file does not have a valid root element", reader);
  295. if (reader.HasAttributes)
  296. ThrowException ("Unrecognized attribute in root element", reader);
  297. if (reader.IsEmptyElement) {
  298. reader.Skip ();
  299. return;
  300. }
  301. reader.ReadStartElement ();
  302. reader.MoveToContent ();
  303. if (reader.LocalName == "configSections") {
  304. if (reader.HasAttributes)
  305. ThrowException ("Unrecognized attribute in <configSections>.", reader);
  306. rootGroup.ReadConfig (this, reader);
  307. }
  308. rootGroup.ReadRootData (reader, this);
  309. }
  310. #endif
  311. private void ThrowException (string text, XmlTextReader reader)
  312. {
  313. throw new ConfigurationException (text, fileName, reader.LineNumber);
  314. }
  315. }
  316. }
  317. #endif