NameTransformer.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. namespace Caliburn.Micro.Core {
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. /// <summary>
  7. /// Class for managing the list of rules for doing name transformation.
  8. /// </summary>
  9. public class NameTransformer : BindableCollection<NameTransformer.Rule> {
  10. #if NET
  11. private const RegexOptions options = RegexOptions.Compiled;
  12. #else
  13. private const RegexOptions options = RegexOptions.None;
  14. #endif
  15. bool useEagerRuleSelection = true;
  16. /// <summary>
  17. /// Flag to indicate if transformations from all matched rules are returned. Otherwise, transformations from only the first matched rule are returned.
  18. /// </summary>
  19. public bool UseEagerRuleSelection {
  20. get { return useEagerRuleSelection; }
  21. set { useEagerRuleSelection = value; }
  22. }
  23. /// <summary>
  24. /// Adds a transform using a single replacement value and a global filter pattern.
  25. /// </summary>
  26. /// <param name = "replacePattern">Regular expression pattern for replacing text</param>
  27. /// <param name = "replaceValue">The replacement value.</param>
  28. /// <param name = "globalFilterPattern">Regular expression pattern for global filtering</param>
  29. public void AddRule(string replacePattern, string replaceValue, string globalFilterPattern = null) {
  30. AddRule(replacePattern, new[] { replaceValue }, globalFilterPattern);
  31. }
  32. /// <summary>
  33. /// Adds a transform using a list of replacement values and a global filter pattern.
  34. /// </summary>
  35. /// <param name = "replacePattern">Regular expression pattern for replacing text</param>
  36. /// <param name = "replaceValueList">The list of replacement values</param>
  37. /// <param name = "globalFilterPattern">Regular expression pattern for global filtering</param>
  38. public void AddRule(string replacePattern, IEnumerable<string> replaceValueList, string globalFilterPattern = null) {
  39. Add(new Rule {
  40. ReplacePattern = replacePattern,
  41. ReplacementValues = replaceValueList,
  42. GlobalFilterPattern = globalFilterPattern
  43. });
  44. }
  45. /// <summary>
  46. /// Gets the list of transformations for a given name.
  47. /// </summary>
  48. /// <param name = "source">The name to transform into the resolved name list</param>
  49. /// <returns>The transformed names.</returns>
  50. public IEnumerable<string> Transform(string source) {
  51. return Transform(source, r => r);
  52. }
  53. /// <summary>
  54. /// Gets the list of transformations for a given name.
  55. /// </summary>
  56. /// <param name = "source">The name to transform into the resolved name list</param>
  57. /// <param name = "getReplaceString">A function to do a transform on each item in the ReplaceValueList prior to applying the regular expression transform</param>
  58. /// <returns>The transformed names.</returns>
  59. public IEnumerable<string> Transform(string source, Func<string, string> getReplaceString) {
  60. var nameList = new List<string>();
  61. var rules = this.Reverse();
  62. foreach(var rule in rules) {
  63. if(!string.IsNullOrEmpty(rule.GlobalFilterPattern) && !rule.GlobalFilterPatternRegex.IsMatch(source)) {
  64. continue;
  65. }
  66. if(!rule.ReplacePatternRegex.IsMatch(source)) {
  67. continue;
  68. }
  69. nameList.AddRange(
  70. rule.ReplacementValues
  71. .Select(getReplaceString)
  72. .Select(repString => rule.ReplacePatternRegex.Replace(source, repString))
  73. );
  74. if (!useEagerRuleSelection) {
  75. break;
  76. }
  77. }
  78. return nameList;
  79. }
  80. ///<summary>
  81. /// A rule that describes a name transform.
  82. ///</summary>
  83. public class Rule {
  84. private Regex replacePatternRegex;
  85. private Regex globalFilterPatternRegex;
  86. /// <summary>
  87. /// Regular expression pattern for global filtering
  88. /// </summary>
  89. public string GlobalFilterPattern;
  90. /// <summary>
  91. /// Regular expression pattern for replacing text
  92. /// </summary>
  93. public string ReplacePattern;
  94. /// <summary>
  95. /// The list of replacement values
  96. /// </summary>
  97. public IEnumerable<string> ReplacementValues;
  98. /// <summary>
  99. /// Regular expression for global filtering
  100. /// </summary>
  101. public Regex GlobalFilterPatternRegex {
  102. get {
  103. return globalFilterPatternRegex ?? (globalFilterPatternRegex = new Regex(GlobalFilterPattern, options));
  104. }
  105. }
  106. /// <summary>
  107. /// Regular expression for replacing text
  108. /// </summary>
  109. public Regex ReplacePatternRegex {
  110. get {
  111. return replacePatternRegex ?? (replacePatternRegex = new Regex(ReplacePattern, options));
  112. }
  113. }
  114. }
  115. }
  116. }