Merge pull request #644 from knocte/connstrings
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConfigurationSection.cs
1 //
2 // System.Configuration.ConfigurationSection.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //      Lluis Sanchez Gual (lluis@novell.com)
7 //      Martin Baulig <martin.baulig@xamarin.com>
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
29 // Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
30 //
31
32 using System.Collections;
33 using System.Xml;
34 using System.IO;
35 #if !TARGET_JVM
36 using System.Security.Cryptography.Xml;
37 #endif
38 using System.Configuration.Internal;
39
40 namespace System.Configuration
41 {
42         public abstract class ConfigurationSection : ConfigurationElement
43         {
44                 SectionInformation sectionInformation;
45                 IConfigurationSectionHandler section_handler;
46                 string externalDataXml;
47                 
48                 protected ConfigurationSection ()
49                 {
50                 }
51
52                 internal string ExternalDataXml {
53                         get { return externalDataXml; }
54                 }
55                 
56                 internal IConfigurationSectionHandler SectionHandler {
57                         get { return section_handler; }
58                         set { section_handler = value; }
59                 }
60
61                 [MonoTODO]
62                 public SectionInformation SectionInformation {
63                         get {
64                                 if (sectionInformation == null)
65                                         sectionInformation = new SectionInformation ();
66                                 return sectionInformation;
67                         }
68                 }
69                 
70                 private object _configContext;
71                 
72                 internal object ConfigContext {
73                         get {
74                                 return _configContext;
75                         }
76                         set {
77                                 _configContext = value;
78                         }
79                 }
80
81                 [MonoTODO ("Provide ConfigContext. Likely the culprit of bug #322493")]
82                 protected internal virtual object GetRuntimeObject ()
83                 {
84                         if (SectionHandler != null) {
85                                 ConfigurationSection parentSection = sectionInformation != null ? sectionInformation.GetParentSection () : null;
86                                 object parent = parentSection != null ? parentSection.GetRuntimeObject () : null;
87                                 if (RawXml == null)
88                                         return parent;
89                                 
90                                 try {
91                                         // This code requires some re-thinking...
92                                         XmlReader reader = new ConfigXmlTextReader (
93                                                 new StringReader (RawXml),
94                                                 Configuration.FilePath);
95
96                                         DoDeserializeSection (reader);
97                                         
98                                         if (!String.IsNullOrEmpty (SectionInformation.ConfigSource)) {
99                                                 string fileDir = SectionInformation.ConfigFilePath;
100                                                 if (!String.IsNullOrEmpty (fileDir))
101                                                         fileDir = Path.GetDirectoryName (fileDir);
102                                                 else
103                                                         fileDir = String.Empty;
104                                         
105                                                 string path = Path.Combine (fileDir, SectionInformation.ConfigSource);
106                                                 if (File.Exists (path)) {
107                                                         RawXml = File.ReadAllText (path);
108                                                         SectionInformation.SetRawXml (RawXml);
109                                                 }
110                                         }
111                                 } catch {
112                                         // ignore, it can fail - we deserialize only in order to get
113                                         // the configSource attribute
114                                 }
115                                 XmlDocument doc = new ConfigurationXmlDocument ();
116                                 doc.LoadXml (RawXml);
117                                 return SectionHandler.Create (parent, ConfigContext, doc.DocumentElement);
118                         }
119                         return this;
120                 }
121
122                 [MonoTODO]
123                 protected internal override bool IsModified ()
124                 {
125                         return base.IsModified ();
126                 }
127
128                 [MonoTODO]
129                 protected internal override void ResetModified ()
130                 {
131                         base.ResetModified ();
132                 }
133
134                 ConfigurationElement CreateElement (Type t)
135                 {
136                         ConfigurationElement elem = (ConfigurationElement) Activator.CreateInstance (t);
137                         elem.Init ();
138                         elem.Configuration = Configuration;
139                         if (IsReadOnly ())
140                                 elem.SetReadOnly ();
141                         return elem;
142                 }
143
144                 void DoDeserializeSection (XmlReader reader)
145                 {
146                         reader.MoveToContent ();
147
148                         string protection_provider = null;
149                         string config_source = null;
150                         string localName;
151                         
152                         while (reader.MoveToNextAttribute ()) {
153                                 localName = reader.LocalName;
154                                 if (localName == "configProtectionProvider")
155                                         protection_provider = reader.Value;
156                                 else if (localName == "configSource")
157                                         config_source = reader.Value;
158                         }
159
160                         /* XXX this stuff shouldn't be here */
161                         {
162                                 if (protection_provider != null) {
163                                         ProtectedConfigurationProvider prov = ProtectedConfiguration.GetProvider (protection_provider, true);
164                                         XmlDocument doc = new ConfigurationXmlDocument ();
165
166                                         reader.MoveToElement ();
167
168                                         doc.Load (new StringReader (reader.ReadInnerXml ()));
169
170                                         XmlNode n = prov.Decrypt (doc);
171
172                                         reader = new XmlNodeReader (n);
173
174                                         SectionInformation.ProtectSection (protection_provider);
175
176                                         reader.MoveToContent ();
177                                 }
178                         }
179
180                         if (config_source != null)
181                                 SectionInformation.ConfigSource = config_source;
182                         
183                         SectionInformation.SetRawXml (RawXml);
184                         if (SectionHandler == null)
185                                 DeserializeElement (reader, false);
186                 }
187                 
188                 [MonoInternalNote ("find the proper location for the decryption stuff")]
189                 protected internal virtual void DeserializeSection (XmlReader reader)
190                 {
191                         try
192                         {
193                                 DoDeserializeSection (reader);
194                         }
195                         catch (ConfigurationErrorsException ex)
196                         {
197                                 throw new ConfigurationErrorsException(String.Format("Error deserializing configuration section {0}: {1}", this.SectionInformation.Name, ex.Message));
198                         }
199                 }
200
201                 internal void DeserializeConfigSource (string basePath)
202                 {
203                         string config_source = SectionInformation.ConfigSource;
204
205                         if (String.IsNullOrEmpty (config_source))
206                                 return;
207
208                         if (Path.IsPathRooted (config_source))
209                                 throw new ConfigurationErrorsException ("The configSource attribute must be a relative physical path.");
210                         
211                         if (HasLocalModifications ())
212                                 throw new ConfigurationErrorsException ("A section using 'configSource' may contain no other attributes or elements.");
213                         
214                         string path = Path.Combine (basePath, config_source);
215                         if (!File.Exists (path)) {
216                                 RawXml = null;
217                                 SectionInformation.SetRawXml (null);
218                                 throw new ConfigurationErrorsException (string.Format ("Unable to open configSource file '{0}'.", path));
219                         }
220                         
221                         RawXml = File.ReadAllText (path);
222                         SectionInformation.SetRawXml (RawXml);
223                         DeserializeElement (new ConfigXmlTextReader (new StringReader (RawXml), path), false);
224                 }
225
226                 protected internal virtual string SerializeSection (ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
227                 {
228                         externalDataXml = null;
229                         ConfigurationElement elem;
230                         if (parentElement != null) {
231                                 elem = (ConfigurationElement) CreateElement (GetType());
232                                 elem.Unmerge (this, parentElement, saveMode);
233                         }
234                         else
235                                 elem = this;
236
237                         /*
238                          * FIXME: LAMESPEC
239                          * 
240                          * Cache the current values of 'parentElement' and 'saveMode' for later use in
241                          * ConfigurationElement.SerializeToXmlElement().
242                          * 
243                          */
244                         elem.PrepareSave (parentElement, saveMode);
245                         bool hasValues = elem.HasValues (parentElement, saveMode);
246
247                         string ret;                     
248                         using (StringWriter sw = new StringWriter ()) {
249                                 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
250                                         tw.Formatting = Formatting.Indented;
251                                         if (hasValues)
252                                                 elem.SerializeToXmlElement (tw, name);
253                                         else if ((saveMode == ConfigurationSaveMode.Modified) && elem.IsModified ()) {
254                                                 // MS emits an empty section element.
255                                                 tw.WriteStartElement (name);
256                                                 tw.WriteEndElement ();
257                                         }
258                                         tw.Close ();
259                                 }
260                                 
261                                 ret = sw.ToString ();
262                         }
263                         
264                         string config_source = SectionInformation.ConfigSource;
265                         
266                         if (String.IsNullOrEmpty (config_source))
267                                 return ret;
268
269                         externalDataXml = ret;
270                         using (StringWriter sw = new StringWriter ()) {
271                                 bool haveName = !String.IsNullOrEmpty (name);
272
273                                 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
274                                         if (haveName)
275                                                 tw.WriteStartElement (name);
276                                         tw.WriteAttributeString ("configSource", config_source);
277                                         if (haveName)
278                                                 tw.WriteEndElement ();
279                                 }
280
281                                 return sw.ToString ();
282                         }
283                 }
284         }
285 }
286