* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Configuration / System.Configuration / SectionInfo.cs
index ca44db8ff497fe9ca70441305e4d844f15f79688..63ede7f880a477a7384cb00b9cbe2f385914ca65 100644 (file)
@@ -30,26 +30,45 @@ using System;
 using System.Collections;
 using System.Collections.Specialized;
 using System.Xml;
+using System.Text;
 using System.IO;
 
 namespace System.Configuration
 {
        internal class SectionInfo: ConfigInfo
        {
-               public bool AllowLocation = true;
-               public ConfigurationAllowDefinition AllowDefinition = ConfigurationAllowDefinition.Everywhere;
+               bool allowLocation = true;
+               bool? requirePermission = true;
+               ConfigurationAllowDefinition allowDefinition = ConfigurationAllowDefinition.Everywhere;
+               ConfigurationAllowExeDefinition allowExeDefinition = ConfigurationAllowExeDefinition.MachineToApplication;
 
                public SectionInfo ()
                {
                }
                
-               public SectionInfo (string sectionName, string typeName,
-                                   bool allowLocation, ConfigurationAllowDefinition allowDefinition)
+               public SectionInfo (string sectionName, SectionInformation info)
                {
                        Name = sectionName;
-                       TypeName = typeName;
-                       AllowLocation = allowLocation;
-                       AllowDefinition = allowDefinition;
+                       TypeName = info.Type;
+                       this.allowLocation = info.AllowLocation;
+                       this.allowDefinition = info.AllowDefinition;
+                       this.allowExeDefinition = info.AllowExeDefinition;
+                       this.requirePermission = info.RequirePermission;
+               }
+               
+               public override object CreateInstance ()
+               {
+                       object ob = base.CreateInstance ();
+                       ConfigurationSection sec = ob as ConfigurationSection;
+                       if (sec != null) {
+                               sec.SectionInformation.AllowLocation = allowLocation;
+                               sec.SectionInformation.AllowDefinition = allowDefinition;
+                               sec.SectionInformation.AllowExeDefinition = allowExeDefinition;
+                               if (requirePermission != null)
+                                       sec.SectionInformation.RequirePermission = requirePermission.Value;
+                               sec.SectionInformation.SetName (Name);
+                       }
+                       return ob;
                }
                
                public override bool HasDataContent (Configuration config)
@@ -64,7 +83,6 @@ namespace System.Configuration
 
                public override void ReadConfig (Configuration cfg, string streamName, XmlTextReader reader)
                {
-                       ConfigurationAllowDefinition allowDefinition;
                        StreamName = streamName;
                        ConfigHost = cfg.ConfigHost;
 
@@ -73,8 +91,8 @@ namespace System.Configuration
                                {
                                        case "allowLocation":
                                                string allowLoc = reader.Value;
-                                               AllowLocation = (allowLoc == "true");
-                                               if (!AllowLocation && allowLoc != "false")
+                                               allowLocation = (allowLoc == "true");
+                                               if (!allowLocation && allowLoc != "false")
                                                        ThrowException ("Invalid attribute value", reader);
                                                break;
        
@@ -88,6 +106,16 @@ namespace System.Configuration
                                                }
                                                break;
        
+                                       case "allowExeDefinition":
+                                               string allowExeDef = reader.Value;
+                                               try {
+                                                       allowExeDefinition = (ConfigurationAllowExeDefinition) Enum.Parse (
+                                                                          typeof (ConfigurationAllowExeDefinition), allowExeDef);
+                                               } catch {
+                                                       ThrowException ("Invalid attribute value", reader);
+                                               }
+                                               break;
+       
                                        case "type":
                                                TypeName = reader.Value;
                                                break;
@@ -98,8 +126,16 @@ namespace System.Configuration
                                                        ThrowException ("location is a reserved section name", reader);
                                                break;
                                                
+                                       case "requirePermission":
+                                               string reqPerm = reader.Value;
+                                               bool reqPermValue = (reqPerm == "true");
+                                               if (!reqPermValue && reqPerm != "false")
+                                                       ThrowException ("Invalid attribute value", reader);
+                                               requirePermission = reqPermValue;
+                                               break;
+
                                        default:
-                                               ThrowException ("Unrecognized attribute.", reader);
+                                               ThrowException (String.Format ("Unrecognized attribute: {0}", reader.Name), reader);
                                                break;
                                }
                        }
@@ -116,15 +152,27 @@ namespace System.Configuration
                        writer.WriteStartElement ("section");
                        writer.WriteAttributeString ("name", Name);
                        writer.WriteAttributeString ("type", TypeName);
-                       if (!AllowLocation)
+                       if (!allowLocation)
                                writer.WriteAttributeString ("allowLocation", "false");
-                       if (AllowDefinition != ConfigurationAllowDefinition.Everywhere)
-                               writer.WriteAttributeString ("allowDefinition", AllowDefinition.ToString ());
+                       if (allowDefinition != ConfigurationAllowDefinition.Everywhere)
+                               writer.WriteAttributeString ("allowDefinition", allowDefinition.ToString ());
+                       if (allowExeDefinition != ConfigurationAllowExeDefinition.MachineToApplication)
+                               writer.WriteAttributeString ("allowExeDefinition", allowExeDefinition.ToString ());
+                       if (requirePermission != null)
+                               writer.WriteAttributeString ("requirePermission", requirePermission.Value ? "true" : "false");
                        writer.WriteEndElement ();
                }
                
