2 // System.Configuration.SectionGroupInfo.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 SectionGroupInfo: ConfigInfo
40 ConfigInfoCollection sections;
41 ConfigInfoCollection groups;
42 static ConfigInfoCollection emptyList = new ConfigInfoCollection ();
44 public SectionGroupInfo ()
46 Type = typeof (ConfigurationSectionGroup);
49 public SectionGroupInfo (string groupName, string typeName)
55 public void AddChild (ConfigInfo data)
58 if (data is SectionInfo) {
59 if (sections == null) sections = new ConfigInfoCollection ();
60 sections [data.Name] = data;
63 if (groups == null) groups = new ConfigInfoCollection ();
64 groups [data.Name] = data;
70 if (sections != null) sections.Clear ();
71 if (groups != null) groups.Clear ();
74 public bool HasChild (string name)
76 if (sections != null && sections [name] != null) return true;
77 return (groups != null && groups [name] != null);
80 public void RemoveChild (string name)
83 sections.Remove (name);
88 public SectionInfo GetChildSection (string name)
91 return sections [name] as SectionInfo;
96 public SectionGroupInfo GetChildGroup (string name)
99 return groups [name] as SectionGroupInfo;
104 public ConfigInfoCollection Sections
106 get { if (sections == null) return emptyList; else return sections; }
109 public ConfigInfoCollection Groups
111 get { if (groups == null) return emptyList; else return groups; }
114 public override bool HasDataContent (Configuration config)
116 foreach (ConfigInfoCollection col in new object[] {Sections, Groups}) {
117 foreach (string key in col) {
118 ConfigInfo cinfo = col [key];
119 if (cinfo.HasDataContent (config))
126 public override bool HasConfigContent (Configuration cfg)
128 if (StreamName == cfg.FileName) return true;
129 foreach (ConfigInfoCollection col in new object[] {Sections, Groups}) {
130 foreach (string key in col) {
131 ConfigInfo cinfo = col [key];
132 if (cinfo.HasConfigContent (cfg))
139 public override void ReadConfig (Configuration cfg, string streamName, XmlTextReader reader)
141 StreamName = streamName;
142 ConfigHost = cfg.ConfigHost;
144 if (reader.LocalName != "configSections")
146 while (reader.MoveToNextAttribute ()) {
147 if (reader.Name == "name")
149 else if (reader.Name == "type") {
150 TypeName = reader.Value;
154 ThrowException ("Unrecognized attribute", reader);
158 ThrowException ("sectionGroup must have a 'name' attribute", reader);
160 if (Name == "location")
161 ThrowException ("location is a reserved section name", reader);
164 if (TypeName == null)
165 TypeName = "System.Configuration.ConfigurationSectionGroup";
167 if (reader.IsEmptyElement) {
172 reader.ReadStartElement ();
173 reader.MoveToContent ();
175 while (reader.NodeType != XmlNodeType.EndElement)
177 if (reader.NodeType != XmlNodeType.Element) {
182 string name = reader.LocalName;
183 ConfigInfo cinfo = null;
185 if (name == "remove") {
186 ReadRemoveSection (reader);
190 if (name == "clear") {
191 if (reader.HasAttributes)
192 ThrowException ("Unrecognized attribute.", reader);
199 if (name == "section")
200 cinfo = new SectionInfo ();
201 else if (name == "sectionGroup") {
202 cinfo = new SectionGroupInfo ();
204 ThrowException ("Unrecognized element: " + reader.Name, reader);
206 cinfo.ReadConfig (cfg, streamName, reader);
207 ConfigInfo actInfo = Groups [cinfo.Name];
208 if (actInfo == null) actInfo = Sections [cinfo.Name];
210 if (actInfo != null) {
211 if (actInfo.GetType () != cinfo.GetType ())
212 ThrowException ("A section or section group named '" + cinfo.Name + "' already exists", reader);
213 // Merge all the new data
214 actInfo.Merge (cinfo);
216 // Make sure that this section is saved in this configuration file:
217 actInfo.StreamName = streamName;
223 reader.ReadEndElement ();
226 public override void WriteConfig (Configuration cfg, XmlWriter writer, ConfigurationSaveMode mode)
229 writer.WriteStartElement ("sectionGroup");
230 writer.WriteAttributeString ("name", Name);
231 if (TypeName != null && TypeName != "" && TypeName != "System.Configuration.ConfigurationSectionGroup")
232 writer.WriteAttributeString ("type", TypeName);
235 writer.WriteStartElement ("configSections");
237 foreach (ConfigInfoCollection col in new object[] {Sections, Groups}) {
238 foreach (string key in col) {
239 ConfigInfo cinfo = col [key];
240 if (cinfo.HasConfigContent (cfg))
241 cinfo.WriteConfig (cfg, writer, mode);
245 writer.WriteEndElement ();
248 private void ReadRemoveSection (XmlTextReader reader)
250 if (!reader.MoveToNextAttribute () || reader.Name != "name")
251 ThrowException ("Unrecognized attribute.", reader);
253 string removeValue = reader.Value;
254 if (removeValue == null || removeValue.Length == 0)
255 ThrowException ("Empty name to remove", reader);
257 reader.MoveToElement ();
259 if (!HasChild (removeValue))
260 ThrowException ("No factory for " + removeValue, reader);
262 RemoveChild (removeValue);
266 public void ReadRootData (XmlTextReader reader, Configuration config, bool overrideAllowed)
268 reader.MoveToContent ();
269 ReadContent (reader, config, overrideAllowed, true);
272 public override void ReadData (Configuration config, XmlTextReader reader, bool overrideAllowed)
274 reader.MoveToContent ();
275 reader.ReadStartElement ();
276 ReadContent (reader, config, overrideAllowed, false);
277 reader.MoveToContent ();
278 reader.ReadEndElement ();
281 void ReadContent (XmlTextReader reader, Configuration config, bool overrideAllowed, bool root)
283 if (reader.IsEmptyElement) {
288 StringBuilder spacing = new StringBuilder ();
289 while (reader.NodeType != XmlNodeType.EndElement) {
290 if (reader.NodeType != XmlNodeType.Element) {
291 if (reader.NodeType == XmlNodeType.Whitespace)
292 spacing.Append (reader.Value);
297 if (reader.LocalName == "dllmap") {
302 if (reader.LocalName == "location") {
304 ThrowException ("<location> elements are only allowed in <configuration> elements.", reader);
306 string allowOverrideAttr = reader.GetAttribute ("allowOverride");
307 bool allowOverride = allowOverrideAttr == null || allowOverrideAttr.Length == 0 || bool.Parse (allowOverrideAttr);
308 string path = reader.GetAttribute ("path");
309 if (path != null && path.Length > 0) {
310 string xml = reader.ReadOuterXml ();
311 string[] pathList = path.Split (',');
312 foreach (string p in pathList) {
313 ConfigurationLocation loc = new ConfigurationLocation (p.Trim (), xml, config, allowOverride);
314 config.Locations.Add (loc);
317 ReadData (config, reader, allowOverride);
323 ConfigInfo data = GetConfigInfo (reader, this);
326 data.ReadData (config, reader, overrideAllowed);
328 ThrowException ("Unrecognized configuration section <" + reader.LocalName + ">", reader);
332 ConfigInfo GetConfigInfo (XmlReader reader, SectionGroupInfo current)
334 ConfigInfo data = null;
335 if (current.sections != null)
336 data = current.sections [reader.LocalName];
339 if (current.groups != null)
340 data = current.groups [reader.LocalName];
343 if (current.groups == null)
345 // It might be a section in descendant sectionGroups
346 foreach (string key in current.groups.AllKeys) {
347 data = GetConfigInfo (reader, (SectionGroupInfo) current.groups [key]);
351 // It might be in the root section group
355 internal override void Merge (ConfigInfo newData)
357 SectionGroupInfo data = newData as SectionGroupInfo;
361 if (data.sections != null && data.sections.Count > 0)
362 foreach (string key in data.sections.AllKeys) {
363 actInfo = sections[key];
366 sections.Add (key, data.sections[key]);
369 if (data.groups != null && data.sections.Count > 0)
370 foreach (string key in data.groups.AllKeys) {
371 actInfo = groups[key];
374 groups.Add (key, data.groups[key]);
378 public void WriteRootData (XmlWriter writer, Configuration config, ConfigurationSaveMode mode)
380 WriteContent (writer, config, mode, false);
383 public override void WriteData (Configuration config, XmlWriter writer, ConfigurationSaveMode mode)
385 writer.WriteStartElement (Name);
386 WriteContent (writer, config, mode, true);
387 writer.WriteEndElement ();
390 public void WriteContent (XmlWriter writer, Configuration config, ConfigurationSaveMode mode, bool writeElem)
392 foreach (ConfigInfoCollection col in new object[] {Sections, Groups}) {
393 foreach (string key in col) {
394 ConfigInfo cinfo = col [key];
395 if (cinfo.HasDataContent (config))
396 cinfo.WriteData (config, writer, mode);
402 internal class ConfigInfoCollection : NameObjectCollectionBase
404 public ConfigInfoCollection ()
405 : base (StringComparer.Ordinal)
409 public ICollection AllKeys
414 public ConfigInfo this [string name]
416 get { return (ConfigInfo) BaseGet (name); }
417 set { BaseSet (name, value); }
420 public ConfigInfo this [int index]
422 get { return (ConfigInfo) BaseGet (index); }
423 set { BaseSet (index, value); }
426 public void Add (string name, ConfigInfo config)
428 BaseAdd (name, config);
436 public string GetKey (int index)
438 return BaseGetKey (index);
441 public void Remove (string name)
446 public void RemoveAt (int index)
448 BaseRemoveAt (index);