2007-11-14 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Configuration / System.Configuration / Configuration.cs
index 19d71b61434779b30576617bbb2e38852950d701..9f6a3ae117511a6d8f2c1030543d4a3fb9d25616 100644 (file)
@@ -51,12 +51,14 @@ namespace System.Configuration {
                
                string configPath;
                string locationConfigPath;
-                       
-               internal Configuration (Configuration parent)
+               string locationSubPath;
+
+               internal Configuration (Configuration parent, string locationSubPath)
                {
                        this.parent = parent;
                        this.system = parent.system;
                        this.rootGroup = parent.rootGroup;
+                       this.locationSubPath = locationSubPath;
                }
                
                internal Configuration (InternalConfigurationSystem system, string locationSubPath)
@@ -140,6 +142,19 @@ namespace System.Configuration {
                        get { return locationConfigPath; }
                }
 
+               internal string GetLocationSubPath ()
+               {
+                       Configuration confg = parent;
+                       string path = null;
+                       while (confg != null) {
+                               path = confg.locationSubPath;
+                               if (!String.IsNullOrEmpty (path))
+                                       return path;
+                               confg = confg.parent;
+                       }
+                       return path;
+               }
+
                internal string ConfigPath {
                        get { return configPath; }
                }
@@ -152,8 +167,15 @@ namespace System.Configuration {
                        get { return (ConnectionStringsSection) GetSection ("connectionStrings"); }
                }
 
+               // MSDN: If the value for this FilePath property represents a merged view and 
+               // no actual file exists for the application, the path to the parent configuration 
+               // file is returned.
                public string FilePath {
-                       get { return streamName; }
+                       get {
+                               if (streamName == null && parent != null)
+                                       return parent.FilePath;
+                               return streamName;
+                       }
                }
 
                public bool HasFile {
@@ -162,12 +184,15 @@ namespace System.Configuration {
                        }
                }
 
-               [MonoTODO ("HostingContext")]
                ContextInformation evaluationContext;
                public ContextInformation EvaluationContext {
                        get {
-                               if (evaluationContext == null)
-                                       evaluationContext = new ContextInformation (this, null /* XXX */);
+                               if (evaluationContext == null) {
+                                       object ctx = system.Host.CreateConfigurationContext (configPath, GetLocationSubPath() );
+                                       evaluationContext = new ContextInformation (this, ctx);
+                               }
+
+
                                return evaluationContext;
                        }
                }
@@ -315,6 +340,8 @@ namespace System.Configuration {
                        section.ConfigHost = system.Host;
                        parentGroup.AddChild (section);
                        elementData [section] = sec;
+
+                       sec.Initialize (this, section);
                }
                
                internal void RemoveConfigInfo (ConfigInfo config)
@@ -339,7 +366,7 @@ namespace System.Configuration {
                        try {
                                Save (stream, mode, forceUpdateAll);
                                system.Host.WriteCompleted (streamName, true, ctx);
-                       } catch (Exception ex) {
+                       } catch (Exception) {
                                system.Host.WriteCompleted (streamName, false, ctx);
                                throw;
                        } finally {
@@ -356,10 +383,13 @@ namespace System.Configuration {
                {
                        SaveAs (filename, mode, false);
                }
-               
-               [MonoTODO ("Detect if file has changed")]
+
+               [MonoInternalNote ("Detect if file has changed")]
                public void SaveAs (string filename, ConfigurationSaveMode mode, bool forceUpdateAll)
                {
+                       string dir = Path.GetDirectoryName (Path.GetFullPath (filename));
+                       if (!Directory.Exists (dir))
+                               Directory.CreateDirectory (dir);
                        Save (new FileStream (filename, FileMode.OpenOrCreate, FileAccess.Write), mode, forceUpdateAll);
                }
 
@@ -407,15 +437,17 @@ namespace System.Configuration {
                
                bool Load ()
                {
-                       if (streamName == null || streamName == "")
+                       if (String.IsNullOrEmpty (streamName))
                                return true;
 
                        XmlTextReader reader = null;
                        Stream stream = null;
                        
+                       // FIXME: we should remove this kind of hack that
+                       // hides the actual error
                        try {
                                stream = system.Host.OpenStreamForRead (streamName);
-                       } catch (Exception e) {
+                       } catch (Exception) {
                                return false;
                        }