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;
35 namespace System.Configuration
37 internal class SectionInfo: ConfigInfo
39 public bool AllowLocation = true;
40 public ConfigurationAllowDefinition AllowDefinition = ConfigurationAllowDefinition.Everywhere;
46 public SectionInfo (string sectionName, string typeName,
47 bool allowLocation, ConfigurationAllowDefinition allowDefinition)
51 AllowLocation = allowLocation;
52 AllowDefinition = allowDefinition;
55 public override bool HasDataContent (Configuration config)
57 return config.GetSectionInstance (this, false) != null || config.GetSectionXml (this) != null;
60 public override bool HasConfigContent (Configuration cfg)
62 return StreamName == cfg.FileName;
65 public override void ReadConfig (Configuration cfg, string streamName, XmlTextReader reader)
67 ConfigurationAllowDefinition allowDefinition;
68 StreamName = streamName;
70 while (reader.MoveToNextAttribute ()) {
74 string allowLoc = reader.Value;
75 AllowLocation = (allowLoc == "true");
76 if (!AllowLocation && allowLoc != "false")
77 ThrowException ("Invalid attribute value", reader);
80 case "allowDefinition":
81 string allowDef = reader.Value;
83 allowDefinition = (ConfigurationAllowDefinition) Enum.Parse (
84 typeof (ConfigurationAllowDefinition), allowDef);
86 ThrowException ("Invalid attribute value", reader);
91 TypeName = reader.Value;
96 if (Name == "location")
97 ThrowException ("location is a reserved section name", reader);
101 ThrowException ("Unrecognized attribute.", reader);
106 if (Name == null || TypeName == null)
107 ThrowException ("Required attribute missing", reader);
109 reader.MoveToElement();
113 public override void WriteConfig (Configuration cfg, XmlWriter writer, ConfigurationSaveMode mode)
115 writer.WriteStartElement ("section");
116 writer.WriteAttributeString ("name", Name);
117 writer.WriteAttributeString ("type", TypeName);
119 writer.WriteAttributeString ("allowLocation", "false");
120 if (AllowDefinition != ConfigurationAllowDefinition.Everywhere)
121 writer.WriteAttributeString ("allowDefinition", AllowDefinition.ToString ());
122 writer.WriteEndElement ();
125 public override void ReadData (Configuration config, XmlTextReader reader)
127 config.SetSectionXml (this, reader.ReadOuterXml ());
130 public override void WriteData (Configuration config, XmlWriter writer, ConfigurationSaveMode mode)
134 ConfigurationSection section = config.GetSectionInstance (this, false);
135 if (section != null) {
136 ConfigurationSection parentSection = config.Parent != null ? config.Parent.GetSectionInstance (this, false) : null;
137 xml = section.SerializeSection (parentSection, Name, mode);
140 xml = config.GetSectionXml (this);
144 XmlTextReader tr = new XmlTextReader (new StringReader (xml));
145 writer.WriteNode (tr, true);