DbProviderConfigurationHandler.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //------------------------------------------------------------------------------
  2. // <copyright file="DbProviderConfigurationHandler.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">Microsoft</owner>
  6. // <owner current="true" primary="false">Microsoft</owner>
  7. //------------------------------------------------------------------------------
  8. namespace System.Data.Common {
  9. using System;
  10. using System.Collections;
  11. using System.Collections.Specialized;
  12. using System.Configuration;
  13. using System.Data;
  14. using System.Diagnostics;
  15. using System.Globalization;
  16. using System.Xml;
  17. // this calss can be used by any provider to support a provider specific configuration section. The configutation
  18. // Object is a NameValueCollection
  19. // <configSections>
  20. // <section name="system.data.<provider>" type="System.data.common.DbProviderConfigurationHandler, System.Data, Version=%ASSEMBLY_VERSION%, Culture=neutral, PublicKeyToken=%ECMA_PUBLICKEY%" />
  21. // </configSections>
  22. // <system.data.<provider>
  23. // <settings>
  24. // <add name="<provider setting" value = "<value of setting>" />
  25. // </settings>
  26. // </system.data.<provider>
  27. // this class is delayed created, use ConfigurationManager.GetSection("system.data.<provider>") to obtain
  28. public class DbProviderConfigurationHandler : IConfigurationSectionHandler { // V1.2.3300
  29. internal const string settings = "settings";
  30. public DbProviderConfigurationHandler() { // V1.2.3300
  31. }
  32. /*
  33. static internal void CheckForChildNodes(XmlNode node) {
  34. if (node.HasChildNodes) {
  35. throw ADP.ConfigBaseNoChildNodes(node.FirstChild);
  36. }
  37. }
  38. static private void CheckForNonElement(XmlNode node) {
  39. if (XmlNodeType.Element != node.NodeType) {
  40. throw ADP.ConfigBaseElementsOnly(node);
  41. }
  42. }
  43. static internal void CheckForUnrecognizedAttributes(XmlNode node) {
  44. if (0 != node.Attributes.Count) {
  45. throw ADP.ConfigUnrecognizedAttributes(node);
  46. }
  47. }
  48. */
  49. static internal NameValueCollection CloneParent(NameValueCollection parentConfig) {
  50. if (null == parentConfig) {
  51. parentConfig = new NameValueCollection();
  52. }
  53. else {
  54. parentConfig = new NameValueCollection(parentConfig);
  55. }
  56. return parentConfig;
  57. }
  58. virtual public object Create(object parent, object configContext, XmlNode section) { // V1.2.3300
  59. #if DEBUG
  60. try {
  61. #endif
  62. return CreateStatic(parent, configContext, section);
  63. #if DEBUG
  64. }
  65. catch(Exception e) {
  66. //
  67. if (ADP.IsCatchableExceptionType(e)) {
  68. ADP.TraceExceptionWithoutRethrow(e); // it will be rethrown
  69. }
  70. throw;
  71. }
  72. #endif
  73. }
  74. static internal object CreateStatic(object parent, object configContext, XmlNode section) {
  75. object config = parent;
  76. if (null != section) {
  77. config = CloneParent(parent as NameValueCollection);
  78. bool foundSettings = false;
  79. HandlerBase.CheckForUnrecognizedAttributes(section);
  80. foreach (XmlNode child in section.ChildNodes) {
  81. if (HandlerBase.IsIgnorableAlsoCheckForNonElement(child)) {
  82. continue;
  83. }
  84. string sectionGroup = child.Name;
  85. switch(sectionGroup) {
  86. case DbProviderConfigurationHandler.settings:
  87. if (foundSettings) {
  88. throw ADP.ConfigSectionsUnique(DbProviderConfigurationHandler.settings);
  89. }
  90. foundSettings= true;
  91. DbProviderDictionarySectionHandler.CreateStatic(config as NameValueCollection, configContext, child);
  92. break;
  93. default:
  94. throw ADP.ConfigUnrecognizedElement(child);
  95. }
  96. }
  97. }
  98. return config;
  99. }
  100. /*
  101. // skip whitespace and comments, throws if non-element
  102. static internal bool IsIgnorableAlsoCheckForNonElement(XmlNode node) {
  103. if ((XmlNodeType.Comment == node.NodeType) || (XmlNodeType.Whitespace == node.NodeType)) {
  104. return true;
  105. }
  106. HandlerBase.CheckForNonElement(node);
  107. return false;
  108. }
  109. */
  110. static internal string RemoveAttribute(XmlNode node, string name) {
  111. XmlNode attribute = node.Attributes.RemoveNamedItem(name);
  112. if (null == attribute) {
  113. throw ADP.ConfigRequiredAttributeMissing(name, node);
  114. }
  115. string value = attribute.Value;
  116. if (0 == value.Length) {
  117. throw ADP.ConfigRequiredAttributeEmpty(name, node);
  118. }
  119. return value;
  120. }
  121. // based off of DictionarySectionHandler
  122. sealed private class DbProviderDictionarySectionHandler/* : IConfigurationSectionHandler*/ {
  123. static internal NameValueCollection CreateStatic(NameValueCollection config, Object context, XmlNode section) {
  124. if (null != section) {
  125. HandlerBase.CheckForUnrecognizedAttributes(section);
  126. }
  127. foreach (XmlNode child in section.ChildNodes) {
  128. if (HandlerBase.IsIgnorableAlsoCheckForNonElement(child)) {
  129. continue;
  130. }
  131. switch(child.Name) {
  132. case "add":
  133. HandleAdd(child, config);
  134. break;
  135. case "remove":
  136. HandleRemove(child, config);
  137. break;
  138. case "clear":
  139. HandleClear(child, config);
  140. break;
  141. default:
  142. throw ADP.ConfigUnrecognizedElement(child);
  143. }
  144. }
  145. return config;
  146. }
  147. static private void HandleAdd(XmlNode child, NameValueCollection config) {
  148. // should add vaildate that setting is a known supported setting
  149. // (i.e. that the value of the name attribute is is good)
  150. HandlerBase.CheckForChildNodes(child);
  151. string name = RemoveAttribute(child, "name");
  152. string value = RemoveAttribute(child, "value");
  153. HandlerBase.CheckForUnrecognizedAttributes(child);
  154. config.Add(name,value);
  155. }
  156. static private void HandleRemove(XmlNode child, NameValueCollection config) {
  157. HandlerBase.CheckForChildNodes(child);
  158. String name = RemoveAttribute(child, "name");
  159. HandlerBase.CheckForUnrecognizedAttributes(child);
  160. config.Remove(name);
  161. }
  162. static private void HandleClear(XmlNode child, NameValueCollection config) {
  163. HandlerBase.CheckForChildNodes(child);
  164. HandlerBase.CheckForUnrecognizedAttributes(child);
  165. config.Clear();
  166. }
  167. }
  168. }
  169. }