Skip to content

Commit

Permalink
GUI: Add base class for XML report classes.
Browse files Browse the repository at this point in the history
A base class is needed for e.g. some common routines that can be
shared between the formats.
  • Loading branch information
kimmov committed Feb 4, 2011
1 parent eaf0bca commit df231aa
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 211 deletions.
2 changes: 2 additions & 0 deletions gui/gui.pro
Expand Up @@ -69,6 +69,7 @@ HEADERS += mainwindow.h \
report.h \
txtreport.h \
xmlreport.h \
xmlreportv1.h \
xmlreportv2.h \
translationhandler.h \
csvreport.h \
Expand Down Expand Up @@ -97,6 +98,7 @@ SOURCES += main.cpp \
report.cpp \
txtreport.cpp \
xmlreport.cpp \
xmlreportv1.cpp \
xmlreportv2.cpp \
translationhandler.cpp \
csvreport.cpp \
Expand Down
5 changes: 3 additions & 2 deletions gui/resultsview.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<ErrorLine> errors;
if (report)
{
Expand Down
148 changes: 1 addition & 147 deletions gui/xmlreport.cpp
Expand Up @@ -18,158 +18,12 @@

#include <QObject>
#include <QString>
#include <QList>
#include <QDir>
#include <QFile>
#include <QXmlStreamWriter>
#include <QDebug>
#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
<error file="gui/test.cpp" line="14" id="mismatchAllocDealloc" severity="error" msg="Mismatching allocation and deallocation: k"/>
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<ErrorLine> XmlReport::Read()
{
QList<ErrorLine> 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;
}
63 changes: 4 additions & 59 deletions gui/xmlreport.h
Expand Up @@ -19,78 +19,23 @@
#ifndef XML_REPORT_H
#define XML_REPORT_H

#include <QObject>
#include <QString>
#include <QStringList>
#include <QFile>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#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<ErrorLine> 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

0 comments on commit df231aa

Please sign in to comment.