2007-02-09 Nagappan A <anagappan@novell.com>
[mono.git] / mcs / class / System.Data / System.Data.Common / DbProviderFactoriesConfigurationHandler.cs
1 //
2 // System.Data.Common.DbProviderFactoriesConfigurationHandler.cs
3 //
4 // Author:
5 //   Sureshkumar T (tsureshkumar@novell.com)
6 //   Tim Coleman (tim@timcoleman.com)
7 //
8 // Copyright (C) Tim Coleman, 2003
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 #if NET_2_0
35
36 using System.IO;
37 using System.Xml;
38 using System.Globalization;
39 using System.Configuration;
40
41 namespace System.Data.Common {
42         public class DbProviderFactoriesConfigurationHandler : IConfigurationSectionHandler
43         {
44                 #region Constructors
45
46                 public DbProviderFactoriesConfigurationHandler ()
47                 {
48                 }
49
50                 #endregion // Constructors
51
52                 #region Methods
53
54                 public virtual object Create (object parent, object configContext, XmlNode section)
55                 {
56                         DataSet ds = new DataSet ("DbProviderFactories");
57                         CreateDataTables (ds, section);
58                         return ds;
59                 }
60
61                 internal virtual void CreateDataTables (DataSet ds, XmlNode section) 
62                 {                        
63                         DataTable dt = ds.Tables.Add ("DbProviderFactories");
64                         DataColumn [] columns = new DataColumn [5];
65                         columns [0] = new DataColumn ("Name", typeof (string));
66                         columns [1] = new DataColumn ("Description", typeof (string));
67                         columns [2] = new DataColumn ("InvariantName", typeof (string));
68                         columns [3] = new DataColumn ("AssemblyQualifiedName", typeof (string));
69                         columns [4] = new DataColumn ("SupportedClasses", typeof (int));
70                         dt.Columns.AddRange (columns);
71                         dt.PrimaryKey = new DataColumn [] {columns [2]};
72                                 
73                         foreach (XmlNode node in section.SelectNodes (".//DbProviderFactories")) {
74                                 AddRows (dt, node);
75                                 RemoveRows (dt, node);
76                        }
77                 }
78
79                 internal string GetAttributeValue (XmlNode node, string name)
80                 {
81                         XmlAttribute attr = node.Attributes[name];
82                         if (attr == null)
83                                 throw new ConfigurationException ("Required Attribute '" + name +
84                                                                   "' is  missing!", node);
85                         string value = attr.Value;
86                         if (value == "")
87                                 throw new ConfigurationException ("Attribute '" + name + "' cannot be empty!",
88                                                                   node);
89                         return value;
90                 }
91
92                 internal virtual void AddRows (DataTable dt, XmlNode factoriesNode)
93                 {
94                         foreach (XmlNode addNode in factoriesNode.SelectNodes (".//add")) {
95                                 if (addNode.NodeType != XmlNodeType.Element)
96                                         continue;
97                                 
98                                 string name = "";
99                                 string description = "";
100                                 string invariant = "";
101                                 string type = "";
102                                 string supportedClasses = "";
103                                 int support;
104
105                                 name            = GetAttributeValue (addNode, "name");
106                                 description     = GetAttributeValue (addNode, "description");
107                                 invariant       = GetAttributeValue (addNode, "invariant");
108                                 type            = GetAttributeValue (addNode, "type");
109                                 supportedClasses = GetAttributeValue (addNode, "support");
110                                 
111                                 support = int.Parse (supportedClasses, NumberStyles.HexNumber);
112                                         
113                                 DataRow row = dt.NewRow ();
114                                 row [0] = name;
115                                 row [1] = description;
116                                 row [2] = invariant;
117                                 row [3] = type;
118                                 row [4] = support;
119                                         
120                                 dt.Rows.Add (row);
121                         }        
122                 }
123
124                 internal virtual void RemoveRows (DataTable dt, XmlNode removeNode)
125                 {
126                         foreach (XmlNode node in removeNode.SelectNodes (".//remove")) {
127                                 if (node.NodeType != XmlNodeType.Element)
128                                         continue;
129                                 
130                                 string invariant = GetAttributeValue (node, "invariant");
131                                 DataRow row = dt.Rows.Find (invariant);
132                                 if (row != null)
133                                         row.Delete ();
134                         }
135                 }
136                 
137
138                 #endregion // Methods
139         }
140 }
141
142 #endif // NET_2_0