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;
76 XmlDocument doc = new XmlDocument ();
78 return SectionHandler.Create (parent, null, doc.DocumentElement);
84 protected internal override bool IsModified ()
86 return base.IsModified ();
90 protected internal override void ResetModified ()
92 base.ResetModified ();
95 ConfigurationElement CreateElement (Type t)
97 ConfigurationElement elem = (ConfigurationElement) Activator.CreateInstance (t);
104 [MonoInternalNote ("find the proper location for the decryption stuff")]
105 protected internal virtual void DeserializeSection (XmlReader reader)
107 reader.MoveToContent ();
109 string protection_provider = null;
110 string config_source = null;
113 while (reader.MoveToNextAttribute ()) {
114 localName = reader.LocalName;
116 if (localName == "configProtectionProvider")
117 protection_provider = reader.Value;
118 else if (localName == "configSource")
119 config_source = reader.Value;
122 /* XXX this stuff shouldn't be here */
124 if (protection_provider != null) {
125 ProtectedConfigurationProvider prov = ProtectedConfiguration.GetProvider (protection_provider, true);
126 XmlDocument doc = new XmlDocument ();
128 reader.MoveToElement ();
130 doc.Load (new StringReader (reader.ReadInnerXml ()));
132 XmlNode n = prov.Decrypt (doc);
134 reader = new XmlNodeReader (n);
136 SectionInformation.ProtectSection (protection_provider);
138 reader.MoveToContent ();
142 if (config_source != null)
143 SectionInformation.ConfigSource = config_source;
145 SectionInformation.SetRawXml (RawXml);
146 DeserializeElement (reader, false);
149 internal void DeserializeConfigSource (string basePath)
151 string config_source = SectionInformation.ConfigSource;
153 if (String.IsNullOrEmpty (config_source))
156 if (Path.IsPathRooted (config_source))
157 throw new ConfigurationException ("The configSource attribute must be a relative physical path.");
159 if (HasLocalModifications ())
160 throw new ConfigurationException ("A section using 'configSource' may contain no other attributes or elements.");
162 string path = Path.Combine (basePath, config_source);
163 if (!File.Exists (path)) {
165 SectionInformation.SetRawXml (null);
169 RawXml = File.ReadAllText (path);
170 SectionInformation.SetRawXml (RawXml);
171 DeserializeElement (new XmlTextReader (new StringReader (RawXml)), false);
174 protected internal virtual string SerializeSection (ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
176 externalDataXml = null;
177 ConfigurationElement elem;
178 if (parentElement != null) {
179 elem = (ConfigurationElement) CreateElement (GetType());
180 elem.Unmerge (this, parentElement, saveMode);
186 using (StringWriter sw = new StringWriter ()) {
187 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
188 tw.Formatting = Formatting.Indented;
189 elem.SerializeToXmlElement (tw, name);
193 ret = sw.ToString ();
196 string config_source = SectionInformation.ConfigSource;
198 if (String.IsNullOrEmpty (config_source))
201 externalDataXml = ret;
202 using (StringWriter sw = new StringWriter ()) {
203 bool haveName = !String.IsNullOrEmpty (name);
205 using (XmlTextWriter tw = new XmlTextWriter (sw)) {
207 tw.WriteStartElement (name);
208 tw.WriteAttributeString ("configSource", config_source);
210 tw.WriteEndElement ();
213 return sw.ToString ();