DbProviderFactoriesConfigurationHandler.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. dt.Columns.AddRange (columns);
  61. dt.PrimaryKey = new DataColumn [] {columns [2]};
  62. foreach (XmlNode node in section.SelectNodes (".//DbProviderFactories")) {
  63. AddRows (dt, node);
  64. RemoveRows (dt, node);
  65. }
  66. }
  67. internal string GetAttributeValue (XmlNode node, string name)
  68. {
  69. XmlAttribute attr = node.Attributes[name];
  70. if (attr == null)
  71. throw new ConfigurationException ("Required Attribute '" + name +
  72. "' is missing!", node);
  73. string value = attr.Value;
  74. if (value == "")
  75. throw new ConfigurationException ("Attribute '" + name + "' cannot be empty!",
  76. node);
  77. return value;
  78. }
  79. internal virtual void AddRows (DataTable dt, XmlNode factoriesNode)
  80. {
  81. foreach (XmlNode addNode in factoriesNode.SelectNodes (".//add")) {
  82. if (addNode.NodeType != XmlNodeType.Element)
  83. continue;
  84. string name = "";
  85. string description = "";
  86. string invariant = "";
  87. string type = "";
  88. int support;
  89. name = GetAttributeValue (addNode, "name");
  90. description = GetAttributeValue (addNode, "description");
  91. invariant = GetAttributeValue (addNode, "invariant");
  92. type = GetAttributeValue (addNode, "type");
  93. DataRow row = dt.NewRow ();
  94. row [0] = name;
  95. row [1] = description;
  96. row [2] = invariant;
  97. row [3] = type;
  98. dt.Rows.Add (row);
  99. }
  100. }
  101. internal virtual void RemoveRows (DataTable dt, XmlNode removeNode)
  102. {
  103. foreach (XmlNode node in removeNode.SelectNodes (".//remove")) {
  104. if (node.NodeType != XmlNodeType.Element)
  105. continue;
  106. string invariant = GetAttributeValue (node, "invariant");
  107. DataRow row = dt.Rows.Find (invariant);
  108. if (row != null)
  109. row.Delete ();
  110. }
  111. }
  112. #endregion // Methods
  113. }
  114. }
  115. #endif // NET_2_0