2 // System.Configuration.ConfigurationSection.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Lluis Sanchez Gual (lluis@novell.com)
7 // Martin Baulig <martin.baulig@xamarin.com>
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
29 // Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
32 using System.Collections;
36 using System.Security.Cryptography.Xml;
38 using System.Configuration.Internal;
40 namespace System.Configuration
42 public abstract class ConfigurationSection : ConfigurationElement
44 SectionInformation sectionInformation;
45 IConfigurationSectionHandler section_handler;
46 string externalDataXml;
48 protected ConfigurationSection ()
52 internal string ExternalDataXml {
53 get { return externalDataXml; }
56 internal IConfigurationSectionHandler SectionHandler {
57 get { return section_handler; }
58 set { section_handler = value; }
62 public SectionInformation SectionInformation {
64 if (sectionInformation == null)
65 sectionInformation = new SectionInformation ();
66 return sectionInformation;
70 private object _configContext;
72 internal object ConfigContext {
74 return _configContext;
77 _configContext = value;
81 [MonoTODO ("Provide ConfigContext. Likely the culprit of bug #322493")]
82 protected internal virtual object GetRuntimeObject ()
84 if (SectionHandler != null) {
85 ConfigurationSection parentSection = sectionInformation != null ? sectionInformation.GetParentSection () : null;
86 object parent = parentSection != null ? parentSection.GetRuntimeObject () : null;
91 // This code requires some re-thinking...
92 XmlReader reader = new ConfigXmlTextReader (
93 new StringReader (RawXml),
94 Configuration.FilePath);
96 DoDeserializeSection (reader);
98 if (!String.IsNullOrEmpty (SectionInformation.ConfigSource)) {
99 string fileDir = SectionInformation.ConfigFilePath;
100 if (!String.IsNullOrEmpty (fileDir))
101 fileDir = Path.GetDirectoryName (fileDir);
103 fileDir = String.Empty;
105 string path = Path.Combine (fileDir, SectionInformation.ConfigSource);
106 if (File.Exists (path)) {
107 RawXml = File.ReadAllText (path);
108 SectionInformation.SetRawXml (RawXml);
112 // ignore, it can fail - we deserialize only in order to get
113 // the configSource attribute
115 XmlDocument doc = new ConfigurationXmlDocument ();
116 doc.LoadXml (RawXml);
117 return SectionHandler.Create (parent, ConfigContext, doc.DocumentElement);
123 protected internal override bool IsModified ()
125 return base.IsModified ();
129 protected internal override void ResetModified ()
131 base.ResetModified ();
134 ConfigurationElement CreateElement (Type t)
136 ConfigurationElement elem = (ConfigurationElement) Activator.CreateInstance (t);
138 elem.Configuration = Configuration;
144 void DoDeserializeSection (XmlReader reader)
146 reader.MoveToContent ();
148 string protection_provider = null;
149 string config_source = null;
152 while (reader.MoveToNextAttribute ()) {
153 localName = reader.LocalName;
154 if (localName == "configProtectionProvider")
155 protection_provider = reader.Value;
156 else if (localName == "configSource")
157 config_source = reader.Value;
160 /* XXX this stuff shouldn't be here */
162 if (protection_provider != null) {
163 ProtectedConfigurationProvider prov = ProtectedConfiguration.GetProvider (protection_provider, true);
164 XmlDocument doc = new ConfigurationXmlDocument ();
166 reader.MoveToElement ();
168 doc.Load (new StringReader (reader.ReadInnerXml ()));
170 XmlNode n = prov.Decrypt (doc);
172 reader = new XmlNodeReader (n);
174 SectionInformation.ProtectSection (protection_provider);
176 reader.MoveToContent ();
180 if (config_source != null)
181 SectionInformation.ConfigSource = config_source;
183 SectionInformation.SetRawXml (RawXml);
184 if (SectionHandler == null)
185 DeserializeElement (reader, false);
188 [MonoInternalNote ("find the proper location for the decryption stuff")]
189 protected internal virtual void DeserializeSection (XmlReader reader)
193 DoDeserializeSection (reader);
195 catch (ConfigurationErrorsException ex)
197 throw new ConfigurationErrorsException(String.Format("Error deserializing configuration section {0}: {1}", this.SectionInformation.Name, ex.Message));
201 internal void DeserializeConfigSource (string basePath)
203 string config_source = SectionInformation.ConfigSource;
205 if (String.IsNullOrEmpty (config_source))
208 if (Path.IsPathRooted (config_source))
209 throw new ConfigurationErrorsException ("The configSource attribute must be a relative physical path.");
211 if (HasLocalModifications ())
212 throw new ConfigurationErrorsException ("A section using 'configSource' may contain no other attributes or elements.");
214 string path = Path.Combine (basePath, config_source);
215 if (!File.Exists (path)) {
217 SectionInformation.SetRawXml (null);
218 throw new ConfigurationErrorsException (string.Format ("Unable to open configSource file '{0}'.", path));
221 RawXml = File.ReadAllText (path);
222 SectionInformation.SetRawXml (RawXml);
223 DeserializeElement (new ConfigXmlTextReader (new StringReader (RawXml), path), false);
226 protected internal virtual string SerializeSection (ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
228 externalDataXml = null;
229 ConfigurationElement elem;
230 if (parentElement != null) {
231 elem = (ConfigurationElement) CreateElement (GetType());
232 elem.Unmerge (this, parentElement, saveMode);
240 * Cache the current values of 'parentElement' and 'saveMode' for later use in
241 * ConfigurationElement.SerializeToXmlElement().
244 elem.PrepareSave (parentElement, saveMode);
245 bool hasValues = elem.HasValues (parentElement, saveMode);
248 using (StringWriter sw = new StringWriter ()) {
249 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
250 tw.Formatting = Formatting.Indented;
252 elem.SerializeToXmlElement (tw, name);
253 else if ((saveMode == ConfigurationSaveMode.Modified) && elem.IsModified ()) {
254 // MS emits an empty section element.
255 tw.WriteStartElement (name);
256 tw.WriteEndElement ();
261 ret = sw.ToString ();
264 string config_source = SectionInformation.ConfigSource;
266 if (String.IsNullOrEmpty (config_source))
269 externalDataXml = ret;
270 using (StringWriter sw = new StringWriter ()) {
271 bool haveName = !String.IsNullOrEmpty (name);
273 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
275 tw.WriteStartElement (name);
276 tw.WriteAttributeString ("configSource", config_source);
278 tw.WriteEndElement ();
281 return sw.ToString ();