DbProviderFactoriesConfigurationHandler.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. [MonoTODO]
  42. public DbProviderFactoriesConfigurationHandler ()
  43. {
  44. }
  45. #endregion // Constructors
  46. #region Methods
  47. [MonoTODO]
  48. public virtual object Create (object parent, object configContext, XmlNode section)
  49. {
  50. DataSet ds = new DataSet ("DbProviderFactories");
  51. CreateDataTables (ds, section);
  52. return ds;
  53. }
  54. internal virtual void CreateDataTables (DataSet ds, XmlNode section)
  55. {
  56. DataTable dt = ds.Tables.Add ("DbProviderFactories");
  57. DataColumn [] columns = new DataColumn [5];
  58. columns [0] = new DataColumn ("Name", typeof (string));
  59. columns [1] = new DataColumn ("Description", typeof (string));
  60. columns [2] = new DataColumn ("InvariantName", typeof (string));
  61. columns [3] = new DataColumn ("AssemblyQualifiedName", typeof (string));
  62. columns [4] = new DataColumn ("SupportedClasses", typeof (int));
  63. dt.Columns.AddRange (columns);
  64. dt.PrimaryKey = new DataColumn [] {columns [2]};
  65. foreach (XmlNode node in section.SelectNodes (".//DbProviderFactories")) {
  66. AddRows (dt, node);
  67. RemoveRows (dt, node);
  68. }
  69. }
  70. internal string GetAttributeValue (XmlNode node, string name)
  71. {
  72. XmlAttribute attr = node.Attributes[name];
  73. if (attr == null)
  74. throw new ConfigurationException ("Required Attribute '" + name +
  75. "' is missing!", node);
  76. string value = attr.Value;
  77. if (value == "")
  78. throw new ConfigurationException ("Attribute '" + name + "' cannot be empty!",
  79. node);
  80. return value;
  81. }
  82. internal virtual void AddRows (DataTable dt, XmlNode factoriesNode)
  83. {
  84. foreach (XmlNode addNode in factoriesNode.SelectNodes (".//add")) {
  85. if (addNode.NodeType != XmlNodeType.Element)
  86. continue;
  87. string name = "";
  88. string description = "";
  89. string invariant = "";
  90. string type = "";
  91. string supportedClasses = "";
  92. int support;
  93. name = GetAttributeValue (addNode, "name");
  94. description = GetAttributeValue (addNode, "description");
  95. invariant = GetAttributeValue (addNode, "invariant");
  96. type = GetAttributeValue (addNode, "type");
  97. supportedClasses = GetAttributeValue (addNode, "support");
  98. support = int.Parse (supportedClasses, NumberStyles.HexNumber);
  99. DataRow row = dt.NewRow ();
  100. row [0] = name;
  101. row [1] = description;
  102. row [2] = invariant;
  103. row [3] = type;
  104. row [4] = support;
  105. dt.Rows.Add (row);
  106. }
  107. }
  108. internal virtual void RemoveRows (DataTable dt, XmlNode removeNode)
  109. {
  110. foreach (XmlNode node in removeNode.SelectNodes (".//remove")) {
  111. if (node.NodeType != XmlNodeType.Element)
  112. continue;
  113. string invariant = GetAttributeValue (node, "invariant");
  114. DataRow row = dt.Rows.Find (invariant);
  115. if (row != null)
  116. row.Delete ();
  117. }
  118. }
  119. #endregion // Methods
  120. }
  121. }
  122. #endif // NET_2_0