GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 83.7% 237 / 0 / 283
Functions: 96.0% 24 / 0 / 25
Branches: 47.3% 264 / 0 / 558

src/utilities/xml/XMLValidator.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "XMLValidator.hpp"
7 #include "XMLErrors.hpp"
8 #include "XMLUtils.hpp"
9 #include "XMLInitializer.hpp"
10
11 #include "../core/Filesystem.hpp"
12 #include "../core/FilesystemHelpers.hpp"
13 #include "../bcl/BCLEnums.hpp"
14
15 #include <libxml/xmlversion.h>
16 #include <libxml/xmlreader.h>
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
19 #include <libxml/schematron.h>
20
21 #include <libxslt/xslt.h>
22 #include <libxslt/xsltInternals.h>
23 #include <libxslt/transform.h>
24 #include <libxslt/xsltutils.h>
25 #include <libxml/xpath.h>
26 #include <libxml/xpathInternals.h> // BAD_CAST
27
28 #include <src/utilities/embedded_files.hxx>
29
30 #include <fmt/format.h>
31
32 #include <algorithm>
33 #include <iterator>
34
35 namespace openstudio {
36
37 4 xmlDoc* applyEmbeddedXSLT(const std::string& embedded_path, xmlDoc* curdoc, const char** params) {
38
39
1/1
✓ Branch 1 taken 4 times.
4 std::string xlstString = ::openstudio::embedded_files::getFileAsString(embedded_path);
40
1/1
✓ Branch 2 taken 4 times.
4 xmlDoc* xlstDoc = xmlParseDoc(detail::xml_string(xlstString));
41
1/1
✓ Branch 1 taken 4 times.
4 xsltStylesheet* style = xsltParseStylesheetDoc(xlstDoc);
42
1/1
✓ Branch 1 taken 4 times.
4 xmlDoc* res = xsltApplyStylesheet(style, curdoc, params);
43
44
1/1
✓ Branch 1 taken 4 times.
4 xsltFreeStylesheet(style);
45 // xmlFreeDoc(xlstDoc); // This is already freed via xlstFreeStyleSheet because we used xsltParseStylesheetDoc
46
1/1
✓ Branch 1 taken 4 times.
4 xmlFreeDoc(curdoc);
47 4 return res;
48 4 }
49
50 2 xmlDoc* applyEmbeddedXSLTWithImports(xmlDoc* curdoc, const openstudio::path& outputDir, const char** params) {
51
52 // Extract the two files we need
53 2 bool quiet = true;
54
55
3/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
6 ::openstudio::embedded_files::extractFile(":/xml/resources/iso_svrl_for_xslt1.xsl", openstudio::toString(outputDir), quiet);
56
3/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
6 ::openstudio::embedded_files::extractFile(":/xml/resources/iso_schematron_skeleton_for_xslt1.xsl", openstudio::toString(outputDir), quiet);
57
58
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
4 auto schematron_filename_str = openstudio::toString(outputDir / "iso_svrl_for_xslt1.xsl");
59
1/1
✓ Branch 2 taken 2 times.
2 xsltStylesheet* style = xsltParseStylesheetFile(detail::xml_string(schematron_filename_str));
60
61
1/1
✓ Branch 1 taken 2 times.
2 xmlDoc* res = xsltApplyStylesheet(style, curdoc, params);
62
63
1/1
✓ Branch 1 taken 2 times.
2 xsltFreeStylesheet(style);
64
1/1
✓ Branch 1 taken 2 times.
2 xmlFreeDoc(curdoc);
65
66 2 return res;
67 2 }
68
69 2 openstudio::path XMLValidator::schematronToXslt(const openstudio::path& schemaPath, const openstudio::path& outputDir) {
70
71 // We need to potentially initialize the libxml2/libxslt stylesheets since this is a static function
72
1/1
✓ Branch 1 taken 2 times.
2 xmlInitializerInstance();
73
74 // This replicates what happens when you do:
75 // from lxml.isoschematron import Schematron;
76 // s = Schematron(file='schematron.sch', store_xslt=True)
77 // with open('schematron.xslt', 'w') as f:
78 // f.write(str(s._validator_xslt))
79
80
1/1
✓ Branch 1 taken 2 times.
2 auto filename_str = openstudio::toString(schemaPath);
81 2 const auto* filename = filename_str.c_str();
82
1/1
✓ Branch 1 taken 2 times.
2 xmlDoc* schematronXmlDoc = xmlParseFile(filename);
83
84 // For debugging
85 2 constexpr bool saveIntermediates = false;
86
87
3/3
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1 time.
2 if (!openstudio::filesystem::exists(outputDir)) {
88
1/1
✓ Branch 1 taken 1 time.
1 openstudio::filesystem::create_directories(outputDir);
89
2/3
✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
1 } else if (!openstudio::filesystem::is_directory(outputDir)) {
90 ✗ LOG_AND_THROW("Path '" << toString(outputDir) << "' already exists but is not a directory.");
91 }
92
93 2 auto saveXmlDocToFile = [](const openstudio::path& outputPath, xmlDoc* doc) {
94
1/1
✓ Branch 1 taken 2 times.
2 auto save_filename_str = openstudio::toString(outputPath);
95 2 const auto* save_filename = save_filename_str.c_str();
96
1/1
✓ Branch 1 taken 2 times.
2 const int result = xmlSaveFormatFileEnc(save_filename, doc, "UTF-8", 1); // 1 means format=true
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result == -1) {
98 // xmlSaveFormatFileEnc returns the number of bytes written or -1 if it failed
99 ✗ LOG_AND_THROW("Writing to file failed for " << openstudio::toString(outputPath));
100 }
101 2 };
102
103 // Extract
104
105 // If we wanted to pass params, it's in this form where i=name and i+1 is the value, and it's null-terminated
106 // constexpr int nbpairs = 1;
107 // constexpr int nbparams = 2 * nbpairs;
108 // const char * params[nbparams+1]:
109 // params[0] = "name";
110 // params[1] = "value";
111 // params[nbparams] = nullptr;
112
1/1
✓ Branch 1 taken 2 times.
2 xmlSubstituteEntitiesDefault(1);
113
1/1
✓ Branch 1 taken 2 times.
2 xmlLoadExtDtdDefaultValue = 1;
114
115 // include
116
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
4 xmlDoc* withIncludes = applyEmbeddedXSLT(":/xml/resources/iso_dsdl_include.xsl", schematronXmlDoc, nullptr);
117 if constexpr (saveIntermediates) {
118 saveXmlDocToFile(outputDir / "1_withIncludes.xslt", withIncludes);
119 }
120
121 // expand
122
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 xmlDoc* withExpand = applyEmbeddedXSLT(":/xml/resources/iso_dsdl_include.xsl", withIncludes, nullptr);
123 if constexpr (saveIntermediates) {
124 saveXmlDocToFile(outputDir / "2_withExpand.xslt", withIncludes);
125 }
126
127 // compile: this one uses an xsl:import ... not sure how to merge them...
128 // xmlDoc* compiled = applyEmbeddedXSLT(":/xml/resources/iso_svrl_for_xslt1.xsl", withExpand, nullptr);
129
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 xmlDoc* compiled = applyEmbeddedXSLTWithImports(withExpand, outputDir, nullptr);
130
131
4/4
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
4 openstudio::path xsltPath = outputDir / openstudio::toPath(openstudio::toString(schemaPath.stem()) + "_stylesheet.xslt");
132
133
2/3
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if (openstudio::filesystem::is_regular_file(xsltPath)) {
134 ✗ if (!openstudio::filesystem::remove(xsltPath)) {
135 ✗ LOG_AND_THROW("Cannot remove existing file '" << toString(xsltPath) << "'.");
136 }
137 }
138
139 // xsltPath.replace_extension(".xslt");
140
1/1
✓ Branch 1 taken 2 times.
2 saveXmlDocToFile(xsltPath, compiled);
141
142
1/1
✓ Branch 1 taken 2 times.
2 xmlFreeDoc(compiled);
143
8/8
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 17 taken 2 times.
✓ Branch 20 taken 2 times.
✓ Branch 23 taken 2 times.
2 LOG(Trace, "Saved transformed XSLT Stylesheet at '" << toString(xsltPath) << "'.");
144
145 4 return xsltPath;
146 2 }
147
148 535 detail::XMLInitializer& XMLValidator::xmlInitializerInstance() {
149
4/7
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 448 times.
✓ Branch 3 taken 87 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 87 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
535 static detail::XMLInitializer xmlInitializer;
150 535 return xmlInitializer;
151 }
152
153
1/1
✓ Branch 2 taken 533 times.
533 XMLValidator::XMLValidator(const openstudio::path& schemaPath) : m_schemaPath(openstudio::filesystem::system_complete(schemaPath)) {
154
155
1/1
✓ Branch 1 taken 533 times.
533 xmlInitializerInstance();
156
157
2/3
✓ Branch 1 taken 533 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 533 times.
533 if (!openstudio::filesystem::exists(schemaPath)) {
158 ✗ std::string logMessage = fmt::format("Schema '{}' does not exist", toString(schemaPath));
159 ✗ m_logMessages.emplace_back(Fatal, "openstudio.XMLValidator", logMessage);
160 ✗ LOG_AND_THROW(logMessage);
161
2/3
✓ Branch 1 taken 533 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 533 times.
533 } else if (!openstudio::filesystem::is_regular_file(schemaPath)) {
162 ✗ std::string logMessage = fmt::format("Schema '{}' cannot be opened", toString(schemaPath));
163 ✗ m_logMessages.emplace_back(Fatal, "openstudio.XMLValidator", logMessage);
164 ✗ LOG_AND_THROW(logMessage);
165 ✗ }
166
167
2/2
✓ Branch 1 taken 530 times.
✓ Branch 2 taken 3 times.
533 if (schemaPath.extension() == ".xsd") {
168 530 m_validatorType = XMLValidatorType::XSD;
169
2/2
✓ Branch 1 taken 530 times.
✓ Branch 4 taken 530 times.
1060 logAndStore(Trace, "Treating schema as a regular XSD");
170
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 2 times.
3 } else if (schemaPath.extension() == ".xslt") {
171 1 m_validatorType = XMLValidatorType::XSLTSchematron;
172
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
2 logAndStore(Trace, "Treating schema as a XLST StyleSheet that derives from a Schematron.");
173
9/15
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 1 time.
✓ Branch 16 taken 1 time.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
8 } else if ((schemaPath.extension() == ".xml") || (schemaPath.extension() == ".sch")) {
174 1 m_validatorType = XMLValidatorType::Schematron;
175
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 logAndStore(Trace, "Treating schema as a Schematron, converting to an XSLT StyleSheet.");
176 // Let's use a temporary directory for this, so we avoid having two instances trying to write to the same file and we avoid issues where the
177 // directory is write protected.
178
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
1 m_tempDir = openstudio::filesystem::create_temporary_directory("xmlvalidation");
179
2/3
✓ Branch 1 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
1 if (m_tempDir->empty()) {
180 ✗ LOG_AND_THROW(fmt::format("Failed to create a temporary directory for extracting the stylesheets needed to transform the Schematron '{}'",
181 toString(schemaPath)));
182 }
183
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 m_schemaPath = schematronToXslt(m_schemaPath, m_tempDir.get());
184
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
2 logAndStore(Trace, fmt::format("Transformed Schematron to an XSLT Stylesheet and saved it at {}.", toString(m_schemaPath)));
185 } else {
186
1/1
✓ Branch 1 taken 1 time.
2 std::string logMessage = fmt::format("Schema path extension '{}' not supported.", toString(schemaPath.extension()));
187
1/1
✓ Branch 1 taken 1 time.
1 m_logMessages.emplace_back(Fatal, "openstudio.XMLValidator", logMessage);
188
13/13
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 22 taken 1 time.
✓ Branch 25 taken 1 time.
✓ Branch 28 taken 1 time.
✓ Branch 31 taken 1 time.
✓ Branch 34 taken 1 time.
✓ Branch 38 taken 1 time.
✓ Branch 41 taken 1 time.
1 LOG_AND_THROW(logMessage);
189 1 }
190 537 }
191
192 532 XMLValidator::~XMLValidator() {
193
2/2
✓ Branch 1 taken 530 times.
✓ Branch 2 taken 2 times.
1064 if (m_tempDir) {
194 try {
195
2/2
✓ Branch 1 taken 530 times.
✓ Branch 4 taken 530 times.
530 const auto count = openstudio::filesystem::remove_all(m_tempDir.get());
196
1/1
✓ Branch 1 taken 530 times.
1060 logAndStore(Trace, fmt::format("Removed temporary directory with {} files", count));
197 ✗ } catch (const std::exception& e) {
198 ✗ logAndStore(Warn, fmt::format("Error removing temporary directory at {}, Description: {}", toString(m_tempDir.get()), e.what()));
199 ✗ }
200 }
201 532 }
202
203 5 openstudio::path XMLValidator::schemaPath() const {
204 5 return m_schemaPath;
205 }
206
207 45 boost::optional<openstudio::path> XMLValidator::xmlPath() const {
208 45 return m_xmlPath;
209 }
210
211 3745 void XMLValidator::logAndStore(LogLevel logLevel, const std::string& logMessage) const {
212 3745 m_logMessages.emplace_back(logLevel, "openstudio.XMLValidator", logMessage);
213
5/5
✓ Branch 1 taken 3745 times.
✓ Branch 4 taken 3745 times.
✓ Branch 7 taken 3745 times.
✓ Branch 10 taken 3745 times.
✓ Branch 13 taken 3745 times.
3745 LOG(logLevel, logMessage);
214 3745 }
215
216 67 std::vector<LogMessage> XMLValidator::errors() const {
217
218 67 std::vector<LogMessage> result;
219
2/2
✓ Branch 1 taken 67 times.
✓ Branch 6 taken 67 times.
67 std::copy_if(m_logMessages.cbegin(), m_logMessages.cend(), std::back_inserter(result),
220 5594 [](const auto& logMessage) { return logMessage.logLevel() > LogLevel::Warn; });
221
222 67 return result;
223 ✗ }
224
225 29 std::vector<LogMessage> XMLValidator::warnings() const {
226
227 29 std::vector<LogMessage> result;
228
2/2
✓ Branch 1 taken 29 times.
✓ Branch 6 taken 29 times.
29 std::copy_if(m_logMessages.cbegin(), m_logMessages.cend(), std::back_inserter(result),
229 1871 [](const auto& logMessage) { return logMessage.logLevel() == LogLevel::Warn; });
230
231 29 return result;
232 ✗ }
233
234 24 bool XMLValidator::isValid() const {
235
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 23 times.
24 if (!m_xmlPath) {
236
2/2
✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
2 logAndStore(Warn, fmt::format("Nothing has yet been validated against '{}'", toString(m_schemaPath)));
237 1 return false;
238 }
239
1/1
✓ Branch 1 taken 23 times.
23 return errors().empty();
240 }
241
242 532 void XMLValidator::reset() {
243
244 532 m_logMessages.clear();
245
246 532 m_xmlPath = boost::none;
247
248 532 m_fullValidationReport.clear();
249 532 }
250
251 532 bool XMLValidator::validate(const openstudio::path& xmlPath) {
252
253 532 reset();
254
255
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 532 times.
532 if (!openstudio::filesystem::exists(xmlPath)) {
256 ✗ std::string logMessage = fmt::format("XML File '{}' does not exist.", toString(xmlPath));
257 ✗ m_logMessages.emplace_back(Fatal, "openstudio.XMLValidator", logMessage);
258 ✗ LOG_AND_THROW(logMessage);
259
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 532 times.
532 } else if (!openstudio::filesystem::is_regular_file(xmlPath)) {
260 ✗ std::string logMessage = fmt::format("XML File '{}' cannot be opened.", toString(xmlPath));
261 ✗ m_logMessages.emplace_back(Fatal, "openstudio.XMLValidator", logMessage);
262 ✗ LOG_AND_THROW(logMessage);
263 ✗ }
264
265
12/21
✓ Branch 1 taken 532 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 531 times.
✓ Branch 7 taken 1 time.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 time.
✓ Branch 12 taken 1 time.
✓ Branch 13 taken 531 times.
✓ Branch 15 taken 1 time.
✓ Branch 16 taken 531 times.
✓ Branch 18 taken 532 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 531 times.
✓ Branch 22 taken 1 time.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
1600 if (xmlPath.extension() == ".xml" || xmlPath.extension() == ".sch" || xmlPath.extension() == ".xsd") {
266
1/1
✓ Branch 1 taken 531 times.
531 auto t_xmlPath = openstudio::filesystem::system_complete(xmlPath);
267
1/1
✓ Branch 1 taken 531 times.
531 m_xmlPath = t_xmlPath;
268 531 } else {
269
1/1
✓ Branch 1 taken 1 time.
2 std::string logMessage = fmt::format("XML path extension '{}' not supported.", toString(xmlPath.extension()));
270
1/1
✓ Branch 1 taken 1 time.
1 m_logMessages.emplace_back(Fatal, "openstudio.XMLValidator", logMessage);
271
13/13
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 22 taken 1 time.
✓ Branch 25 taken 1 time.
✓ Branch 28 taken 1 time.
✓ Branch 31 taken 1 time.
✓ Branch 34 taken 1 time.
✓ Branch 38 taken 1 time.
✓ Branch 41 taken 1 time.
1 LOG_AND_THROW(logMessage);
272 1 }
273
274
2/2
✓ Branch 0 taken 527 times.
✓ Branch 1 taken 4 times.
531 if (m_validatorType == XMLValidatorType::XSD) {
275 527 return xsdValidate();
276
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 } else if ((m_validatorType == XMLValidatorType::XSLTSchematron) || (m_validatorType == XMLValidatorType::Schematron)) {
277 4 return xsltValidate();
278 }
279
280 ✗ return false;
281 }
282
283 527 bool XMLValidator::xsdValidate() const {
284
285 // schema path
286
1/1
✓ Branch 1 taken 527 times.
527 auto schema_filename_str = toString(m_schemaPath);
287 527 const auto* schema_filename = schema_filename_str.c_str();
288
289 // xml path
290
2/2
✓ Branch 1 taken 527 times.
✓ Branch 4 taken 527 times.
527 auto xml_filename_str = toString(m_xmlPath.get());
291 527 const auto* xml_filename = xml_filename_str.c_str();
292
293 // schema parser ptr
294
1/1
✓ Branch 1 taken 527 times.
527 xmlSchemaParserCtxt* parser_ctxt = xmlSchemaNewParserCtxt(schema_filename);
295
296 // set parser errors
297 527 detail::ErrorCollector schemaParserErrorCollector;
298
1/1
✓ Branch 1 taken 527 times.
527 xmlSchemaSetParserErrors(parser_ctxt, detail::callback_messages_error, detail::callback_messages_warning, &schemaParserErrorCollector);
299
300 // schema parser
301
1/1
✓ Branch 1 taken 527 times.
527 xmlSchema* schema = xmlSchemaParse(parser_ctxt);
302
1/1
✓ Branch 1 taken 527 times.
527 xmlSchemaValidCtxt* ctxt = xmlSchemaNewValidCtxt(schema);
303
304 // set valid errors
305 527 detail::ErrorCollector schemaValidErrorCollector;
306
1/1
✓ Branch 1 taken 527 times.
527 xmlSchemaSetValidErrors(ctxt, detail::callback_messages_error, detail::callback_messages_warning, &schemaValidErrorCollector);
307
308 527 detail::ErrorCollector parseFileErrorCollector;
309
1/1
✓ Branch 1 taken 527 times.
527 xmlSetStructuredErrorFunc(&parseFileErrorCollector, detail::callback_messages_structured_error);
310
1/1
✓ Branch 1 taken 527 times.
527 xmlSetGenericErrorFunc(&parseFileErrorCollector, detail::callback_messages_error);
311
312 // xml doc ptr
313
1/1
✓ Branch 1 taken 527 times.
527 xmlDoc* doc = xmlParseFile(xml_filename);
314
315 // validate doc
316
1/1
✓ Branch 1 taken 527 times.
527 int ret = xmlSchemaValidateDoc(ctxt, doc);
317 527 bool result = false;
318
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 486 times.
527 if (ret > 0) {
319 // LOG(Fatal, "Valid instance " << toString(m_xmlPath.get()) << " failed to validate against " << toString(m_schemaPath));
320 41 result = false;
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 486 times.
486 } else if (ret < 0) {
322 ✗ logAndStore(Fatal,
323 ✗ fmt::format("Valid instance '{}' got internal error validating against '{}'", toString(m_xmlPath.get()), toString(m_schemaPath)));
324 ✗ result = false;
325 } else {
326 486 result = true;
327 }
328
329
1/1
✓ Branch 4 taken 527 times.
1054 m_logMessages.reserve(m_logMessages.size() + schemaValidErrorCollector.logMessages.size() + schemaParserErrorCollector.logMessages.size()
330 527 + parseFileErrorCollector.logMessages.size());
331
332
1/2
✗ Branch 4 not taken.
✓ Branch 5 taken 527 times.
527 for (const auto& logMessage : schemaValidErrorCollector.logMessages) {
333 ✗ logAndStore(logMessage.logLevel(), "xsdValidate.schemaValidError: " + logMessage.logMessage());
334 }
335
336
1/2
✗ Branch 4 not taken.
✓ Branch 5 taken 527 times.
527 for (const auto& logMessage : schemaParserErrorCollector.logMessages) {
337 ✗ logAndStore(logMessage.logLevel(), "xsdValidate.schemaParserError: " + logMessage.logMessage());
338 }
339
340
2/2
✓ Branch 4 taken 2677 times.
✓ Branch 5 taken 527 times.
3204 for (const auto& logMessage : parseFileErrorCollector.logMessages) {
341
4/4
✓ Branch 1 taken 2677 times.
✓ Branch 4 taken 2677 times.
✓ Branch 7 taken 2677 times.
✓ Branch 10 taken 2677 times.
2677 logAndStore(logMessage.logLevel(), "xsdValidate.parseFileError: " + logMessage.logMessage());
342 }
343
344 // free
345
1/1
✓ Branch 1 taken 527 times.
527 xmlSchemaFreeParserCtxt(parser_ctxt);
346
1/1
✓ Branch 1 taken 527 times.
527 xmlSchemaFreeValidCtxt(ctxt);
347
1/1
✓ Branch 1 taken 527 times.
527 xmlSchemaFree(schema);
348
349
1/1
✓ Branch 1 taken 527 times.
527 xmlFreeDoc(doc);
350
351 527 return result;
352 527 }
353
354 4 std::vector<std::string> processXSLTApplyResult(xmlDoc* res) {
355
356 4 xmlXPathContext* xpathCtx = nullptr;
357 4 xmlXPathObject* xpathObj = nullptr;
358
359 4 const char* xpathExpr = "//svrl:failed-assert";
360 /* Create xpath evaluation context */
361
1/1
✓ Branch 1 taken 4 times.
4 xpathCtx = xmlXPathNewContext(res);
362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (xpathCtx == nullptr) {
363 ✗ throw std::runtime_error("Error: unable to create new XPath context");
364 }
365
366 // namesapce
367 // xmlns:svrl="http://purl.oclc.org/dsdl/svrl"
368
2/3
✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
4 if (xmlXPathRegisterNs(xpathCtx, BAD_CAST "svrl", BAD_CAST "http://purl.oclc.org/dsdl/svrl") != 0) {
369 ✗ throw std::runtime_error("Error: unable to register NS svrl with prefix");
370 }
371
372 /* Evaluate xpath expression */
373 // cppcheck-suppress cstyleCast
374
1/1
✓ Branch 1 taken 4 times.
4 xpathObj = xmlXPathEvalExpression((const xmlChar*)xpathExpr, xpathCtx);
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (xpathObj == nullptr) {
376 ✗ xmlXPathFreeContext(xpathCtx);
377 ✗ throw std::runtime_error(fmt::format("Error: unable to evaluate xpath expression '{}'\n", xpathExpr));
378 }
379
380 4 std::vector<std::string> result;
381
382
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
4 if (xmlXPathNodeSetIsEmpty(xpathObj->nodesetval)) {
383 ✗ return {};
384 } else {
385
386 4 xmlNodeSet* nodeset = xpathObj->nodesetval;
387
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (int i = 0; i < nodeset->nodeNr; i++) {
388 4 xmlNode* error_node = nodeset->nodeTab[i];
389 4 error_node = error_node->xmlChildrenNode;
390
1/1
✓ Branch 1 taken 4 times.
4 detail::xmlchar_helper error_message(xmlNodeListGetString(res, error_node->xmlChildrenNode, 1));
391
392
1/1
✓ Branch 2 taken 4 times.
4 result.emplace_back(error_message.get());
393 // while (error_node != nullptr) {
394 // if ((xmlStrcmp(error_node->name, (const xmlChar*)"text") == 0)) {
395 // keyword = xmlNodeListGetString(doc, error_node->xmlChildrenNode, 1);
396 // printf("text: %s\n", keyword);
397 // }
398
399 // error_node = error_node->next;
400 // }
401 4 }
402 }
403
404 /* Cleanup of XPath data */
405
1/1
✓ Branch 1 taken 4 times.
4 xmlXPathFreeObject(xpathObj);
406
1/1
✓ Branch 1 taken 4 times.
4 xmlXPathFreeContext(xpathCtx);
407
408 4 return result;
409 4 }
410
411 4 std::string dumpXSLTApplyResultToString(xmlDoc* res, xsltStylesheet* style) {
412
413 4 xmlChar* xml_string = nullptr;
414 4 int xml_string_length = 0;
415
416 4 std::string result;
417
418
2/3
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
4 if (xsltSaveResultToString(&xml_string, &xml_string_length, res, style) == 0) {
419
420 4 detail::xmlchar_helper helper(xml_string);
421
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (xml_string_length > 0) {
422
2/2
✓ Branch 1 taken 4 times.
✓ Branch 5 taken 4 times.
4 result.assign(helper.get(), detail::checked_size_t_cast(xml_string_length));
423 }
424 4 }
425
426 8 return result;
427 ✗ }
428
429 4 bool XMLValidator::xsltValidate() const {
430
431
1/1
✓ Branch 1 taken 4 times.
4 xmlSubstituteEntitiesDefault(1);
432
1/1
✓ Branch 1 taken 4 times.
4 xmlLoadExtDtdDefaultValue = 1;
433
434
1/1
✓ Branch 1 taken 4 times.
4 auto schematron_filename_str = openstudio::toString(m_schemaPath);
435
1/1
✓ Branch 2 taken 4 times.
4 xsltStylesheet* style = xsltParseStylesheetFile(detail::xml_string(schematron_filename_str));
436
437
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
4 auto filename_str = openstudio::toString(m_xmlPath.get());
438 4 const auto* filename = filename_str.c_str();
439
1/1
✓ Branch 1 taken 4 times.
4 xmlDoc* doc = xmlParseFile(filename);
440
1/1
✓ Branch 1 taken 4 times.
4 xmlDoc* res = xsltApplyStylesheet(style, doc, nullptr);
441
442 // Dump result of xlstApply
443
1/1
✓ Branch 1 taken 4 times.
4 m_fullValidationReport = dumpXSLTApplyResultToString(res, style);
444 // fmt::print("\n====== Full Validation Report =====\n\n{}", m_fullValidationReport);
445 // xsltSaveResultToFile(stdout, res, style);
446
447
1/1
✓ Branch 1 taken 4 times.
4 std::vector<std::string> errors = processXSLTApplyResult(res);
448
2/2
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 4 times.
8 for (const auto& error : errors) {
449
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
4 logAndStore(Error, "xsltValidate: " + error);
450 }
451
452 /* dump the resulting document */
453 // xmlDocDump(stdout, res);
454
455
1/1
✓ Branch 1 taken 4 times.
4 xsltFreeStylesheet(style);
456
1/1
✓ Branch 1 taken 4 times.
4 xmlFreeDoc(res);
457
1/1
✓ Branch 1 taken 4 times.
4 xmlFreeDoc(doc);
458
459 8 return errors.empty();
460 4 }
461
462 8 boost::optional<std::string> XMLValidator::fullValidationReport() const {
463
464
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 if ((m_validatorType == XMLValidatorType::Schematron) || (m_validatorType == XMLValidatorType::XSLTSchematron)) {
465 8 return m_fullValidationReport;
466 }
467
468 ✗ return boost::none;
469 }
470
471 ✗ std::vector<LogMessage> XMLValidator::logMessages() const {
472 ✗ return m_logMessages;
473 }
474
475 52 XMLValidator XMLValidator::gbxmlValidator(const VersionString& schemaVersion) {
476
2/2
✓ Branch 1 taken 52 times.
✓ Branch 4 taken 52 times.
52 const auto tmpDir = openstudio::filesystem::create_temporary_directory("xmlvalidation");
477
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 52 times.
52 if (tmpDir.empty()) {
478 ✗ LOG_AND_THROW("Failed to create a temporary directory for extracting the embedded path");
479 }
480
3/4
✓ Branch 1 taken 52 times.
✓ Branch 4 taken 52 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 52 times.
52 if (schemaVersion != VersionString(7, 3)) {
481 ✗ LOG_AND_THROW("Unexpected gbXML Schema Version: accepted = [7.03]");
482 }
483 52 const bool quiet = true;
484
2/2
✓ Branch 1 taken 52 times.
✓ Branch 4 taken 52 times.
52 std::string schemaName = fmt::format("GreenBuildingXML_Ver{}.{:02}.xsd", schemaVersion.major(), schemaVersion.minor());
485
2/2
✓ Branch 1 taken 52 times.
✓ Branch 5 taken 52 times.
104 ::openstudio::embedded_files::extractFile(fmt::format(":/xml/resources/{}", schemaName), openstudio::toString(tmpDir), quiet);
486
2/2
✓ Branch 1 taken 52 times.
✓ Branch 4 taken 52 times.
104 auto validator = XMLValidator(tmpDir / schemaName);
487
1/1
✓ Branch 1 taken 52 times.
52 validator.m_tempDir = tmpDir;
488 104 return validator;
489 52 }
490
491 477 XMLValidator XMLValidator::bclXMLValidator(openstudio::BCLXMLType bclXMLType, const VersionString& schemaVersion) {
492
493
2/2
✓ Branch 1 taken 477 times.
✓ Branch 4 taken 477 times.
477 const auto tmpDir = openstudio::filesystem::create_temporary_directory("xmlvalidation");
494
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 477 times.
477 if (tmpDir.empty()) {
495 ✗ LOG_AND_THROW("Failed to create a temporary directory for extracting the embedded path");
496 }
497
498
1/1
✓ Branch 1 taken 477 times.
477 int schemaVersionMajor = schemaVersion.major();
499
1/1
✓ Branch 1 taken 477 times.
477 int schemaVersionMinor = schemaVersion.minor();
500
501
2/4
✓ Branch 0 taken 477 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 477 times.
477 if (schemaVersionMajor < 2 || schemaVersionMajor > 3) {
502 ✗ LOG_AND_THROW("Unknown schema major version " << schemaVersionMajor << ", accepted = [2, 3]");
503 }
504
4/6
✓ Branch 0 taken 444 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 444 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 444 times.
477 if ((schemaVersionMinor != 0) && !((schemaVersionMajor == 3) && (schemaVersionMinor == 1))) {
505 ✗ LOG_AND_THROW("Unknown schema version combination " << schemaVersionMajor << "." << schemaVersionMinor << ", accepted = [2.0, 3.0, 3.1]");
506 }
507
508 477 std::string schemaName;
509
2/3
✓ Branch 1 taken 477 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 477 times.
477 if (bclXMLType == BCLXMLType::ComponentXML) {
510 ✗ schemaName = "component";
511
2/3
✓ Branch 1 taken 477 times.
✓ Branch 4 taken 477 times.
✗ Branch 5 not taken.
477 } else if (bclXMLType == BCLXMLType::MeasureXML) {
512
1/1
✓ Branch 1 taken 477 times.
477 schemaName = "measure";
513 } else {
514 ✗ LOG_AND_THROW("Unknown BCLXMLType " << bclXMLType.valueName());
515 }
516 954 schemaName = fmt::format("{}_v{}.{}.xsd", schemaName, schemaVersionMajor, schemaVersionMinor);
517
518 477 const bool quiet = true;
519
2/2
✓ Branch 1 taken 477 times.
✓ Branch 5 taken 477 times.
954 ::openstudio::embedded_files::extractFile(fmt::format(":/xml/resources/bcl/{}", schemaName), openstudio::toString(tmpDir), quiet);
520
2/2
✓ Branch 1 taken 477 times.
✓ Branch 4 taken 477 times.
954 auto validator = XMLValidator(tmpDir / schemaName);
521
1/1
✓ Branch 1 taken 477 times.
477 validator.m_tempDir = tmpDir;
522 954 return validator;
523 477 }
524
525 } // namespace openstudio
526