-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.cs
More file actions
155 lines (137 loc) · 3.77 KB
/
validator.cs
File metadata and controls
155 lines (137 loc) · 3.77 KB
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
#!/usr/bin/env dotnet run
#:package DocumentFormat.OpenXml@3.0.2
#pragma warning disable
using System.IO.Compression;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using System.Xml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
var result = new List<object>();
if (args.Length == 0)
{
Console.WriteLine("Usage: dotnet run validator.cs <file.xlsx>");
Environment.Exit(1);
}
var file = args[0];
if (!File.Exists(file))
{
Console.WriteLine("File not found.");
Environment.Exit(1);
}
// ------------------------
// 1. ZIP + XML inspection
// ------------------------
try
{
using var zip = ZipFile.OpenRead(file);
foreach (var entry in zip.Entries)
{
try
{
using var stream = entry.Open();
using var ms = new MemoryStream();
stream.CopyTo(ms);
ms.Position = 0;
if (entry.FullName.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
{
try
{
var settings = new XmlReaderSettings
{
DtdProcessing = DtdProcessing.Prohibit,
IgnoreWhitespace = true,
IgnoreComments = true
};
ms.Position = 0;
using var reader = XmlReader.Create(ms, settings);
while (reader.Read()) { }
}
catch (Exception ex)
{
result.Add(new
{
type = "xml",
entry = entry.FullName,
error = ex.Message
});
}
}
}
catch (Exception ex)
{
result.Add(new
{
type = "zip-entry",
entry = entry.FullName,
error = ex.Message
});
}
}
}
catch (Exception ex)
{
result.Add(new
{
type = "zip",
error = ex.Message
});
PrintAndExit(result);
}
// ------------------------
// 2. Attempt OpenXML validation if possible
// ------------------------
try
{
using var doc = SpreadsheetDocument.Open(file, false);
var validator = new OpenXmlValidator();
var errors = validator.Validate(doc);
foreach (var e in errors)
{
result.Add(new
{
type = "openxml",
description = e.Description,
part = e.Part?.Uri?.ToString(),
path = e.Path?.XPath,
errorType = e.ErrorType.ToString(),
node = e.Node?.OuterXml,
relatedNode = e.RelatedNode?.OuterXml
});
}
}
catch (Exception ex)
{
// Report all nested exceptions for OpenXML
int level = 0;
var current = ex;
while (current != null)
{
result.Add(new
{
type = "openxml-exception",
level,
exception = current.GetType().Name,
message = current.Message,
innerMessage = ex.InnerException?.Message
});
current = current.InnerException;
level++;
}
}
// ------------------------
// 3. Output JSON + exit code
// ------------------------
PrintAndExit(result);
// ------------------------
void PrintAndExit(List<object> result)
{
var options = new JsonSerializerOptions
{
WriteIndented = true,
TypeInfoResolver = new DefaultJsonTypeInfoResolver()
};
string json = JsonSerializer.Serialize(result, options);
Console.WriteLine(json);
Environment.Exit(result.Count == 0 ? 0 : 1);
}