File indexing completed on 2024-04-21 03:48:18

0001 /*
0002  * read a KEduVocDocument from a KVTML2 file
0003  * SPDX-FileCopyrightText: 2007 Jeremy Whiting <jpwhiting@kde.org>
0004  * SPDX-FileCopyrightText: 2007-2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #ifndef KEDUVOCKVTML2READER_H
0009 #define KEDUVOCKVTML2READER_H
0010 
0011 #include <QDomDocument>
0012 #include <QMap>
0013 
0014 #include "keduvocarticle.h"
0015 #include "keduvocdocument.h"
0016 #include "keduvocexpression.h"
0017 #include "keduvocmultiplechoice.h"
0018 #include "keduvocpersonalpronoun.h"
0019 #include "readerbase.h"
0020 
0021 class QIODevice;
0022 class KEduVocDocument;
0023 class KEduVocWordType;
0024 
0025 /**
0026  * @brief class to read kvtml2 data files into keduvocdocument
0027  * @author Jeremy Whiting
0028  */
0029 class KEduVocKvtml2Reader : public QObject, public ReaderBase
0030 {
0031     Q_OBJECT
0032 public:
0033     /** constructor */
0034     explicit KEduVocKvtml2Reader(QIODevice &file);
0035     /**destructor*/
0036     ~KEduVocKvtml2Reader() override{};
0037 
0038     /** @brief Can this reader parse this file
0039      *
0040      Read a small portion of the header of the file
0041      and decide if it is a suitable type.
0042      @return true if parsable
0043      */
0044     bool isParsable() Q_DECL_OVERRIDE;
0045 
0046     /** @brief returns the KEduVocDocument::FileType that this reader handles
0047         @return KEduVocDocument::FileType handled
0048      */
0049     KEduVocDocument::FileType fileTypeHandled() Q_DECL_OVERRIDE;
0050 
0051     /**  @brief Parse file and write into doc
0052      @param doc to be written
0053      @return error status of the read.*/
0054     KEduVocDocument::ErrorCode read(KEduVocDocument &doc) Q_DECL_OVERRIDE;
0055 
0056     /** an error message.
0057         @return the error message
0058     */
0059     QString errorMessage() const Q_DECL_OVERRIDE
0060     {
0061         return m_errorMessage;
0062     }
0063 
0064 private:
0065     /** read information entries
0066      * @param informationElement QDomElement information
0067      */
0068     bool readInformation(QDomElement &informationElement);
0069 
0070     /** read group elements: identifiers, entries, types, usages, lessons */
0071     bool readGroups(QDomElement &domElementParent);
0072 
0073     /** read an identifier
0074      * @param identifierElement QDomElement for the identifier to read
0075      */
0076     bool readIdentifier(QDomElement &identifierElement);
0077 
0078     /** read an identifiers articles
0079      * @param articleElement QDomElement for the article group
0080      * @param identifierNum number of the identifier this article is inside of
0081      */
0082     bool readArticle(QDomElement &articleElement, int identifierNum);
0083 
0084     bool readPersonalPronoun(QDomElement &conjugElement, KEduVocPersonalPronoun &pronoun);
0085 
0086     bool readPersonalPronounChild(QDomElement &personElement, KEduVocPersonalPronoun &pronoun, KEduVocWordFlags flags);
0087 
0088     /** read the types
0089      * @param typesElement QDomElement for the types group
0090      */
0091     bool readWordType(KEduVocWordType *parentContainer, QDomElement &typesElement);
0092 
0093     /**
0094      * Read a leitner box container.
0095      * This is a grading system where the vocabulary are kept in boxes and promoted/demoted during the learning.
0096      * Be aware that leitner boxes are a list only and no sub boxes will ever be read or written.
0097      * While reusing the lesson class is quite easy for this a proper subclass of KEduVocContainer would be the better solution.
0098      * @param parentContainer the parent to append the new leitner container to
0099      * @param leitnerElement the element in the dom
0100      * @return success
0101      */
0102     bool readLeitner(KEduVocLeitnerBox *parentContainer, QDomElement &leitnerElement);
0103 
0104     /**
0105      * Read all <container> tags within a word type definition.
0106      * @param parentContainer
0107      * @param lessonElement
0108      * @return
0109      */
0110     bool readChildWordTypes(KEduVocWordType *parentContainer, QDomElement &lessonElement);
0111 
0112     /** read the tenses
0113      * @param tensesElement QDomElement for the tenses group
0114      */
0115     QStringList readTenses(QDomElement &tensesElement);
0116 
0117     /** read the usages
0118      * @param usagesElement QDomElement for the usages group
0119      */
0120     bool readUsages(QDomElement &usagesElement);
0121 
0122     /** read an entry
0123      * @param entryElement QDomElement for the entry to read
0124      */
0125     bool readEntry(QDomElement &entryElement);
0126 
0127     /** read a translation
0128      * @param translationElement QDomElement for the translation to read
0129      */
0130     bool readTranslation(QDomElement &translationElement, KEduVocExpression *expr, int index);
0131 
0132     /** read a comparison
0133      * @param comparisonElement comparison group element
0134      * @param comp comparison object to read into
0135      */
0136     bool readComparison(QDomElement &comparisonElement, KEduVocTranslation *translation);
0137 
0138     /** read a multiple choice group
0139      * @param multipleChoiceElement element to read from
0140      * @param mc KEduVocMultipleChoice object to read to
0141      */
0142     bool readMultipleChoice(QDomElement &multipleChoiceElement, KEduVocTranslation *translation);
0143 
0144     /**
0145      * Read <lesson> tags.
0146      * @param parentLesson
0147      * @param lessonElement
0148      * @return
0149      */
0150     bool readChildLessons(KEduVocLesson *parentLesson, QDomElement &lessonElement);
0151 
0152     /** read a lesson, and append it to the document
0153      * @param lessonElement element to read from
0154      */
0155     bool readLesson(KEduVocLesson *parentLesson, QDomElement &lessonElement);
0156 
0157     bool readSynonymsAntonymsFalseFriends(QDomElement &rootElement);
0158 
0159     /** pre-opened QIODevice to read from */
0160     QIODevice *m_inputFile;
0161 
0162     /** KEduVocDocument to read to */
0163     KEduVocDocument *m_doc;
0164 
0165     /** because we read the entries first, we store them here temporarily.
0166      * later we read the lessons and put the entries there based on the key (their id) */
0167     QMap<int, KEduVocExpression *> m_allEntries;
0168 
0169     /** error message */
0170     QString m_errorMessage;
0171 };
0172 
0173 #endif