-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
423 lines (346 loc) · 13.8 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using Clarotech.Verifier.classes;
using Clarotech.Verifier.Properties;
using Hl7.Fhir.Model;
using Hl7.Fhir.Serialization;
using Hl7.Fhir.Specification.Source;
using Hl7.Fhir.Utility;
using Hl7.Fhir.Validation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Clarotech.Verifier
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
btnErrors.Tag = "OFF";
btnWarnings.Tag = "OFF";
btnMessages.Tag = "OFF";
btnError_Click(btnErrors, null);
btnWarning_Click(btnWarnings, null);
btnMessage_Click(btnMessages, null);
// Create a resource resolver that searches for the core resources in 'specification.zip', which comes with the .NET FHIR Specification NuGet package
// We create a source that takes its contents from a ZIP file (in this case the default 'specification.zip'). We decorate that source by encapsulating
// it in a CachedResolver, which speeds up access by caching conformance resources once we got them from the large files in the ZIP.
CoreSource = new CachedResolver(ZipSource.CreateValidationSource());
}
internal IResourceResolver DirectorySource;
internal IResourceResolver CoreSource;
internal IResourceResolver CombinedSource;
internal bool includeErrors;
internal bool includeWarnings;
internal bool includeMessages;
internal ExampleFiles exampleList;
internal ExampleFile result;
private void refreshProfileSource()
{
try
{
var profilePath = txtProfileDirectory.Text;
if (!String.IsNullOrEmpty(profilePath) && Directory.Exists(profilePath))
{
// We not only have a source for core data, we also read data from a user-specified directory. We also cache the contents of this source, like
// we did with the CoreSource above.
DirectorySource = new CachedResolver(new DirectorySource(profilePath, includeSubdirectories: true));
// Finally, we combine both sources, so we will find profiles both from the core zip as well as from the directory.
// By mentioning the directory source first, anything in the user directory will override what is in the core zip.
CombinedSource = new MultiResolver(DirectorySource, CoreSource);
}
else
CombinedSource = CoreSource;
}
catch (Exception e)
{
MessageBox.Show($"Composing the profile source failed: {e.Message}", "Profile source");
}
}
private void txtProfileDirectory_TextChanged(object sender, EventArgs e)
{
refreshProfileSource();
}
private void btnReload_Click(object sender, EventArgs e)
{
refreshProfileSource();
}
private void MainForm_Load(object sender, EventArgs e)
{
refreshProfileSource();
SizeLastColumn(listView1);
}
private void btnOpenProfileDir_Click(object sender, EventArgs e)
{
//if (!String.IsNullOrEmpty(txtProfileDirectory.Text))
//{
folderBrowserDialog = new FolderBrowserDialog();
folderBrowserDialog.SelectedPath = txtProfileDirectory.Text;
folderBrowserDialog.Description = "Select folder containing the profiles";
DialogResult result = folderBrowserDialog.ShowDialog();
if (result == DialogResult.OK)
txtProfileDirectory.Text = folderBrowserDialog.SelectedPath;
//}
}
private void btnOpenExampleDir_Click(object sender, EventArgs e)
{
folderBrowserDialog = new FolderBrowserDialog();
folderBrowserDialog.SelectedPath = txtExampleDirectory.Text;
folderBrowserDialog.Description = "Select folder containing the examples";
DialogResult result = folderBrowserDialog.ShowDialog();
if (result == DialogResult.OK)
{
txtExampleDirectory.Text = folderBrowserDialog.SelectedPath;
populateFileList(folderBrowserDialog.SelectedPath);
}
}
private void populateFileList(string dir)
{
if (Directory.Exists(dir))
{
string[] array2 = Directory.GetFiles(dir, "*.xml");
exampleList = new ExampleFiles();
exampleList.files = new List<ExampleFile>();
foreach (var item in array2)
{
ExampleFile ef = new ExampleFile();
ef.fileName = Path.GetFileName(item);
ef.fullFileName = item;
ef.status = "Waiting..";
ef.validationResult = string.Empty;
ef.fileContents = File.ReadAllText(item);
exampleList.files.Add(ef);
}
}
else
{
exampleList = null;
}
result = null;
populateLog(result);
}
private void loadResultsList()
{
if (!String.IsNullOrEmpty(txtExampleDirectory.Text))
{
populateFileList(txtExampleDirectory.Text);
refreshResultsUI();
}
}
private void refreshResultsUI()
{
lvResults.Items.Clear();
if (exampleList != null)
{
foreach (var item in exampleList.files)
{
if (item.status == "Waiting..")
{
lvResults.Items.Add(
new ListViewItem(
new string[] {
item.fileName,
item.status,
"..",
"..",
".."
}, 4));
}
else
{
lvResults.Items.Add(
new ListViewItem(
new string[] {
item.fileName,
item.status,
item.countErrors.ToString(),
item.countWarnings.ToString(),
item.countMessages.ToString()
}, 4));
}
}
lvResults.Columns[1].Width = -2;
}
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
Settings.Default.Save();
}
private void btnValidate_Click(object sender, EventArgs e)
{
refreshProfileSource();
// The validator generates an OperationOutcome as output;
result = null;
// Configure the validator based on the user's settings
// This includes a reference to the resolver that we have constructed in previous methods
// and which helps the validator to look for profiles
ValidationSettings settings = ValidationSettings.Default;
settings.EnableXsdValidation = chkXsdValidation.Checked;
settings.Trace = chkShowTraceInfo.Checked;
settings.ResourceResolver = this.CombinedSource;
if (exampleList != null)
{
foreach (var item in exampleList.files)
{
var validator = new Validator(settings);
var reader = SerializationUtil.XmlReaderFromXmlText(item.fileContents);
item.result = validator.Validate(reader);
item.countMessages = item.result.Issue.Count(p => p.Code.ToString() == "Informational");
item.countErrors = item.result.Errors;
item.countWarnings = item.result.Warnings;
if (item.result.Errors > 0)
{
item.status = "FAILURE";
}
else
{
item.status = "SUCCESS";
}
item.validationResult = $"Errors { item.countErrors } Warnings {item.countWarnings} Messages{item.countMessages}";
}
// Populate
result = exampleList.files[0];
populateLog(result);
refreshResultsUI();
lvResults.Select();
lvResults.Items[0].Selected = true;
lvResults.Items[0].Focused = true;
}
}
private void populateLog(ExampleFile res)
{
if (res != null && res.result != null)
{
txtOutcome.Text = res.result.ToString();
var mCount = res.result.Issue.Count(p => p.Code.ToString() == "Informational");
btnMessages.Text = mCount + " Messages";
btnErrors.Text = res.result.Errors + " Errors";
btnWarnings.Text = res.result.Warnings + " Warnings";
listView1.Items.Clear();
int img = 0;
bool show = false;
foreach (var item in res.result.Issue)
{
var xx = item.Code.ToString();
show = false;
switch (xx)
{
case "Invalid":
img = 1;
if (includeErrors) show = true;
break;
case "Informational":
img = 3;
if (includeMessages) show = true;
break;
case "Incomplete":
img = 0;
if (includeWarnings) show = true;
break;
default:
img = 0;
show = true;
break;
}
if (item.Details.Text.Contains("FhirPath expression")) show = false;
if (show)
{
listView1.Items.Add(
new ListViewItem(
new string[] {
"",
xx,
item.LocationElement[0].ToString(),
item.Details.Text
}, img));
}
}
listView1.Columns[0].Width = -2;
listView1.Columns[1].Width = -2;
listView1.Columns[2].Width = -2;
listView1.Columns[3].Width = -2;
}
else
{
listView1.Items.Clear();
btnMessages.Text = "0 Messages";
btnErrors.Text = "0 Errors";
btnWarnings.Text = "0 Warnings";
}
}
private void btnError_Click(object sender, EventArgs e)
{
Button thisButton = toggle((Button)sender);
includeErrors = (thisButton.Tag.ToString() == "ON");
populateLog(result);
}
private void btnWarning_Click(object sender, EventArgs e)
{
Button thisButton = toggle((Button)sender);
includeWarnings = (thisButton.Tag.ToString() == "ON");
populateLog(result);
}
private void btnMessage_Click(object sender, EventArgs e)
{
Button thisButton = toggle((Button)sender);
includeMessages = (thisButton.Tag.ToString() == "ON");
populateLog(result);
}
private Button toggle(Button thisButton)
{
if (thisButton.Tag.ToString() == "ON")
{
thisButton.Tag = "OFF";
thisButton.FlatAppearance.BorderSize = 0;
}
else
{
thisButton.Tag = "ON";
thisButton.FlatAppearance.BorderSize = 2;
}
return thisButton;
}
private void listView1_Resize(object sender, System.EventArgs e)
{
SizeLastColumn((ListView)sender);
}
private void SizeLastColumn(ListView lv)
{
lv.Columns[lv.Columns.Count - 1].Width = -2;
}
private void txtExampleDirectory_TextChanged(object sender, EventArgs e)
{
string dir = ((TextBox)sender).Text;
populateFileList(dir);
refreshResultsUI();
}
private void MainForm_Shown(object sender, EventArgs e)
{
string dir = txtExampleDirectory.Text;
populateFileList(dir);
refreshResultsUI();
}
private void lvResults_SelectedIndexChanged(object sender, EventArgs e)
{
ListView lv = (ListView)sender;
if (lv.SelectedItems.Count > 0)
{
if (((ListView)sender).FocusedItem == null)
{
}
else
{
int sel = ((ListView)sender).FocusedItem.Index;
result = exampleList.files[sel];
populateLog(result);
}
}
}
}
}