GUI: Quote special chars in the XML output.

Fixes ticket #2543 (GUI: Xml report does not quote special characters)
This commit is contained in:
Kimmo Varis 2011-02-05 00:23:25 +02:00
parent df231aa738
commit 299e200d45
4 changed files with 29 additions and 5 deletions

View File

@ -27,3 +27,13 @@ XmlReport::XmlReport(const QString &filename, QObject * parent) :
{ {
} }
QString XmlReport::quoteMessage(const QString &message)
{
QString quotedMessage(message);
quotedMessage.replace("&", "&");
quotedMessage.replace("\"", """);
quotedMessage.replace("'", "'");
quotedMessage.replace("<", "&lt;");
quotedMessage.replace(">", "&gt;");
return quotedMessage;
}

View File

@ -35,6 +35,13 @@ class XmlReport : public Report
{ {
public: public:
XmlReport(const QString &filename, QObject * parent = 0); XmlReport(const QString &filename, QObject * parent = 0);
/**
* @brief Quote the message.
* @param message Message to quote.
* @return quoted message.
*/
static QString quoteMessage(const QString &message);
}; };
/// @} /// @}

View File

@ -25,6 +25,7 @@
#include <QDebug> #include <QDebug>
#include "report.h" #include "report.h"
#include "erroritem.h" #include "erroritem.h"
#include "xmlreport.h"
#include "xmlreportv1.h" #include "xmlreportv1.h"
static const char ResultElementName[] = "results"; static const char ResultElementName[] = "results";
@ -93,13 +94,15 @@ void XmlReportV1::WriteError(const ErrorItem &error)
*/ */
mXmlWriter->writeStartElement(ErrorElementName); mXmlWriter->writeStartElement(ErrorElementName);
const QString file = QDir::toNativeSeparators(error.files[error.files.size() - 1]); QString file = QDir::toNativeSeparators(error.files[error.files.size() - 1]);
file = XmlReport::quoteMessage(file);
mXmlWriter->writeAttribute(FilenameAttribute, file); mXmlWriter->writeAttribute(FilenameAttribute, file);
const QString line = QString::number(error.lines[error.lines.size() - 1]); const QString line = QString::number(error.lines[error.lines.size() - 1]);
mXmlWriter->writeAttribute(LineAttribute, line); mXmlWriter->writeAttribute(LineAttribute, line);
mXmlWriter->writeAttribute(IdAttribute, error.id); mXmlWriter->writeAttribute(IdAttribute, error.id);
mXmlWriter->writeAttribute(SeverityAttribute, error.severity); mXmlWriter->writeAttribute(SeverityAttribute, error.severity);
mXmlWriter->writeAttribute(MsgAttribute, error.message); const QString message = XmlReport::quoteMessage(error.message);
mXmlWriter->writeAttribute(MsgAttribute, message);
mXmlWriter->writeEndElement(); mXmlWriter->writeEndElement();
} }

View File

@ -25,6 +25,7 @@
#include <QDebug> #include <QDebug>
#include "report.h" #include "report.h"
#include "erroritem.h" #include "erroritem.h"
#include "xmlreport.h"
#include "xmlreportv2.h" #include "xmlreportv2.h"
#include "cppcheck.h" #include "cppcheck.h"
@ -110,14 +111,17 @@ void XmlReportV2::WriteError(const ErrorItem &error)
mXmlWriter->writeStartElement(ErrorElementName); mXmlWriter->writeStartElement(ErrorElementName);
mXmlWriter->writeAttribute(IdAttribute, error.id); mXmlWriter->writeAttribute(IdAttribute, error.id);
mXmlWriter->writeAttribute(SeverityAttribute, error.severity); mXmlWriter->writeAttribute(SeverityAttribute, error.severity);
mXmlWriter->writeAttribute(MsgAttribute, error.summary); const QString summary = XmlReport::quoteMessage(error.summary);
mXmlWriter->writeAttribute(VerboseAttribute, error.message); mXmlWriter->writeAttribute(MsgAttribute, summary);
const QString message = XmlReport::quoteMessage(error.message);
mXmlWriter->writeAttribute(VerboseAttribute, message);
for (int i = 0; i < error.files.count(); i++) for (int i = 0; i < error.files.count(); i++)
{ {
mXmlWriter->writeStartElement(LocationElementName); mXmlWriter->writeStartElement(LocationElementName);
const QString file = QDir::toNativeSeparators(error.files[i]); QString file = QDir::toNativeSeparators(error.files[i]);
file = XmlReport::quoteMessage(file);
mXmlWriter->writeAttribute(FilenameAttribute, file); mXmlWriter->writeAttribute(FilenameAttribute, file);
const QString line = QString::number(error.lines[i]); const QString line = QString::number(error.lines[i]);
mXmlWriter->writeAttribute(LineAttribute, line); mXmlWriter->writeAttribute(LineAttribute, line);