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 ConfigurationAllowDefinition allowDefinition = ConfigurationAllowDefinition.Everywhere;
43 ConfigurationAllowExeDefinition allowExeDefinition = ConfigurationAllowExeDefinition.MachineToApplication;
49 public SectionInfo (string sectionName, SectionInformation info)
53 this.allowLocation = info.AllowLocation;
54 this.allowDefinition = info.AllowDefinition;
55 this.allowExeDefinition = info.AllowExeDefinition;
56 this.requirePermission = info.RequirePermission;
59 public override object CreateInstance ()
61 object ob = base.CreateInstance ();
62 ConfigurationSection sec = ob as ConfigurationSection;
64 sec.SectionInformation.AllowLocation = allowLocation;
65 sec.SectionInformation.AllowDefinition = allowDefinition;
66 sec.SectionInformation.AllowExeDefinition = allowExeDefinition;
67 if (requirePermission != null)
68 sec.SectionInformation.RequirePermission = requirePermission.Value;
69 sec.SectionInformation.SetName (Name);
74 public override bool HasDataContent (Configuration config)
76 return config.GetSectionInstance (this, false) != null || config.GetSectionXml (this) != null;
79 public override bool HasConfigContent (Configuration cfg)
81 return StreamName == cfg.FileName;
84 public override void ReadConfig (Configuration cfg, string streamName, XmlTextReader reader)
86 StreamName = streamName;
87 ConfigHost = cfg.ConfigHost;
89 while (reader.MoveToNextAttribute ()) {
93 string allowLoc = reader.Value;
94 allowLocation = (allowLoc == "true");
95 if (!allowLocation && allowLoc != "false")
96 ThrowException ("Invalid attribute value", reader);
99 case "allowDefinition":
100 string allowDef = reader.Value;
102 allowDefinition = (ConfigurationAllowDefinition) Enum.Parse (
103 typeof (ConfigurationAllowDefinition), allowDef);
105 ThrowException ("Invalid attribute value", reader);
109 case "allowExeDefinition":
110 string allowExeDef = reader.Value;
112 allowExeDefinition = (ConfigurationAllowExeDefinition) Enum.Parse (
113 typeof (ConfigurationAllowExeDefinition), allowExeDef);
115 ThrowException ("Invalid attribute value", reader);
120 TypeName = reader.Value;
125 if (Name == "location")
126 ThrowException ("location is a reserved section name", reader);
129 case "requirePermission":
130 string reqPerm = reader.Value;
131 bool reqPermValue = (reqPerm == "true");
132 if (!reqPermValue && reqPerm != "false")
133 ThrowException ("Invalid attribute value", reader);
134 requirePermission = reqPermValue;
138 ThrowException (String.Format ("Unrecognized attribute: {0}", reader.Name), reader);
143 if (Name == null || TypeName == null)
144 ThrowException ("Required attribute missing", reader);
146 reader.MoveToElement();
150 public override void WriteConfig (Configuration cfg, XmlWriter writer, ConfigurationSaveMode mode)
152 writer.WriteStartElement ("section");
153 writer.WriteAttributeString ("name", Name);
154 writer.WriteAttributeString ("type", TypeName);
156 writer.WriteAttributeString ("allowLocation", "false");
157 if (allowDefinition != ConfigurationAllowDefinition.Everywhere)
158 writer.WriteAttributeString ("allowDefinition", allowDefinition.ToString ());
159 if (allowExeDefinition != ConfigurationAllowExeDefinition.MachineToApplication)
160 writer.WriteAttributeString ("allowExeDefinition", allowExeDefinition.ToString ());
161 if (requirePermission != null)
162 writer.WriteAttributeString ("requirePermission", requirePermission.Value ? "true" : "false");
163 writer.WriteEndElement ();
166 public override void ReadData (Configuration config, XmlTextReader reader, bool overrideAllowed)
168 if (!config.HasFile && !allowLocation)
169 throw new ConfigurationErrorsException ("The configuration section <" + Name + "> cannot be defined inside a <location> element.", reader);
170 if (!config.ConfigHost.IsDefinitionAllowed (config.ConfigPath, allowDefinition, allowExeDefinition)) {
171 object ctx = allowExeDefinition != ConfigurationAllowExeDefinition.MachineToApplication ? (object) allowExeDefinition : (object) allowDefinition;
172 throw new ConfigurationErrorsException ("The section <" + Name + "> can't be defined in this configuration file (the allowed definition context is '" + ctx + "').", reader);
174 if (config.GetSectionXml (this) != null)
175 ThrowException ("The section <" + Name + "> is defined more than once in the same configuration file.", reader);
176 config.SetSectionXml (this, reader.ReadOuterXml ());
179 public override void WriteData (Configuration config, XmlWriter writer, ConfigurationSaveMode mode)
183 ConfigurationSection section = config.GetSectionInstance (this, false);
184 if (section != null) {
185 ConfigurationSection parentSection = config.Parent != null ? config.Parent.GetSectionInstance (this, false) : null;
186 xml = section.SerializeSection (parentSection, Name, mode);
188 if (section.SectionInformation.IsProtected) {
189 StringBuilder sb = new StringBuilder ();
190 sb.AppendFormat ("<{0} configProtectionProvider=\"{1}\">\n",
192 section.SectionInformation.ProtectionProvider.Name);
193 sb.Append (config.ConfigHost.EncryptSection (xml,
194 section.SectionInformation.ProtectionProvider,
195 ProtectedConfiguration.Section));
196 sb.AppendFormat ("</{0}>", Name);
197 xml = sb.ToString ();
201 xml = config.GetSectionXml (this);
205 writer.WriteRaw (xml);
206 /* XmlTextReader tr = new XmlTextReader (new StringReader (xml));
207 writer.WriteNode (tr, true);