NTP Analyzer  0.8.2
Analyze the operation of time servers
Validator.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.IO;
24 using System.Linq;
25 using System.Windows.Forms;
26 using Ntp.Analyzer.Config;
28 using Ntp.Analyzer.Config.Node;
29 
30 namespace Ntp.Analyzer.Validate.Gui
31 {
32  public partial class FormValidator : Form
33  {
34  public FormValidator()
35  {
36  InitializeComponent();
37  file = null;
38  }
39 
40  private string file;
41 
42  private void button1_Click(object sender, EventArgs e)
43  {
44  if (openFileDialog.ShowDialog() != DialogResult.OK)
45  return;
46 
47  file = openFileDialog.FileName;
48  textBoxFile.Text = file;
49  textBoxOutput.Clear();
50  ValidateConfiguration();
51  }
52 
53  private void checkBoxDefaultValues_CheckedChanged(object sender, EventArgs e)
54  {
55  ValidateConfiguration();
56  }
57 
58  private void checkBoxTabulator_CheckedChanged(object sender, EventArgs e)
59  {
60  ValidateConfiguration();
61  }
62 
63  private void checkBoxValidate_CheckedChanged(object sender, EventArgs e)
64  {
65  ValidateConfiguration();
66  }
67 
68  private Configuration LoadConfig(string configFile)
69  {
70  if (!File.Exists(configFile))
71  {
72  textBoxOutput.Text = $"Cannot find configuration file {configFile}";
73  return null;
74  }
75 
76  var reader = new ConfigBuilder(configFile);
77  var config = reader.Execute();
78  if (config != null)
79  return config;
80 
81  textBoxOutput.Lines = reader.Errors.ToArray();
82  return null;
83  }
84 
85  private void ValidateConfiguration()
86  {
87  if (string.IsNullOrEmpty(file))
88  return;
89 
90  textBoxOutput.Clear();
91 
92  Configuration config;
93  try
94  {
95  config = LoadConfig(file);
96  }
97  catch (Exception e)
98  {
99  textBoxOutput.Text = @"Unexpected error while loading configuration file: " + e.Message;
100  return;
101  }
102 
103  if (config == null)
104  return;
105 
106  if (checkBoxValidate.Checked)
107  {
108  textBoxOutput.Text = @"Configuration is valid.";
109  return;
110  }
111 
112  try
113  {
114  var decompiler = new Decompiler
115  {
116  Configuration = config,
117  IndentChar = checkBoxTabulator.Checked ? '\t' : ' ',
118  IndentSize = checkBoxTabulator.Checked ? 1 : 8,
119  ShowDefaultValues = checkBoxDefaultValues.Checked
120  };
121 
122  textBoxOutput.Text = decompiler.Execute();
123  }
124  catch (Exception e)
125  {
126  textBoxOutput.Text = @"Unexpected error while decompiling configuration: " + e.Message;
127  }
128  }
129  }
130 }
void button1_Click(object sender, EventArgs e)
Definition: Validator.cs:42
var e
Definition: bootstrap.min.js:6
Configuration LoadConfig(string configFile)
Definition: Validator.cs:68
void checkBoxDefaultValues_CheckedChanged(object sender, EventArgs e)
Definition: Validator.cs:53
void checkBoxValidate_CheckedChanged(object sender, EventArgs e)
Definition: Validator.cs:63
void checkBoxTabulator_CheckedChanged(object sender, EventArgs e)
Definition: Validator.cs:58