NTP Analyzer  0.8.2
Analyze the operation of time servers
PeerPageSyntaxNode.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
28 using Ntp.Analyzer.Config.Table;
29 
30 namespace Ntp.Analyzer.Config.Syntax
31 {
32  public sealed class PeerPageSyntaxNode : SyntaxNode<PeerPageConfiguration>
33  {
34  public PeerPageSyntaxNode(string name, int line)
35  : base(Symbol.KeywordPeerPage, name, line, true)
36  {
37  }
38 
39  private Uri location;
40 
42  {
43  var frequency = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordFrequency) as IntegerSettingNode;
44  var initialRun = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordInitialRun) as BooleanSettingNode;
45  var fixedRun = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordFixedRun) as BooleanSettingNode;
46  var title = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordTitle) as StringSettingNode ??
47  Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordPageTitle) as StringSettingNode;
48  var pageTemplate = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordPageTemplate) as PageThemeNode;
49  var pageTime = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordPageTime) as TimeStampNode ??
50  Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordTimeStamp) as TimeStampNode;
51  var graphSets = Nodes.Where(n => n.Symbol == Symbol.KeywordGraphSet).Cast<GraphSetSyntaxNode>();
52  var dest = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordDestinations) as DirDestinationSyntaxNode;
53 
54  return new PeerPageConfiguration(
55  Name,
56  frequency?.Value,
57  initialRun?.Value,
58  fixedRun?.Value,
59  pageTime?.DateTimeKind,
60  pageTemplate?.PageTheme,
61  title?.Value,
62  graphSets.Select(g => g.Compile()),
63  dest?.Compile(),
64  location
65  );
66  }
67 
68  protected override void ValidateMandatories()
69  {
70  CheckIsUnique(new List<Symbol>
71  {
72  Symbol.KeywordFrequency,
73  Symbol.KeywordInitialRun,
74  Symbol.KeywordFixedRun,
75  Symbol.KeywordTitle,
76  Symbol.KeywordPageTitle,
77  Symbol.KeywordLink,
78  Symbol.KeywordPageTemplate,
79  Symbol.KeywordPageTime,
80  Symbol.KeywordTimeStamp,
81  Symbol.KeywordDestinations
82  });
83 
84  CheckOnlyOneIsPresent(new List<Symbol>
85  {
86  Symbol.KeywordTitle,
87  Symbol.KeywordPageTitle
88  });
89 
90  CheckOnlyOneIsPresent(new List<Symbol>
91  {
92  Symbol.KeywordPageTime,
93  Symbol.KeywordTimeStamp
94  });
95 
96  CheckAllIsPresent(new List<Symbol> {Symbol.KeywordDestinations});
97  }
98 
99  protected override void ValidateReferences(SymbolTable table)
100  {
101  var link = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordLink) as StringSettingNode;
102  if (link == null)
103  return;
104 
105  var keyword = Keyword.Find(Symbol.KeywordLink).Name;
106  location = CheckLink(link.Value, keyword);
107  }
108 
109  protected override void ValidateTypes()
110  {
111  CheckTypeIs<IntegerSettingNode>(Symbol.KeywordFrequency);
112  CheckTypeIs<BooleanSettingNode>(Symbol.KeywordInitialRun);
113  CheckTypeIs<BooleanSettingNode>(Symbol.KeywordFixedRun);
114  CheckTypeIs<StringSettingNode>(Symbol.KeywordTitle);
115  CheckTypeIs<StringSettingNode>(Symbol.KeywordPageTitle);
116  CheckTypeIs<StringSettingNode>(Symbol.KeywordLink);
117  CheckTypeIs<PageThemeNode>(Symbol.KeywordPageTemplate);
118  CheckTypeIs<TimeStampNode>(Symbol.KeywordPageTime);
119  CheckTypeIs<TimeStampNode>(Symbol.KeywordTimeStamp);
120  }
121  }
122 }
static Keyword Find(Symbol symbol)
Definition: Keyword.cs:216
override void ValidateReferences(SymbolTable table)
override void ValidateMandatories()
Override to validates the mandatory types in this syntax node.
override PeerPageConfiguration InternalCompile()
override void ValidateTypes()
Override to validates the types in this syntax node.