/ActiveRecord/Castle.ActiveRecord/Framework/Config/XmlConfigurationSource.cs

https://github.com/nats/castle-1.0.3-mono · C# · 182 lines · 107 code · 35 blank · 40 comment · 31 complexity · 9a0c62a5e9896554d7352ee09b928070 MD5 · raw file

  1. // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. namespace Castle.ActiveRecord.Framework.Config
  15. {
  16. using System;
  17. using System.Collections;
  18. using System.Collections.Specialized;
  19. using System.Configuration;
  20. using System.IO;
  21. using System.Xml;
  22. /// <summary>
  23. /// Source of configuration based on Xml
  24. /// source like files, streams or readers.
  25. /// </summary>
  26. public class XmlConfigurationSource : InPlaceConfigurationSource
  27. {
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="XmlConfigurationSource"/> class.
  30. /// </summary>
  31. protected XmlConfigurationSource()
  32. {
  33. }
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="XmlConfigurationSource"/> class.
  36. /// </summary>
  37. /// <param name="xmlFileName">Name of the XML file.</param>
  38. public XmlConfigurationSource(String xmlFileName)
  39. {
  40. XmlDocument doc = new XmlDocument();
  41. doc.Load(xmlFileName);
  42. PopulateSource(doc.DocumentElement);
  43. }
  44. /// <summary>
  45. /// Initializes a new instance of the <see cref="XmlConfigurationSource"/> class.
  46. /// </summary>
  47. /// <param name="stream">The stream.</param>
  48. public XmlConfigurationSource(Stream stream)
  49. {
  50. XmlDocument doc = new XmlDocument();
  51. doc.Load(stream);
  52. PopulateSource(doc.DocumentElement);
  53. }
  54. /// <summary>
  55. /// Initializes a new instance of the <see cref="XmlConfigurationSource"/> class.
  56. /// </summary>
  57. /// <param name="reader">The reader.</param>
  58. public XmlConfigurationSource(TextReader reader)
  59. {
  60. XmlDocument doc = new XmlDocument();
  61. doc.Load(reader);
  62. PopulateSource(doc.DocumentElement);
  63. }
  64. /// <summary>
  65. /// Populate this instance with values from the given XML node
  66. /// </summary>
  67. protected void PopulateSource(XmlNode section)
  68. {
  69. XmlAttribute isWebAtt = section.Attributes["isWeb"];
  70. XmlAttribute threadInfoAtt = section.Attributes["threadinfotype"];
  71. XmlAttribute isDebug = section.Attributes["isDebug"];
  72. XmlAttribute lazyByDefault = section.Attributes["default-lazy"];
  73. XmlAttribute pluralize = section.Attributes["pluralizeTableNames"];
  74. XmlAttribute verifyModelsAgainstDBSchemaAtt = section.Attributes["verifyModelsAgainstDBSchema"];
  75. SetUpThreadInfoType(isWebAtt != null && "true" == isWebAtt.Value,
  76. threadInfoAtt != null ? threadInfoAtt.Value : String.Empty);
  77. XmlAttribute sessionfactoryholdertypeAtt =
  78. section.Attributes["sessionfactoryholdertype"];
  79. SetUpSessionFactoryHolderType(sessionfactoryholdertypeAtt != null
  80. ?
  81. sessionfactoryholdertypeAtt.Value
  82. : String.Empty);
  83. XmlAttribute namingStrategyTypeAtt = section.Attributes["namingstrategytype"];
  84. SetUpNamingStrategyType(namingStrategyTypeAtt != null ? namingStrategyTypeAtt.Value : String.Empty);
  85. SetDebugFlag(isDebug != null && "true" == isDebug.Value);
  86. SetIsLazyByDefault(lazyByDefault != null && lazyByDefault.Value == "true");
  87. SetPluralizeTableNames(pluralize != null && pluralize.Value == "true");
  88. SetVerifyModelsAgainstDBSchema(verifyModelsAgainstDBSchemaAtt != null && verifyModelsAgainstDBSchemaAtt.Value == "true");
  89. PopulateConfigNodes(section);
  90. }
  91. private void PopulateConfigNodes(XmlNode section)
  92. {
  93. const string Config_Node_Name = "config";
  94. foreach(XmlNode node in section.ChildNodes)
  95. {
  96. if (node.NodeType != XmlNodeType.Element) continue;
  97. if (!Config_Node_Name.Equals(node.Name))
  98. {
  99. String message = String.Format("Unexpected node. Expect '{0}' found '{1}'",
  100. Config_Node_Name, node.Name);
  101. throw new ConfigurationErrorsException(message);
  102. }
  103. Type targetType = typeof(ActiveRecordBase);
  104. if (node.Attributes.Count != 0)
  105. {
  106. XmlAttribute typeNameAtt = node.Attributes["type"];
  107. if (typeNameAtt == null)
  108. {
  109. String message = String.Format("Invalid attribute at node '{0}'. " +
  110. "The only supported attribute is 'type'", Config_Node_Name);
  111. throw new ConfigurationErrorsException(message);
  112. }
  113. String typeName = typeNameAtt.Value;
  114. targetType = Type.GetType(typeName, false, false);
  115. if (targetType == null)
  116. {
  117. String message = String.Format("Could not obtain type from name '{0}'", typeName);
  118. throw new ConfigurationErrorsException(message);
  119. }
  120. }
  121. Add(targetType, BuildProperties(node));
  122. }
  123. }
  124. /// <summary>
  125. /// Builds the configuration properties.
  126. /// </summary>
  127. /// <param name="node">The node.</param>
  128. /// <returns></returns>
  129. protected IDictionary BuildProperties(XmlNode node)
  130. {
  131. HybridDictionary dict = new HybridDictionary();
  132. foreach(XmlNode addNode in node.SelectNodes("add"))
  133. {
  134. XmlAttribute keyAtt = addNode.Attributes["key"];
  135. XmlAttribute valueAtt = addNode.Attributes["value"];
  136. if (keyAtt == null || valueAtt == null)
  137. {
  138. String message = String.Format("For each 'add' element you must specify 'key' and 'value' attributes");
  139. throw new ConfigurationErrorsException(message);
  140. }
  141. string value = valueAtt.Value;
  142. dict.Add(keyAtt.Value, value);
  143. }
  144. return dict;
  145. }
  146. }
  147. }