2006-04-28 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / class / System.Configuration / System.Configuration / SectionInfo.cs
1 //
2 // System.Configuration.SectionInfo.cs
3 //
4 // Authors:
5 //      Lluis Sanchez (lluis@novell.com)
6 //
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:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
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.
25 //
26 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
27 //
28 #if NET_2_0
29 using System;
30 using System.Collections;
31 using System.Collections.Specialized;
32 using System.Xml;
33 using System.Text;
34 using System.IO;
35
36 namespace System.Configuration
37 {
38         internal class SectionInfo: ConfigInfo
39         {
40                 bool allowLocation = true;
41                 bool? requirePermission = true;
42                 ConfigurationAllowDefinition allowDefinition = ConfigurationAllowDefinition.Everywhere;
43                 ConfigurationAllowExeDefinition allowExeDefinition = ConfigurationAllowExeDefinition.MachineToApplication;
44
45                 public SectionInfo ()
46                 {
47                 }
48                 
49                 public SectionInfo (string sectionName, SectionInformation info)
50                 {
51                         Name = sectionName;
52                         TypeName = info.Type;
53                         this.allowLocation = info.AllowLocation;
54                         this.allowDefinition = info.AllowDefinition;
55                         this.allowExeDefinition = info.AllowExeDefinition;
56                         this.requirePermission = info.RequirePermission;
57                 }
58                 
59                 public override object CreateInstance ()
60                 {
61                         object ob = base.CreateInstance ();
62                         ConfigurationSection sec = ob as ConfigurationSection;
63                         if (sec != null) {
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);
70                         }
71                         return ob;
72                 }
73                 
74                 public override bool HasDataContent (Configuration config)
75                 {
76                         return config.GetSectionInstance (this, false) != null || config.GetSectionXml (this) != null;
77                 }
78                 
79                 public override bool HasConfigContent (Configuration cfg)
80                 {
81                         return StreamName == cfg.FileName;
82                 }
83
84                 public override void ReadConfig (Configuration cfg, string streamName, XmlTextReader reader)
85                 {
86                         StreamName = streamName;
87                         ConfigHost = cfg.ConfigHost;
88
89                         while (reader.MoveToNextAttribute ()) {
90                                 switch (reader.Name)
91                                 {
92                                         case "allowLocation":
93                                                 string allowLoc = reader.Value;
94                                                 allowLocation = (allowLoc == "true");
95                                                 if (!allowLocation && allowLoc != "false")
96                                                         ThrowException ("Invalid attribute value", reader);
97                                                 break;
98         
99                                         case "allowDefinition":
100                                                 string allowDef = reader.Value;
101                                                 try {
102                                                         allowDefinition = (ConfigurationAllowDefinition) Enum.Parse (
103                                                                            typeof (ConfigurationAllowDefinition), allowDef);
104                                                 } catch {
105                                                         ThrowException ("Invalid attribute value", reader);
106                                                 }
107                                                 break;
108         
109                                         case "allowExeDefinition":
110                                                 string allowExeDef = reader.Value;
111                                                 try {
112                                                         allowExeDefinition = (ConfigurationAllowExeDefinition) Enum.Parse (
113                                                                            typeof (ConfigurationAllowExeDefinition), allowExeDef);
114                                                 } catch {
115                                                         ThrowException ("Invalid attribute value", reader);
116                                                 }
117                                                 break;
118         
119                                         case "type":
120                                                 TypeName = reader.Value;
121                                                 break;
122                                         
123                                         case "name":
124                                                 Name = reader.Value;
125                                                 if (Name == "location")
126                                                         ThrowException ("location is a reserved section name", reader);
127                                                 break;
128                                                 
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;
135                                                 break;
136
137                                         default:
138                                                 ThrowException (String.Format ("Unrecognized attribute: {0}", reader.Name), reader);
139                                                 break;
140                                 }
141                         }
142
143                         if (Name == null || TypeName == null)
144                                 ThrowException ("Required attribute missing", reader);
145
146                         reader.MoveToElement();
147                         reader.Skip ();
148                 }
149                 
150                 public override void WriteConfig (Configuration cfg, XmlWriter writer, ConfigurationSaveMode mode)
151                 {
152                         writer.WriteStartElement ("section");
153                         writer.WriteAttributeString ("name", Name);
154                         writer.WriteAttributeString ("type", TypeName);
155                         if (!allowLocation)
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 ();
164                 }
165                 
166                 public override void ReadData (Configuration config, XmlTextReader reader, bool overrideAllowed)
167                 {
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);
173                         }
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 ());
177                 }
178                 
179                 public override void WriteData (Configuration config, XmlWriter writer, ConfigurationSaveMode mode)
180                 {
181                         string xml;
182                         
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);
187
188                                 if (section.SectionInformation.IsProtected) {
189                                         StringBuilder sb = new StringBuilder ();
190                                         sb.AppendFormat ("<{0} configProtectionProvider=\"{1}\">\n",
191                                                          Name,
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 ();
198                                 }
199                         }
200                         else {
201                                 xml = config.GetSectionXml (this);
202                         }
203                         
204                         if (xml != null) {
205                                 writer.WriteRaw (xml);
206 /*                              XmlTextReader tr = new XmlTextReader (new StringReader (xml));
207                                 writer.WriteNode (tr, true);
208                                 tr.Close ();*/
209                         }
210                 }
211         }
212 }
213
214 #endif