File indexing completed on 2024-04-14 03:46:36

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Andreas Xavier <andxav at zoho dot com>
0003  * SPDX-License-Identifier: GPL-2.0-or-later
0004  */
0005 
0006 #ifndef DUMMYREADER_H
0007 #define DUMMYREADER_H
0008 
0009 #include "readerbase.h"
0010 
0011 #include <QXmlStreamReader>
0012 
0013 #include <QDebug>
0014 
0015 class QIODevice;
0016 
0017 /**
0018  * @brief A dummy reader  to be used in unit tests
0019  * @details DummyReader always returns the error message corresponding to the
0020  * file generated with makeDummyString()
0021  *
0022  * */
0023 class DummyReader : public ReaderBase, private QXmlStreamReader
0024 {
0025 public:
0026     /** @brief Given an error produce a file that the dummy reader can detect and return the error.
0027         @param error The error to return
0028         @param msg the message to return
0029         @return string of the file that will generate the error */
0030     static QString makeDummyString(KEduVocDocument::ErrorCode error, const QString &msg = QStringLiteral("Dummy Reader Error"))
0031     // Note: This should be defined in the cpp file, but I was having linker problems
0032     {
0033         QString out;
0034         QXmlStreamWriter stream(&out);
0035         stream.setAutoFormatting(true);
0036         stream.writeStartDocument();
0037         stream.writeStartElement(mTag());
0038         stream.writeTextElement(makeErrorTag(error), msg);
0039         stream.writeEndElement(); // m_tag
0040         stream.writeEndDocument();
0041         qDebug() << "The file" << out;
0042         return out;
0043     }
0044 
0045     /** constructor
0046      @param dev device to parse*/
0047     explicit DummyReader(QIODevice &dev);
0048     /**destructor*/
0049     ~DummyReader() override{};
0050 
0051     /** @brief Can this reader parse this file
0052      *
0053      Read a small portion of the header of the file
0054      and decide if it is a suitable type.
0055      @return true if parsable
0056      */
0057     bool isParsable() Q_DECL_OVERRIDE;
0058 
0059     /** @brief returns the KEduVocDocument::FileType that this reader handles
0060         @return KEduVocDocument::FileType handled
0061      */
0062     KEduVocDocument::FileType fileTypeHandled() Q_DECL_OVERRIDE;
0063 
0064     /**  @brief Parse file and write into doc
0065      @param doc to be written
0066      @return error status of the read.*/
0067     KEduVocDocument::ErrorCode read(KEduVocDocument &doc) Q_DECL_OVERRIDE;
0068 
0069     /** an error message.
0070         @return the error message
0071     */
0072     QString errorMessage() const Q_DECL_OVERRIDE;
0073 
0074 private:
0075     /**
0076         @brief XML tag to identify a dummy file
0077         @return tagname that indicates this is a dummy document
0078     */
0079     static QString mTag()
0080     // Note: This should be defined in the cpp file, but I was having linker problems
0081     {
0082         return QStringLiteral("kvocdocdummyreadertestelement");
0083     }
0084 
0085     /**
0086         @brief Make error into a tabname
0087         @param err Error code to convert
0088         @return tagname
0089     */
0090     static QString makeErrorTag(KEduVocDocument::ErrorCode err)
0091     // Note: This should be defined in the cpp file, but I was having linker problems
0092     {
0093         return "errnum-" + QString::number(int(err));
0094     }
0095 
0096     KEduVocDocument::ErrorCode m_error; ///< The error code to always return;
0097     QString m_errorMessage; ///< The error message
0098     QIODevice &m_dev; ///< input device
0099 };
0100 
0101 #endif // DUMMYREADER_H