System.Drawing: added email to icon and test file headers
[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                         if (IsReadOnly ())
138                                 elem.SetReadOnly ();
139                         return elem;
140                 }
141
142                 void DoDeserializeSection (XmlReader reader)
143                 {
144                         reader.MoveToContent ();
145
146                         string protection_provider = null;
147                         string config_source = null;
148                         string localName;
149                         
150                         while (reader.MoveToNextAttribute ()) {
151                                 localName = reader.LocalName;
152                                 if (localName == "configProtectionProvider")
153                                         protection_provider = reader.Value;
154                                 else if (localName == "configSource")
155                                         config_source = reader.Value;
156                         }
157
158                         /* XXX this stuff shouldn't be here */
159                         {
160                                 if (protection_provider != null) {
161                                         ProtectedConfigurationProvider prov = ProtectedConfiguration.GetProvider (protection_provider, true);
162                                         XmlDocument doc = new ConfigurationXmlDocument ();
163
164                                         reader.MoveToElement ();
165
166                                         doc.Load (new StringReader (reader.ReadInnerXml ()));
167
168                                         XmlNode n = prov.Decrypt (doc);
169
170                                         reader = new XmlNodeReader (n);
171
172                                         SectionInformation.ProtectSection (protection_provider);
173
174                                         reader.MoveToContent ();
175                                 }
176                         }
177
178                         if (config_source != null)
179                                 SectionInformation.ConfigSource = config_source;
180                         
181                         SectionInformation.SetRawXml (RawXml);
182                         if (SectionHandler == null)
183                                 DeserializeElement (reader, false);
184                 }
185                 
186                 [MonoInternalNote ("find the proper location for the decryption stuff")]
187                 protected internal virtual void DeserializeSection (XmlReader reader)
188                 {
189                         DoDeserializeSection (reader);
190                 }
191
192                 internal void DeserializeConfigSource (string basePath)
193                 {
194                         string config_source = SectionInformation.ConfigSource;
195
196                         if (String.IsNullOrEmpty (config_source))
197                                 return;
198
199                         if (Path.IsPathRooted (config_source))
200                                 throw new ConfigurationErrorsException ("The configSource attribute must be a relative physical path.");
201                         
202                         if (HasLocalModifications ())
203                                 throw new ConfigurationErrorsException ("A section using 'configSource' may contain no other attributes or elements.");
204                         
205                         string path = Path.Combine (basePath, config_source);
206                         if (!File.Exists (path)) {
207                                 RawXml = null;
208                                 SectionInformation.SetRawXml (null);
209                                 throw new ConfigurationErrorsException (string.Format ("Unable to open configSource file '{0}'.", path));
210                         }
211                         
212                         RawXml = File.ReadAllText (path);
213                         SectionInformation.SetRawXml (RawXml);
214                         DeserializeElement (new ConfigXmlTextReader (new StringReader (RawXml), path), false);
215                 }
216                 
217                 protected internal virtual string SerializeSection (ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
218                 {
219                         externalDataXml = null;
220                         ConfigurationElement elem;
221                         if (parentElement != null) {
222                                 elem = (ConfigurationElement) CreateElement (GetType());
223                                 elem.Unmerge (this, parentElement, saveMode);
224                         }
225                         else
226                                 elem = this;
227                         
228                         string ret;                     
229                         using (StringWriter sw = new StringWriter ()) {
230                                 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
231                                         tw.Formatting = Formatting.Indented;
232                                         elem.SerializeToXmlElement (tw, name);
233                                         tw.Close ();
234                                 }
235                                 
236                                 ret = sw.ToString ();
237                         }
238                         
239                         string config_source = SectionInformation.ConfigSource;
240                         
241                         if (String.IsNullOrEmpty (config_source))
242                                 return ret;
243
244                         externalDataXml = ret;
245                         using (StringWriter sw = new StringWriter ()) {
246                                 bool haveName = !String.IsNullOrEmpty (name);
247
248                                 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
249                                         if (haveName)
250                                                 tw.WriteStartElement (name);
251                                         tw.WriteAttributeString ("configSource", config_source);
252                                         if (haveName)
253                                                 tw.WriteEndElement ();
254                                 }
255
256                                 return sw.ToString ();
257                         }
258                 }
259         }
260 }
261 #endif