Cppcheck
xmlreport.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
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 3 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, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "xmlreport.h"
20 
21 #include "report.h"
22 
23 #include <QFile>
24 #include <QIODevice>
25 #include <QXmlStreamAttributes>
26 #include <QXmlStreamReader>
27 
28 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
29 #include <QStringRef>
30 #endif
31 
32 static constexpr char ResultElementName[] = "results";
33 static constexpr char VersionAttribute[] = "version";
34 
35 XmlReport::XmlReport(const QString &filename) :
36  Report(filename)
37 {}
38 
39 QString XmlReport::quoteMessage(const QString &message)
40 {
41  QString quotedMessage(message);
42  quotedMessage.replace("&", "&amp;");
43  quotedMessage.replace("\"", "&quot;");
44  quotedMessage.replace("'", "&#039;");
45  quotedMessage.replace("<", "&lt;");
46  quotedMessage.replace(">", "&gt;");
47  return quotedMessage;
48 }
49 
50 QString XmlReport::unquoteMessage(const QString &message)
51 {
52  QString quotedMessage(message);
53  quotedMessage.replace("&amp;", "&");
54  quotedMessage.replace("&quot;", "\"");
55  quotedMessage.replace("&#039;", "'");
56  quotedMessage.replace("&lt;", "<");
57  quotedMessage.replace("&gt;", ">");
58  return quotedMessage;
59 }
60 
61 int XmlReport::determineVersion(const QString &filename)
62 {
63  QFile file;
64  file.setFileName(filename);
65  const bool succeed = file.open(QIODevice::ReadOnly | QIODevice::Text);
66  if (!succeed)
67  return 0;
68 
69  QXmlStreamReader reader(&file);
70  while (!reader.atEnd()) {
71  switch (reader.readNext()) {
72  case QXmlStreamReader::StartElement:
73  if (reader.name() == QString(ResultElementName)) {
74  QXmlStreamAttributes attribs = reader.attributes();
75  if (attribs.hasAttribute(QString(VersionAttribute))) {
76  const int ver = attribs.value(QString(), VersionAttribute).toString().toInt();
77  return ver;
78  }
79  return 1;
80  }
81  break;
82 
83  // Not handled
84  case QXmlStreamReader::EndElement:
85  case QXmlStreamReader::NoToken:
86  case QXmlStreamReader::Invalid:
87  case QXmlStreamReader::StartDocument:
88  case QXmlStreamReader::EndDocument:
89  case QXmlStreamReader::Characters:
90  case QXmlStreamReader::Comment:
91  case QXmlStreamReader::DTD:
92  case QXmlStreamReader::EntityReference:
93  case QXmlStreamReader::ProcessingInstruction:
94  break;
95  }
96  }
97  return 0;
98 }
A base class for reports.
Definition: report.h:34
XmlReport(const QString &filename)
Definition: xmlreport.cpp:35
static QString quoteMessage(const QString &message)
Quote the message.
Definition: xmlreport.cpp:39
static QString unquoteMessage(const QString &message)
Unquote the message.
Definition: xmlreport.cpp:50
static int determineVersion(const QString &filename)
Get the XML report format version from the file.
Definition: xmlreport.cpp:61
static constexpr char ResultElementName[]
Definition: xmlreport.cpp:32
static constexpr char VersionAttribute[]
Definition: xmlreport.cpp:33