cppcheck/gui/xmlreport.cpp

93 lines
2.9 KiB
C++
Raw Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
2023-01-28 10:16:34 +01:00
* Copyright (C) 2007-2023 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "xmlreport.h"
2022-02-02 16:17:28 +01:00
#include "report.h"
#include <QFile>
#include <QXmlStreamReader>
static const char ResultElementName[] = "results";
static const char VersionAttribute[] = "version";
XmlReport::XmlReport(const QString &filename) :
Report(filename)
2021-08-07 20:51:18 +02:00
{}
QString XmlReport::quoteMessage(const QString &message)
{
QString quotedMessage(message);
quotedMessage.replace("&", "&amp;");
quotedMessage.replace("\"", "&quot;");
quotedMessage.replace("'", "&#039;");
quotedMessage.replace("<", "&lt;");
quotedMessage.replace(">", "&gt;");
return quotedMessage;
}
QString XmlReport::unquoteMessage(const QString &message)
{
QString quotedMessage(message);
quotedMessage.replace("&amp;", "&");
quotedMessage.replace("&quot;", "\"");
quotedMessage.replace("&#039;", "'");
quotedMessage.replace("&lt;", "<");
quotedMessage.replace("&gt;", ">");
return quotedMessage;
}
int XmlReport::determineVersion(const QString &filename)
{
QFile file;
file.setFileName(filename);
const bool succeed = file.open(QIODevice::ReadOnly | QIODevice::Text);
if (!succeed)
return 0;
QXmlStreamReader reader(&file);
2011-10-13 20:53:06 +02:00
while (!reader.atEnd()) {
switch (reader.readNext()) {
case QXmlStreamReader::StartElement:
if (reader.name() == QString(ResultElementName)) {
QXmlStreamAttributes attribs = reader.attributes();
2011-10-13 20:53:06 +02:00
if (attribs.hasAttribute(QString(VersionAttribute))) {
const int ver = attribs.value(QString(), VersionAttribute).toString().toInt();
return ver;
2011-10-13 20:53:06 +02:00
} else
return 1;
}
break;
// Not handled
case QXmlStreamReader::EndElement:
case QXmlStreamReader::NoToken:
case QXmlStreamReader::Invalid:
case QXmlStreamReader::StartDocument:
case QXmlStreamReader::EndDocument:
case QXmlStreamReader::Characters:
case QXmlStreamReader::Comment:
case QXmlStreamReader::DTD:
case QXmlStreamReader::EntityReference:
case QXmlStreamReader::ProcessingInstruction:
break;
}
}
return 0;
}