Torc  0.1
torcxmlvalidator.cpp
Go to the documentation of this file.
1 /* Class TorcXmlValidator
2 *
3 * Copyright (C) Mark Kendall 2015-18
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 
20 // Qt
21 #include <QFile>
22 #include <QRegExp>
23 #include <QXmlSchema>
24 #include <QXmlSchemaValidator>
25 
26 // Torc
27 #include "torclogging.h"
28 #include "torcxmlvalidator.h"
29 
30 TorcXmlValidator::TorcXmlValidator(const QString &XmlFile, const QString &XSDFile, bool Silent/*=false*/)
31  : m_xmlFile(XmlFile),
32  m_xsdFile(XSDFile),
33  m_xsdData(QByteArray()),
34  m_valid(false),
35  m_xsdDone(false),
36  m_silent(Silent)
37 {
38  Validate();
39 }
40 
41 TorcXmlValidator::TorcXmlValidator(const QString &XmlFile, const QByteArray &XSDData, bool Silent/*=false*/)
42  : m_xmlFile(XmlFile),
43  m_xsdFile(QStringLiteral("")),
44  m_xsdData(XSDData),
45  m_valid(false),
46  m_xsdDone(false),
47  m_silent(Silent)
48 {
49  Validate();
50 }
51 
53 {
54 }
55 
57 {
58  // sense check xml
59  if (!QFile::exists(m_xmlFile))
60  {
61  LOG(VB_GENERAL, LOG_ERR, QString("Xml file '%1' does not exist").arg(m_xmlFile));
62  return;
63  }
64 
65  QFile xml(m_xmlFile);
66  if (!xml.open(QIODevice::ReadOnly))
67  {
68  LOG(VB_GENERAL, LOG_ERR, QString("Failed to open Xml file '%1'").arg(m_xmlFile));
69  return;
70  }
71 
72  bool xsdvalid = false;
73  QXmlSchema schema;
74  schema.setMessageHandler(this);
75 
76  // sense check xsd
77  if (!m_xsdFile.isEmpty())
78  {
79  if (!QFile::exists(m_xsdFile))
80  {
81  LOG(VB_GENERAL, LOG_ERR, QString("XSD file '%1' does not exist").arg(m_xsdFile));
82  return;
83  }
84 
85  QFile xsd(m_xsdFile);
86  if (!xsd.open(QIODevice::ReadOnly))
87  {
88  LOG(VB_GENERAL, LOG_ERR, QString("Failed to open XSD file '%1'").arg(m_xsdFile));
89  return;
90  }
91 
92  xsdvalid = schema.load(&xsd);
93  xsd.close();
94  }
95  else if (!m_xsdData.isEmpty())
96  {
97  xsdvalid = schema.load(m_xsdData);
98  }
99  else
100  {
101  LOG(VB_GENERAL, LOG_ERR, "No XSD file or data specified");
102  }
103 
104  if (!xsdvalid)
105  {
106  LOG(VB_GENERAL, LOG_ERR, QString("XSD schema '%1' is not valid").arg(m_xsdFile));
107  return;
108  }
109 
110  m_xsdDone = true;
111 
112  QXmlSchemaValidator validator(schema);
113  validator.setMessageHandler(this);
114  if (!validator.validate(&xml))
115  {
116  if (!m_silent)
117  LOG(VB_GENERAL, LOG_ERR, QString("Failed to validate Xml from '%1'").arg(m_xmlFile));
118  return;
119  }
120 
121  m_valid = true;
122 }
123 
125 {
126  return m_valid;
127 }
128 
129 void TorcXmlValidator::handleMessage(QtMsgType Type, const QString &Description,
130  const QUrl &Identifier, const QSourceLocation &SourceLocation)
131 {
132  if (m_silent)
133  return;
134 
135  (void)Identifier;
136  QString desc = Description;
137  desc.remove(QRegExp("<[^>]*>"));
138  if (!m_xsdDone && m_xsdFile.isEmpty())
139  {
140  LOG(VB_GENERAL, (Type == QtFatalMsg) ? LOG_ERR : LOG_WARNING, QString("Error in XSD at line %1: '%2'")
141  .arg(SourceLocation.line()).arg(desc));
142  }
143  else
144  {
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));
148  }
149 }
150 
void handleMessage(QtMsgType Type, const QString &Description, const QUrl &Identifier, const QSourceLocation &SourceLocation)
~TorcXmlValidator()=default
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
bool Validated(void) const
TorcXmlValidator(const QString &XmlFile, const QString &XSDFile, bool Silent=false)