TurnerSoftware / RobotsExclusionTools
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using TurnerSoftware.RobotsExclusionTools.Tokenization;
5
using TurnerSoftware.RobotsExclusionTools.Tokenization.Tokenizers;
6
using TurnerSoftware.RobotsExclusionTools.Tokenization.TokenParsers;
7
using TurnerSoftware.RobotsExclusionTools.Tokenization.Validators;
8

9
namespace TurnerSoftware.RobotsExclusionTools
10
{
11
	public class RobotsPageParser : IRobotsPageDefinitionParser
12
	{
13 2
		private ITokenizer Tokenizer { get; }
14 2
		private ITokenPatternValidator PatternValidator { get; }
15 2
		private IRobotsPageTokenParser TokenParser { get; }
16

17 2
		public RobotsPageParser() : this(new RobotsPageTokenizer(), new RobotsPageTokenPatternValidator(), new RobotsPageTokenParser()) { }
18

19 2
		public RobotsPageParser(ITokenizer tokenizer, ITokenPatternValidator patternValidator, IRobotsPageTokenParser tokenParser)
20 2
		{
21 2
			Tokenizer = tokenizer ?? throw new ArgumentNullException(nameof(tokenizer));
22 2
			PatternValidator = patternValidator ?? throw new ArgumentNullException(nameof(patternValidator));
23 2
			TokenParser = tokenParser ?? throw new ArgumentNullException(nameof(tokenParser));
24 2
		}
25
		
26
		public RobotsPageDefinition FromRules(IEnumerable<string> rules)
27 2
		{
28 2
			var tokens = new List<Token>();
29 2
			foreach (var ruleLine in rules)
30 2
			{
31 2
				tokens.AddRange(Tokenizer.Tokenize(ruleLine));
32 2
				tokens.Add(Token.NewLineToken);
33 2
			}
34

35 2
			return FromTokens(tokens);
36 2
		}
37
		
38
		private RobotsPageDefinition FromTokens(IEnumerable<Token> tokens)
39 2
		{
40 2
			var validationResult = PatternValidator.Validate(tokens);
41

42 2
			if (validationResult.IsValid)
43 2
			{
44 2
				return new RobotsPageDefinition
45 2
				{
46 2
					PageAccessEntries = TokenParser.GetPageAccessEntries(tokens)
47 2
				};
48
			}
49

50 0
			return new RobotsPageDefinition();
51 2
		}
52
	}
53
}

Read our documentation on viewing source code .

Loading