Commit 6e0ed4af authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

QXmlDefaultHandler is deprecated port to QXmlStreamReader

parent 68902c29
......@@ -63,12 +63,8 @@ Game::Game() :
KapmanParser kapmanParser(this);
// Set the XML file as input source for the parser
QFile mazeXmlFile(QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("defaultmaze.xml")));
QXmlInputSource source(&mazeXmlFile);
// Create the XML file reader
QXmlSimpleReader reader;
reader.setContentHandler(&kapmanParser);
// Parse the XML file
reader.parse(source);
mazeXmlFile.open(QIODevice::ReadOnly);
kapmanParser.parse(&mazeXmlFile);
connect(m_kapman, &Kapman::sWinPoints, this, &Game::winPoints);
......
......@@ -19,6 +19,8 @@
#include "element.h"
#include "pill.h"
#include "energizer.h"
#include <QDebug>
#include <QXmlStreamAttributes>
KapmanParser::KapmanParser(Game *p_game)
{
......@@ -31,117 +33,166 @@ KapmanParser::~KapmanParser()
}
bool KapmanParser::characters(const QString &ch)
bool KapmanParser::parse(QIODevice *input)
{
m_buffer = ch;
QXmlStreamReader reader(input);
while (!reader.atEnd()) {
reader.readNext();
if (reader.hasError())
return false;
switch (reader.tokenType()) {
case QXmlStreamReader::StartElement:
if (!startElement(reader.namespaceUri(), reader.name(),
reader.qualifiedName(), reader.attributes())) {
return false;
}
break;
case QXmlStreamReader::EndElement:
if (!endElement(reader.namespaceUri(), reader.name(),
reader.qualifiedName())) {
return false;
}
break;
case QXmlStreamReader::Characters:
if (!reader.isWhitespace() && !reader.text().toString().trimmed().isEmpty()) {
if (!characters(reader.text()))
return false;
}
break;
default:
break;
}
}
if (reader.isEndDocument())
return false;
return true;
}
bool KapmanParser::characters(const QStringRef &ch)
{
m_buffer = ch.toString();
return true;
}
bool KapmanParser::startElement(const QString &, const QString &, const QString &p_qName, const QXmlAttributes &p_atts)
bool KapmanParser::startElement(const QStringRef &namespaceURI, const QStringRef &localName, const QStringRef &qName, const QXmlStreamAttributes &atts)
{
Q_UNUSED(namespaceURI)
Q_UNUSED(localName)
qreal x_position = 0.0;
qreal y_position = 0.0;
if (p_qName == QLatin1String("Maze")) {
if (qName == QLatin1String("Maze")) {
int nbRows = 0;
int nbColumns = 0;
// Initialize the number of rows and columns
for (int i = 0; i < p_atts.count(); ++i) {
const QString &qname = p_atts.qName(i);
if (qname == QLatin1String("rowCount")) {
nbRows = p_atts.value(i).toInt();
} else if (qname == QLatin1String("colCount")) {
nbColumns = p_atts.value(i).toInt();
}
if (atts.hasAttribute(QLatin1String("rowCount"))) {
nbRows = atts.value(QLatin1String("rowCount")).toInt();
}
if (atts.hasAttribute(QLatin1String("colCount"))) {
nbColumns = atts.value(QLatin1String("colCount")).toInt();
}
// Create the Maze matrix
m_game->getMaze()->init(nbRows, nbColumns);
} else if (p_qName == QLatin1String("Bonus")) {
} else if (qName == QLatin1String("Bonus")) {
// Initialize the number of rows and columns
for (int i = 0; i < p_atts.count(); ++i) {
const QString &qname = p_atts.qName(i);
if (qname == QLatin1String("rowIndex")) {
y_position = p_atts.value(i).toInt();
} else if (qname == QLatin1String("colIndex")) {
x_position = p_atts.value(i).toInt();
} else if (qname == QLatin1String("x-align")) {
if (p_atts.value(i) == QLatin1String("center")) {
x_position += 0.5;
}
} else if (qname == QLatin1String("y-align")) {
if (p_atts.value(i) == QLatin1String("center")) {
y_position += 0.5;
}
if (atts.hasAttribute(QLatin1String("rowIndex"))) {
y_position = atts.value(QLatin1String("rowIndex")).toInt();
}
if (atts.hasAttribute(QLatin1String("colIndex"))) {
x_position = atts.value(QLatin1String("colIndex")).toInt();
}
if (atts.hasAttribute(QLatin1String("x-align"))) {
if (atts.value(QLatin1String("x-align")).toString() == QLatin1String("center")) {
x_position += 0.5;
}
}
if (atts.hasAttribute(QLatin1String("y-align"))) {
if (atts.value(QLatin1String("y-align")).toString() == QLatin1String("center")) {
y_position += 0.5;
}
}
m_game->createBonus(QPointF(x_position, y_position));
} else if (p_qName == QLatin1String("Kapman")) {
// Initialize the number of rows and columns
for (int i = 0; i < p_atts.count(); ++i) {
const QString &qname = p_atts.qName(i);
if (qname == QLatin1String("rowIndex")) {
y_position = p_atts.value(i).toInt();
} else if (qname == QLatin1String("colIndex")) {
x_position = p_atts.value(i).toInt();
} else if (qname == QLatin1String("x-align")) {
if (p_atts.value(i) == QLatin1String("center")) {
x_position += 0.5;
}
} else if (qname == QLatin1String("y-align")) {
if (p_atts.value(i) == QLatin1String("center")) {
y_position += 0.5;
}
} else if (qName == QLatin1String("Kapman")) {
if (atts.hasAttribute(QLatin1String("rowIndex"))) {
y_position = atts.value(QLatin1String("rowIndex")).toInt();
}
if (atts.hasAttribute(QLatin1String("colIndex"))) {
x_position = atts.value(QLatin1String("colIndex")).toInt();
}
if (atts.hasAttribute(QLatin1String("x-align"))) {
if (atts.value(QLatin1String("x-align")).toString() == QLatin1String("center")) {
x_position += 0.5;
}
}
if (atts.hasAttribute(QLatin1String("y-align"))) {
if (atts.value(QLatin1String("y-align")).toString() == QLatin1String("center")) {
y_position += 0.5;
}
}
m_game->createKapman(QPointF(x_position, y_position));
} else if (p_qName == QLatin1String("Ghost")) {
} else if (qName == QLatin1String("Ghost")) {
QString imageId;
// Initialize the number of rows and columns
for (int i = 0; i < p_atts.count(); ++i) {
const QString &qname = p_atts.qName(i);
if (qname == QLatin1String("rowIndex")) {
y_position = p_atts.value(i).toInt();
} else if (qname == QLatin1String("colIndex")) {
x_position = p_atts.value(i).toInt();
} else if (qname == QLatin1String("x-align")) {
if (p_atts.value(i) == QLatin1String("center")) {
x_position += 0.5;
}
} else if (qname == QLatin1String("y-align")) {
if (p_atts.value(i) == QLatin1String("center")) {
y_position += 0.5;
}
} else if (qname == QLatin1String("imageId")) {
imageId = p_atts.value(i);
if (atts.hasAttribute(QLatin1String("rowIndex"))) {
y_position = atts.value(QLatin1String("rowIndex")).toInt();
}
if (atts.hasAttribute(QLatin1String("colIndex"))) {
x_position = atts.value(QLatin1String("colIndex")).toInt();
}
if (atts.hasAttribute(QLatin1String("x-align"))) {
if (atts.value(QLatin1String("x-align")).toString() == QLatin1String("center")) {
x_position += 0.5;
}
}
if (atts.hasAttribute(QLatin1String("y-align"))) {
if (atts.value(QLatin1String("y-align")).toString() == QLatin1String("center")) {
y_position += 0.5;
}
}
if (atts.hasAttribute(QLatin1String("imageId"))) {
imageId = atts.value(QLatin1String("imageId")).toString();
}
m_game->createGhost(QPointF(x_position, y_position), imageId);
}
return true;
}
bool KapmanParser::endElement(const QString &, const QString &, const QString &p_qName)
bool KapmanParser::endElement(const QStringRef &namespaceURI, const QStringRef &localName, const QStringRef &qName)
{
if (p_qName == QLatin1String("Row")) {
Q_UNUSED(namespaceURI)
Q_UNUSED(localName)
if (qName.toString() == QLatin1String("Row")) {
for (int i = 0, total = m_buffer.length(); i < total; ++i) {
switch (m_buffer.at(i).toLatin1()) {
case '|':
case '=': m_game->getMaze()->setCellType(m_counterRows, i, Cell::WALL);
case '=':
m_game->getMaze()->setCellType(m_counterRows, i, Cell::WALL);
break;
case ' ': m_game->getMaze()->setCellType(m_counterRows, i, Cell::CORRIDOR);
case ' ':
m_game->getMaze()->setCellType(m_counterRows, i, Cell::CORRIDOR);
break;
case '.': m_game->getMaze()->setCellType(m_counterRows, i, Cell::CORRIDOR);
case '.':
m_game->getMaze()->setCellType(m_counterRows, i, Cell::CORRIDOR);
m_game->getMaze()->setCellElement(m_counterRows, i,
new Pill(m_counterRows, i, m_game->getMaze(), QStringLiteral("pill")));
break;
case 'o': m_game->getMaze()->setCellType(m_counterRows, i, Cell::CORRIDOR);
case 'o':
m_game->getMaze()->setCellType(m_counterRows, i, Cell::CORRIDOR);
m_game->getMaze()->setCellElement(m_counterRows, i,
new Energizer(m_counterRows, i, m_game->getMaze(), QStringLiteral("energizer")));
break;
case 'x': m_game->getMaze()->setCellType(m_counterRows, i, Cell::GHOSTCAMP);
case 'x':
m_game->getMaze()->setCellType(m_counterRows, i, Cell::GHOSTCAMP);
break;
case 'X': m_game->getMaze()->setCellType(m_counterRows, i, Cell::GHOSTCAMP);
case 'X':
m_game->getMaze()->setCellType(m_counterRows, i, Cell::GHOSTCAMP);
m_game->getMaze()->setResurrectionCell(QPoint(m_counterRows, i));
break;
}
......
......@@ -21,12 +21,12 @@
#include "game.h"
#include <QXmlDefaultHandler>
#include <QXmlStreamAttributes>
/**
* @brief This class handles XML reader events in order to initialize the Maze properties.
*/
class KapmanParser : public QXmlDefaultHandler
class KapmanParser
{
private:
......@@ -52,21 +52,15 @@ public:
*/
~KapmanParser();
/**
* Implement QXmlDefaultHandler::characters
*/
bool characters(const QString &ch) override;
bool characters(const QStringRef &ch);
/**
* Implements QXmlDefaultHandler::startElement()
*/
bool startElement(const QString &, const QString &, const QString &p_qName, const QXmlAttributes &p_atts) override;
bool startElement(const QStringRef &namespaceURI, const QStringRef &localName,
const QStringRef &qName, const QXmlStreamAttributes &atts);
/**
* Implements QXmlDefaultHandler::endElement()
*/
bool endElement(const QString &, const QString &, const QString &p_qName) override;
bool endElement(const QStringRef &namespaceURI,
const QStringRef &localName, const QStringRef &qName);
bool parse(QIODevice *input);
};
#endif
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment