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