NTP Analyzer  0.8.2
Analyze the operation of time servers
HostPageSyntaxNode.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 HostPageSyntaxNode : SyntaxNode<HostPageConfiguration>
33  {
34  public HostPageSyntaxNode(string name, int line)
35  : base(Symbol.KeywordHostPage, name, line, true)
36  {
37  }
38 
39  private Uri location;
41 
43  {
44  var frequency = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordFrequency) as IntegerSettingNode;
45  var initialRun = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordInitialRun) as BooleanSettingNode;
46  var fixedRun = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordFixedRun) as BooleanSettingNode;
47  var title = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordTitle) as StringSettingNode ??
48  Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordPageTitle) as StringSettingNode;
49  var poolMember = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordPoolMember) as BooleanSettingNode;
50  var queryDirect = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordQueryDirect) as BooleanSettingNode;
51  var pageTemplate = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordPageTemplate) as PageThemeNode;
52  var pageTime = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordPageTime) as TimeStampNode ??
53  Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordTimeStamp) as TimeStampNode;
54  var dest = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordDestinations) as FileDestinationSyntaxNode;
55  var summaries = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordSummaries) as SummariesSyntaxNode;
56  var graphSets = Nodes.Where(n => n.Symbol == Symbol.KeywordGraphSet).Cast<GraphSetSyntaxNode>();
57 
58  return new HostPageConfiguration(
59  Name,
60  frequency?.Value,
61  initialRun?.Value,
62  fixedRun?.Value,
63  pageTime?.DateTimeKind,
64  pageTemplate?.PageTheme,
65  title?.Value,
66  poolMember?.Value,
67  queryDirect?.Value,
68  location,
69  peerPagesNode?.Compile(),
70  graphSets.Select(g => g.Compile()),
71  summaries?.Compile(),
72  dest?.Compile()
73  );
74  }
75 
76  protected override void InternalResolve(SymbolTable table)
77  {
78  var page = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordPeerPage) as StringSettingNode;
79  if (page != null)
80  peerPagesNode = table.Lookup(page.Value) as PeerPageSyntaxNode;
81  }
82 
83  protected override void ValidateMandatories()
84  {
85  CheckIsUnique(new List<Symbol>
86  {
87  Symbol.KeywordFrequency,
88  Symbol.KeywordInitialRun,
89  Symbol.KeywordFixedRun,
90  Symbol.KeywordTitle,
91  Symbol.KeywordPageTitle,
92  Symbol.KeywordPoolMember,
93  Symbol.KeywordQueryDirect,
94  Symbol.KeywordPageTemplate,
95  Symbol.KeywordPageTime,
96  Symbol.KeywordTimeStamp,
97  Symbol.KeywordPeerPage,
98  Symbol.KeywordLink
99  });
100 
101  CheckOnlyOneIsPresent(new List<Symbol>
102  {
103  Symbol.KeywordTitle,
104  Symbol.KeywordPageTitle
105  });
106 
107  CheckOnlyOneIsPresent(new List<Symbol>
108  {
109  Symbol.KeywordPageTime,
110  Symbol.KeywordTimeStamp
111  });
112 
113  CheckAllIsPresent(new List<Symbol> {Symbol.KeywordDestinations});
114  }
115 
116  protected override void ValidateReferences(SymbolTable table)
117  {
118  var pageName = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordPeerPage) as StringSettingNode;
119  if (pageName != null)
120  {
121  string keyword = Keyword.Find(Symbol.KeywordPeerPage).Name;
122  var reference = table.Lookup(pageName.Value);
123  if (reference == null)
124  AddReferenceNameError(pageName, keyword, pageName.Value);
125  else if (!(reference is PeerPageSyntaxNode))
126  AddReferenceTypeError(pageName, keyword, keyword, pageName.Value);
127  }
128 
129  var link = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordLink) as StringSettingNode;
130  if (link == null)
131  return;
132 
133  var linkKeyword = Keyword.Find(Symbol.KeywordLink).Name;
134  location = CheckLink(link.Value, linkKeyword);
135  }
136 
137  protected override void ValidateTypes()
138  {
139  CheckTypeIs<IntegerSettingNode>(Symbol.KeywordFrequency);
140  CheckTypeIs<BooleanSettingNode>(Symbol.KeywordInitialRun);
141  CheckTypeIs<BooleanSettingNode>(Symbol.KeywordFixedRun);
142  CheckTypeIs<StringSettingNode>(Symbol.KeywordTitle);
143  CheckTypeIs<StringSettingNode>(Symbol.KeywordPageTitle);
144  CheckTypeIs<StringSettingNode>(Symbol.KeywordPeerPage);
145  CheckTypeIs<StringSettingNode>(Symbol.KeywordLink);
146  CheckTypeIs<BooleanSettingNode>(Symbol.KeywordPoolMember);
147  CheckTypeIs<BooleanSettingNode>(Symbol.KeywordQueryDirect);
148  CheckTypeIs<PageThemeNode>(Symbol.KeywordPageTemplate);
149  CheckTypeIs<TimeStampNode>(Symbol.KeywordPageTime);
150  CheckTypeIs<TimeStampNode>(Symbol.KeywordTimeStamp);
151  }
152  }
153 }
override void ValidateMandatories()
Override to validates the mandatory types in this syntax node.
static Keyword Find(Symbol symbol)
Definition: Keyword.cs:216
override void InternalResolve(SymbolTable table)
override void ValidateReferences(SymbolTable table)
override HostPageConfiguration InternalCompile()
override void ValidateTypes()
Override to validates the types in this syntax node.
Configuration for a HTML page showing status and graphs for a hosted NTP server.
ISyntaxNode Lookup(string name)
Definition: SymbolTable.cs:41