2 // System.Configuration.SectionInfo.cs
5 // Lluis Sanchez (lluis@novell.com)
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
30 using System.Collections;
31 using System.Collections.Specialized;
36 namespace System.Configuration
38 internal class SectionInfo: ConfigInfo
40 bool allowLocation = true;
41 bool requirePermission = true;
42 bool restartOnExternalChanges;
43 ConfigurationAllowDefinition allowDefinition = ConfigurationAllowDefinition.Everywhere;
44 ConfigurationAllowExeDefinition allowExeDefinition = ConfigurationAllowExeDefinition.MachineToApplication;
50 public SectionInfo (string sectionName, SectionInformation info)
54 this.allowLocation = info.AllowLocation;
55 this.allowDefinition = info.AllowDefinition;
56 this.allowExeDefinition = info.AllowExeDefinition;
57 this.requirePermission = info.RequirePermission;
58 this.restartOnExternalChanges = info.RestartOnExternalChanges;
61 public override object CreateInstance ()
63 object ob = base.CreateInstance ();
64 ConfigurationSection sec = ob as ConfigurationSection;
66 sec.SectionInformation.AllowLocation = allowLocation;
67 sec.SectionInformation.AllowDefinition = allowDefinition;
68 sec.SectionInformation.AllowExeDefinition = allowExeDefinition;
69 sec.SectionInformation.RequirePermission = requirePermission;
70 sec.SectionInformation.RestartOnExternalChanges = restartOnExternalChanges;
71 sec.SectionInformation.SetName (Name);
76 public override bool HasDataContent (Configuration config)
78 return config.GetSectionInstance (this, false) != null || config.GetSectionXml (this) != null;
81 public override bool HasConfigContent (Configuration cfg)
83 return StreamName == cfg.FileName;
86 public override void ReadConfig (Configuration cfg, string streamName, XmlReader reader)
88 StreamName = streamName;
89 ConfigHost = cfg.ConfigHost;
91 while (reader.MoveToNextAttribute ()) {
95 string allowLoc = reader.Value;
96 allowLocation = (allowLoc == "true");
97 if (!allowLocation && allowLoc != "false")
98 ThrowException ("Invalid attribute value", reader);
101 case "allowDefinition":
102 string allowDef = reader.Value;
104 allowDefinition = (ConfigurationAllowDefinition) Enum.Parse (
105 typeof (ConfigurationAllowDefinition), allowDef);
107 ThrowException ("Invalid attribute value", reader);
111 case "allowExeDefinition":
112 string allowExeDef = reader.Value;
114 allowExeDefinition = (ConfigurationAllowExeDefinition) Enum.Parse (
115 typeof (ConfigurationAllowExeDefinition), allowExeDef);
117 ThrowException ("Invalid attribute value", reader);
122 TypeName = reader.Value;
127 if (Name == "location")
128 ThrowException ("location is a reserved section name", reader);
131 case "requirePermission":
132 string reqPerm = reader.Value;
133 bool reqPermValue = (reqPerm == "true");
134 if (!reqPermValue && reqPerm != "false")
135 ThrowException ("Invalid attribute value", reader);
136 requirePermission = reqPermValue;
139 case "restartOnExternalChanges":
140 string restart = reader.Value;
141 bool restartValue = (restart == "true");
142 if (!restartValue && restart != "false")
143 ThrowException ("Invalid attribute value", reader);
144 restartOnExternalChanges = restartValue;
148 ThrowException (String.Format ("Unrecognized attribute: {0}", reader.Name), reader);
153 if (Name == null || TypeName == null)
154 ThrowException ("Required attribute missing", reader);
156 reader.MoveToElement();
160 public override void WriteConfig (Configuration cfg, XmlWriter writer, ConfigurationSaveMode mode)
162 writer.WriteStartElement ("section");
163 writer.WriteAttributeString ("name", Name);
164 writer.WriteAttributeString ("type", TypeName);
166 writer.WriteAttributeString ("allowLocation", "false");
167 if (allowDefinition != ConfigurationAllowDefinition.Everywhere)
168 writer.WriteAttributeString ("allowDefinition", allowDefinition.ToString ());
169 if (allowExeDefinition != ConfigurationAllowExeDefinition.MachineToApplication)
170 writer.WriteAttributeString ("allowExeDefinition", allowExeDefinition.ToString ());
171 if (!requirePermission)
172 writer.WriteAttributeString ("requirePermission", "false");
173 writer.WriteEndElement ();
176 public override void ReadData (Configuration config, XmlReader reader, bool overrideAllowed)
178 if (!config.HasFile && !allowLocation)
179 throw new ConfigurationErrorsException ("The configuration section <" + Name + "> cannot be defined inside a <location> element.", reader);
180 if (!config.ConfigHost.IsDefinitionAllowed (config.ConfigPath, allowDefinition, allowExeDefinition)) {
181 object ctx = allowExeDefinition != ConfigurationAllowExeDefinition.MachineToApplication ? (object) allowExeDefinition : (object) allowDefinition;
182 throw new ConfigurationErrorsException ("The section <" + Name + "> can't be defined in this configuration file (the allowed definition context is '" + ctx + "').", reader);
184 if (config.GetSectionXml (this) != null)
185 ThrowException ("The section <" + Name + "> is defined more than once in the same configuration file.", reader);
186 config.SetSectionXml (this, reader.ReadOuterXml ());
189 public override void WriteData (Configuration config, XmlWriter writer, ConfigurationSaveMode mode)
193 ConfigurationSection section = config.GetSectionInstance (this, false);
194 if (section != null) {
195 ConfigurationSection parentSection = config.Parent != null ? config.Parent.GetSectionInstance (this, false) : null;
196 xml = section.SerializeSection (parentSection, Name, mode);
198 string externalDataXml = section.ExternalDataXml;
199 string filePath = config.FilePath;
201 if (!String.IsNullOrEmpty (filePath) && !String.IsNullOrEmpty (externalDataXml)) {
202 string path = Path.Combine (Path.GetDirectoryName (filePath), section.SectionInformation.ConfigSource);
203 using (StreamWriter sw = new StreamWriter (path)) {
204 sw.Write (externalDataXml);
208 if (section.SectionInformation.IsProtected) {
209 StringBuilder sb = new StringBuilder ();
210 sb.AppendFormat ("<{0} configProtectionProvider=\"{1}\">\n",
212 section.SectionInformation.ProtectionProvider.Name);
213 sb.Append (config.ConfigHost.EncryptSection (xml,
214 section.SectionInformation.ProtectionProvider,
215 ProtectedConfiguration.Section));
216 sb.AppendFormat ("</{0}>", Name);
217 xml = sb.ToString ();
221 xml = config.GetSectionXml (this);
224 if (!string.IsNullOrEmpty (xml)) {
225 writer.WriteRaw (xml);
226 /* XmlTextReader tr = new XmlTextReader (new StringReader (xml));
227 writer.WriteNode (tr, true);
232 internal override void Merge (ConfigInfo data)
235 internal override bool HasValues (Configuration config, ConfigurationSaveMode mode)
237 var section = config.GetSectionInstance (this, false);
241 var parent = config.Parent != null ? config.Parent.GetSectionInstance (this, false) : null;
242 return section.HasValues (parent, mode);
245 internal override void ResetModified (Configuration config)
247 ConfigurationSection section = config.GetSectionInstance (this, false);
249 section.ResetModified ();