src/utilities/xml/XMLValidator.hpp
| 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 | #ifndef UTILITIES_XML_XMLVALIDATOR_HPP | ||
| 7 | #define UTILITIES_XML_XMLVALIDATOR_HPP | ||
| 8 | |||
| 9 | #include "../UtilitiesAPI.hpp" | ||
| 10 | |||
| 11 | #include "../core/Compare.hpp" | ||
| 12 | #include "../core/Logger.hpp" | ||
| 13 | #include "../core/LogMessage.hpp" | ||
| 14 | #include "../core/Optional.hpp" | ||
| 15 | #include "../core/Path.hpp" | ||
| 16 | #include "../core/StringStreamLogSink.hpp" | ||
| 17 | #include "../bcl/BCLEnums.hpp" | ||
| 18 | |||
| 19 | #include <boost/optional.hpp> | ||
| 20 | |||
| 21 | #include <string> | ||
| 22 | |||
| 23 | namespace openstudio { | ||
| 24 | |||
| 25 | enum class XMLValidatorType | ||
| 26 | { | ||
| 27 | XSD, | ||
| 28 | XSLTSchematron, | ||
| 29 | Schematron | ||
| 30 | }; | ||
| 31 | |||
| 32 | namespace detail { | ||
| 33 | struct XMLInitializer; | ||
| 34 | } | ||
| 35 | |||
| 36 | class UTILITIES_API XMLValidator | ||
| 37 | { | ||
| 38 | public: | ||
| 39 | /** @name Constructors */ | ||
| 40 | //@{ | ||
| 41 | |||
| 42 | /** Constructor for a new validator | ||
| 43 | * | ||
| 44 | * The schemaPath extension is used to determine the type of validation to perform: | ||
| 45 | * - `*.xsd` => XSD | ||
| 46 | * - `*.xml` or `*.sch` => Schematron (convert to XSLT then validate) | ||
| 47 | * - `*.xslt` => Schematron that is already transformed to an XSLT stylesheet */ | ||
| 48 | explicit XMLValidator(const openstudio::path& schemaPath); | ||
| 49 | ~XMLValidator(); | ||
| 50 | ✗ | XMLValidator(const XMLValidator& other) = default; | |
| 51 | ✗ | XMLValidator(XMLValidator&& other) noexcept = default; | |
| 52 | XMLValidator& operator=(const XMLValidator& other) = default; | ||
| 53 | XMLValidator& operator=(XMLValidator&& other) noexcept = default; | ||
| 54 | |||
| 55 | static XMLValidator gbxmlValidator(const VersionString& schemaVersion = VersionString(7, 3)); | ||
| 56 | |||
| 57 | static XMLValidator bclXMLValidator(openstudio::BCLXMLType bclXMLType = openstudio::BCLXMLType::MeasureXML, | ||
| 58 | const VersionString& schemaVersion = VersionString(3, 1)); | ||
| 59 | |||
| 60 | //@} | ||
| 61 | /** @name Getters */ | ||
| 62 | //@{ | ||
| 63 | |||
| 64 | openstudio::path schemaPath() const; | ||
| 65 | |||
| 66 | bool validate(const openstudio::path& xmlPath); | ||
| 67 | |||
| 68 | // Below functions are related to the last call to validate | ||
| 69 | |||
| 70 | bool isValid() const; | ||
| 71 | boost::optional<openstudio::path> xmlPath() const; | ||
| 72 | |||
| 73 | std::vector<LogMessage> errors() const; | ||
| 74 | |||
| 75 | std::vector<LogMessage> warnings() const; | ||
| 76 | |||
| 77 | // Returns all log messages | ||
| 78 | std::vector<LogMessage> logMessages() const; | ||
| 79 | |||
| 80 | /** Only applicable for Schematron validation **/ | ||
| 81 | boost::optional<std::string> fullValidationReport() const; | ||
| 82 | |||
| 83 | //@} | ||
| 84 | /** @name Other */ | ||
| 85 | //@{ | ||
| 86 | |||
| 87 | /** Static helper to convert a Schematron file to an XSLT StyleSheet */ | ||
| 88 | static openstudio::path schematronToXslt(const openstudio::path& schemaPath, const openstudio::path& outputDir); | ||
| 89 | |||
| 90 | //@} | ||
| 91 | |||
| 92 | private: | ||
| 93 | − | REGISTER_LOGGER("openstudio.XMLValidator"); | |
| 94 | |||
| 95 | // Instead of using a useless log sink (it's not like we'll trigger warnings/errors coming from model/ or whatnot) | ||
| 96 | // We just store the log messages directly | ||
| 97 | mutable std::vector<LogMessage> m_logMessages; | ||
| 98 | |||
| 99 | // LOG the message (to console) and store it in m_logMessages | ||
| 100 | void logAndStore(LogLevel logLevel, const std::string& logMessage) const; | ||
| 101 | |||
| 102 | openstudio::path m_schemaPath; | ||
| 103 | boost::optional<openstudio::path> m_xmlPath; | ||
| 104 | |||
| 105 | boost::optional<openstudio::path> m_tempDir; | ||
| 106 | |||
| 107 | XMLValidatorType m_validatorType; | ||
| 108 | |||
| 109 | bool xsdValidate() const; | ||
| 110 | |||
| 111 | bool xsltValidate() const; | ||
| 112 | mutable std::string m_fullValidationReport; | ||
| 113 | |||
| 114 | // reset the state of the XMLValidator between translations | ||
| 115 | void reset(); | ||
| 116 | |||
| 117 | // A static Initializer, as a Meyers Singleton, to know when we need to init/cleanup the libxml2 & libxslt libs | ||
| 118 | // This lazily does the initialization, and clean up will be done completey at the end of the program | ||
| 119 | static detail::XMLInitializer& xmlInitializerInstance(); | ||
| 120 | }; | ||
| 121 | |||
| 122 | /// optional XMLValidator | ||
| 123 | using OptionalXMLValidator = boost::optional<XMLValidator>; | ||
| 124 | |||
| 125 | } // namespace openstudio | ||
| 126 | |||
| 127 | #endif // UTILITIES_XML_VALIDATOR_HPP | ||
| 128 |