/Runtime/PlatformSpecific/ConfigurationSectionHandler.cs

https://github.com/danemorgridge/SpecFlow · C# · 309 lines · 273 code · 36 blank · 0 comment · 0 complexity · 2347033934c9463d3a2e0db2135230c8 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Xml;
  8. using BoDi;
  9. namespace TechTalk.SpecFlow.Configuration
  10. {
  11. public class ConfigurationSectionHandler : ConfigurationSection
  12. {
  13. [ConfigurationProperty("language", IsRequired = false, DefaultValue = null)]
  14. public LanguageConfigElement Language
  15. {
  16. get { return (LanguageConfigElement)this["language"]; }
  17. set { this["language"] = value; }
  18. }
  19. [ConfigurationProperty("bindingCulture", IsRequired = false)]
  20. public BindingCultureConfigElement BindingCulture
  21. {
  22. get { return (BindingCultureConfigElement)this["bindingCulture"]; }
  23. set { this["bindingCulture"] = value; }
  24. }
  25. [ConfigurationProperty("unitTestProvider", IsRequired = false)]
  26. public UnitTestProviderConfigElement UnitTestProvider
  27. {
  28. get { return (UnitTestProviderConfigElement)this["unitTestProvider"]; }
  29. set { this["unitTestProvider"] = value; }
  30. }
  31. [ConfigurationProperty("generator", IsRequired = false)]
  32. public GeneratorConfigElement Generator
  33. {
  34. get { return (GeneratorConfigElement)this["generator"]; }
  35. set { this["generator"] = value; }
  36. }
  37. [ConfigurationProperty("runtime", IsRequired = false)]
  38. public RuntimeConfigElement Runtime
  39. {
  40. get { return (RuntimeConfigElement)this["runtime"]; }
  41. set { this["runtime"] = value; }
  42. }
  43. [ConfigurationProperty("trace", IsRequired = false)]
  44. public TraceConfigElement Trace
  45. {
  46. get { return (TraceConfigElement)this["trace"]; }
  47. set { this["trace"] = value; }
  48. }
  49. [ConfigurationProperty("stepAssemblies", IsDefaultCollection = false, IsRequired = false)]
  50. [ConfigurationCollection(typeof(StepAssemblyCollection), AddItemName = "stepAssembly")]
  51. public StepAssemblyCollection StepAssemblies
  52. {
  53. get { return (StepAssemblyCollection)this["stepAssemblies"]; }
  54. set { this["stepAssemblies"] = value; }
  55. }
  56. [ConfigurationProperty("plugins", IsDefaultCollection = false, IsRequired = false)]
  57. [ConfigurationCollection(typeof(PluginCollection), AddItemName = "add")]
  58. public PluginCollection Plugins
  59. {
  60. get { return (PluginCollection)this["plugins"]; }
  61. set { this["plugins"] = value; }
  62. }
  63. static public ConfigurationSectionHandler CreateFromXml(string xmlContent)
  64. {
  65. ConfigurationSectionHandler section = new ConfigurationSectionHandler();
  66. section.Init();
  67. section.Reset(null);
  68. using (var reader = new XmlTextReader(new StringReader(xmlContent.Trim())))
  69. {
  70. section.DeserializeSection(reader);
  71. }
  72. section.ResetModified();
  73. return section;
  74. }
  75. static public ConfigurationSectionHandler CreateFromXml(XmlNode xmlContent)
  76. {
  77. ConfigurationSectionHandler section = new ConfigurationSectionHandler();
  78. section.Init();
  79. section.Reset(null);
  80. using (var reader = new XmlNodeReader(xmlContent))
  81. {
  82. section.DeserializeSection(reader);
  83. }
  84. section.ResetModified();
  85. return section;
  86. }
  87. }
  88. public class LanguageConfigElement : ConfigurationElement
  89. {
  90. [ConfigurationProperty("feature", DefaultValue = "en", IsRequired = false)]
  91. [RegexStringValidator(@"\w{2}(-\w{2})?")]
  92. public string Feature
  93. {
  94. get { return (String)this["feature"]; }
  95. set { this["feature"] = value; }
  96. }
  97. [ConfigurationProperty("tool", DefaultValue = "", IsRequired = false)]
  98. [RegexStringValidator(@"\w{2}(-\w{2})?|")]
  99. public string Tool
  100. {
  101. get { return (String)this["tool"]; }
  102. set { this["tool"] = value; }
  103. }
  104. }
  105. public class BindingCultureConfigElement : ConfigurationElement
  106. {
  107. [ConfigurationProperty("name", DefaultValue = "en-US", IsRequired = false)]
  108. [RegexStringValidator(@"\w{2}(-\w{2})?")]
  109. public string Name
  110. {
  111. get { return (String)this["name"]; }
  112. set { this["name"] = value; }
  113. }
  114. }
  115. public class UnitTestProviderConfigElement : ConfigurationElement
  116. {
  117. [ConfigurationProperty("name", IsRequired = true, DefaultValue = "NUnit")]
  118. [StringValidator(MinLength = 1)]
  119. public string Name
  120. {
  121. get { return (String)this["name"]; }
  122. set { this["name"] = value; }
  123. }
  124. [ConfigurationProperty("generatorProvider", DefaultValue = null, IsRequired = false)]
  125. public string GeneratorProvider
  126. {
  127. get { return (string)this["generatorProvider"]; }
  128. set { this["generatorProvider"] = value; }
  129. }
  130. [ConfigurationProperty("runtimeProvider", DefaultValue = null, IsRequired = false)]
  131. public string RuntimeProvider
  132. {
  133. get { return (string)this["runtimeProvider"]; }
  134. set { this["runtimeProvider"] = value; }
  135. }
  136. }
  137. public class RuntimeConfigElement : ConfigurationElement
  138. {
  139. [ConfigurationProperty("dependencies", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
  140. [ConfigurationCollection(typeof(ContainerRegistrationCollection), AddItemName = "register")]
  141. public ContainerRegistrationCollection Dependencies
  142. {
  143. get { return (ContainerRegistrationCollection)this["dependencies"]; }
  144. set { this["dependencies"] = value; }
  145. }
  146. [ConfigurationProperty("detectAmbiguousMatches", DefaultValue = ConfigDefaults.DetectAmbiguousMatches, IsRequired = false)]
  147. public bool DetectAmbiguousMatches
  148. {
  149. get { return (bool)this["detectAmbiguousMatches"]; }
  150. set { this["detectAmbiguousMatches"] = value; }
  151. }
  152. [ConfigurationProperty("stopAtFirstError", DefaultValue = ConfigDefaults.StopAtFirstError, IsRequired = false)]
  153. public bool StopAtFirstError
  154. {
  155. get { return (bool)this["stopAtFirstError"]; }
  156. set { this["stopAtFirstError"] = value; }
  157. }
  158. [ConfigurationProperty("missingOrPendingStepsOutcome", DefaultValue = ConfigDefaults.MissingOrPendingStepsOutcome, IsRequired = false)]
  159. public MissingOrPendingStepsOutcome MissingOrPendingStepsOutcome
  160. {
  161. get { return (MissingOrPendingStepsOutcome)this["missingOrPendingStepsOutcome"]; }
  162. set { this["missingOrPendingStepsOutcome"] = value; }
  163. }
  164. }
  165. public class GeneratorConfigElement : ConfigurationElement
  166. {
  167. [ConfigurationProperty("dependencies", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
  168. [ConfigurationCollection(typeof(ContainerRegistrationCollection), AddItemName = "register")]
  169. public ContainerRegistrationCollection Dependencies
  170. {
  171. get { return (ContainerRegistrationCollection)this["dependencies"]; }
  172. set { this["dependencies"] = value; }
  173. }
  174. [ConfigurationProperty("allowDebugGeneratedFiles", DefaultValue = ConfigDefaults.AllowDebugGeneratedFiles, IsRequired = false)]
  175. public bool AllowDebugGeneratedFiles
  176. {
  177. get { return (bool)this["allowDebugGeneratedFiles"]; }
  178. set { this["allowDebugGeneratedFiles"] = value; }
  179. }
  180. [ConfigurationProperty("allowRowTests", DefaultValue = ConfigDefaults.AllowRowTests, IsRequired = false)]
  181. public bool AllowRowTests
  182. {
  183. get { return (bool)this["allowRowTests"]; }
  184. set { this["allowRowTests"] = value; }
  185. }
  186. [ConfigurationProperty("generateAsyncTests", DefaultValue = ConfigDefaults.GenerateAsyncTests, IsRequired = false)]
  187. public bool GenerateAsyncTests
  188. {
  189. get { return (bool)this["generateAsyncTests"]; }
  190. set { this["generateAsyncTests"] = value; }
  191. }
  192. [ConfigurationProperty("path", DefaultValue = ConfigDefaults.GeneratorPath, IsRequired = false)]
  193. public string GeneratorPath
  194. {
  195. get { return (string)this["path"]; }
  196. set { this["path"] = value; }
  197. }
  198. }
  199. public class TraceConfigElement : ConfigurationElement
  200. {
  201. [ConfigurationProperty("traceSuccessfulSteps", DefaultValue = ConfigDefaults.TraceSuccessfulSteps, IsRequired = false)]
  202. public bool TraceSuccessfulSteps
  203. {
  204. get { return (bool)this["traceSuccessfulSteps"]; }
  205. set { this["traceSuccessfulSteps"] = value; }
  206. }
  207. [ConfigurationProperty("traceTimings", DefaultValue = ConfigDefaults.TraceTimings, IsRequired = false)]
  208. public bool TraceTimings
  209. {
  210. get { return (bool)this["traceTimings"]; }
  211. set { this["traceTimings"] = value; }
  212. }
  213. [ConfigurationProperty("minTracedDuration", DefaultValue = ConfigDefaults.MinTracedDuration, IsRequired = false)]
  214. public TimeSpan MinTracedDuration
  215. {
  216. get { return (TimeSpan)this["minTracedDuration"]; }
  217. set { this["minTracedDuration"] = value; }
  218. }
  219. [ConfigurationProperty("listener", DefaultValue = null, IsRequired = false)]
  220. public string Listener
  221. {
  222. get { return (string)this["listener"]; }
  223. set { this["listener"] = value; }
  224. }
  225. }
  226. public class StepAssemblyCollection : ConfigurationElementCollection
  227. {
  228. protected override ConfigurationElement CreateNewElement()
  229. {
  230. return new StepAssemblyConfigElement();
  231. }
  232. protected override object GetElementKey(ConfigurationElement element)
  233. {
  234. return ((StepAssemblyConfigElement)element).Assembly;
  235. }
  236. }
  237. public class StepAssemblyConfigElement : ConfigurationElement
  238. {
  239. [ConfigurationProperty("assembly", IsRequired = true)]
  240. public string Assembly
  241. {
  242. get { return (string)this["assembly"]; }
  243. set { this["assembly"] = value; }
  244. }
  245. }
  246. public class PluginCollection : ConfigurationElementCollection, IEnumerable<PluginConfigElement>
  247. {
  248. protected override ConfigurationElement CreateNewElement()
  249. {
  250. return new PluginConfigElement();
  251. }
  252. protected override object GetElementKey(ConfigurationElement element)
  253. {
  254. return ((PluginConfigElement)element).Name;
  255. }
  256. IEnumerator<PluginConfigElement> IEnumerable<PluginConfigElement>.GetEnumerator()
  257. {
  258. foreach (var item in this)
  259. {
  260. yield return (PluginConfigElement)item;
  261. }
  262. }
  263. }
  264. public class PluginConfigElement : ConfigurationElement
  265. {
  266. [ConfigurationProperty("name", IsRequired = true)]
  267. public string Name
  268. {
  269. get { return (string)this["name"]; }
  270. set { this["name"] = value; }
  271. }
  272. }
  273. }