DbProviderFactoriesConfigurationHandler.cs 6.2 KB

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