From 299e200d45957768f15d34835e0249cf2d30f729 Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Sat, 5 Feb 2011 00:23:25 +0200 Subject: [PATCH] GUI: Quote special chars in the XML output. Fixes ticket #2543 (GUI: Xml report does not quote special characters) --- gui/xmlreport.cpp | 10 ++++++++++ gui/xmlreport.h | 7 +++++++ gui/xmlreportv1.cpp | 7 +++++-- gui/xmlreportv2.cpp | 10 +++++++--- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/gui/xmlreport.cpp b/gui/xmlreport.cpp index ce009e3ef..a63aa5be2 100644 --- a/gui/xmlreport.cpp +++ b/gui/xmlreport.cpp @@ -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("<", "<"); + quotedMessage.replace(">", ">"); + return quotedMessage; +} diff --git a/gui/xmlreport.h b/gui/xmlreport.h index b1afd192f..bb94bb79b 100644 --- a/gui/xmlreport.h +++ b/gui/xmlreport.h @@ -35,6 +35,13 @@ class XmlReport : public Report { public: 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); }; /// @} diff --git a/gui/xmlreportv1.cpp b/gui/xmlreportv1.cpp index 9fa8cbfd3..7fdf2d5fb 100644 --- a/gui/xmlreportv1.cpp +++ b/gui/xmlreportv1.cpp @@ -25,6 +25,7 @@ #include #include "report.h" #include "erroritem.h" +#include "xmlreport.h" #include "xmlreportv1.h" static const char ResultElementName[] = "results"; @@ -93,13 +94,15 @@ void XmlReportV1::WriteError(const ErrorItem &error) */ 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); const QString line = QString::number(error.lines[error.lines.size() - 1]); mXmlWriter->writeAttribute(LineAttribute, line); mXmlWriter->writeAttribute(IdAttribute, error.id); mXmlWriter->writeAttribute(SeverityAttribute, error.severity); - mXmlWriter->writeAttribute(MsgAttribute, error.message); + const QString message = XmlReport::quoteMessage(error.message); + mXmlWriter->writeAttribute(MsgAttribute, message); mXmlWriter->writeEndElement(); } diff --git a/gui/xmlreportv2.cpp b/gui/xmlreportv2.cpp index 4caf7d05e..6c4fc088f 100644 --- a/gui/xmlreportv2.cpp +++ b/gui/xmlreportv2.cpp @@ -25,6 +25,7 @@ #include #include "report.h" #include "erroritem.h" +#include "xmlreport.h" #include "xmlreportv2.h" #include "cppcheck.h" @@ -110,14 +111,17 @@ void XmlReportV2::WriteError(const ErrorItem &error) mXmlWriter->writeStartElement(ErrorElementName); mXmlWriter->writeAttribute(IdAttribute, error.id); mXmlWriter->writeAttribute(SeverityAttribute, error.severity); - mXmlWriter->writeAttribute(MsgAttribute, error.summary); - mXmlWriter->writeAttribute(VerboseAttribute, error.message); + const QString summary = XmlReport::quoteMessage(error.summary); + mXmlWriter->writeAttribute(MsgAttribute, summary); + const QString message = XmlReport::quoteMessage(error.message); + mXmlWriter->writeAttribute(VerboseAttribute, message); for (int i = 0; i < error.files.count(); i++) { 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); const QString line = QString::number(error.lines[i]); mXmlWriter->writeAttribute(LineAttribute, line);