-               public override void ReadData (Configuration config, XmlTextReader reader)
+               public override void ReadData (Configuration config, XmlTextReader reader, bool overrideAllowed)
                {
+                       if (!config.HasFile && !allowLocation)
+                               throw new ConfigurationErrorsException ("The configuration section <" + Name + "> cannot be defined inside a <location> element.", reader); 
+                       if (!config.ConfigHost.IsDefinitionAllowed (config.ConfigPath, allowDefinition, allowExeDefinition)) {
+                               object ctx = allowExeDefinition != ConfigurationAllowExeDefinition.MachineToApplication ? (object) allowExeDefinition : (object) allowDefinition;
+                               throw new ConfigurationErrorsException ("The section <" + Name + "> can't be defined in this configuration file (the allowed definition context is '" + ctx + "').", reader);
+                       }
+                       if (config.GetSectionXml (this) != null)
+                               ThrowException ("The section <" + Name + "> is defined more than once in the same configuration file.", reader);
                        config.SetSectionXml (this, reader.ReadOuterXml ());
                }
                
@@ -136,15 +184,28 @@ namespace System.Configuration
                        if (section != null) {
                                ConfigurationSection parentSection = config.Parent != null ? config.Parent.GetSectionInstance (this, false) : null;
                                xml = section.SerializeSection (parentSection, Name, mode);
+
+                               if (section.SectionInformation.IsProtected) {
+                                       StringBuilder sb = new StringBuilder ();
+                                       sb.AppendFormat ("<{0} configProtectionProvider=\"{1}\">\n",
+                                                        Name,
+                                                        section.SectionInformation.ProtectionProvider.Name);
+                                       sb.Append (config.ConfigHost.EncryptSection (xml,
+                                                                                    section.SectionInformation.ProtectionProvider,
+                                                                                    ProtectedConfiguration.Section));
+                                       sb.AppendFormat ("</{0}>", Name);
+                                       xml = sb.ToString ();
+                               }
                        }
                        else {
                                xml = config.GetSectionXml (this);
                        }
                        
                        if (xml != null) {
-                               XmlTextReader tr = new XmlTextReader (new StringReader (xml));
+                               writer.WriteRaw (xml);
+/*                             XmlTextReader tr = new XmlTextReader (new StringReader (xml));
                                writer.WriteNode (tr, true);
-                               tr.Close ();
+                               tr.Close ();*/
                        }
                }
        }