PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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