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;
35 namespace System.Configuration
37 internal class SectionGroupInfo: ConfigInfo
39 ConfigInfoCollection sections;
40 ConfigInfoCollection groups;
41 static ConfigInfoCollection emptyList = new ConfigInfoCollection ();
43 public SectionGroupInfo ()
45 TypeName = "System.Configuration.ConfigurationSectionGroup";
48 public SectionGroupInfo (string groupName, string typeName)
54 public void AddChild (ConfigInfo data)
57 if (data is SectionInfo) {
58 if (sections == null) sections = new ConfigInfoCollection ();
59 sections [data.Name] = data;
62 if (groups == null) groups = new ConfigInfoCollection ();
63 groups [data.Name] = data;
69 if (sections != null) sections.Clear ();
70 if (groups != null) groups.Clear ();
73 public bool HasChild (string name)
75 if (sections != null && sections [name] != null) return true;
76 return (groups != null && groups [name] != null);
79 public void RemoveChild (string name)
82 sections.Remove (name);
87 public SectionInfo GetChildSection (string name)
90 return sections [name] as SectionInfo;
95 public SectionGroupInfo GetChildGroup (string name)
98 return groups [name] as SectionGroupInfo;
103 public ConfigInfoCollection Sections
105 get { if (sections == null) return emptyList; else return sections; }
108 public ConfigInfoCollection Groups
110 get { if (groups == null) return emptyList; else return groups; }
113 public override bool HasDataContent (Configuration config)
115 foreach (ConfigInfoCollection col in new object[] {Sections, Groups}) {
116 foreach (string key in col) {
117 ConfigInfo cinfo = col [key];
118 if (cinfo.HasDataContent (config))
125 public override bool HasConfigContent (Configuration cfg)
127 if (FileName == cfg.FileName) return true;
128 foreach (ConfigInfoCollection col in new object[] {Sections, Groups}) {
129 foreach (string key in col) {
130 ConfigInfo cinfo = col [key];
131 if (cinfo.HasConfigContent (cfg))
138 public override void ReadConfig (Configuration cfg, XmlTextReader reader)
140 FileName = cfg.FileName;
142 if (reader.LocalName != "configSections")
144 while (reader.MoveToNextAttribute ()) {
145 if (reader.Name == "name")
147 else if (reader.Name == "type")
148 TypeName = reader.Value;
150 ThrowException ("Unrecognized attribute", reader);
154 ThrowException ("sectionGroup must have a 'name' attribute", reader);
156 if (Name == "location")
157 ThrowException ("location is a reserved section name", reader);
160 if (TypeName == null)
161 TypeName = "System.Configuration.ConfigurationSectionGroup";
163 if (reader.IsEmptyElement) {
168 reader.ReadStartElement ();
169 reader.MoveToContent ();
171 while (reader.NodeType != XmlNodeType.EndElement)
173 if (reader.NodeType != XmlNodeType.Element) {
178 string name = reader.LocalName;
179 ConfigInfo cinfo = null;
181 if (name == "remove") {
182 ReadRemoveSection (reader);
186 if (name == "clear") {
187 if (reader.HasAttributes)
188 ThrowException ("Unrecognized attribute.", reader);
195 if (name == "section")
196 cinfo = new SectionInfo ();
197 else if (name == "sectionGroup")
198 cinfo = new SectionGroupInfo ();
200 ThrowException ("Unrecognized element: " + reader.Name, reader);
202 cinfo.ReadConfig (cfg, reader);
203 ConfigInfo actInfo = Groups [cinfo.Name];
204 if (actInfo == null) actInfo = Sections [cinfo.Name];
206 if (actInfo != null) {
207 if (actInfo.GetType () != cinfo.GetType ())
208 ThrowException ("A section or section group named '" + cinfo.Name + "' already exists", reader);
209 // Make sure that this section is saved in this configuration file:
210 actInfo.FileName = cfg.FileName;
216 reader.ReadEndElement ();
219 public override void WriteConfig (Configuration cfg, XmlWriter writer, ConfigurationSaveMode mode)
222 writer.WriteStartElement ("sectionGroup");
223 writer.WriteAttributeString ("name", Name);
224 if (TypeName != null && TypeName != "" && TypeName != "System.Configuration.ConfigurationSectionGroup")
225 writer.WriteAttributeString ("type", TypeName);
228 writer.WriteStartElement ("configSections");
230 foreach (ConfigInfoCollection col in new object[] {Sections, Groups}) {
231 foreach (string key in col) {
232 ConfigInfo cinfo = col [key];
233 if (cinfo.HasConfigContent (cfg))
234 cinfo.WriteConfig (cfg, writer, mode);
238 writer.WriteEndElement ();
241 private void ReadRemoveSection (XmlTextReader reader)
243 if (!reader.MoveToNextAttribute () || reader.Name != "name")
244 ThrowException ("Unrecognized attribute.", reader);
246 string removeValue = reader.Value;
247 if (removeValue == null || removeValue.Length == 0)
248 ThrowException ("Empty name to remove", reader);
250 reader.MoveToElement ();
252 if (!HasChild (removeValue))
253 ThrowException ("No factory for " + removeValue, reader);
255 RemoveChild (removeValue);
259 public void ReadRootData (XmlTextReader reader, Configuration config)
261 reader.MoveToContent ();
262 ReadContent (reader, config);
265 public override void ReadData (Configuration config, XmlTextReader reader)
267 reader.MoveToContent ();
268 reader.ReadStartElement ();
269 ReadContent (reader, config);
270 reader.MoveToContent ();
271 reader.ReadEndElement ();
274 void ReadContent (XmlTextReader reader, Configuration config)
276 while (reader.NodeType != XmlNodeType.EndElement) {
277 if (reader.NodeType != XmlNodeType.Element) {
281 if (reader.LocalName == "location") {
282 Configuration locConfig = new Configuration (config);
283 string path = reader.GetAttribute ("path");
284 ConfigurationLocation loc = new ConfigurationLocation (path, locConfig);
285 config.Locations.Add (loc);
286 ReadData (locConfig, reader);
289 ConfigInfo data = (sections != null) ? (ConfigInfo) sections [reader.LocalName] : (ConfigInfo) null;
290 if (data == null) data = (groups != null) ? (ConfigInfo) groups [reader.LocalName] : (ConfigInfo) null;
293 data.ReadData (config, reader);
295 ThrowException ("Unrecognized configuration section <" + reader.LocalName + ">", reader);
299 public void WriteRootData (XmlWriter writer, Configuration config, ConfigurationSaveMode mode)
301 WriteContent (writer, config, mode, false);
304 public override void WriteData (Configuration config, XmlWriter writer, ConfigurationSaveMode mode)
306 writer.WriteStartElement (Name);
307 WriteContent (writer, config, mode, true);
308 writer.WriteEndElement ();
311 public void WriteContent (XmlWriter writer, Configuration config, ConfigurationSaveMode mode, bool writeElem)
313 foreach (ConfigInfoCollection col in new object[] {Sections, Groups}) {
314 foreach (string key in col) {
315 ConfigInfo cinfo = col [key];
316 if (cinfo.HasDataContent (config))
317 cinfo.WriteData (config, writer, mode);
323 internal class ConfigInfoCollection : NameObjectCollectionBase
325 public ICollection AllKeys
330 public ConfigInfo this [string name]
332 get { return (ConfigInfo) BaseGet (name); }
333 set { BaseSet (name, value); }
336 public ConfigInfo this [int index]
338 get { return (ConfigInfo) BaseGet (index); }
339 set { BaseSet (index, value); }
342 public void Add (string name, ConfigInfo config)
344 BaseAdd (name, config);
352 public string GetKey (int index)
354 return BaseGetKey (index);
357 public void Remove (string name)
362 public void RemoveAt (int index)
364 BaseRemoveAt (index);