From 0f0f53f9193cf0c277c8009658d3add1f25d7211 Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Sat, 5 Feb 2011 12:12:34 +0200 Subject: [PATCH] GUI: Determine the XML report format before reading report. --- gui/resultsview.cpp | 17 +++++++++++++++- gui/xmlreport.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++++ gui/xmlreport.h | 13 ++++++++++++ gui/xmlreportv2.cpp | 2 +- gui/xmlreportv2.h | 2 +- 5 files changed, 79 insertions(+), 3 deletions(-) diff --git a/gui/resultsview.cpp b/gui/resultsview.cpp index 6c3615f56..1ded1c4b4 100644 --- a/gui/resultsview.cpp +++ b/gui/resultsview.cpp @@ -244,7 +244,22 @@ void ResultsView::DisableProgressbar() void ResultsView::ReadErrorsXml(const QString &filename) { - XmlReportV1 *report = new XmlReportV1(filename, this); + const int version = XmlReport::determineVersion(filename); + if (version == 0) + { + QMessageBox msgBox; + msgBox.setText(tr("Failed to read the report.")); + msgBox.setIcon(QMessageBox::Critical); + msgBox.exec(); + return; + } + + XmlReport *report = NULL; + if (version == 1) + report = new XmlReportV1(filename, this); + else if (version == 2) + report = new XmlReportV2(filename, this); + QList errors; if (report) { diff --git a/gui/xmlreport.cpp b/gui/xmlreport.cpp index a63aa5be2..c3cc521d3 100644 --- a/gui/xmlreport.cpp +++ b/gui/xmlreport.cpp @@ -18,9 +18,13 @@ #include #include +#include +#include #include "report.h" #include "xmlreport.h" +static const char ResultElementName[] = "results"; +static const char VersionAttribute[] = "version"; XmlReport::XmlReport(const QString &filename, QObject * parent) : Report(filename, parent) @@ -37,3 +41,47 @@ QString XmlReport::quoteMessage(const QString &message) quotedMessage.replace(">", ">"); return quotedMessage; } + +int XmlReport::determineVersion(const QString &filename) +{ + QFile file; + file.setFileName(filename); + bool succeed = file.open(QIODevice::ReadOnly | QIODevice::Text); + if (!succeed) + return 0; + + QXmlStreamReader reader(&file); + while (!reader.atEnd()) + { + switch (reader.readNext()) + { + case QXmlStreamReader::StartElement: + if (reader.name() == ResultElementName) + { + QXmlStreamAttributes attribs = reader.attributes(); + if (attribs.hasAttribute(QString(VersionAttribute))) + { + int ver = attribs.value("", VersionAttribute).toString().toInt(); + return ver; + } + 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; +} diff --git a/gui/xmlreport.h b/gui/xmlreport.h index bb94bb79b..45ce6f3c7 100644 --- a/gui/xmlreport.h +++ b/gui/xmlreport.h @@ -20,6 +20,7 @@ #define XML_REPORT_H #include +#include #include "report.h" class QObject; @@ -36,12 +37,24 @@ class XmlReport : public Report public: XmlReport(const QString &filename, QObject * parent = 0); + /** + * @brief Read contents of the report file. + */ + virtual QList Read() = 0; + /** * @brief Quote the message. * @param message Message to quote. * @return quoted message. */ static QString quoteMessage(const QString &message); + + /** + * @brief Get the XML report format version from the file. + * @param filename Filename of the report file. + * @return XML report format version or 0 if error happened. + */ + static int determineVersion(const QString &filename); }; /// @} diff --git a/gui/xmlreportv2.cpp b/gui/xmlreportv2.cpp index 6c4fc088f..0659cf03f 100644 --- a/gui/xmlreportv2.cpp +++ b/gui/xmlreportv2.cpp @@ -43,7 +43,7 @@ static const char VersionAttribute[] = "version"; static const char VerboseAttribute[] = "verbose"; XmlReportV2::XmlReportV2(const QString &filename, QObject * parent) : - Report(filename, parent), + XmlReport(filename, parent), mXmlReader(NULL), mXmlWriter(NULL) { diff --git a/gui/xmlreportv2.h b/gui/xmlreportv2.h index 3107b32b3..ba793061c 100644 --- a/gui/xmlreportv2.h +++ b/gui/xmlreportv2.h @@ -36,7 +36,7 @@ * This report outputs XML-formatted report. The XML format must match command * line version's XML output. */ -class XmlReportV2 : public Report +class XmlReportV2 : public XmlReport { public: XmlReportV2(const QString &filename, QObject * parent = 0);