svn path=/branches/mono-1-1-9/mono/; revision=51217
[mono.git] / mcs / class / System.Configuration / System.Configuration / Configuration.cs
1 //
2 // System.Configuration.Configuration.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //      Lluis Sanchez Gual (lluis@novell.com)
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
30 using System;
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Reflection;
34 using System.Xml;
35 using System.IO;
36 using System.Configuration.Internal;
37
38 namespace System.Configuration {
39
40         public sealed class Configuration
41         {
42                 Configuration parent;
43                 Hashtable elementData = new Hashtable ();
44                 string streamName;
45                 ConfigurationSectionGroup rootSectionGroup;
46                 ConfigurationLocationCollection locations;
47                 SectionGroupInfo rootGroup;
48                 IConfigSystem system;
49                 bool hasFile;
50                 
51                 string configPath;
52                 string locationConfigPath;
53                         
54                 internal Configuration (Configuration parent)
55                 {
56                         Init (parent.system, null, parent);
57                 }
58                 
59                 internal Configuration (InternalConfigurationSystem system, string locationSubPath)
60                 {
61                         hasFile = true;
62                         this.system = system;
63                         
64                         system.InitForConfiguration (ref locationSubPath, out configPath, out locationConfigPath);
65                         
66                         Configuration parent = null;
67                         
68                         if (locationSubPath != null) {
69                                 parent = new Configuration (system, locationSubPath);
70                                 if (locationConfigPath != null)
71                                         parent = parent.FindLocationConfiguration (locationConfigPath, parent);
72                         }
73                         
74                         Init (system, configPath, parent);
75                 }
76                 
77                 internal Configuration FindLocationConfiguration (string relativePath, Configuration defaultConfiguration)
78                 {
79                         ConfigurationLocation loc = Locations.Find (relativePath);
80                         
81                         Configuration parentConfig = defaultConfiguration;
82                         
83                         if (LocationConfigPath != null) {
84                                 Configuration parentFile = GetParentWithFile ();
85                                 if (parentFile != null) {
86                                         string parentRelativePath = system.Host.GetConfigPathFromLocationSubPath (LocationConfigPath, relativePath);
87                                         parentConfig = parentFile.FindLocationConfiguration (parentRelativePath, defaultConfiguration);
88                                 }
89                         }
90
91                         if (loc == null)
92                                 return parentConfig;
93                         
94                         loc.SetParentConfiguration (parentConfig);
95                         return loc.OpenConfiguration ();
96                 }
97                 
98                 internal void Init (IConfigSystem system, string configPath, Configuration parent)
99                 {
100                         this.system = system;
101                         streamName = system.Host.GetStreamName (configPath);
102                         this.parent = parent;
103                         if (parent != null)
104                                 rootGroup = parent.rootGroup;
105                         else {
106                                 rootGroup = new SectionGroupInfo ();
107                                 rootGroup.StreamName = streamName;
108                         }
109                         
110                         if (configPath != null)
111                                 Load ();
112                 }
113                 
114                 internal Configuration Parent {
115                         get { return parent; }
116                         set { parent = value; }
117                 }
118                 
119                 internal Configuration GetParentWithFile ()
120                 {
121                         Configuration parentFile = Parent;
122                         while (parentFile != null && !parentFile.HasFile)
123                                 parentFile = parentFile.Parent;
124                         return parentFile;
125                 }
126                 
127                 internal string FileName {
128                         get { return streamName; }
129                 }
130
131                 internal IInternalConfigHost ConfigHost {
132                         get { return system.Host; }
133                 }
134                 
135                 internal string LocationConfigPath {
136                         get { return locationConfigPath; }
137                 }
138
139                 internal string ConfigPath {
140                         get { return configPath; }
141                 }
142
143                 public AppSettingsSection AppSettings {
144                         get { return Sections ["appSettings"] as AppSettingsSection; }
145                 }
146
147                 public ConnectionStringsSection ConnectionStrings {
148                         get { return (ConnectionStringsSection) GetSection ("connectionStrings"); }
149                 }
150
151                 public string FilePath {
152                         get { return streamName; }
153                 }
154
155                 public bool HasFile {
156                         get {
157                                 return hasFile;
158                         }
159                 }
160                 
161                 public ConfigurationLocationCollection Locations {
162                         get {
163                                 if (locations == null) locations = new ConfigurationLocationCollection ();
164                                 return locations;
165                         }
166                 }
167
168                 public ConfigurationSectionGroup RootSectionGroup {
169                         get {
170                                 if (rootSectionGroup == null) {
171                                         rootSectionGroup = new ConfigurationSectionGroup ();
172                                         rootSectionGroup.Initialize (this, rootGroup);
173                                 }
174                                 return rootSectionGroup;
175                         }                        
176                 }
177
178                 public ConfigurationSectionGroupCollection SectionGroups {
179                         get { return RootSectionGroup.SectionGroups; }                        
180                 }
181
182                 public ConfigurationSectionCollection Sections {
183                         get { return RootSectionGroup.Sections; }
184                 }
185                 
186                 public ConfigurationSection GetSection (string path)
187                 {
188                         string[] parts = path.Split ('/');
189                         if (parts.Length == 1)
190                                 return Sections [parts[0]];
191
192                         ConfigurationSectionGroup group = SectionGroups [parts[0]];
193                         for (int n=1; group != null && n<parts.Length-1; n++)
194                                 group = group.SectionGroups [parts [n]];
195
196                         if (group != null)
197                                 return group.Sections [parts [parts.Length - 1]];
198                         else
199                                 return null;
200                 }
201                 
202                 public ConfigurationSectionGroup GetSectionGroup (string path)
203                 {
204                         string[] parts = path.Split ('/');
205                         ConfigurationSectionGroup group = SectionGroups [parts[0]];
206                         for (int n=1; group != null && n<parts.Length; n++)
207                                 group = group.SectionGroups [parts [n]];
208                         return group;
209                 }
210                 
211                 internal ConfigurationSection GetSectionInstance (SectionInfo config, bool createDefaultInstance)
212                 {
213                         object data = elementData [config];
214                         ConfigurationSection sec = data as ConfigurationSection;
215                         if (sec != null || !createDefaultInstance) return sec;
216                         
217                         object secObj = config.CreateInstance () as ConfigurationSection;
218                         if (!(secObj is ConfigurationSection))
219                                 sec = new RuntimeOnlySection ();
220                         else {
221                                 sec = (ConfigurationSection) secObj;
222                         }
223                                 
224                         ConfigurationSection parentSection = parent != null ? parent.GetSectionInstance (config, true) : null;
225                         sec.RawXml = data as string;
226                         sec.Reset (parentSection);
227                         
228                         if (data != null) {
229                                 XmlTextReader r = new XmlTextReader (new StringReader (data as string));
230                                 sec.DeserializeSection (r);
231                                 r.Close ();
232                         }
233                         
234                         elementData [config] = sec;
235                         return sec;
236                 }
237                 
238                 internal ConfigurationSectionGroup GetSectionGroupInstance (SectionGroupInfo group)
239                 {
240                         ConfigurationSectionGroup gr = group.CreateInstance () as ConfigurationSectionGroup;
241                         if (gr != null) gr.Initialize (this, group);
242                         return gr;
243                 }
244                 
245                 internal void SetConfigurationSection (SectionInfo config, ConfigurationSection sec)
246                 {
247                         elementData [config] = sec;
248                 }
249                 
250                 internal void SetSectionXml (SectionInfo config, string data)
251                 {
252                         elementData [config] = data;
253                 }
254                 
255                 internal string GetSectionXml (SectionInfo config)
256                 {
257                         return elementData [config] as string;
258                 }
259                 
260                 internal void CreateSection (SectionGroupInfo group, string name, ConfigurationSection sec)
261                 {
262                         if (group.HasChild (name))
263                                 throw new ConfigurationException ("Cannot add a ConfigurationSection. A section or section group already exists with the name '" + name + "'");
264                                 
265                         if (!HasFile && !sec.SectionInformation.AllowLocation)
266                                 throw new ConfigurationErrorsException ("The configuration section <" + name + "> cannot be defined inside a <location> element."); 
267
268                         if (!system.Host.IsDefinitionAllowed (configPath, sec.SectionInformation.AllowDefinition, sec.SectionInformation.AllowExeDefinition)) {
269                                 object ctx = sec.SectionInformation.AllowExeDefinition != ConfigurationAllowExeDefinition.MachineToApplication ? (object) sec.SectionInformation.AllowExeDefinition : (object) sec.SectionInformation.AllowDefinition;
270                                 throw new ConfigurationErrorsException ("The section <" + name + "> can't be defined in this configuration file (the allowed definition context is '" + ctx + "').");
271                         }
272                                                 
273                         if (sec.SectionInformation.Type == null)
274                                 sec.SectionInformation.Type = system.Host.GetConfigTypeName (sec.GetType ());
275                         
276                         sec.SectionInformation.SetName (name);
277
278                         SectionInfo section = new SectionInfo (name, sec.SectionInformation.Type, sec.SectionInformation.AllowLocation, sec.SectionInformation.AllowDefinition, sec.SectionInformation.AllowExeDefinition);
279                         section.StreamName = streamName;
280                         section.ConfigHost = system.Host;
281                         group.AddChild (section);
282                         elementData [section] = sec;
283                 }
284                 
285                 internal void CreateSectionGroup (SectionGroupInfo parentGroup, string name, ConfigurationSectionGroup sec)
286                 {
287                         if (parentGroup.HasChild (name)) throw new ConfigurationException ("Cannot add a ConfigurationSectionGroup. A section or section group already exists with the name '" + name + "'");
288                         if (sec.Type == null) sec.Type = system.Host.GetConfigTypeName (sec.GetType ());
289                         sec.SetName (name);
290
291                         SectionGroupInfo section = new SectionGroupInfo (name, sec.Type);
292                         section.StreamName = streamName;
293                         section.ConfigHost = system.Host;
294                         parentGroup.AddChild (section);
295                         elementData [section] = sec;
296                 }
297                 
298                 internal void RemoveConfigInfo (ConfigInfo config)
299                 {
300                         elementData.Remove (config);
301                 }
302                 
303                 public void Save ()
304                 {
305                         Save (ConfigurationSaveMode.Modified, false);
306                 }
307                 
308                 public void Save (ConfigurationSaveMode mode)
309                 {
310                         Save (mode, false);
311                 }
312                 
313                 public void Save (ConfigurationSaveMode mode, bool forceUpdateAll)
314                 {
315                         object ctx = null;
316                         Stream stream = system.Host.OpenStreamForWrite (streamName, null, ref ctx);
317                         try {
318                                 Save (stream, mode, forceUpdateAll);
319                                 system.Host.WriteCompleted (streamName, true, ctx);
320                         } catch (Exception ex) {
321                                 system.Host.WriteCompleted (streamName, false, ctx);
322                                 throw;
323                         } finally {
324                                 stream.Close ();
325                         }
326                 }
327                 
328                 public void SaveAs (string filename)
329                 {
330                         SaveAs (filename, ConfigurationSaveMode.Modified, false);
331                 }
332                 
333                 public void SaveAs (string filename, ConfigurationSaveMode mode)
334                 {
335                         SaveAs (filename, mode, false);
336                 }
337                 
338                 [MonoTODO ("Detect if file has changed")]
339                 public void SaveAs (string filename, ConfigurationSaveMode mode, bool forceUpdateAll)
340                 {
341                         Save (new FileStream (filename, FileMode.Open, FileAccess.Write), mode, forceUpdateAll);
342                 }
343                 
344                 void Save (Stream stream, ConfigurationSaveMode mode, bool forceUpdateAll)
345                 {
346                         XmlTextWriter tw = new XmlTextWriter (new StreamWriter (stream));
347                         tw.Formatting = Formatting.Indented;
348                         try {
349                                 tw.WriteStartElement ("configuration");
350                                 if (rootGroup.HasConfigContent (this)) {
351                                         rootGroup.WriteConfig (this, tw, mode);
352                                 }
353                                 
354                                 foreach (ConfigurationLocation loc in Locations) {
355                                         if (loc.OpenedConfiguration == null) {
356                                                 tw.WriteRaw ("\n");
357                                                 tw.WriteRaw (loc.XmlContent);
358                                         }
359                                         else {
360                                                 tw.WriteStartElement ("location");
361                                                 tw.WriteAttributeString ("path", loc.Path); 
362                                                 if (!loc.AllowOverride)
363                                                         tw.WriteAttributeString ("allowOverride", "false");
364                                                 loc.OpenedConfiguration.SaveData (tw, mode, forceUpdateAll);
365                                                 tw.WriteEndElement ();
366                                         }
367                                 }
368                                 
369                                 SaveData (tw, mode, forceUpdateAll);
370                                 tw.WriteEndElement ();
371                         }
372                         finally {
373                                 tw.Close ();
374                         }
375                 }
376                 
377                 void SaveData (XmlTextWriter tw, ConfigurationSaveMode mode, bool forceUpdateAll)
378                 {
379                         rootGroup.WriteRootData (tw, this, mode);
380                 }
381                 
382                 bool Load ()
383                 {
384                         if (streamName == null)
385                                 return true;
386                         
387                         Stream stream = system.Host.OpenStreamForRead (streamName);
388                         XmlTextReader reader = null;
389
390                         try {
391                                 reader = new XmlTextReader (stream);
392                                 ReadConfigFile (reader, streamName);
393 /*                      } catch (ConfigurationException) {
394                                 throw;
395                         } catch (Exception e) {
396                                 throw new ConfigurationException ("Error reading " + fileName, e);
397 */                      } finally {
398                                 if (reader != null)
399                                         reader.Close();
400                         }
401                         return true;
402                 }
403
404
405                 internal void ReadConfigFile (XmlTextReader reader, string fileName)
406                 {
407                         reader.MoveToContent ();
408                         if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
409                                 ThrowException ("Configuration file does not have a valid root element", reader);
410
411                         if (reader.HasAttributes)
412                                 ThrowException ("Unrecognized attribute in root element", reader);
413
414                         if (reader.IsEmptyElement) {
415                                 reader.Skip ();
416                                 return;
417                         }
418                         
419                         reader.ReadStartElement ();
420                         reader.MoveToContent ();
421
422                         if (reader.LocalName == "configSections") {
423                                 if (reader.HasAttributes)
424                                         ThrowException ("Unrecognized attribute in <configSections>.", reader);
425                                 
426                                 rootGroup.ReadConfig (this, fileName, reader);
427                         }
428                         
429                         rootGroup.ReadRootData (reader, this, true);
430                 }
431                 
432                 internal void ReadData (XmlTextReader reader, bool allowOverride)
433                 {
434                         rootGroup.ReadRootData (reader, this, allowOverride);
435                 }
436                 
437
438                 private void ThrowException (string text, XmlTextReader reader)
439                 {
440                         throw new ConfigurationException (text, streamName, reader.LineNumber);
441                 }
442         }
443 }
444
445 #endif