diff --git a/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.cpp b/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.cpp index 1e84434e..21e0dd85 100644 --- a/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.cpp +++ b/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.cpp @@ -27,21 +27,76 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include +#include #include #include #include +#include +#include +#include +#include #include "PChangeDefaultPathsDialog.h" //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /** - * @brief PDefaultPathsXMLParser::PDefaultPathsXMLParser + *

XML Parser class for the musrfit administration file. + * + * \param fln file name of the musredit startup xml-file * @param defaultPaths */ -PDefaultPathsXMLParser::PDefaultPathsXMLParser(PDefaultPaths *defaultPaths) : +PDefaultPathsXMLParser::PDefaultPathsXMLParser(const QString& fln, PDefaultPaths *defaultPaths) : fDefaultPaths(defaultPaths) { + fValid = false; fKeyWord = eEmpty; + + QFile file(fln); + if (!file.open(QFile::ReadOnly | QFile::Text)) { + // warning and create default - STILL MISSING + } + + fValid = parse(&file); +} + +//-------------------------------------------------------------------------- +/** + *

parse the musrfit startup xml-file. + * + * \param device QFile object of the musrfit startup xml-file + * + * @return true on success, false otherwise + */ +bool PDefaultPathsXMLParser::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; } //-------------------------------------------------------------------------- @@ -50,6 +105,7 @@ PDefaultPathsXMLParser::PDefaultPathsXMLParser(PDefaultPaths *defaultPaths) : */ bool PDefaultPathsXMLParser::startDocument() { + // nothing to be done here for now return true; } @@ -57,13 +113,11 @@ bool PDefaultPathsXMLParser::startDocument() /** *

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 PDefaultPathsXMLParser::startElement( const QString&, const QString&, - const QString& qName, - const QXmlAttributes& ) +bool PDefaultPathsXMLParser::startElement() { + QStringRef qName = fXml.name(); + if (qName == "data_path") { fKeyWord = eDataPath; } @@ -75,10 +129,8 @@ bool PDefaultPathsXMLParser::startElement( const QString&, const QString&, /** *

Routine called when the end XML tag is found. It is used to * put the filtering tag to 'empty'. - * - * \param qName name of the element. */ -bool PDefaultPathsXMLParser::endElement( const QString&, const QString&, const QString & ) +bool PDefaultPathsXMLParser::endElement() { fKeyWord = eEmpty; @@ -89,11 +141,13 @@ bool PDefaultPathsXMLParser::endElement( const QString&, const QString&, const Q /** *

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 PDefaultPathsXMLParser::characters(const QString& str) +bool PDefaultPathsXMLParser::characters() { + QString str = *fXml.text().string(); + if (str.isEmpty()) + return true; + switch (fKeyWord) { case eDataPath: fDefaultPaths->appendDefaultPath(str); @@ -115,67 +169,10 @@ bool PDefaultPathsXMLParser::endDocument() return true; } -//-------------------------------------------------------------------------- -/** - *

Report XML warnings. - * - * \param exception holds the information of the XML warning - */ -bool PDefaultPathsXMLParser::warning( const QXmlParseException & exception ) -{ - QString msg; - - msg = QString("**WARNING** while parsing musrfit_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; -} - -//-------------------------------------------------------------------------- -/** - *

Report recoverable XML errors. - * - * \param exception holds the information of the XML recoverable errors. - */ -bool PDefaultPathsXMLParser::error( const QXmlParseException & exception ) -{ - QString msg; - - msg = QString("**ERROR** while parsing musrfit_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; -} - -//-------------------------------------------------------------------------- -/** - *

Report fatal XML errors. - * - * \param exception holds the information of the XML fatal errors. - */ -bool PDefaultPathsXMLParser::fatalError( const QXmlParseException & exception ) -{ - QString msg; - - msg = QString("**FATAL ERROR** while parsing musrfit_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; -} - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +/** + * @brief PDefaultPaths::PDefaultPaths + */ PDefaultPaths::PDefaultPaths() : QObject() { fValid = true; @@ -204,13 +201,8 @@ PDefaultPaths::PDefaultPaths() : QObject() fPrefPathName = pathFln; // read musrfit_startup.xml and extract default data file search paths - PDefaultPathsXMLParser handler(this); - QFile xmlFile(fPrefPathName); - QXmlInputSource source( &xmlFile ); - QXmlSimpleReader reader; - reader.setContentHandler( &handler ); - reader.setErrorHandler( &handler ); - if (!reader.parse( source )) { + PDefaultPathsXMLParser handler(fPrefPathName, this); + if (!handler.isValid()) { fValid = false; QMessageBox::critical(0, "ERROR", "Error parsing musrfit_startup.xml settings file.\nProbably a few things will not work porperly.\nPlease fix this first.", diff --git a/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.h b/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.h index 0eb73061..e4aa0de4 100644 --- a/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.h +++ b/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.h @@ -32,36 +32,36 @@ #include #include -#include +#include +#include #include "ui_PChangeDefaultPathsDialog.h" class PDefaultPaths; //--------------------------------------------------------------------------- -class PDefaultPathsXMLParser : public QXmlDefaultHandler +class PDefaultPathsXMLParser { public: - PDefaultPathsXMLParser(PDefaultPaths *defaultPaths); + PDefaultPathsXMLParser(const QString &fln, PDefaultPaths *defaultPaths); virtual ~PDefaultPathsXMLParser() {} + virtual bool isValid() { return fValid; } + private: enum EAdminKeyWords {eEmpty, eDataPath}; + 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 - PDefaultPaths *fDefaultPaths; ///< keeps the default search paths for the data files + 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 + PDefaultPaths *fDefaultPaths; ///< keeps the default search paths for the data files }; //---------------------------------------------------------------------------