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;
38 namespace System.Configuration
40 public abstract class ConfigurationSection : ConfigurationElement
42 SectionInformation sectionInformation;
43 IConfigurationSectionHandler section_handler;
44 string externalDataXml;
46 protected ConfigurationSection ()
50 internal string ExternalDataXml {
51 get { return externalDataXml; }
54 internal IConfigurationSectionHandler SectionHandler {
55 get { return section_handler; }
56 set { section_handler = value; }
60 public SectionInformation SectionInformation {
62 if (sectionInformation == null)
63 sectionInformation = new SectionInformation ();
64 return sectionInformation;
68 [MonoTODO ("Provide ConfigContext. Likely the culprit of bug #322493")]
69 protected internal virtual object GetRuntimeObject ()
71 if (SectionHandler != null) {
72 ConfigurationSection parentSection = sectionInformation != null ? sectionInformation.GetParentSection () : null;
73 object parent = parentSection != null ? parentSection.GetRuntimeObject () : null;
78 // This code requires some re-thinking...
79 XmlReader reader = new XmlTextReader (new StringReader (RawXml));
81 DoDeserializeSection (reader);
82 if (!String.IsNullOrEmpty (SectionInformation.ConfigSource)) {
83 string fileDir = SectionInformation.ConfigFilePath;
84 if (!String.IsNullOrEmpty (fileDir))
85 fileDir = Path.GetDirectoryName (fileDir);
87 fileDir = String.Empty;
89 string path = Path.Combine (fileDir, SectionInformation.ConfigSource);
90 if (File.Exists (path)) {
91 RawXml = File.ReadAllText (path);
92 SectionInformation.SetRawXml (RawXml);
96 // ignore, it can fail - we deserialize only in order to get
97 // the configSource attribute
100 XmlDocument doc = new XmlDocument ();
101 doc.LoadXml (RawXml);
102 return SectionHandler.Create (parent, null, doc.DocumentElement);
108 protected internal override bool IsModified ()
110 return base.IsModified ();
114 protected internal override void ResetModified ()
116 base.ResetModified ();
119 ConfigurationElement CreateElement (Type t)
121 ConfigurationElement elem = (ConfigurationElement) Activator.CreateInstance (t);
128 void DoDeserializeSection (XmlReader reader)
130 reader.MoveToContent ();
132 string protection_provider = null;
133 string config_source = null;
136 while (reader.MoveToNextAttribute ()) {
137 localName = reader.LocalName;
138 if (localName == "configProtectionProvider")
139 protection_provider = reader.Value;
140 else if (localName == "configSource")
141 config_source = reader.Value;
144 /* XXX this stuff shouldn't be here */
146 if (protection_provider != null) {
147 ProtectedConfigurationProvider prov = ProtectedConfiguration.GetProvider (protection_provider, true);
148 XmlDocument doc = new XmlDocument ();
150 reader.MoveToElement ();
152 doc.Load (new StringReader (reader.ReadInnerXml ()));
154 XmlNode n = prov.Decrypt (doc);
156 reader = new XmlNodeReader (n);
158 SectionInformation.ProtectSection (protection_provider);
160 reader.MoveToContent ();
164 if (config_source != null)
165 SectionInformation.ConfigSource = config_source;
167 SectionInformation.SetRawXml (RawXml);
168 DeserializeElement (reader, false);
171 [MonoInternalNote ("find the proper location for the decryption stuff")]
172 protected internal virtual void DeserializeSection (XmlReader reader)
174 DoDeserializeSection (reader);
177 internal void DeserializeConfigSource (string basePath)
179 string config_source = SectionInformation.ConfigSource;
181 if (String.IsNullOrEmpty (config_source))
184 if (Path.IsPathRooted (config_source))
185 throw new ConfigurationException ("The configSource attribute must be a relative physical path.");
187 if (HasLocalModifications ())
188 throw new ConfigurationException ("A section using 'configSource' may contain no other attributes or elements.");
190 string path = Path.Combine (basePath, config_source);
191 if (!File.Exists (path)) {
193 SectionInformation.SetRawXml (null);
197 RawXml = File.ReadAllText (path);
198 SectionInformation.SetRawXml (RawXml);
199 DeserializeElement (new XmlTextReader (new StringReader (RawXml)), false);
202 protected internal virtual string SerializeSection (ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
204 externalDataXml = null;
205 ConfigurationElement elem;
206 if (parentElement != null) {
207 elem = (ConfigurationElement) CreateElement (GetType());
208 elem.Unmerge (this, parentElement, saveMode);
214 using (StringWriter sw = new StringWriter ()) {
215 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
216 tw.Formatting = Formatting.Indented;
217 elem.SerializeToXmlElement (tw, name);
221 ret = sw.ToString ();
224 string config_source = SectionInformation.ConfigSource;
226 if (String.IsNullOrEmpty (config_source))
229 externalDataXml = ret;
230 using (StringWriter sw = new StringWriter ()) {
231 bool haveName = !String.IsNullOrEmpty (name);
233 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
235 tw.WriteStartElement (name);
236 tw.WriteAttributeString ("configSource", config_source);
238 tw.WriteEndElement ();
241 return sw.ToString ();