1 /*************************************************************************** 2 File : XmlStreamReader.h 3 Project : SciDAVis 4 Description : XML stream parser that supports errors as well as warnings 5 -------------------------------------------------------------------- 6 Copyright : (C) 2008-2009 Tilman Benkert (thzs*gmx.net) 7 (replace * with @ in the email addresses) 8 9 ***************************************************************************/ 10 11 /*************************************************************************** 12 * * 13 * This program is free software; you can redistribute it and/or modify * 14 * it under the terms of the GNU General Public License as published by * 15 * the Free Software Foundation; either version 2 of the License, or * 16 * (at your option) any later version. * 17 * * 18 * This program is distributed in the hope that it will be useful, * 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 21 * GNU General Public License for more details. * 22 * * 23 * You should have received a copy of the GNU General Public License * 24 * along with this program; if not, write to the Free Software * 25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 26 * Boston, MA 02110-1301 USA * 27 * * 28 ***************************************************************************/ 29 #ifndef XML_STREAM_READER_H 30 #define XML_STREAM_READER_H 31 32 #include <QXmlStreamReader> 33 #include <QString> 34 #include <QStringList> 35 #include "lib/macros.h" 36 37 //! XML stream parser that supports errors as well as warnings 38 /** 39 * This class also adds line and column numbers to the error message. 40 */ 41 class XmlStreamReader : public QXmlStreamReader 42 { 43 public: 44 XmlStreamReader(); 45 XmlStreamReader(QIODevice *device); 46 XmlStreamReader(const QByteArray &data); 47 XmlStreamReader(const QString &data); 48 XmlStreamReader(const char *data); 49 50 QStringList warningStrings() const; 51 bool hasWarnings() const; 52 void raiseWarning(const QString &message = QString()); 53 void raiseError(const QString &message = QString()); 54 CLASS_ACCESSOR(QString, d_error_prefix, errorPrefix, ErrorPrefix); 55 CLASS_ACCESSOR(QString, d_error_postfix, errorPostfix, ErrorPostfix); 56 CLASS_ACCESSOR(QString, d_warning_prefix, warningPrefix, WarningPrefix); 57 CLASS_ACCESSOR(QString, d_warning_postfix, warningPostfix, WarningPostfix); 58 59 //! Go to the next start or end element tag 60 /** 61 * If the end of the document is reached, an error is raised. 62 * 63 * \return false if end of document reached, otherwise true 64 */ 65 bool skipToNextTag(); 66 //! Go to the end element tag of the current element 67 /** 68 * If the end of the document is reached, an error is raised. 69 * 70 * \return false if end of document reached, otherwise true 71 */ 72 bool skipToEndElement(); 73 74 //! Read an XML attribute and convert it to int 75 /** 76 * \param name attribute name 77 * \param ok pointer to report back whether the attribute value could be determined (may be 78 * NULL) \return the attriute value if found and converted, otherwise zero (in this case *ok is 79 * false) 80 */ 81 int readAttributeInt(const QString &name, bool *ok); 82 double readAttributeDouble(const QString &name, bool *ok); 83 84 private: 85 QStringList d_warnings; 86 QString d_error_prefix; 87 QString d_error_postfix; 88 QString d_warning_prefix; 89 QString d_warning_postfix; 90 91 void init(); 92 }; 93 94 #endif // XML_STREAM_READER_H 95