-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
DataSheetValidationHandler.cs
60 lines (46 loc) · 1.79 KB
/
DataSheetValidationHandler.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
namespace Vezel.Novadrop;
internal sealed class DataSheetValidationHandler
{
public bool HasProblems => _problems.Count != 0;
private readonly List<(FileInfo File, int, int, XmlSeverityType, string)> _problems = new();
public void Print()
{
if (_problems.Count == 0)
return;
Log.WriteLine();
foreach (var fileGroup in _problems.GroupBy(tup => tup.File.Name))
{
var shownProblems = fileGroup.Take(10).ToArray();
Log.WriteLine($"{fileGroup.Key}:");
foreach (var (_, line, col, severity, msg) in shownProblems)
{
var (type, color) = severity switch
{
XmlSeverityType.Error => ('E', "red"),
XmlSeverityType.Warning => ('W', "yellow"),
_ => throw new UnreachableException(),
};
Log.MarkupLineInterpolated($" [[[{color}]{type}[/]]] ([blue]{line}[/],[blue]{col}[/]): {msg}");
}
var remainingProblems = fileGroup.Count() - shownProblems.Length;
if (remainingProblems != 0)
Log.MarkupLineInterpolated($" ... [darkorange]{remainingProblems}[/] more problem(s) ...");
}
Log.WriteLine();
}
public void HandleException(FileInfo file, XmlException exception)
{
lock (_problems)
_problems.Add(
(file, exception.LineNumber, exception.LinePosition, XmlSeverityType.Error, exception.Message));
}
public ValidationEventHandler GetEventHandlerFor(FileInfo file)
{
return (_, e) =>
{
var ex = e.Exception;
lock (_problems)
_problems.Add((file, ex.LineNumber, ex.LinePosition, e.Severity, e.Message));
};
}
}