DbProviderFactoriesConfigurationHandler.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. {
  39. public class DbProviderFactoriesConfigurationHandler : IConfigurationSectionHandler
  40. {
  41. #region Constructors
  42. public DbProviderFactoriesConfigurationHandler ()
  43. {
  44. }
  45. #endregion // Constructors
  46. #region Methods
  47. public virtual object Create (object parent, object configContext, XmlNode section)
  48. {
  49. DataSet ds = new DataSet (DbProviderFactories.CONFIG_SECTION_NAME);
  50. CreateDataTables (ds, section);
  51. return ds;
  52. }
  53. internal virtual void CreateDataTables (DataSet ds, XmlNode section)
  54. {
  55. DataTable dt = ds.Tables.Add (DbProviderFactories.CONFIG_SEC_TABLE_NAME);
  56. DataColumn [] columns = new DataColumn [4];
  57. columns [0] = new DataColumn ("Name", typeof (string));
  58. columns [1] = new DataColumn ("Description", typeof (string));
  59. columns [2] = new DataColumn ("InvariantName", typeof (string));
  60. columns [3] = new DataColumn ("AssemblyQualifiedName", typeof (string));
  61. dt.Columns.AddRange (columns);
  62. dt.PrimaryKey = new DataColumn [] { columns [2] };
  63. foreach (XmlNode node in section.ChildNodes) {
  64. if (node.NodeType != XmlNodeType.Element)
  65. continue;
  66. if (node.Name == DbProviderFactories.CONFIG_SEC_TABLE_NAME) {
  67. foreach (XmlNode factoryNode in node.ChildNodes) {
  68. if (factoryNode.NodeType != XmlNodeType.Element)
  69. continue;
  70. switch (factoryNode.Name) {
  71. case "add":
  72. AddRow (dt, factoryNode);
  73. break;
  74. case "clear":
  75. dt.Rows.Clear ();
  76. break;
  77. case "remove":
  78. RemoveRow (dt, factoryNode);
  79. break;
  80. default:
  81. throw new ConfigurationErrorsException (
  82. "Unrecognized element.", factoryNode);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. string GetAttributeValue (XmlNode node, string name, bool required)
  89. {
  90. XmlAttribute attr = node.Attributes [name];
  91. if (attr == null) {
  92. if (!required)
  93. return null;
  94. throw new ConfigurationErrorsException ("Required Attribute '"
  95. + name + "' is missing!", node);
  96. }
  97. string value = attr.Value;
  98. if (value == "")
  99. throw new ConfigurationException ("Attribute '" + name
  100. + "' cannot be empty!", node);
  101. return value;
  102. }
  103. void AddRow (DataTable dt, XmlNode addNode)
  104. {
  105. string name = GetAttributeValue (addNode, "name", true);
  106. string description = GetAttributeValue (addNode, "description", true);
  107. string invariant = GetAttributeValue (addNode, "invariant", true);
  108. string type = GetAttributeValue (addNode, "type", true);
  109. // FIXME: throw ConfigurationErrorsException for unrecognized
  110. // attributes. Consider "supports" valid although we're not using
  111. // it
  112. DataRow row = dt.NewRow ();
  113. row [0] = name;
  114. row [1] = description;
  115. row [2] = invariant;
  116. row [3] = type;
  117. dt.Rows.Add (row);
  118. }
  119. void RemoveRow (DataTable dt, XmlNode removeNode)
  120. {
  121. // FIXME: throw ConfigurationErrorsException for unrecognized
  122. // attributes.
  123. string invariant = GetAttributeValue (removeNode, "invariant", true);
  124. DataRow row = dt.Rows.Find (invariant);
  125. if (row != null)
  126. row.Delete ();
  127. }
  128. #endregion // Methods
  129. }
  130. }
  131. #endif // NET_2_0