2008-02-21 Marek Habersack <mhabersack@novell.com>
[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                                 XmlDocument doc = new XmlDocument ();
77                                 doc.LoadXml (RawXml);
78                                 return SectionHandler.Create (parent, null, doc.DocumentElement);
79                         }
80                         return this;
81                 }
82
83                 [MonoTODO]
84                 protected internal override bool IsModified ()
85                 {
86                         return base.IsModified ();
87                 }
88
89                 [MonoTODO]
90                 protected internal override void ResetModified ()
91                 {
92                         base.ResetModified ();
93                 }
94
95                 ConfigurationElement CreateElement (Type t)
96                 {
97                         ConfigurationElement elem = (ConfigurationElement) Activator.CreateInstance (t);
98                         elem.Init ();
99                         if (IsReadOnly ())
100                                 elem.SetReadOnly ();
101                         return elem;
102                 }
103                 
104                 [MonoInternalNote ("find the proper location for the decryption stuff")]
105                 protected internal virtual void DeserializeSection (XmlReader reader)
106                 {
107                         reader.MoveToContent ();
108
109                         string protection_provider = null;
110                         string config_source = null;
111                         string localName;
112                         
113                         while (reader.MoveToNextAttribute ()) {
114                                 localName = reader.LocalName;
115                                 
116                                 if (localName == "configProtectionProvider")
117                                         protection_provider = reader.Value;
118                                 else if (localName == "configSource")
119                                         config_source = reader.Value;
120                         }
121
122                         /* XXX this stuff shouldn't be here */
123                         {
124                                 if (protection_provider != null) {
125                                         ProtectedConfigurationProvider prov = ProtectedConfiguration.GetProvider (protection_provider, true);
126                                         XmlDocument doc = new XmlDocument ();
127
128                                         reader.MoveToElement ();
129
130                                         doc.Load (new StringReader (reader.ReadInnerXml ()));
131
132                                         XmlNode n = prov.Decrypt (doc);
133
134                                         reader = new XmlNodeReader (n);
135
136                                         SectionInformation.ProtectSection (protection_provider);
137
138                                         reader.MoveToContent ();
139                                 }
140                         }
141
142                         if (config_source != null)
143                                 SectionInformation.ConfigSource = config_source;
144                         
145                         SectionInformation.SetRawXml (RawXml);
146                         DeserializeElement (reader, false);
147                 }
148
149                 internal void DeserializeConfigSource (string basePath)
150                 {
151                         string config_source = SectionInformation.ConfigSource;
152
153                         if (String.IsNullOrEmpty (config_source))
154                                 return;
155
156                         if (Path.IsPathRooted (config_source))
157                                 throw new ConfigurationException ("The configSource attribute must be a relative physical path.");
158                         
159                         if (HasLocalModifications ())
160                                 throw new ConfigurationException ("A section using 'configSource' may contain no other attributes or elements.");
161                         
162                         string path = Path.Combine (basePath, config_source);
163                         if (!File.Exists (path)) {
164                                 RawXml = null;
165                                 SectionInformation.SetRawXml (null);
166                                 return;
167                         }
168                         
169                         RawXml = File.ReadAllText (path);
170                         SectionInformation.SetRawXml (RawXml);
171                         DeserializeElement (new XmlTextReader (new StringReader (RawXml)), false);
172                 }
173                 
174                 protected internal virtual string SerializeSection (ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
175                 {
176                         externalDataXml = null;
177                         ConfigurationElement elem;
178                         if (parentElement != null) {
179                                 elem = (ConfigurationElement) CreateElement (GetType());
180                                 elem.Unmerge (this, parentElement, saveMode);
181                         }
182                         else
183                                 elem = this;
184                         
185                         string ret;                     
186                         using (StringWriter sw = new StringWriter ()) {
187                                 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
188                                         tw.Formatting = Formatting.Indented;
189                                         elem.SerializeToXmlElement (tw, name);
190                                         tw.Close ();
191                                 }
192                                 
193                                 ret = sw.ToString ();
194                         }
195                         
196                         string config_source = SectionInformation.ConfigSource;
197                         
198                         if (String.IsNullOrEmpty (config_source))
199                                 return ret;
200
201                         externalDataXml = ret;
202                         using (StringWriter sw = new StringWriter ()) {
203                                 bool haveName = !String.IsNullOrEmpty (name);
204
205                                 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
206                                         if (haveName)
207                                                 tw.WriteStartElement (name);
208                                         tw.WriteAttributeString ("configSource", config_source);
209                                         if (haveName)
210                                                 tw.WriteEndElement ();
211                                 }
212
213                                 return sw.ToString ();
214                         }
215                 }
216         }
217 }
218 #endif