ProvidersSectionHandler.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // System.Data.OleDb.OleDbConnection
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. // Boris Kirzner <[email protected]>
  7. //
  8. // (C) 2006 Mainsoft Corporation (http://www.mainsoft.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Configuration;
  32. using System.Xml;
  33. using System.Collections;
  34. using System.Collections.Specialized;
  35. namespace Mainsoft.Data.Configuration
  36. {
  37. class ProvidersSectionHandler : IConfigurationSectionHandler
  38. {
  39. public virtual object Create (object parent, object configContext, XmlNode section) {
  40. if (section.Attributes != null && section.Attributes.Count != 0)
  41. ThrowException ("Unrecognized attribute", section);
  42. ArrayList providers = new ArrayList();
  43. XmlNodeList providerElements = section.SelectNodes ("provider");
  44. foreach (XmlNode child in providerElements) {
  45. XmlNodeType ntype = child.NodeType;
  46. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  47. continue;
  48. if (ntype != XmlNodeType.Element)
  49. ThrowException ("Only elements allowed", child);
  50. object parentProvider = null;
  51. XmlAttribute extends = child.Attributes["parent"];
  52. if (extends != null) {
  53. string super = extends.Value;
  54. string sectionName = super.Substring(0, super.IndexOf('#'));
  55. string id = super.Substring(sectionName.Length+1);
  56. IList supers = (IList) ConfigurationSettings.GetConfig(sectionName);
  57. for (int i = 0; i < supers.Count; i++) {
  58. Hashtable ht = (Hashtable)supers[i];
  59. string curId = (string)ht["id"];
  60. if (String.CompareOrdinal(id,curId) == 0) {
  61. parentProvider = ht;
  62. break;
  63. }
  64. }
  65. child.Attributes.Remove(extends);
  66. }
  67. DictionarySectionHandler handler = new DictionarySectionHandler();
  68. Hashtable col = (Hashtable) handler.Create (parentProvider, null, child);
  69. providers.Add(col);
  70. }
  71. if (parent != null && parent is IList) {
  72. IList parentList = (IList)parent;
  73. for (int i = 0; i < parentList.Count; i++) {
  74. Hashtable htParent = (Hashtable)parentList[i];
  75. string parentId = (string)htParent["id"];
  76. bool overriden = false;
  77. for (int y = 0; y < providers.Count; y++) {
  78. Hashtable htMe = (Hashtable)providers[y];
  79. string myId = (string)htMe["id"];
  80. if (String.CompareOrdinal(parentId,myId) == 0) {
  81. overriden = true;
  82. foreach (object key in htParent.Keys)
  83. if (!htMe.ContainsKey(key))
  84. htMe[key] = htParent[key];
  85. break;
  86. }
  87. }
  88. if (!overriden)
  89. providers.Add(htParent);
  90. }
  91. }
  92. return providers;
  93. }
  94. static internal void ThrowException (string msg, XmlNode node)
  95. {
  96. if (node != null && node.Name != String.Empty)
  97. msg = msg + " (node name: " + node.Name + ") ";
  98. throw new ConfigurationException (msg, node);
  99. }
  100. }
  101. }