NTP Analyzer  0.8.2
Analyze the operation of time servers
DatabaseSyntaxNode.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;
25 using Ntp.Analyzer.Config.Node;
28 using Ntp.Analyzer.Config.Table;
29 
30 namespace Ntp.Analyzer.Config.Syntax
31 {
32  public sealed class DatabaseSyntaxNode : SyntaxNode<DatabaseConfiguration>
33  {
34  public DatabaseSyntaxNode(string name, int line)
35  : base(Symbol.KeywordDatabase, name, line)
36  {
37  }
38 
40  {
41  var host = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordIp) as StringSettingNode ??
42  Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordHost) as StringSettingNode ??
43  Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordAddress) as StringSettingNode ??
44  Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordHostAddress) as StringSettingNode;
45  var port = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordPort) as IntegerSettingNode;
46  var database = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordName) as StringSettingNode ??
47  Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordDatabase) as StringSettingNode;
48  var user = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordUser) as StringSettingNode ??
49  Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordDatabaseUser) as StringSettingNode;
50  var pass = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordPass) as StringSettingNode ??
51  Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordDatabasePass) as StringSettingNode;
52  var create = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordCreate) as BooleanSettingNode;
53  var upgrade = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordUpgrade) as BooleanSettingNode;
54  var import = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordImport) as BooleanSettingNode;
55  var provider = Nodes.Single(n => n.Symbol == Symbol.KeywordDatabaseProvider) as DatabaseProviderNode;
56  var conString = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordConString) as StringSettingNode;
57  var certFile = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordCertFile) as StringSettingNode;
58  var certPass = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordCertPass) as StringSettingNode;
59  var timeout = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordConnectionTimeout) as IntegerSettingNode;
60  var protocol = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordProtocol) as IntegerSettingNode;
61  var ssl = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordEnableSsl) as BooleanSettingNode;
62 
63  if (provider == null || host == null || database == null || user == null || pass == null)
64  {
65  throw new InvalidOperationException(@"Internal compiler error: DatabaseSyntaxNode");
66  }
67 
68  return new DatabaseConfiguration(
69  Name,
70  provider.DatabaseProvider,
71  host.Value,
72  port?.Value,
73  database.Value,
74  user.Value,
75  pass.Value,
76  conString?.Value,
77  certFile?.Value,
78  certPass?.Value,
79  timeout?.Value,
80  protocol?.Value,
81  ssl?.Value,
82  create?.Value,
83  upgrade?.Value,
84  import?.Value
85  );
86  }
87 
88  protected override void ValidateMandatories()
89  {
90  CheckIsUnique(new List<Symbol>
91  {
92  Symbol.KeywordDatabaseProvider,
93  Symbol.KeywordName,
94  Symbol.KeywordHost,
95  Symbol.KeywordIp,
96  Symbol.KeywordAddress,
97  Symbol.KeywordHostAddress,
98  Symbol.KeywordPort,
99  Symbol.KeywordUser,
100  Symbol.KeywordPass,
101  Symbol.KeywordCreate,
102  Symbol.KeywordImport,
103  Symbol.KeywordUpgrade,
104  Symbol.KeywordDatabaseName,
105  Symbol.KeywordDatabaseUser,
106  Symbol.KeywordDatabasePass,
107  Symbol.KeywordConString,
108  Symbol.KeywordCertFile,
109  Symbol.KeywordCertPass,
110  Symbol.KeywordConnectionTimeout,
111  Symbol.KeywordProtocol,
112  Symbol.KeywordEnableSsl,
113  Symbol.KeywordSslMode
114  });
115 
116  CheckAllIsPresent(new List<Symbol> {Symbol.KeywordDatabaseProvider});
117 
118  CheckOneIsPresent(new List<Symbol>
119  {
120  Symbol.KeywordIp,
121  Symbol.KeywordHost,
122  Symbol.KeywordAddress,
123  Symbol.KeywordHostAddress
124  });
125 
126  CheckOneIsPresent(new List<Symbol>
127  {
128  Symbol.KeywordName,
129  Symbol.KeywordDatabaseName
130  });
131 
132  CheckOneIsPresent(new List<Symbol>
133  {
134  Symbol.KeywordUser,
135  Symbol.KeywordDatabaseUser
136  });
137 
138  CheckOneIsPresent(new List<Symbol>
139  {
140  Symbol.KeywordPass,
141  Symbol.KeywordDatabasePass
142  });
143  }
144 
145  protected override void ValidateTypes()
146  {
147  CheckTypeIs<DatabaseProviderNode>(Symbol.KeywordDatabaseProvider);
148  CheckTypeIs<StringSettingNode>(Symbol.KeywordName);
149  CheckTypeIs<StringSettingNode>(Symbol.KeywordIp);
150  CheckTypeIs<StringSettingNode>(Symbol.KeywordHost);
151  CheckTypeIs<StringSettingNode>(Symbol.KeywordAddress);
152  CheckTypeIs<StringSettingNode>(Symbol.KeywordHostAddress);
153  CheckTypeIs<IntegerSettingNode>(Symbol.KeywordPort);
154  CheckTypeIs<StringSettingNode>(Symbol.KeywordUser);
155  CheckTypeIs<StringSettingNode>(Symbol.KeywordPass);
156  CheckTypeIs<BooleanSettingNode>(Symbol.KeywordCreate);
157  CheckTypeIs<BooleanSettingNode>(Symbol.KeywordUpgrade);
158  CheckTypeIs<StringSettingNode>(Symbol.KeywordConString);
159  CheckTypeIs<StringSettingNode>(Symbol.KeywordCertFile);
160  CheckTypeIs<StringSettingNode>(Symbol.KeywordCertPass);
161  CheckTypeIs<IntegerSettingNode>(Symbol.KeywordConnectionTimeout);
162  CheckTypeIs<IntegerSettingNode>(Symbol.KeywordProtocol);
163  CheckTypeIs<BooleanSettingNode>(Symbol.KeywordEnableSsl);
164  }
165  }
166 }
override void ValidateTypes()
Override to validates the types in this syntax node.
override DatabaseConfiguration InternalCompile()
override void ValidateMandatories()
Override to validates the mandatory types in this syntax node.