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 1
		private ITokenizer Tokenizer { get; }
14 1
		private ITokenPatternValidator PatternValidator { get; }
15 1
		private IRobotsPageTokenParser TokenParser { get; }
16

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

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

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

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

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

Read our documentation on viewing source code .

Loading