2 // System.Configuration.ConfigurationSection.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Lluis Sanchez Gual (lluis@novell.com)
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:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
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.
27 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
31 using System.Collections;
35 using System.Security.Cryptography.Xml;
37 using System.Configuration.Internal;
39 namespace System.Configuration
41 public abstract class ConfigurationSection : ConfigurationElement
43 SectionInformation sectionInformation;
44 IConfigurationSectionHandler section_handler;
45 string externalDataXml;
47 protected ConfigurationSection ()
51 internal string ExternalDataXml {
52 get { return externalDataXml; }
55 internal IConfigurationSectionHandler SectionHandler {
56 get { return section_handler; }
57 set { section_handler = value; }
61 public SectionInformation SectionInformation {
63 if (sectionInformation == null)
64 sectionInformation = new SectionInformation ();
65 return sectionInformation;
69 private object _configContext;
71 internal object ConfigContext {
73 return _configContext;
76 _configContext = value;
80 [MonoTODO ("Provide ConfigContext. Likely the culprit of bug #322493")]
81 protected internal virtual object GetRuntimeObject ()
83 if (SectionHandler != null) {
84 ConfigurationSection parentSection = sectionInformation != null ? sectionInformation.GetParentSection () : null;
85 object parent = parentSection != null ? parentSection.GetRuntimeObject () : null;
90 // This code requires some re-thinking...
91 XmlReader reader = new ConfigXmlTextReader (
92 new StringReader (RawXml),
93 Configuration.FilePath);
95 DoDeserializeSection (reader);
97 if (!String.IsNullOrEmpty (SectionInformation.ConfigSource)) {
98 string fileDir = SectionInformation.ConfigFilePath;
99 if (!String.IsNullOrEmpty (fileDir))
100 fileDir = Path.GetDirectoryName (fileDir);
102 fileDir = String.Empty;
104 string path = Path.Combine (fileDir, SectionInformation.ConfigSource);
105 if (File.Exists (path)) {
106 RawXml = File.ReadAllText (path);
107 SectionInformation.SetRawXml (RawXml);
111 // ignore, it can fail - we deserialize only in order to get
112 // the configSource attribute
114 XmlDocument doc = new ConfigurationXmlDocument ();
115 doc.LoadXml (RawXml);
116 return SectionHandler.Create (parent, ConfigContext, doc.DocumentElement);
122 protected internal override bool IsModified ()
124 return base.IsModified ();
128 protected internal override void ResetModified ()
130 base.ResetModified ();
133 ConfigurationElement CreateElement (Type t)
135 ConfigurationElement elem = (ConfigurationElement) Activator.CreateInstance (t);
137 elem.Configuration = Configuration;
143 void DoDeserializeSection (XmlReader reader)
145 reader.MoveToContent ();
147 string protection_provider = null;
148 string config_source = null;
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;
159 /* XXX this stuff shouldn't be here */
161 if (protection_provider != null) {
162 ProtectedConfigurationProvider prov = ProtectedConfiguration.GetProvider (protection_provider, true);
163 XmlDocument doc = new ConfigurationXmlDocument ();
165 reader.MoveToElement ();
167 doc.Load (new StringReader (reader.ReadInnerXml ()));
169 XmlNode n = prov.Decrypt (doc);
171 reader = new XmlNodeReader (n);
173 SectionInformation.ProtectSection (protection_provider);
175 reader.MoveToContent ();
179 if (config_source != null)
180 SectionInformation.ConfigSource = config_source;
182 SectionInformation.SetRawXml (RawXml);
183 if (SectionHandler == null)
184 DeserializeElement (reader, false);
187 [MonoInternalNote ("find the proper location for the decryption stuff")]
188 protected internal virtual void DeserializeSection (XmlReader reader)
192 DoDeserializeSection (reader);
194 catch (ConfigurationErrorsException ex)
196 throw new ConfigurationErrorsException(String.Format("Error deserializing configuration section {0}: {1}", this.SectionInformation.Name, ex.Message));
200 internal void DeserializeConfigSource (string basePath)
202 string config_source = SectionInformation.ConfigSource;
204 if (String.IsNullOrEmpty (config_source))
207 if (Path.IsPathRooted (config_source))
208 throw new ConfigurationErrorsException ("The configSource attribute must be a relative physical path.");
210 if (HasLocalModifications ())
211 throw new ConfigurationErrorsException ("A section using 'configSource' may contain no other attributes or elements.");
213 string path = Path.Combine (basePath, config_source);
214 if (!File.Exists (path)) {
216 SectionInformation.SetRawXml (null);
217 throw new ConfigurationErrorsException (string.Format ("Unable to open configSource file '{0}'.", path));
220 RawXml = File.ReadAllText (path);
221 SectionInformation.SetRawXml (RawXml);
222 DeserializeElement (new ConfigXmlTextReader (new StringReader (RawXml), path), false);
225 protected internal virtual string SerializeSection (ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
227 externalDataXml = null;
228 ConfigurationElement elem;
229 if (parentElement != null) {
230 elem = (ConfigurationElement) CreateElement (GetType());
231 elem.Unmerge (this, parentElement, saveMode);
237 using (StringWriter sw = new StringWriter ()) {
238 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
239 tw.Formatting = Formatting.Indented;
240 elem.SerializeToXmlElement (tw, name);
244 ret = sw.ToString ();
247 string config_source = SectionInformation.ConfigSource;
249 if (String.IsNullOrEmpty (config_source))
252 externalDataXml = ret;
253 using (StringWriter sw = new StringWriter ()) {
254 bool haveName = !String.IsNullOrEmpty (name);
256 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
258 tw.WriteStartElement (name);
259 tw.WriteAttributeString ("configSource", config_source);
261 tw.WriteEndElement ();
264 return sw.ToString ();