replaced obsolete QXmlDefaultHandler by QXmlStreamReader in mupp

This commit is contained in:
suter_a 2020-06-18 20:24:31 +02:00
parent a2d7c58a97
commit 5e9e075133
4 changed files with 80 additions and 96 deletions

View File

@ -32,6 +32,7 @@
#include <QMessageBox>
#include <QString>
#include <QStringRef>
#include <QFile>
#include <QTextStream>
#include <QVector>
@ -82,9 +83,56 @@ void PmuppColor::setRGB(const int r, const int g, const int b)
*
* \param admin pointer to an admin class instance.
*/
PmuppAdminXMLParser::PmuppAdminXMLParser(PmuppAdmin *admin) : fAdmin(admin)
PmuppAdminXMLParser::PmuppAdminXMLParser(const QString& fln, PmuppAdmin *admin) : fAdmin(admin)
{
fValid = false;
fKeyWord = eEmpty;
QFile file(fln);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
// warning and create default - STILL MISSING
}
fValid = parse(&file);
}
//--------------------------------------------------------------------------
/**
* <p>parse the mupp startup xml-file.
*
* \param device QFile object of the mupp startup xml-file
*
* @return true on success, false otherwise
*/
bool PmuppAdminXMLParser::parse(QIODevice *device)
{
fXml.setDevice(device);
bool expectChars = false;
while (!fXml.atEnd()) {
fXml.readNext();
if (fXml.isStartDocument()) {
startDocument();
} else if (fXml.isStartElement()) {
startElement();
expectChars = true;
} else if (fXml.isCharacters() && expectChars) {
characters();
} else if (fXml.isEndElement()) {
endElement();
expectChars = false;
} else if (fXml.isEndDocument()) {
endDocument();
}
}
if (fXml.hasError()) {
QString msg;
msg = QString("%1 Line %2, column %3").arg(fXml.errorString()).arg(fXml.lineNumber()).arg(fXml.columnNumber());
QMessageBox::critical(0, "**ERROR**", msg, QMessageBox::Ok, QMessageBox::NoButton);
return false;
}
return true;
}
//--------------------------------------------------------------------------
@ -93,6 +141,7 @@ PmuppAdminXMLParser::PmuppAdminXMLParser(PmuppAdmin *admin) : fAdmin(admin)
*/
bool PmuppAdminXMLParser::startDocument()
{
// nothing to be done here for now
return true;
}
@ -100,13 +149,11 @@ bool PmuppAdminXMLParser::startDocument()
/**
* <p>Routine called when a new XML tag is found. Here it is used
* to set a tag for filtering afterwards the content.
*
* \param qName name of the XML tag.
*/
bool PmuppAdminXMLParser::startElement( const QString&, const QString&,
const QString& qName,
const QXmlAttributes& )
bool PmuppAdminXMLParser::startElement()
{
QStringRef qName = fXml.name();
if (qName == "path_file_name") {
fKeyWord = eRecentFile;
} else if (qName == "dark_theme") {
@ -125,10 +172,8 @@ bool PmuppAdminXMLParser::startElement( const QString&, const QString&,
* <p>Routine called when the end XML tag is found. It is used to
* put the filtering tag to 'empty'. It also resets the fFunc flag in case
* the entry was a theory function.
*
* \param qName name of the element.
*/
bool PmuppAdminXMLParser::endElement( const QString&, const QString&, const QString& )
bool PmuppAdminXMLParser::endElement()
{
fKeyWord = eEmpty;
@ -139,11 +184,13 @@ bool PmuppAdminXMLParser::endElement( const QString&, const QString&, const QStr
/**
* <p>This routine delivers the content of an XML tag. It fills the
* content into the load data structure.
*
* \param str keeps the content of the XML tag.
*/
bool PmuppAdminXMLParser::characters(const QString& str)
bool PmuppAdminXMLParser::characters()
{
QString str = *fXml.text().string();
if (str.isEmpty())
return true;
bool ok;
int ival, r, g, b;
double dval;
@ -218,66 +265,6 @@ bool PmuppAdminXMLParser::endDocument()
return true;
}
//--------------------------------------------------------------------------
/**
* <p>Report XML warnings.
*
* \param exception holds the information of the XML warning
*/
bool PmuppAdminXMLParser::warning( const QXmlParseException & exception )
{
QString msg;
msg = QString("**WARNING** while parsing mupp_startup.xml in line no %1\n").arg(exception.lineNumber());
msg += QString("**WARNING MESSAGE** ") + exception.message();
qWarning() << endl << msg << endl;
QMessageBox::warning(0, "WARNING", msg, QMessageBox::Ok, QMessageBox::NoButton);
return true;
}
//--------------------------------------------------------------------------
/**
* <p>Report recoverable XML errors.
*
* \param exception holds the information of the XML recoverable errors.
*/
bool PmuppAdminXMLParser::error( const QXmlParseException & exception )
{
QString msg;
msg = QString("**ERROR** while parsing mupp_startup.xml in line no %1\n").arg(exception.lineNumber());
msg += QString("**ERROR MESSAGE** ") + exception.message();
qWarning() << endl << msg << endl;
QMessageBox::critical(0, "ERROR", msg, QMessageBox::Ok, QMessageBox::NoButton);
return true;
}
//--------------------------------------------------------------------------
/**
* <p>Report fatal XML errors.
*
* \param exception holds the information of the XML fatal errors.
*/
bool PmuppAdminXMLParser::fatalError( const QXmlParseException & exception )
{
QString msg;
msg = QString("**FATAL ERROR** while parsing mupp_startup.xml in line no %1\n").arg(exception.lineNumber());
msg += QString("**FATAL ERROR MESSAGE** ") + exception.message();
qWarning() << endl << msg << endl;
QMessageBox::critical(0, "FATAL ERROR", msg, QMessageBox::Ok, QMessageBox::NoButton);
return true;
}
//--------------------------------------------------------------------------
// implementation of PmuppAdmin class
//--------------------------------------------------------------------------
@ -315,21 +302,16 @@ PmuppAdmin::PmuppAdmin() : QObject(), fDarkTheme(false)
}
}
if (QFile::exists(pathFln)) { // administration file present
PmuppAdminXMLParser handler(this);
QFile xmlFile(pathFln);
QXmlInputSource source( &xmlFile );
QXmlSimpleReader reader;
reader.setContentHandler( &handler );
reader.setErrorHandler( &handler );
if (!reader.parse( source )) {
QMessageBox::critical(0, "ERROR",
if (QFile::exists(pathFln)) { // administration file present
PmuppAdminXMLParser handler(pathFln, this);
if (!handler.isValid()) {
QMessageBox::critical(0, "**ERROR**",
"Error parsing mupp_startup.xml settings file.\nProbably a few things will not work porperly.\nPlease fix this first.",
QMessageBox::Ok, QMessageBox::NoButton);
return;
}
} else {
QMessageBox::critical(0, "ERROR",
QMessageBox::critical(0, "**ERROR**",
"Couldn't find the mupp_startup.xml settings file.\nProbably a few things will not work porperly.\nPlease fix this first.",
QMessageBox::Ok, QMessageBox::NoButton);
return;

View File

@ -34,7 +34,7 @@
#include <QVector>
#include <QMap>
#include <QPixmap>
#include <QtXml>
#include <QXmlStreamReader>
#include "mupp.h"
@ -93,29 +93,28 @@ class PmuppMarker {
* necessary informations about executable pathes, online help informations,
* default font sizes, etc.
*/
class PmuppAdminXMLParser : public QXmlDefaultHandler
class PmuppAdminXMLParser
{
public:
PmuppAdminXMLParser(PmuppAdmin*);
PmuppAdminXMLParser(const QString &fln, PmuppAdmin*);
virtual ~PmuppAdminXMLParser() {}
virtual bool isValid() { return fValid; }
private:
enum EAdminKeyWords {eEmpty, eRecentFile, eDarkTheme, eMarker, eColor};
bool parse(QIODevice *device);
bool startDocument();
bool startElement( const QString&, const QString&, const QString& ,
const QXmlAttributes& );
bool endElement( const QString&, const QString&, const QString& );
bool characters(const QString&);
bool startElement();
bool endElement();
bool characters();
bool endDocument();
bool warning( const QXmlParseException & exception );
bool error( const QXmlParseException & exception );
bool fatalError( const QXmlParseException & exception );
EAdminKeyWords fKeyWord; ///< key word tag to know how to handle the content
PmuppAdmin *fAdmin; ///< a pointer to the main administration class object
QXmlStreamReader fXml; ///< xml stream reader object
bool fValid; ///< flag showing if XML read has been successful
EAdminKeyWords fKeyWord; ///< key word tag to know how to handle the content
PmuppAdmin *fAdmin; ///< a pointer to the main administration class object
};
//---------------------------------------------------------------------------

View File

@ -27,6 +27,7 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <cmath>
#include <iostream>
#include <QProcessEnvironment>

View File

@ -37,6 +37,8 @@
#include <QString>
#include <QStringList>
#include <QVector>
#include <QTextStream>
#include <QTimer>
#include "mupp_version.h"
#include "PmuppScript.h"