Merge pull request #644 from knocte/connstrings
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConfigurationSection.cs
index 8831cb5539b941484aedd8f1da5c3a5bad62ebbe..8a76dd6fff5a0ff29f7e323a158db89ce1b61e52 100644 (file)
@@ -3,7 +3,8 @@
 //
 // Authors:
 //     Duncan Mak (duncan@ximian.com)
-//  Lluis Sanchez Gual (lluis@novell.com)
+//     Lluis Sanchez Gual (lluis@novell.com)
+//     Martin Baulig <martin.baulig@xamarin.com>
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -25,9 +26,9 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
 //
 
-#if NET_2_0
 using System.Collections;
 using System.Xml;
 using System.IO;
@@ -111,7 +112,7 @@ namespace System.Configuration
                                        // ignore, it can fail - we deserialize only in order to get
                                        // the configSource attribute
                                }
-                               XmlDocument doc = new XmlDocument ();
+                               XmlDocument doc = new ConfigurationXmlDocument ();
                                doc.LoadXml (RawXml);
                                return SectionHandler.Create (parent, ConfigContext, doc.DocumentElement);
                        }
@@ -134,6 +135,7 @@ namespace System.Configuration
                {
                        ConfigurationElement elem = (ConfigurationElement) Activator.CreateInstance (t);
                        elem.Init ();
+                       elem.Configuration = Configuration;
                        if (IsReadOnly ())
                                elem.SetReadOnly ();
                        return elem;
@@ -159,7 +161,7 @@ namespace System.Configuration
                        {
                                if (protection_provider != null) {
                                        ProtectedConfigurationProvider prov = ProtectedConfiguration.GetProvider (protection_provider, true);
-                                       XmlDocument doc = new XmlDocument ();
+                                       XmlDocument doc = new ConfigurationXmlDocument ();
 
                                        reader.MoveToElement ();
 
@@ -186,7 +188,14 @@ namespace System.Configuration
                [MonoInternalNote ("find the proper location for the decryption stuff")]
                protected internal virtual void DeserializeSection (XmlReader reader)
                {
-                       DoDeserializeSection (reader);
+                       try
+                       {
+                               DoDeserializeSection (reader);
+                       }
+                       catch (ConfigurationErrorsException ex)
+                       {
+                               throw new ConfigurationErrorsException(String.Format("Error deserializing configuration section {0}: {1}", this.SectionInformation.Name, ex.Message));
+                       }
                }
 
                internal void DeserializeConfigSource (string basePath)
@@ -197,23 +206,23 @@ namespace System.Configuration
                                return;
 
                        if (Path.IsPathRooted (config_source))
-                               throw new ConfigurationException ("The configSource attribute must be a relative physical path.");
+                               throw new ConfigurationErrorsException ("The configSource attribute must be a relative physical path.");
                        
                        if (HasLocalModifications ())
-                               throw new ConfigurationException ("A section using 'configSource' may contain no other attributes or elements.");
+                               throw new ConfigurationErrorsException ("A section using 'configSource' may contain no other attributes or elements.");
                        
                        string path = Path.Combine (basePath, config_source);
                        if (!File.Exists (path)) {
                                RawXml = null;
                                SectionInformation.SetRawXml (null);
-                               return;
+                               throw new ConfigurationErrorsException (string.Format ("Unable to open configSource file '{0}'.", path));
                        }
                        
                        RawXml = File.ReadAllText (path);
                        SectionInformation.SetRawXml (RawXml);
                        DeserializeElement (new ConfigXmlTextReader (new StringReader (RawXml), path), false);
                }
-               
+
                protected internal virtual string SerializeSection (ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
                {
                        externalDataXml = null;
@@ -224,12 +233,28 @@ namespace System.Configuration
                        }
                        else
                                elem = this;
-                       
+
+                       /*
+                        * FIXME: LAMESPEC
+                        * 
+                        * Cache the current values of 'parentElement' and 'saveMode' for later use in
+                        * ConfigurationElement.SerializeToXmlElement().
+                        * 
+                        */
+                       elem.PrepareSave (parentElement, saveMode);
+                       bool hasValues = elem.HasValues (parentElement, saveMode);
+
                        string ret;                     
                        using (StringWriter sw = new StringWriter ()) {
                                using (XmlTextWriter tw = new XmlTextWriter (sw)) {
                                        tw.Formatting = Formatting.Indented;
-                                       elem.SerializeToXmlElement (tw, name);
+                                       if (hasValues)
+                                               elem.SerializeToXmlElement (tw, name);
+                                       else if ((saveMode == ConfigurationSaveMode.Modified) && elem.IsModified ()) {
+                                               // MS emits an empty section element.
+                                               tw.WriteStartElement (name);
+                                               tw.WriteEndElement ();
+                                       }
                                        tw.Close ();
                                }
                                
@@ -258,4 +283,4 @@ namespace System.Configuration
                }
        }
 }
-#endif
+