Merge pull request #2548 from esdrubal/discovery
[mono.git] / mcs / class / System.Configuration / System.Configuration / Configuration.cs
index c0f0b485b7d243784973cd8c59fea805c749111a..fb48cc9664f27b743154806f992f342e7e9a8f35 100644 (file)
@@ -26,7 +26,7 @@
 //
 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
 //
-#if NET_2_0
+
 using System;
 using System.Collections;
 using System.Collections.Specialized;
@@ -133,8 +133,12 @@ namespace System.Configuration {
                                rootGroup.StreamName = streamName;
                        }
                        
-                       if (streamName != null)
-                               Load ();
+                       try {
+                               if (streamName != null)
+                                       Load ();
+                       } catch (XmlException ex) {
+                               throw new ConfigurationErrorsException (ex.Message, ex, streamName, 0);
+                       }
                }
                
                internal Configuration Parent {
@@ -300,7 +304,7 @@ namespace System.Configuration {
                        sec.RawXml = xml;
                        sec.Reset (parentSection);
 
-                       if (xml != null && xml == data) {
+                       if (xml != null) {
                                XmlTextReader r = new ConfigXmlTextReader (new StringReader (xml), FilePath);
                                sec.DeserializeSection (r);
                                r.Close ();
@@ -338,7 +342,7 @@ namespace System.Configuration {
                internal void CreateSection (SectionGroupInfo group, string name, ConfigurationSection sec)
                {
                        if (group.HasChild (name))
-                               throw new ConfigurationException ("Cannot add a ConfigurationSection. A section or section group already exists with the name '" + name + "'");
+                               throw new ConfigurationErrorsException ("Cannot add a ConfigurationSection. A section or section group already exists with the name '" + name + "'");
                                
                        if (!HasFile && !sec.SectionInformation.AllowLocation)
                                throw new ConfigurationErrorsException ("The configuration section <" + name + "> cannot be defined inside a <location> element."); 
@@ -356,11 +360,12 @@ namespace System.Configuration {
                        section.ConfigHost = system.Host;
                        group.AddChild (section);
                        elementData [section] = sec;
+                       sec.Configuration = this;
                }
                
                internal void CreateSectionGroup (SectionGroupInfo parentGroup, string name, ConfigurationSectionGroup sec)
                {
-                       if (parentGroup.HasChild (name)) throw new ConfigurationException ("Cannot add a ConfigurationSectionGroup. A section or section group already exists with the name '" + name + "'");
+                       if (parentGroup.HasChild (name)) throw new ConfigurationErrorsException ("Cannot add a ConfigurationSectionGroup. A section or section group already exists with the name '" + name + "'");
                        if (sec.Type == null) sec.Type = system.Host.GetConfigTypeName (sec.GetType ());
                        sec.SetName (name);
 
@@ -390,6 +395,11 @@ namespace System.Configuration {
                
                public void Save (ConfigurationSaveMode mode, bool forceUpdateAll)
                {
+                       if (!forceUpdateAll && (mode != ConfigurationSaveMode.Full) && !HasValues (mode)) {
+                               ResetModified ();
+                               return;
+                       }
+
                        ConfigurationSaveEventHandler saveStart = SaveStart;
                        ConfigurationSaveEventHandler saveEnd = SaveEnd;
                        
@@ -426,6 +436,11 @@ namespace System.Configuration {
                [MonoInternalNote ("Detect if file has changed")]
                public void SaveAs (string filename, ConfigurationSaveMode mode, bool forceUpdateAll)
                {
+                       if (!forceUpdateAll && (mode != ConfigurationSaveMode.Full) && !HasValues (mode)) {
+                               ResetModified ();
+                               return;
+                       }
+                       
                        string dir = Path.GetDirectoryName (Path.GetFullPath (filename));
                        if (!Directory.Exists (dir))
                                Directory.CreateDirectory (dir);
@@ -463,6 +478,7 @@ namespace System.Configuration {
                                
                                SaveData (tw, mode, forceUpdateAll);
                                tw.WriteEndElement ();
+                               ResetModified ();
                        }
                        finally {
                                tw.Flush ();
@@ -474,6 +490,29 @@ namespace System.Configuration {
                {
                        rootGroup.WriteRootData (tw, this, mode);
                }
+
+               bool HasValues (ConfigurationSaveMode mode)
+               {
+                       foreach (ConfigurationLocation loc in Locations) {
+                               if (loc.OpenedConfiguration == null)
+                                       continue;
+                               if (loc.OpenedConfiguration.HasValues (mode))
+                                       return true;
+                       }
+
+                       return rootGroup.HasValues (this, mode);
+               }
+
+               void ResetModified ()
+               {
+                       foreach (ConfigurationLocation loc in Locations) {
+                               if (loc.OpenedConfiguration == null)
+                                       continue;
+                               loc.OpenedConfiguration.ResetModified ();
+                       }
+                       
+                       rootGroup.ResetModified (this);
+               }
                
                bool Load ()
                {
@@ -492,6 +531,7 @@ namespace System.Configuration {
                        using (XmlTextReader reader = new ConfigXmlTextReader (stream, streamName)) {
                                ReadConfigFile (reader, streamName);
                        }
+                       ResetModified ();
                        return true;
                }
 
@@ -541,9 +581,8 @@ namespace System.Configuration {
                private void ThrowException (string text, XmlReader reader)
                {
                        IXmlLineInfo li = reader as IXmlLineInfo;
-                       throw new ConfigurationException (text, streamName, li != null ? li.LineNumber : 0);
+                       throw new ConfigurationErrorsException (text, streamName, li != null ? li.LineNumber : 0);
                }
        }
 }
 
-#endif