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