24 #include <QXmlSchemaValidator> 33 m_xsdData(QByteArray()),
43 m_xsdFile(QStringLiteral(
"")),
59 if (!QFile::exists(m_xmlFile))
61 LOG(VB_GENERAL, LOG_ERR, QString(
"Xml file '%1' does not exist").arg(m_xmlFile));
66 if (!xml.open(QIODevice::ReadOnly))
68 LOG(VB_GENERAL, LOG_ERR, QString(
"Failed to open Xml file '%1'").arg(m_xmlFile));
72 bool xsdvalid =
false;
74 schema.setMessageHandler(
this);
77 if (!m_xsdFile.isEmpty())
79 if (!QFile::exists(m_xsdFile))
81 LOG(VB_GENERAL, LOG_ERR, QString(
"XSD file '%1' does not exist").arg(m_xsdFile));
86 if (!xsd.open(QIODevice::ReadOnly))
88 LOG(VB_GENERAL, LOG_ERR, QString(
"Failed to open XSD file '%1'").arg(m_xsdFile));
92 xsdvalid = schema.load(&xsd);
95 else if (!m_xsdData.isEmpty())
97 xsdvalid = schema.load(m_xsdData);
101 LOG(VB_GENERAL, LOG_ERR,
"No XSD file or data specified");
106 LOG(VB_GENERAL, LOG_ERR, QString(
"XSD schema '%1' is not valid").arg(m_xsdFile));
112 QXmlSchemaValidator validator(schema);
113 validator.setMessageHandler(
this);
114 if (!validator.validate(&xml))
117 LOG(VB_GENERAL, LOG_ERR, QString(
"Failed to validate Xml from '%1'").arg(m_xmlFile));
130 const QUrl &Identifier,
const QSourceLocation &SourceLocation)
136 QString desc = Description;
137 desc.remove(QRegExp(
"<[^>]*>"));
138 if (!m_xsdDone && m_xsdFile.isEmpty())
140 LOG(VB_GENERAL, (Type == QtFatalMsg) ? LOG_ERR : LOG_WARNING, QString(
"Error in XSD at line %1: '%2'")
141 .arg(SourceLocation.line()).arg(desc));
145 QString file = m_xsdDone ? m_xsdFile : m_xmlFile;
146 LOG(VB_GENERAL, (Type == QtFatalMsg) ? LOG_ERR : LOG_WARNING, QString(
"Error in file '%1' at line %2: '%3'")
147 .arg(file).arg(SourceLocation.line()).arg(desc));
void handleMessage(QtMsgType Type, const QString &Description, const QUrl &Identifier, const QSourceLocation &SourceLocation)
~TorcXmlValidator()=default
#define LOG(_MASK_, _LEVEL_, _STRING_)
bool Validated(void) const
TorcXmlValidator(const QString &XmlFile, const QString &XSDFile, bool Silent=false)