From df231aa7387085eba4cfbf85a90b122ec18ff13c Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Fri, 4 Feb 2011 23:56:14 +0200 Subject: [PATCH] GUI: Add base class for XML report classes. A base class is needed for e.g. some common routines that can be shared between the formats. --- gui/gui.pro | 2 + gui/resultsview.cpp | 5 +- gui/xmlreport.cpp | 148 +------------------------------------ gui/xmlreport.h | 63 +--------------- gui/xmlreportv1.cpp | 175 ++++++++++++++++++++++++++++++++++++++++++++ gui/xmlreportv1.h | 96 ++++++++++++++++++++++++ gui/xmlreportv2.cpp | 2 +- gui/xmlreportv2.h | 4 +- 8 files changed, 284 insertions(+), 211 deletions(-) create mode 100644 gui/xmlreportv1.cpp create mode 100644 gui/xmlreportv1.h diff --git a/gui/gui.pro b/gui/gui.pro index 5df4c2bab4a..25f3a73a22e 100644 --- a/gui/gui.pro +++ b/gui/gui.pro @@ -69,6 +69,7 @@ HEADERS += mainwindow.h \ report.h \ txtreport.h \ xmlreport.h \ + xmlreportv1.h \ xmlreportv2.h \ translationhandler.h \ csvreport.h \ @@ -97,6 +98,7 @@ SOURCES += main.cpp \ report.cpp \ txtreport.cpp \ xmlreport.cpp \ + xmlreportv1.cpp \ xmlreportv2.cpp \ translationhandler.cpp \ csvreport.cpp \ diff --git a/gui/resultsview.cpp b/gui/resultsview.cpp index e74d7b982ca..6c3615f5636 100644 --- a/gui/resultsview.cpp +++ b/gui/resultsview.cpp @@ -31,6 +31,7 @@ #include "report.h" #include "txtreport.h" #include "xmlreport.h" +#include "xmlreportv1.h" #include "xmlreportv2.h" #include "csvreport.h" #include "applicationlist.h" @@ -131,7 +132,7 @@ void ResultsView::Save(const QString &filename, Report::Type type) report = new TxtReport(filename, this); break; case Report::XML: - report = new XmlReport(filename, this); + report = new XmlReportV1(filename, this); break; case Report::XMLV2: report = new XmlReportV2(filename, this); @@ -243,7 +244,7 @@ void ResultsView::DisableProgressbar() void ResultsView::ReadErrorsXml(const QString &filename) { - XmlReport *report = new XmlReport(filename, this); + XmlReportV1 *report = new XmlReportV1(filename, this); QList errors; if (report) { diff --git a/gui/xmlreport.cpp b/gui/xmlreport.cpp index f481f36151f..ce009e3efb5 100644 --- a/gui/xmlreport.cpp +++ b/gui/xmlreport.cpp @@ -18,158 +18,12 @@ #include #include -#include -#include -#include -#include -#include #include "report.h" -#include "erroritem.h" #include "xmlreport.h" -static const char ResultElementName[] = "results"; -static const char ErrorElementName[] = "error"; -static const char FilenameAttribute[] = "file"; -static const char LineAttribute[] = "line"; -static const char IdAttribute[] = "id"; -static const char SeverityAttribute[] = "severity"; -static const char MsgAttribute[] = "msg"; XmlReport::XmlReport(const QString &filename, QObject * parent) : - Report(filename, parent), - mXmlReader(NULL), - mXmlWriter(NULL) + Report(filename, parent) { } -XmlReport::~XmlReport() -{ - delete mXmlReader; - delete mXmlWriter; - Close(); -} - -bool XmlReport::Create() -{ - bool success = false; - if (Report::Create()) - { - mXmlWriter = new QXmlStreamWriter(Report::GetFile()); - success = true; - } - return success; -} - -bool XmlReport::Open() -{ - bool success = false; - if (Report::Open()) - { - mXmlReader = new QXmlStreamReader(Report::GetFile()); - success = true; - } - return success; -} - -void XmlReport::WriteHeader() -{ - mXmlWriter->setAutoFormatting(true); - mXmlWriter->writeStartDocument(); - mXmlWriter->writeStartElement(ResultElementName); -} - -void XmlReport::WriteFooter() -{ - mXmlWriter->writeEndElement(); - mXmlWriter->writeEndDocument(); -} - -void XmlReport::WriteError(const ErrorItem &error) -{ - /* - Error example from the core program in xml - - The callstack seems to be ignored here as well, instead last item of the stack is used - */ - - mXmlWriter->writeStartElement(ErrorElementName); - const QString file = QDir::toNativeSeparators(error.files[error.files.size() - 1]); - 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); - mXmlWriter->writeEndElement(); -} - -QList XmlReport::Read() -{ - QList errors; - bool insideResults = false; - if (!mXmlReader) - { - qDebug() << "You must Open() the file before reading it!"; - return errors; - } - while (!mXmlReader->atEnd()) - { - switch (mXmlReader->readNext()) - { - case QXmlStreamReader::StartElement: - if (mXmlReader->name() == ResultElementName) - insideResults = true; - - // Read error element from inside result element - if (insideResults && mXmlReader->name() == ErrorElementName) - { - ErrorLine line = ReadError(mXmlReader); - errors.append(line); - } - break; - - case QXmlStreamReader::EndElement: - if (mXmlReader->name() == ResultElementName) - insideResults = false; - break; - - // Not handled - 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 errors; -} - -ErrorLine XmlReport::ReadError(QXmlStreamReader *reader) -{ - ErrorLine line; - if (reader->name().toString() == ErrorElementName) - { - QXmlStreamAttributes attribs = reader->attributes(); - line.file = attribs.value("", FilenameAttribute).toString(); - line.line = attribs.value("", LineAttribute).toString().toUInt(); - line.id = attribs.value("", IdAttribute).toString(); - line.severity = attribs.value("", SeverityAttribute).toString(); - - // NOTE: This dublicates the message to Summary-field. But since - // old XML format doesn't have separate summary and verbose messages - // we must add same message to both data so it shows up in GUI. - // Check if there is full stop and cut the summary to it. - QString summary = attribs.value("", MsgAttribute).toString(); - const int ind = summary.indexOf('.'); - if (ind != -1) - summary = summary.left(ind + 1); - line.summary = summary; - line.message = attribs.value("", MsgAttribute).toString(); - } - return line; -} diff --git a/gui/xmlreport.h b/gui/xmlreport.h index 0c8058dd049..b1afd192f48 100644 --- a/gui/xmlreport.h +++ b/gui/xmlreport.h @@ -19,78 +19,23 @@ #ifndef XML_REPORT_H #define XML_REPORT_H -#include #include -#include -#include -#include -#include #include "report.h" +class QObject; + /// @addtogroup GUI /// @{ /** -* @brief XML file report. -* This report outputs XML-formatted report. The XML format must match command -* line version's XML output. +* @brief Base class for XML report classes. */ class XmlReport : public Report { public: XmlReport(const QString &filename, QObject * parent = 0); - virtual ~XmlReport(); - - /** - * @brief Create the report (file). - * @return true if succeeded, false if file could not be created. - */ - virtual bool Create(); - - /** - * @brief Open existing report file. - */ - bool Open(); - - /** - * @brief Write report header. - */ - virtual void WriteHeader(); - - /** - * @brief Write report footer. - */ - virtual void WriteFooter(); - - /** - * @brief Write error to report. - * @param error Error data. - */ - virtual void WriteError(const ErrorItem &error); - - /** - * @brief Read contents of the report file. - */ - QList Read(); - -protected: - /** - * @brief Read and parse error item from XML stream. - * @param reader XML stream reader to use. - */ - ErrorLine ReadError(QXmlStreamReader *reader); - -private: - /** - * @brief XML stream reader for reading the report in XML format. - */ - QXmlStreamReader *mXmlReader; - - /** - * @brief XML stream writer for writing the report in XML format. - */ - QXmlStreamWriter *mXmlWriter; }; /// @} + #endif // XML_REPORT_H diff --git a/gui/xmlreportv1.cpp b/gui/xmlreportv1.cpp new file mode 100644 index 00000000000..9fa8cbfd3fd --- /dev/null +++ b/gui/xmlreportv1.cpp @@ -0,0 +1,175 @@ +/* + * Cppcheck - A tool for static C/C++ code analysis + * Copyright (C) 2007-2011 Daniel Marjamäki and 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 . + */ + +#include +#include +#include +#include +#include +#include +#include +#include "report.h" +#include "erroritem.h" +#include "xmlreportv1.h" + +static const char ResultElementName[] = "results"; +static const char ErrorElementName[] = "error"; +static const char FilenameAttribute[] = "file"; +static const char LineAttribute[] = "line"; +static const char IdAttribute[] = "id"; +static const char SeverityAttribute[] = "severity"; +static const char MsgAttribute[] = "msg"; + +XmlReportV1::XmlReportV1(const QString &filename, QObject * parent) : + XmlReport(filename, parent), + mXmlReader(NULL), + mXmlWriter(NULL) +{ +} + +XmlReportV1::~XmlReportV1() +{ + delete mXmlReader; + delete mXmlWriter; + Close(); +} + +bool XmlReportV1::Create() +{ + bool success = false; + if (Report::Create()) + { + mXmlWriter = new QXmlStreamWriter(Report::GetFile()); + success = true; + } + return success; +} + +bool XmlReportV1::Open() +{ + bool success = false; + if (Report::Open()) + { + mXmlReader = new QXmlStreamReader(Report::GetFile()); + success = true; + } + return success; +} + +void XmlReportV1::WriteHeader() +{ + mXmlWriter->setAutoFormatting(true); + mXmlWriter->writeStartDocument(); + mXmlWriter->writeStartElement(ResultElementName); +} + +void XmlReportV1::WriteFooter() +{ + mXmlWriter->writeEndElement(); + mXmlWriter->writeEndDocument(); +} + +void XmlReportV1::WriteError(const ErrorItem &error) +{ + /* + Error example from the core program in xml + + The callstack seems to be ignored here as well, instead last item of the stack is used + */ + + mXmlWriter->writeStartElement(ErrorElementName); + const QString file = QDir::toNativeSeparators(error.files[error.files.size() - 1]); + 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); + mXmlWriter->writeEndElement(); +} + +QList XmlReportV1::Read() +{ + QList errors; + bool insideResults = false; + if (!mXmlReader) + { + qDebug() << "You must Open() the file before reading it!"; + return errors; + } + while (!mXmlReader->atEnd()) + { + switch (mXmlReader->readNext()) + { + case QXmlStreamReader::StartElement: + if (mXmlReader->name() == ResultElementName) + insideResults = true; + + // Read error element from inside result element + if (insideResults && mXmlReader->name() == ErrorElementName) + { + ErrorLine line = ReadError(mXmlReader); + errors.append(line); + } + break; + + case QXmlStreamReader::EndElement: + if (mXmlReader->name() == ResultElementName) + insideResults = false; + break; + + // Not handled + 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 errors; +} + +ErrorLine XmlReportV1::ReadError(QXmlStreamReader *reader) +{ + ErrorLine line; + if (reader->name().toString() == ErrorElementName) + { + QXmlStreamAttributes attribs = reader->attributes(); + line.file = attribs.value("", FilenameAttribute).toString(); + line.line = attribs.value("", LineAttribute).toString().toUInt(); + line.id = attribs.value("", IdAttribute).toString(); + line.severity = attribs.value("", SeverityAttribute).toString(); + + // NOTE: This dublicates the message to Summary-field. But since + // old XML format doesn't have separate summary and verbose messages + // we must add same message to both data so it shows up in GUI. + // Check if there is full stop and cut the summary to it. + QString summary = attribs.value("", MsgAttribute).toString(); + const int ind = summary.indexOf('.'); + if (ind != -1) + summary = summary.left(ind + 1); + line.summary = summary; + line.message = attribs.value("", MsgAttribute).toString(); + } + return line; +} diff --git a/gui/xmlreportv1.h b/gui/xmlreportv1.h new file mode 100644 index 00000000000..685afa9efea --- /dev/null +++ b/gui/xmlreportv1.h @@ -0,0 +1,96 @@ +/* + * Cppcheck - A tool for static C/C++ code analysis + * Copyright (C) 2007-2011 Daniel Marjamäki and 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 . + */ + +#ifndef XML_REPORTV1_H +#define XML_REPORTV1_H + +#include +#include +#include +#include +#include +#include +#include "xmlreport.h" + +/// @addtogroup GUI +/// @{ + + +/** +* @brief XML file report version 1. +* This report outputs XML-formatted report, version 1. The XML format must match command +* line version's XML output. +*/ +class XmlReportV1 : public XmlReport +{ +public: + XmlReportV1(const QString &filename, QObject * parent = 0); + virtual ~XmlReportV1(); + + /** + * @brief Create the report (file). + * @return true if succeeded, false if file could not be created. + */ + virtual bool Create(); + + /** + * @brief Open existing report file. + */ + bool Open(); + + /** + * @brief Write report header. + */ + virtual void WriteHeader(); + + /** + * @brief Write report footer. + */ + virtual void WriteFooter(); + + /** + * @brief Write error to report. + * @param error Error data. + */ + virtual void WriteError(const ErrorItem &error); + + /** + * @brief Read contents of the report file. + */ + virtual QList Read(); + +protected: + /** + * @brief Read and parse error item from XML stream. + * @param reader XML stream reader to use. + */ + ErrorLine ReadError(QXmlStreamReader *reader); + +private: + /** + * @brief XML stream reader for reading the report in XML format. + */ + QXmlStreamReader *mXmlReader; + + /** + * @brief XML stream writer for writing the report in XML format. + */ + QXmlStreamWriter *mXmlWriter; +}; +/// @} +#endif // XML_REPORTV1_H diff --git a/gui/xmlreportv2.cpp b/gui/xmlreportv2.cpp index 165811b2e11..4caf7d05ef6 100644 --- a/gui/xmlreportv2.cpp +++ b/gui/xmlreportv2.cpp @@ -1,6 +1,6 @@ /* * Cppcheck - A tool for static C/C++ code analysis - * Copyright (C) 2007-2011 Daniel Marjamäki and Cppcheck team. + * Copyright (C) 2007-2011 Daniel Marjamäki and 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 diff --git a/gui/xmlreportv2.h b/gui/xmlreportv2.h index 33f96349d55..3107b32b3f0 100644 --- a/gui/xmlreportv2.h +++ b/gui/xmlreportv2.h @@ -1,6 +1,6 @@ /* * Cppcheck - A tool for static C/C++ code analysis - * Copyright (C) 2007-2011 Daniel Marjamäki and Cppcheck team. + * Copyright (C) 2007-2011 Daniel Marjamäki and 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 @@ -72,7 +72,7 @@ class XmlReportV2 : public Report /** * @brief Read contents of the report file. */ - QList Read(); + virtual QList Read(); protected: /**