2010-01-09 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Configuration / System.Configuration / SectionInfo.cs
index bb22167fd530f520d3742d8d95fe63d0e6eb8731..c9a99aff3b24a771e758dacf1272fc0ab872656a 100644 (file)
@@ -30,6 +30,7 @@ using System;
 using System.Collections;
 using System.Collections.Specialized;
 using System.Xml;
+using System.Text;
 using System.IO;
 
 namespace System.Configuration
@@ -37,6 +38,8 @@ namespace System.Configuration
        internal class SectionInfo: ConfigInfo
        {
                bool allowLocation = true;
+               bool requirePermission = true;
+               bool restartOnExternalChanges;
                ConfigurationAllowDefinition allowDefinition = ConfigurationAllowDefinition.Everywhere;
                ConfigurationAllowExeDefinition allowExeDefinition = ConfigurationAllowExeDefinition.MachineToApplication;
 
@@ -44,14 +47,15 @@ namespace System.Configuration
                {
                }
                
-               public SectionInfo (string sectionName, string typeName,
-                                   bool allowLocation, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
+               public SectionInfo (string sectionName, SectionInformation info)
                {
                        Name = sectionName;
-                       TypeName = typeName;
-                       this.allowLocation = allowLocation;
-                       this.allowDefinition = allowDefinition;
-                       this.allowExeDefinition = allowExeDefinition;
+                       TypeName = info.Type;
+                       this.allowLocation = info.AllowLocation;
+                       this.allowDefinition = info.AllowDefinition;
+                       this.allowExeDefinition = info.AllowExeDefinition;
+                       this.requirePermission = info.RequirePermission;
+                       this.restartOnExternalChanges = info.RestartOnExternalChanges;
                }
                
                public override object CreateInstance ()
@@ -62,6 +66,9 @@ namespace System.Configuration
                                sec.SectionInformation.AllowLocation = allowLocation;
                                sec.SectionInformation.AllowDefinition = allowDefinition;
                                sec.SectionInformation.AllowExeDefinition = allowExeDefinition;
+                               sec.SectionInformation.RequirePermission = requirePermission;
+                               sec.SectionInformation.RestartOnExternalChanges = restartOnExternalChanges;
+                               sec.SectionInformation.SetName (Name);
                        }
                        return ob;
                }
@@ -76,7 +83,7 @@ namespace System.Configuration
                        return StreamName == cfg.FileName;
                }
 
-               public override void ReadConfig (Configuration cfg, string streamName, XmlTextReader reader)
+               public override void ReadConfig (Configuration cfg, string streamName, XmlReader reader)
                {
                        StreamName = streamName;
                        ConfigHost = cfg.ConfigHost;
@@ -121,8 +128,24 @@ 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;
+
+                                       case "restartOnExternalChanges":
+                                               string restart = reader.Value;
+                                               bool restartValue = (restart == "true");
+                                               if (!restartValue && restart != "false")
+                                                       ThrowException ("Invalid attribute value", reader);
+                                               restartOnExternalChanges = restartValue;
+                                               break;
+
                                        default:
-                                               ThrowException ("Unrecognized attribute.", reader);
+                                               ThrowException (String.Format ("Unrecognized attribute: {0}", reader.Name), reader);
                                                break;
                                }
                        }
@@ -145,10 +168,12 @@ namespace System.Configuration
                                writer.WriteAttributeString ("allowDefinition", allowDefinition.ToString ());
                        if (allowExeDefinition != ConfigurationAllowExeDefinition.MachineToApplication)
                                writer.WriteAttributeString ("allowExeDefinition", allowExeDefinition.ToString ());
+                       if (!requirePermission)
+                               writer.WriteAttributeString ("requirePermission", "false");
                        writer.WriteEndElement ();
                }
                
-               public override void ReadData (Configuration config, XmlTextReader reader, bool overrideAllowed)
+               public override void ReadData (Configuration config, XmlReader reader, bool overrideAllowed)
                {
                        if (!config.HasFile && !allowLocation)
                                throw new ConfigurationErrorsException ("The configuration section <" + Name + "> cannot be defined inside a <location> element.", reader); 
@@ -169,9 +194,31 @@ namespace System.Configuration
                        if (section != null) {
                                ConfigurationSection parentSection = config.Parent != null ? config.Parent.GetSectionInstance (this, false) : null;
                                xml = section.SerializeSection (parentSection, Name, mode);
+
+                               string externalDataXml = section.ExternalDataXml;
+                               string filePath = config.FilePath;
+                               
+                               if (!String.IsNullOrEmpty (filePath) && !String.IsNullOrEmpty (externalDataXml)) {
+                                       string path = Path.Combine (Path.GetDirectoryName (filePath), section.SectionInformation.ConfigSource);
+                                       using (StreamWriter sw = new StreamWriter (path)) {
+                                               sw.Write (externalDataXml);
+                                       }
+                               }
+                               
+                               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)  + " <!-- dd -->";
+                               xml = config.GetSectionXml (this);
                        }
                        
                        if (xml != null) {
@@ -181,6 +228,9 @@ namespace System.Configuration
                                tr.Close ();*/
                        }
                }
+               
+               internal override void Merge (ConfigInfo data)
+               {}
        }
 }