PageRenderTime 33ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/project/core/Config/preprocessor/ElementProcessors/ElementProcessor.cs

https://github.com/psgirard/CruiseControl.NET
C# | 153 lines | 72 code | 19 blank | 62 comment | 3 complexity | 0cb43d9b62e11171ba3f574b87dc84cf MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using System.Xml.Linq;
  6. namespace ThoughtWorks.CruiseControl.Core.Config.Preprocessor.ElementProcessors
  7. {
  8. /// <summary>
  9. /// Interface that node processors must implement
  10. /// </summary>
  11. public interface IElementProcessor
  12. {
  13. /// <summary>
  14. /// Retrieve the name of the element that this processor processes.
  15. /// </summary>
  16. XName TargetElementName { get; }
  17. /// <summary>
  18. /// Called by the preprocessor to process a node.
  19. /// </summary>
  20. /// <param name="node"></param>
  21. /// <returns></returns>
  22. IEnumerable< XNode > Process(XNode node);
  23. }
  24. /// <summary>
  25. /// Abstract base class for node processors
  26. /// </summary>
  27. public abstract class ElementProcessor : IElementProcessor
  28. {
  29. /// <summary>
  30. /// Regex matches 2 or more contiguous whitespace characters
  31. /// </summary>
  32. private readonly Regex _space_matcher = new Regex( @"\s\s\s*" );
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="ElementProcessor" /> class.
  35. /// </summary>
  36. /// <param name="targetElementName">The target_element_name.</param>
  37. /// <param name="env">The env.</param>
  38. /// <remarks></remarks>
  39. protected ElementProcessor(XName targetElementName, PreprocessorEnvironment env)
  40. {
  41. TargetElementName = targetElementName;
  42. _Env = env;
  43. }
  44. /// <summary>
  45. /// Gets or sets the _ env.
  46. /// </summary>
  47. /// <value>The _ env.</value>
  48. /// <remarks></remarks>
  49. protected PreprocessorEnvironment _Env { get; private set; }
  50. #region IElementProcessor Members
  51. /// <summary>
  52. /// Processes the specified node.
  53. /// </summary>
  54. /// <param name="node">The node.</param>
  55. /// <returns></returns>
  56. /// <remarks></remarks>
  57. public abstract IEnumerable< XNode > Process(XNode node);
  58. /// <summary>
  59. /// Gets or sets the name of the target element.
  60. /// </summary>
  61. /// <value>The name of the target element.</value>
  62. /// <remarks></remarks>
  63. public XName TargetElementName { get; protected set; }
  64. #endregion
  65. /// <summary>
  66. /// Perform default preprocessing on the given nodes
  67. /// </summary>
  68. /// <param name="nodes"></param>
  69. /// <returns></returns>
  70. protected IEnumerable< XNode > _ProcessNodes(IEnumerable< XNode > nodes)
  71. {
  72. if ( nodes.Any() )
  73. {
  74. var return_nodes = new List< XNode >();
  75. foreach ( XNode node in nodes )
  76. {
  77. return_nodes.AddRange( _Env._DefaultNodeProcessor.Process( node ) );
  78. }
  79. return return_nodes;
  80. }
  81. return new XNode[] {};
  82. }
  83. /// <summary>
  84. /// _s the process text.
  85. /// </summary>
  86. /// <param name="value">The value.</param>
  87. /// <returns></returns>
  88. /// <remarks></remarks>
  89. protected IEnumerable< XNode > _ProcessText(string value)
  90. {
  91. IEnumerable< XNode > result = _Env.EvalTextSymbols( value );
  92. //return result.GetTextValue() != value ? _ProcessNodes( result ).Cast< XNode >() : result;
  93. var textvalue = result.GetTextValue();
  94. if (textvalue != value)
  95. {
  96. return _ProcessNodes(result);
  97. }
  98. else
  99. {
  100. return result;
  101. }
  102. }
  103. /// <summary>
  104. /// _s the define from attributes.
  105. /// </summary>
  106. /// <param name="element">The element.</param>
  107. /// <remarks></remarks>
  108. protected void _DefineFromAttributes(XElement element)
  109. {
  110. foreach ( XAttribute attr in element.Attributes() )
  111. {
  112. _Env.DefineTextSymbol( attr.Name.LocalName, attr.Value );
  113. }
  114. }
  115. /// <summary>
  116. /// _s the assume element.
  117. /// </summary>
  118. /// <param name="node">The node.</param>
  119. /// <returns></returns>
  120. /// <remarks></remarks>
  121. protected static XElement _AssumeElement(XNode node)
  122. {
  123. try
  124. {
  125. return ( XElement ) node;
  126. }
  127. catch ( InvalidCastException )
  128. {
  129. throw new InvalidOperationException( "Expected an XML element, got a " +
  130. node.NodeType );
  131. }
  132. }
  133. }
  134. }