PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/project/core/Config/preprocessor/ConfigPreprocessor.cs

https://github.com/psgirard/CruiseControl.NET
C# | 115 lines | 73 code | 9 blank | 33 comment | 2 complexity | 23b55f2c15c2838688aa26f89a2dd7d7 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Xml;
  6. using System.Xml.Linq;
  7. namespace ThoughtWorks.CruiseControl.Core.Config.Preprocessor
  8. {
  9. /// <summary>
  10. /// The preprocessor
  11. /// </summary>
  12. public class ConfigPreprocessor
  13. {
  14. private readonly PreprocessorSettings _settings;
  15. private PreprocessorEnvironment _env;
  16. /// <summary>
  17. /// Occurs when [subfile loaded].
  18. /// </summary>
  19. /// <remarks></remarks>
  20. public event ConfigurationSubfileLoadedHandler SubfileLoaded;
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="ConfigPreprocessor" /> class.
  23. /// </summary>
  24. /// <param name="settings">The settings.</param>
  25. /// <remarks></remarks>
  26. public ConfigPreprocessor(PreprocessorSettings settings)
  27. {
  28. _settings = settings;
  29. }
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="ConfigPreprocessor" /> class.
  32. /// </summary>
  33. /// <remarks></remarks>
  34. public ConfigPreprocessor() : this( new PreprocessorSettings
  35. {
  36. ExplicitDeclarationRequired = false,
  37. InitialDefinitions =
  38. new Dictionary< string, string >(),
  39. NamesAreCaseSensitve = false,
  40. UseOsEnvironment = true
  41. } )
  42. {
  43. }
  44. /// <summary>
  45. /// Pres the process.
  46. /// </summary>
  47. /// <param name="input">The input.</param>
  48. /// <param name="output">The output.</param>
  49. /// <param name="resolver">The resolver.</param>
  50. /// <param name="inputUri">The input_uri.</param>
  51. /// <returns></returns>
  52. /// <remarks></remarks>
  53. public PreprocessorEnvironment PreProcess(XmlReader input, XmlWriter output,
  54. XmlUrlResolver resolver, Uri inputUri)
  55. {
  56. // The base URI is needed to resolve includes of relative paths, as well as to generate
  57. // error messages.
  58. // If none is given explicitly, try to use the XmlReader's BaseUri.
  59. // If that doesn't exist either, use the current working directory and a fake filename.
  60. Uri base_uri = inputUri ??
  61. ( String.IsNullOrEmpty( input.BaseURI )
  62. ? new Uri(
  63. Path.Combine(
  64. Environment.CurrentDirectory,
  65. "nofile.xml" ) )
  66. : new Uri( input.BaseURI ) );
  67. // Create the environment
  68. _env = new PreprocessorEnvironment( _settings, base_uri, resolver );
  69. // Load the input document
  70. XDocument doc = XDocument.Load( input, LoadOptions.PreserveWhitespace | LoadOptions.SetBaseUri | LoadOptions.SetLineInfo );
  71. // Process the input document's nodes and write the results to the output stream
  72. foreach ( XNode out_node in
  73. doc.Nodes().SelectMany(
  74. node => _env._DefaultNodeProcessor.Process( node ) ).Where( node => !(node is XText)) )
  75. {
  76. out_node.WriteTo( output );
  77. }
  78. // Notify listeners of all files encountered.
  79. if (SubfileLoaded != null)
  80. {
  81. foreach (Uri path in _env.Fileset)
  82. {
  83. SubfileLoaded( path );
  84. }
  85. }
  86. return _env;
  87. }
  88. }
  89. internal sealed class AttrName
  90. {
  91. private AttrName()
  92. {}
  93. public static XName AssemblyLocation = "assembly-location";
  94. public static XName CounterName = "counter-name";
  95. public static XName CountExpr = "count-expr";
  96. public static XName Expr = "expr";
  97. public static XName Href = "href";
  98. public static XName InitExpr = "init-expr";
  99. public static XName IteratorExpr = "iterator-expr";
  100. public static XName IteratorName = "iterator-name";
  101. public static XName Max = "max";
  102. public static XName Name = "name";
  103. public static XName TestExpr = "test-expr";
  104. public static XName Type = "type";
  105. }
  106. }