File indexing completed on 2024-05-12 15:55:24

0001 // SPDX-FileCopyrightText: 2013 Jesper K. Pedersen <jesper.pedersen@kdab.com>
0002 // SPDX-FileCopyrightText: 2014-2023 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0003 //
0004 // SPDX-License-Identifier: GPL-2.0-or-later
0005 
0006 #ifndef XMLREADER_H
0007 #define XMLREADER_H
0008 
0009 #include <QSharedPointer>
0010 #include <QXmlStreamReader>
0011 
0012 namespace DB
0013 {
0014 class UIDelegate;
0015 
0016 struct ElementInfo {
0017     ElementInfo(bool isStartToken, const QString &tokenName)
0018         : isValid(true)
0019         , isStartToken(isStartToken)
0020         , tokenName(tokenName)
0021     {
0022     }
0023     ElementInfo()
0024         : isValid(false)
0025         , isStartToken(false)
0026     {
0027     }
0028 
0029     bool isValid;
0030     bool isStartToken;
0031     QString tokenName;
0032 };
0033 
0034 class XmlReader : public QXmlStreamReader
0035 {
0036 public:
0037     /**
0038      * @brief XmlReader
0039      * @param ui the UIDelegate for error messages
0040      * @param friendlyStreamName a stream/file name to be displayed in messages
0041      */
0042     explicit XmlReader(DB::UIDelegate &ui, const QString &friendlyStreamName);
0043 
0044     QString attribute(const QString &name, const QString &defaultValue = QString());
0045     ElementInfo readNextStartOrStopElement(const QString &expectedStart);
0046     /**
0047      * Read the next element and ensure that it's an EndElement.
0048      * If the XmlReader has already read the EndElement (e.g. by calling readNextStartOrStopElement()),
0049      * but you want to use this method to ensure consistent error messages, you can
0050      * set the parameter readNextElement to false.
0051      *
0052      * @param readNextElement if set to false, don't read the next element.
0053      */
0054     void readEndElement(bool readNextElement = true);
0055     bool hasAttribute(const QString &name);
0056     ElementInfo peekNext();
0057     [[noreturn]] void complainStartElementExpected(const QString &name);
0058 
0059 private:
0060     [[noreturn]] void reportError(const QString &);
0061     QString tokenToString(TokenType);
0062     TokenType readNextInternal();
0063 
0064     DB::UIDelegate &m_ui;
0065     ElementInfo m_peek;
0066     const QString m_streamName;
0067 };
0068 
0069 typedef QSharedPointer<XmlReader> ReaderPtr;
0070 
0071 }
0072 
0073 #endif // XMLREADER_H
0074 // vi:expandtab:tabstop=4 shiftwidth=4: