minor fix for bug 9520:
[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 #if NET_2_0
33 using System.Collections;
34 using System.Xml;
35 using System.IO;
36 #if !TARGET_JVM
37 using System.Security.Cryptography.Xml;
38 #endif
39 using System.Configuration.Internal;
40
41 namespace System.Configuration
42 {
43         public abstract class ConfigurationSection : ConfigurationElement
44         {
45                 SectionInformation sectionInformation;
46                 IConfigurationSectionHandler section_handler;
47                 string externalDataXml;
48                 
49                 protected ConfigurationSection ()
50                 {
51                 }
52
53                 internal string ExternalDataXml {
54                         get { return externalDataXml; }
55                 }
56                 
57                 internal IConfigurationSectionHandler SectionHandler {
58                         get { return section_handler; }
59                         set { section_handler = value; }
60                 }
61
62                 [MonoTODO]
63                 public SectionInformation SectionInformation {
64                         get {
65                                 if (sectionInformation == null)
66                                         sectionInformation = new SectionInformation ();
67                                 return sectionInformation;
68                         }
69                 }
70                 
71                 private object _configContext;
72                 
73                 internal object ConfigContext {
74                         get {
75                                 return _configContext;
76                         }
77                         set {
78                                 _configContext = value;
79                         }
80                 }
81
82                 [MonoTODO ("Provide ConfigContext. Likely the culprit of bug #322493")]
83                 protected internal virtual object GetRuntimeObject ()
84                 {
85                         if (SectionHandler != null) {
86                                 ConfigurationSection parentSection = sectionInformation != null ? sectionInformation.GetParentSection () : null;
87                                 object parent = parentSection != null ? parentSection.GetRuntimeObject () : null;
88                                 if (RawXml == null)
89                                         return parent;
90                                 
91                                 try {
92                                         // This code requires some re-thinking...
93                                         XmlReader reader = new ConfigXmlTextReader (
94                                                 new StringReader (RawXml),
95                                                 Configuration.FilePath);
96
97                                         DoDeserializeSection (reader);
98                                         
99                                         if (!String.IsNullOrEmpty (SectionInformation.ConfigSource)) {
100                                                 string fileDir = SectionInformation.ConfigFilePath;
101                                                 if (!String.IsNullOrEmpty (fileDir))
102                                                         fileDir = Path.GetDirectoryName (fileDir);
103                                                 else
104                                                         fileDir = String.Empty;
105                                         
106                                                 string path = Path.Combine (fileDir, SectionInformation.ConfigSource);
107                                                 if (File.Exists (path)) {
108                                                         RawXml = File.ReadAllText (path);
109                                                         SectionInformation.SetRawXml (RawXml);
110                                                 }
111                                         }
112                                 } catch {
113                                         // ignore, it can fail - we deserialize only in order to get
114                                         // the configSource attribute
115                                 }
116                                 XmlDocument doc = new ConfigurationXmlDocument ();
117                                 doc.LoadXml (RawXml);
118                                 return SectionHandler.Create (parent, ConfigContext, doc.DocumentElement);
119                         }
120                         return this;
121                 }
122
123                 [MonoTODO]
124                 protected internal override bool IsModified ()
125                 {
126                         return base.IsModified ();
127                 }
128
129                 [MonoTODO]
130                 protected internal override void ResetModified ()
131                 {
132                         base.ResetModified ();
133                 }
134
135                 ConfigurationElement CreateElement (Type t)
136                 {
137                         ConfigurationElement elem = (ConfigurationElement) Activator.CreateInstance (t);
138                         elem.Init ();
139                         elem.Configuration = Configuration;
140                         if (IsReadOnly ())
141                                 elem.SetReadOnly ();
142                         return elem;
143                 }
144
145                 void DoDeserializeSection (XmlReader reader)
146                 {
147                         reader.MoveToContent ();
148
149                         string protection_provider = null;
150                         string config_source = null;
151                         string localName;
152                         
153                         while (reader.MoveToNextAttribute ()) {
154                                 localName = reader.LocalName;
155                                 if (localName == "configProtectionProvider")
156                                         protection_provider = reader.Value;
157                                 else if (localName == "configSource")
158                                         config_source = reader.Value;
159                         }
160
161                         /* XXX this stuff shouldn't be here */
162                         {
163                                 if (protection_provider != null) {
164                                         ProtectedConfigurationProvider prov = ProtectedConfiguration.GetProvider (protection_provider, true);
165                                         XmlDocument doc = new ConfigurationXmlDocument ();
166
167                                         reader.MoveToElement ();
168
169                                         doc.Load (new StringReader (reader.ReadInnerXml ()));
170
171                                         XmlNode n = prov.Decrypt (doc);
172
173                                         reader = new XmlNodeReader (n);
174
175                                         SectionInformation.ProtectSection (protection_provider);
176
177                                         reader.MoveToContent ();
178                                 }
179                         }
180
181                         if (config_source != null)
182                                 SectionInformation.ConfigSource = config_source;
183                         
184                         SectionInformation.SetRawXml (RawXml);
185                         if (SectionHandler == null)
186                                 DeserializeElement (reader, false);
187                 }
188                 
189                 [MonoInternalNote ("find the proper location for the decryption stuff")]
190                 protected internal virtual void DeserializeSection (XmlReader reader)
191                 {
192                         try
193                         {
194                                 DoDeserializeSection (reader);
195                         }
196                         catch (ConfigurationErrorsException ex)
197                         {
198                                 throw new ConfigurationErrorsException(String.Format("Error deserializing configuration section {0}: {1}", this.SectionInformation.Name, ex.Message));
199                         }
200                 }
201
202                 internal void DeserializeConfigSource (string basePath)
203                 {
204                         string config_source = SectionInformation.ConfigSource;
205
206                         if (String.IsNullOrEmpty (config_source))
207                                 return;
208
209                         if (Path.IsPathRooted (config_source))
210                                 throw new ConfigurationErrorsException ("The configSource attribute must be a relative physical path.");
211                         
212                         if (HasLocalModifications ())
213                                 throw new ConfigurationErrorsException ("A section using 'configSource' may contain no other attributes or elements.");
214                         
215                         string path = Path.Combine (basePath, config_source);
216                         if (!File.Exists (path)) {
217                                 RawXml = null;
218                                 SectionInformation.SetRawXml (null);
219                                 throw new ConfigurationErrorsException (string.Format ("Unable to open configSource file '{0}'.", path));
220                         }
221                         
222                         RawXml = File.ReadAllText (path);
223                         SectionInformation.SetRawXml (RawXml);
224                         DeserializeElement (new ConfigXmlTextReader (new StringReader (RawXml), path), false);
225                 }
226
227                 protected internal virtual string SerializeSection (ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
228                 {
229                         externalDataXml = null;
230                         ConfigurationElement elem;
231                         if (parentElement != null) {
232                                 elem = (ConfigurationElement) CreateElement (GetType());
233                                 elem.Unmerge (this, parentElement, saveMode);
234                         }
235                         else
236                                 elem = this;
237
238                         /*
239                          * FIXME: LAMESPEC
240                          * 
241                          * Cache the current values of 'parentElement' and 'saveMode' for later use in
242                          * ConfigurationElement.SerializeToXmlElement().
243                          * 
244                          */
245                         elem.PrepareSave (parentElement, saveMode);
246                         bool hasValues = elem.HasValues (parentElement, saveMode);
247
248                         string ret;                     
249                         using (StringWriter sw = new StringWriter ()) {
250                                 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
251                                         tw.Formatting = Formatting.Indented;
252                                         if (hasValues)
253                                                 elem.SerializeToXmlElement (tw, name);
254                                         else if ((saveMode == ConfigurationSaveMode.Modified) && elem.IsModified ()) {
255                                                 // MS emits an empty section element.
256                                                 tw.WriteStartElement (name);
257                                                 tw.WriteEndElement ();
258                                         }
259                                         tw.Close ();
260                                 }
261                                 
262                                 ret = sw.ToString ();
263                         }
264                         
265                         string config_source = SectionInformation.ConfigSource;
266                         
267                         if (String.IsNullOrEmpty (config_source))
268                                 return ret;
269
270                         externalDataXml = ret;
271                         using (StringWriter sw = new StringWriter ()) {
272                                 bool haveName = !String.IsNullOrEmpty (name);
273
274                                 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
275                                         if (haveName)
276                                                 tw.WriteStartElement (name);
277                                         tw.WriteAttributeString ("configSource", config_source);
278                                         if (haveName)
279                                                 tw.WriteEndElement ();
280                                 }
281
282                                 return sw.ToString ();
283                         }
284                 }
285         }
286 }
287 #endif