/source/app/NOS.Registration/PageFormatter.cs

http://github.com/agross/netopenspace · C# · 90 lines · 76 code · 14 blank · 0 comment · 10 complexity · f19edd4dbaa87efdd0dfc0501121be73 MD5 · raw file

  1. using System;
  2. using System.Text.RegularExpressions;
  3. using NOS.Registration.EntryPositioning;
  4. namespace NOS.Registration
  5. {
  6. internal class PageFormatter : IPageFormatter
  7. {
  8. readonly ILogger _logger;
  9. readonly IOpinionEvaluator _opinionEvaluator;
  10. public PageFormatter(ILogger logger, IOpinionEvaluator opinionEvaluator)
  11. {
  12. _logger = logger;
  13. _opinionEvaluator = opinionEvaluator;
  14. }
  15. #region IPageFormatter Members
  16. public string AddEntry(string content, string entry, User user, IPluginConfiguration configuration)
  17. {
  18. var entryMatcher = NewRegex(configuration.EntryPattern);
  19. var listStartMatcher = NewRegex(configuration.ListStartPattern);
  20. var listEndMatcher = NewRegex(configuration.ListEndPattern);
  21. var waitingListEndMatcher = NewRegex(configuration.WaitingListEndPattern);
  22. int listStart = AssertMatchesOnce(content, listStartMatcher, "list start");
  23. int listEnd = AssertMatchesOnce(content, listEndMatcher, "list end");
  24. int waitingListEnd = AssertMatchesOnce(content, waitingListEndMatcher, "waiting list end");
  25. if (listEnd == -1 || listEnd == -1 || waitingListEnd == -1)
  26. {
  27. throw new InvalidOperationException(
  28. "The list and/or waiting list regular expressions did not match once. See previous errors.");
  29. }
  30. if (listEnd < listStart)
  31. {
  32. _logger.Error(String.Format("The list end position ({0}) is before the list start position ({1})",
  33. listEnd,
  34. listStart),
  35. "SYSTEM");
  36. throw new InvalidOperationException("The list start and end positions are not sanitized. See previous error.");
  37. }
  38. int numberOfAttendees = CountNumberOfEntries(content.Substring(listStart, listEnd - listStart), entryMatcher);
  39. int addAtIndex = _opinionEvaluator.Evaluate(new EvaluationContext
  40. {
  41. NumberOfAttendees = numberOfAttendees,
  42. ListEnd = listEnd,
  43. WaitingListEnd = waitingListEnd,
  44. Configuration = configuration,
  45. User = user,
  46. Logger = _logger
  47. });
  48. return content.Insert(addAtIndex, entry);
  49. }
  50. #endregion
  51. int AssertMatchesOnce(string content, Regex matcher, string matchType)
  52. {
  53. MatchCollection matches = matcher.Matches(content);
  54. if (matches.Count == 0)
  55. {
  56. _logger.Error(String.Format("The {0} cannot be found: {1}", matchType, matcher), "SYSTEM");
  57. return -1;
  58. }
  59. if (matches.Count > 1)
  60. {
  61. _logger.Error(String.Format("The {0} was found {1} times: {2}", matchType, matches.Count, matcher),
  62. "SYSTEM");
  63. return -1;
  64. }
  65. return matches[0].Captures[0].Index;
  66. }
  67. static Regex NewRegex(string value)
  68. {
  69. return new Regex(value, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);
  70. }
  71. static int CountNumberOfEntries(string content, Regex matcher)
  72. {
  73. return matcher.Matches(content).Count;
  74. }
  75. }
  76. }