Commit 1ba41465 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

QXmlSimpleReader is deprecated port to QXmlStreamReader

parent 09faf14f
......@@ -290,13 +290,10 @@ void ArenaSelector::Private::_k_updatePreview(QListWidgetItem* currentItem)
m_arena = new Arena;
MapParser mapParser(m_arena);
QFile arenaXmlFile(selArena->graphics());
QXmlInputSource source(&arenaXmlFile);
// Create the XML file reader
QXmlSimpleReader reader;
reader.setContentHandler(&mapParser);
// Parse the XML file
reader.parse(source);
if (!arenaXmlFile.open(QIODevice::ReadOnly)) {
qWarning() << " impossible to open file " << arenaXmlFile.fileName();
}
mapParser.parse(&arenaXmlFile);
while(!m_arenaItems.isEmpty())
{
if(m_graphicsScene->items().contains(m_arenaItems.last()))
......
......@@ -145,12 +145,10 @@ void Game::init()
QString arenaFileName = group.readEntry("FileName");
QFile arenaXmlFile(QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("arenas/%1").arg(arenaFileName)));
QXmlInputSource source(&arenaXmlFile);
// Create the XML file reader
QXmlSimpleReader reader;
reader.setContentHandler(&mapParser);
// Parse the XML file
reader.parse(source);
if (!arenaXmlFile.open(QIODevice::ReadOnly)) {
qWarning() << " impossible to open file " << arenaFileName;
}
mapParser.parse(&arenaXmlFile);
QString arenaName = group.readEntry("Name");
m_arena->setName(arenaName);
......
......@@ -33,34 +33,63 @@ MapParser::~MapParser()
{
}
bool MapParser::characters(const QString & ch)
bool MapParser::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 MapParser::characters(const QStringRef &ch)
{
m_buffer = ch.toString();
return true;
}
bool MapParser::startElement(const QString&, const QString&, const QString& p_qName, const QXmlAttributes& p_atts)
bool MapParser::startElement(const QStringRef &namespaceURI, const QStringRef &localName, const QStringRef &qName, const QXmlStreamAttributes &atts)
{
if (p_qName == QLatin1String("Arena"))
{
Q_UNUSED(namespaceURI)
Q_UNUSED(localName)
if (qName == QLatin1String("Arena")) {
int nbRows = 0;
int nbColumns = 0;
// Initialize the number of rows and columns
for (int i = 0; i < p_atts.count(); ++i)
{
if (p_atts.qName(i) == QLatin1String("rowCount"))
{
nbRows = p_atts.value(i).toInt();
}
if (p_atts.qName(i) == QLatin1String("colCount"))
{
nbColumns = p_atts.value(i).toInt();
}
//TODO:check for the right arenaFileVersion
//if (p_atts.qName(i) == "arenaFileVersion")
//{
// m_arenaFileVersion = 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 Arena matrix
m_arena->init(nbRows, nbColumns);
......@@ -69,10 +98,12 @@ bool MapParser::startElement(const QString&, const QString&, const QString& p_qN
return true;
}
bool MapParser::endElement(const QString &, const QString &, const QString & p_qName)
bool MapParser::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; i<m_buffer.length();++i)
{
switch(m_buffer.at(i).toLatin1())
......
......@@ -19,15 +19,13 @@
#ifndef MAPPARSER_H
#define MAPPARSER_H
#include <QXmlDefaultHandler>
#include <QXmlStreamAttributes>
class Arena;
/**
* @brief This class handles XML reader events in order to initialize the Arena properties.
*/
class MapParser : public QXmlDefaultHandler
class MapParser
{
private:
......@@ -51,23 +49,17 @@ public:
/**
* Deletes the GameParser instance.
*/
~MapParser() override;
~MapParser();
/**
* Implement QXmlDefaultHandler::characters
*/
bool characters(const QString & ch ) override;
Q_REQUIRED_RESULT bool characters(const QStringRef &ch);
/**
* Implements QXmlDefaultHandler::startElement()
*/
bool startElement(const QString&, const QString&, const QString& p_qName, const QXmlAttributes& p_atts) override;
Q_REQUIRED_RESULT 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;
Q_REQUIRED_RESULT bool endElement(const QStringRef &namespaceURI,
const QStringRef &localName, const QStringRef &qName);
Q_REQUIRED_RESULT 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