[System] Uri handling from reference source
[mono.git] / mcs / class / System / System.Configuration / ConfigurationSettings.cs
index dba99ccde2333ca967b3ca7628835fdd5e2fc744..9b2f8e9986b8768ff7ae95a95620bbb19cb9d7a1 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if CONFIGURATION_DEP
-extern alias PrebuiltSystem;
-using NameValueCollection = PrebuiltSystem.System.Collections.Specialized.NameValueCollection;
-#endif
-
 using System;
 using System.Collections;
 using System.Collections.Specialized;
@@ -47,52 +42,32 @@ using System.Security.Permissions;
 using System.Xml;
 using System.Xml.XPath;
 #endif
-#if TARGET_JVM
-using vmw.common;
-using vmw.@internal.io;
-#endif
 
 namespace System.Configuration
 {
        public sealed class ConfigurationSettings
        {
-#if !TARGET_JVM
                static IConfigurationSystem config = DefaultConfig.GetInstance ();
-#else
-               static IConfigurationSystem config {
-                       get {
-                               IConfigurationSystem conf = (IConfigurationSystem) AppDomain.CurrentDomain.GetData ("ConfigurationSettings.Config");
-                               if (conf == null) {
-                                       conf = DefaultConfig.GetInstance ();
-                                       AppDomain.CurrentDomain.SetData ("ConfigurationSettings.Config", conf);
-                               }
-                               return conf;
-                       }
-                       set {
-                               AppDomain.CurrentDomain.SetData ("ConfigurationSettings.Config", value);
-                       }
-               }
-#endif
                static object lockobj = new object ();
                private ConfigurationSettings ()
                {
                }
 
-#if NET_2_0
                [Obsolete ("This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.GetSection")]
-#endif
                public static object GetConfig (string sectionName)
                {
+#if CONFIGURATION_DEP
+                       return ConfigurationManager.GetSection (sectionName);
+#else
                        return config.GetConfig (sectionName);
+#endif
                }
 
-#if NET_2_0
                [Obsolete ("This property is obsolete.  Please use System.Configuration.ConfigurationManager.AppSettings")]
-#endif
                public static NameValueCollection AppSettings
                {
                        get {
-#if NET_2_0 && CONFIGURATION_DEP
+#if CONFIGURATION_DEP
                                object appSettings = ConfigurationManager.GetSection ("appSettings");
 #else
                                object appSettings = GetConfig ("appSettings");
@@ -123,23 +98,7 @@ namespace System.Configuration
        //
        class DefaultConfig : IConfigurationSystem
        {
-#if !TARGET_JVM
                static readonly DefaultConfig instance = new DefaultConfig ();        
-#else
-               static DefaultConfig instance {
-                       get {
-                               DefaultConfig conf = (DefaultConfig) AppDomain.CurrentDomain.GetData ("DefaultConfig.instance");
-                               if (conf == null) {
-                                       conf = new DefaultConfig ();
-                                       AppDomain.CurrentDomain.SetData ("DefaultConfig.instance", conf);
-                               }
-                               return conf;
-                       }
-                       set {
-                               AppDomain.CurrentDomain.SetData ("DefaultConfig.instance", value);
-                       }
-               }
-#endif
                ConfigurationData config;
                
                private DefaultConfig ()
@@ -151,9 +110,7 @@ namespace System.Configuration
                        return instance;
                }
 
-#if NET_2_0
                [Obsolete ("This method is obsolete.  Please use System.Configuration.ConfigurationManager.GetConfig")]
-#endif
                public object GetConfig (string sectionName)
                {
                        Init ();
@@ -167,9 +124,13 @@ namespace System.Configuration
                                        return;
 
                                ConfigurationData data = new ConfigurationData ();
-                               if (!data.Load (GetMachineConfigPath ()))
-                                       throw new ConfigurationException ("Cannot find " + GetMachineConfigPath ());
+                               if (data.LoadString (GetBundledMachineConfig ())) {
+                                       // do nothing
+                               } else {
+                                       if (!data.Load (GetMachineConfigPath ()))
+                                               throw new ConfigurationException ("Cannot find " + GetMachineConfigPath ());
 
+                               }
                                string appfile = GetAppConfigPath ();
                                if (appfile == null) {
                                        config = data;
@@ -183,20 +144,18 @@ namespace System.Configuration
                                        config = data;
                        }
                }
-#if TARGET_JVM
-               internal static string GetMachineConfigPath ()
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               extern private static string get_bundled_machine_config ();
+               internal static string GetBundledMachineConfig ()
                {
-                       return "/machine.config";
+                       return get_bundled_machine_config ();
                }
-#else
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                extern private static string get_machine_config_path ();
                internal static string GetMachineConfigPath ()
                {
                        return get_machine_config_path ();
                }
-#endif
-
                private static string GetAppConfigPath ()
                {
                        AppDomainSetup currentInfo = AppDomain.CurrentDomain.SetupInformation;
@@ -223,7 +182,9 @@ namespace System.Configuration
                public readonly string TypeName;
                public readonly bool AllowLocation;
                public readonly AllowDefinition AllowDefinition;
+#if XML_DEP
                public string FileName;
+#endif
                public readonly bool RequirePermission;
 
                public SectionData (string sectionName, string typeName,
@@ -278,32 +239,41 @@ namespace System.Configuration
 #if (XML_DEP)
                        this.fileName = fileName;
                        if (fileName == null
-#if !TARGET_JVM
                                || !File.Exists (fileName)
-#endif
 )
                                return false;
                        
                        XmlTextReader reader = null;
 
                        try {
-#if !TARGET_JVM
                                FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
-#else
-                               Stream fs = (Stream) vmw.common.IOUtils.getStream (fileName);
-
-                               //patch for machine.config
-                               if (fs == null && fileName.EndsWith ("machine.config")) {
-                                       fs = (Stream) IOUtils.getStreamForGHConfigs (fileName);
-                               }
-
-                               if (fs == null) {
-                                       return false;
-                               }
-#endif
                                reader = new XmlTextReader (fs);
-                               InitRead (reader);
-                               ReadConfigFile (reader);
+                               if (InitRead (reader))
+                                       ReadConfigFile (reader);
+                       } catch (ConfigurationException) {
+                               throw;
+                       } catch (Exception e) {
+                               throw new ConfigurationException ("Error reading " + fileName, e);
+                       } finally {
+                               if (reader != null)
+                                       reader.Close();
+                       }
+#endif
+                       return true;
+               }
+               
+               public bool LoadString (string data)
+               {
+                       if (data == null)
+                               return false;
+#if (XML_DEP)
+                       XmlTextReader reader = null;
+
+                       try {
+                               TextReader tr = new StringReader (data);
+                               reader = new XmlTextReader (tr);
+                               if (InitRead (reader))
+                                       ReadConfigFile (reader);
                        } catch (ConfigurationException) {
                                throw;
                        } catch (Exception e) {
@@ -451,7 +421,7 @@ namespace System.Configuration
                        return null;
                }
 #if (XML_DEP)
-               private void InitRead (XmlTextReader reader)
+               private bool InitRead (XmlTextReader reader)
                {
                        reader.MoveToContent ();
                        if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
@@ -459,10 +429,16 @@ namespace System.Configuration
 
                        if (reader.HasAttributes)
                                ThrowException ("Unrecognized attribute in root element", reader);
-
-                       MoveToNextElement (reader);
+                       if (reader.IsEmptyElement) {
+                               reader.Skip ();
+                               return false;
+                       }
+                       reader.Read ();
+                       reader.MoveToContent ();
+                       return reader.NodeType != XmlNodeType.EndElement;
                }
 
+               // FIXME: this approach is not always safe and likely to cause bugs.
                private void MoveToNextElement (XmlTextReader reader)
                {
                        while (reader.Read ()) {
@@ -565,7 +541,17 @@ namespace System.Configuration
                        section.FileName = fileName;
                        factories [nameValue] = section;
 
-                       MoveToNextElement (reader);
+                       if (reader.IsEmptyElement)
+                               reader.Skip ();
+                       else {
+                               reader.Read ();
+                               reader.MoveToContent ();
+                               if (reader.NodeType != XmlNodeType.EndElement)
+                                       // sub-section inside a section
+                                       ReadSections (reader, nameValue);
+                               reader.ReadEndElement ();
+                       }
+                       reader.MoveToContent ();
                }
 
                private void ReadRemoveSection (XmlTextReader reader, string sectionName)
@@ -603,9 +589,7 @@ namespace System.Configuration
                                        value = reader.Value;
                                }
                                else
-#if NET_2_0
                                if (reader.Name != "type")
-#endif
                                        ThrowException ("Unrecognized attribute.", reader);
                        } while (reader.MoveToNextAttribute ());
 
@@ -623,14 +607,28 @@ namespace System.Configuration
                                ThrowException ("Already have a factory for " + value, reader);
 
                        factories [value] = groupMark;
-                       MoveToNextElement (reader);
-                       ReadSections (reader, value);
+
+                       if (reader.IsEmptyElement) {
+                               reader.Skip ();
+                               reader.MoveToContent ();
+                       } else {
+                               reader.Read ();
+                               reader.MoveToContent ();
+                               if (reader.NodeType != XmlNodeType.EndElement)
+                                       ReadSections (reader, value);
+                               reader.ReadEndElement ();
+                               reader.MoveToContent ();
+                       }
                }
 
+               // It stops XmlReader consumption at where it found
+               // surrounding EndElement i.e. EndElement is not consumed here
                private void ReadSections (XmlTextReader reader, string configSection)
                {
                        int depth = reader.Depth;
-                       while (reader.Depth == depth) {
+                       for (reader.MoveToContent ();
+                            reader.Depth == depth;
+                            reader.MoveToContent ()) {
                                string name = reader.Name;
                                if (name == "section") {
                                        ReadSection (reader, configSection);
@@ -671,16 +669,23 @@ namespace System.Configuration
 
                private void ReadConfigFile (XmlTextReader reader)
                {
-                       int depth = reader.Depth;
-                       while (!reader.EOF && reader.Depth == depth) {
+                       //int depth = reader.Depth;
+                       for (reader.MoveToContent ();
+                            !reader.EOF && reader.NodeType != XmlNodeType.EndElement;
+                            reader.MoveToContent ()) {
                                string name = reader.Name;
                                if (name == "configSections") {
                                        if (reader.HasAttributes)
                                                ThrowException ("Unrecognized attribute in <configSections>.", reader);
-
-                                       MoveToNextElement (reader);
-                                       if (reader.Depth > depth)
-                                               ReadSections (reader, null);
+                                       if (reader.IsEmptyElement)
+                                               reader.Skip ();
+                                       else {
+                                               reader.Read ();
+                                               reader.MoveToContent ();
+                                               if (reader.NodeType != XmlNodeType.EndElement)
+                                                       ReadSections (reader, null);
+                                               reader.ReadEndElement ();
+                                       }
                                } else if (name != null && name != "") {
                                        StorePending (name, reader);
                                        MoveToNextElement (reader);