2007-11-15 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Data / System.Data.Common / DbProviderFactoriesConfigurationHandler.cs
index 0c9f97c6f1d46dac3d5f325faffb3a9aa6d2c354..5074b09fc1c09b2f34d49d49f3182ff2e0a666d2 100644 (file)
@@ -38,12 +38,12 @@ using System.Xml;
 using System.Globalization;
 using System.Configuration;
 
-namespace System.Data.Common {
+namespace System.Data.Common
+{
        public class DbProviderFactoriesConfigurationHandler : IConfigurationSectionHandler
        {
                #region Constructors
 
-               [MonoTODO]
                public DbProviderFactoriesConfigurationHandler ()
                {
                }
@@ -52,90 +52,104 @@ namespace System.Data.Common {
 
                #region Methods
 
-               [MonoTODO]
                public virtual object Create (object parent, object configContext, XmlNode section)
                {
-                        DataSet ds = new DataSet ("DbProviderFactories");
-                        CreateDataTables (ds, section);
-                        return ds;
+                       DataSet ds = parent as DataSet ?? CreateDataSet ();
+                       FillDataTables (ds, section);
+                       return ds;
                }
 
-                internal virtual void CreateDataTables (DataSet ds, XmlNode section) 
-                {                        
-                        DataTable dt = ds.Tables.Add ("DbProviderFactories");
-                        DataColumn [] columns = new DataColumn [5];
-                        columns [0] = new DataColumn ("Name", typeof (string));
-                        columns [1] = new DataColumn ("Description", typeof (string));
-                        columns [2] = new DataColumn ("InvariantName", typeof (string));
-                        columns [3] = new DataColumn ("AssemblyQualifiedName", typeof (string));
-                        columns [4] = new DataColumn ("SupportedClasses", typeof (int));
-                        dt.Columns.AddRange (columns);
-                        dt.PrimaryKey = new DataColumn [] {columns [2]};
-                                
-                        foreach (XmlNode node in section.SelectNodes (".//DbProviderFactories")) {
-                                AddRows (dt, node);
-                                RemoveRows (dt, node);
-                       }
-                }
-
-                internal string GetAttributeValue (XmlNode node, string name)
-                {
-                        XmlAttribute attr = node.Attributes[name];
-                        if (attr == null)
-                                throw new ConfigurationException ("Required Attribute '" + name +
-                                                                  "' is  missing!", node);
-                        string value = attr.Value;
-                        if (value == "")
-                                throw new ConfigurationException ("Attribute '" + name + "' cannot be empty!",
-                                                                  node);
-                        return value;
-                }
-
-                internal virtual void AddRows (DataTable dt, XmlNode factoriesNode)
-                {
-                        foreach (XmlNode addNode in factoriesNode.SelectNodes (".//add")) {
-                                if (addNode.NodeType != XmlNodeType.Element)
-                                        continue;
-                                
-                                string name = "";
-                                string description = "";
-                                string invariant = "";
-                                string type = "";
-                                string supportedClasses = "";
-                                int support;
-
-                                name            = GetAttributeValue (addNode, "name");
-                                description     = GetAttributeValue (addNode, "description");
-                                invariant       = GetAttributeValue (addNode, "invariant");
-                                type            = GetAttributeValue (addNode, "type");
-                                supportedClasses = GetAttributeValue (addNode, "support");
-                                
-                                support = int.Parse (supportedClasses, NumberStyles.HexNumber);
-                                        
-                                DataRow row = dt.NewRow ();
-                                row [0] = name;
-                                row [1] = description;
-                                row [2] = invariant;
-                                row [3] = type;
-                                row [4] = support;
-                                        
-                                dt.Rows.Add (row);
-                        }        
-                }
-
-                internal virtual void RemoveRows (DataTable dt, XmlNode removeNode)
-                {
-                        foreach (XmlNode node in removeNode.SelectNodes (".//remove")) {
-                                if (node.NodeType != XmlNodeType.Element)
-                                        continue;
-                                
-                                string invariant = GetAttributeValue (node, "invariant");
-                                DataRow row = dt.Rows.Find (invariant);
-                                if (row != null)
-                                        row.Delete ();
-                        }
-                }
-                
+               DataSet CreateDataSet ()
+               {
+                       DataSet ds = new DataSet (DbProviderFactories.CONFIG_SECTION_NAME);
+                       DataTable dt = ds.Tables.Add (DbProviderFactories.CONFIG_SEC_TABLE_NAME);
+                       DataColumn [] columns = new DataColumn [4];
+                       columns [0] = new DataColumn ("Name", typeof (string));
+                       columns [1] = new DataColumn ("Description", typeof (string));
+                       columns [2] = new DataColumn ("InvariantName", typeof (string));
+                       columns [3] = new DataColumn ("AssemblyQualifiedName", typeof (string));
+                       dt.Columns.AddRange (columns);
+                       dt.PrimaryKey = new DataColumn [] { columns [2] };
+                       return ds;
+               }
+
+               void FillDataTables (DataSet ds, XmlNode section)
+               {
+                       DataTable dt = ds.Tables [0];
+                       foreach (XmlNode node in section.ChildNodes) {
+                               if (node.NodeType != XmlNodeType.Element)
+                                       continue;
+
+                               if (node.Name == DbProviderFactories.CONFIG_SEC_TABLE_NAME) {
+                                       foreach (XmlNode factoryNode in node.ChildNodes) {
+                                               if (factoryNode.NodeType != XmlNodeType.Element)
+                                                       continue;
+
+                                               switch (factoryNode.Name) {
+                                               case "add":
+                                                       AddRow (dt, factoryNode);
+                                                       break;
+                                               case "clear":
+                                                       dt.Rows.Clear ();
+                                                       break;
+                                               case "remove":
+                                                       RemoveRow (dt, factoryNode);
+                                                       break;
+                                               default:
+                                                       throw new ConfigurationErrorsException (
+                                                               "Unrecognized element.", factoryNode);
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               string GetAttributeValue (XmlNode node, string name, bool required)
+               {
+                       XmlAttribute attr = node.Attributes [name];
+                       if (attr == null) {
+                               if (!required)
+                                       return null;
+                               throw new ConfigurationErrorsException ("Required Attribute '" 
+                                       + name + "' is  missing!", node);
+                       }
+                       string value = attr.Value;
+                       if (value == "")
+                               throw new ConfigurationException ("Attribute '" + name 
+                                       + "' cannot be empty!", node);
+                       return value;
+               }
+
+               void AddRow (DataTable dt, XmlNode addNode)
+               {
+                       string name = GetAttributeValue (addNode, "name", true);
+                       string description = GetAttributeValue (addNode, "description", true);
+                       string invariant = GetAttributeValue (addNode, "invariant", true);
+                       string type = GetAttributeValue (addNode, "type", true);
+
+                       // FIXME: throw ConfigurationErrorsException for unrecognized
+                       // attributes. Consider "supports" valid although we're not using
+                       // it
+
+                       DataRow row = dt.NewRow ();
+                       row [0] = name;
+                       row [1] = description;
+                       row [2] = invariant;
+                       row [3] = type;
+
+                       dt.Rows.Add (row);
+               }
+
+               void RemoveRow (DataTable dt, XmlNode removeNode)
+               {
+                       // FIXME: throw ConfigurationErrorsException for unrecognized
+                       // attributes.
+
+                       string invariant = GetAttributeValue (removeNode, "invariant", true);
+                       DataRow row = dt.Rows.Find (invariant);
+                       if (row != null)
+                               row.Delete ();
+               }
 
                #endregion // Methods
        }