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)
33 using System.Collections;
37 using System.Security.Cryptography.Xml;
39 using System.Configuration.Internal;
41 namespace System.Configuration
43 public abstract class ConfigurationSection : ConfigurationElement
45 SectionInformation sectionInformation;
46 IConfigurationSectionHandler section_handler;
47 string externalDataXml;
49 protected ConfigurationSection ()
53 internal string ExternalDataXml {
54 get { return externalDataXml; }
57 internal IConfigurationSectionHandler SectionHandler {
58 get { return section_handler; }
59 set { section_handler = value; }
63 public SectionInformation SectionInformation {
65 if (sectionInformation == null)
66 sectionInformation = new SectionInformation ();
67 return sectionInformation;
71 private object _configContext;
73 internal object ConfigContext {
75 return _configContext;
78 _configContext = value;
82 [MonoTODO ("Provide ConfigContext. Likely the culprit of bug #322493")]
83 protected internal virtual object GetRuntimeObject ()
85 if (SectionHandler != null) {
86 ConfigurationSection parentSection = sectionInformation != null ? sectionInformation.GetParentSection () : null;
87 object parent = parentSection != null ? parentSection.GetRuntimeObject () : null;
92 // This code requires some re-thinking...
93 XmlReader reader = new ConfigXmlTextReader (
94 new StringReader (RawXml),
95 Configuration.FilePath);
97 DoDeserializeSection (reader);
99 if (!String.IsNullOrEmpty (SectionInformation.ConfigSource)) {
100 string fileDir = SectionInformation.ConfigFilePath;
101 if (!String.IsNullOrEmpty (fileDir))
102 fileDir = Path.GetDirectoryName (fileDir);
104 fileDir = String.Empty;
106 string path = Path.Combine (fileDir, SectionInformation.ConfigSource);
107 if (File.Exists (path)) {
108 RawXml = File.ReadAllText (path);
109 SectionInformation.SetRawXml (RawXml);
113 // ignore, it can fail - we deserialize only in order to get
114 // the configSource attribute
116 XmlDocument doc = new ConfigurationXmlDocument ();
117 doc.LoadXml (RawXml);
118 return SectionHandler.Create (parent, ConfigContext, doc.DocumentElement);
124 protected internal override bool IsModified ()
126 return base.IsModified ();
130 protected internal override void ResetModified ()
132 base.ResetModified ();
135 ConfigurationElement CreateElement (Type t)
137 ConfigurationElement elem = (ConfigurationElement) Activator.CreateInstance (t);
139 elem.Configuration = Configuration;
145 void DoDeserializeSection (XmlReader reader)
147 reader.MoveToContent ();
149 string protection_provider = null;
150 string config_source = null;
153 while (reader.MoveToNextAttribute ()) {
154 localName = reader.LocalName;
155 if (localName == "configProtectionProvider")
156 protection_provider = reader.Value;
157 else if (localName == "configSource")
158 config_source = reader.Value;
161 /* XXX this stuff shouldn't be here */
163 if (protection_provider != null) {
164 ProtectedConfigurationProvider prov = ProtectedConfiguration.GetProvider (protection_provider, true);
165 XmlDocument doc = new ConfigurationXmlDocument ();
167 reader.MoveToElement ();
169 doc.Load (new StringReader (reader.ReadInnerXml ()));
171 XmlNode n = prov.Decrypt (doc);
173 reader = new XmlNodeReader (n);
175 SectionInformation.ProtectSection (protection_provider);
177 reader.MoveToContent ();
181 if (config_source != null)
182 SectionInformation.ConfigSource = config_source;
184 SectionInformation.SetRawXml (RawXml);
185 if (SectionHandler == null)
186 DeserializeElement (reader, false);
189 [MonoInternalNote ("find the proper location for the decryption stuff")]
190 protected internal virtual void DeserializeSection (XmlReader reader)
194 DoDeserializeSection (reader);
196 catch (ConfigurationErrorsException ex)
198 throw new ConfigurationErrorsException(String.Format("Error deserializing configuration section {0}: {1}", this.SectionInformation.Name, ex.Message));
202 internal void DeserializeConfigSource (string basePath)
204 string config_source = SectionInformation.ConfigSource;
206 if (String.IsNullOrEmpty (config_source))
209 if (Path.IsPathRooted (config_source))
210 throw new ConfigurationErrorsException ("The configSource attribute must be a relative physical path.");
212 if (HasLocalModifications ())
213 throw new ConfigurationErrorsException ("A section using 'configSource' may contain no other attributes or elements.");
215 string path = Path.Combine (basePath, config_source);
216 if (!File.Exists (path)) {
218 SectionInformation.SetRawXml (null);
219 throw new ConfigurationErrorsException (string.Format ("Unable to open configSource file '{0}'.", path));
222 RawXml = File.ReadAllText (path);
223 SectionInformation.SetRawXml (RawXml);
224 DeserializeElement (new ConfigXmlTextReader (new StringReader (RawXml), path), false);
227 protected internal virtual string SerializeSection (ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
229 externalDataXml = null;
230 ConfigurationElement elem;
231 if (parentElement != null) {
232 elem = (ConfigurationElement) CreateElement (GetType());
233 elem.Unmerge (this, parentElement, saveMode);
241 * Cache the current values of 'parentElement' and 'saveMode' for later use in
242 * ConfigurationElement.SerializeToXmlElement().
245 elem.PrepareSave (parentElement, saveMode);
246 bool hasValues = elem.HasValues (parentElement, saveMode);
249 using (StringWriter sw = new StringWriter ()) {
250 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
251 tw.Formatting = Formatting.Indented;
253 elem.SerializeToXmlElement (tw, name);
254 else if ((saveMode == ConfigurationSaveMode.Modified) && elem.IsModified ()) {
255 // MS emits an empty section element.
256 tw.WriteStartElement (name);
257 tw.WriteEndElement ();
262 ret = sw.ToString ();
265 string config_source = SectionInformation.ConfigSource;
267 if (String.IsNullOrEmpty (config_source))
270 externalDataXml = ret;
271 using (StringWriter sw = new StringWriter ()) {
272 bool haveName = !String.IsNullOrEmpty (name);
274 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
276 tw.WriteStartElement (name);
277 tw.WriteAttributeString ("configSource", config_source);
279 tw.WriteEndElement ();
282 return sw.ToString ();