File indexing completed on 2024-04-28 07:36:27

0001 /*
0002  * create a KEduVocDocument from a Pauker file
0003  * SPDX-FileCopyrightText: 2004, 2007 Peter Hedlund <peter.hedlund@kdemail.net>
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "keduvocpaukerreader.h"
0008 
0009 #include <KLocalizedString>
0010 #include <QIODevice>
0011 #include <QTextStream>
0012 
0013 #include "keduvocexpression.h"
0014 #include "keduvoclesson.h"
0015 #include <QDebug>
0016 
0017 KEduVocPaukerReader::KEduVocPaukerReader(QIODevice &dev)
0018     : m_dev(dev)
0019 {
0020 }
0021 
0022 QString KEduVocPaukerReader::errorMessage() const
0023 {
0024     return i18n("Parse error at line %1, column %2:\n%3", lineNumber(), columnNumber(), errorString());
0025 }
0026 
0027 bool KEduVocPaukerReader::isParsable()
0028 {
0029     //@todo fix the xml isParsable to not expect lines as xml doesn't require lines
0030     QTextStream ts(&m_dev);
0031     QString line1(ts.readLine());
0032     QString line2(ts.readLine());
0033 
0034     m_dev.seek(0);
0035     return ((line1.startsWith(QLatin1String("<?xml"))) && (line2.indexOf(QLatin1String("pauker"), 0) > 0));
0036 }
0037 
0038 KEduVocDocument::FileType KEduVocPaukerReader::fileTypeHandled()
0039 {
0040     return KEduVocDocument::Pauker;
0041 }
0042 
0043 KEduVocDocument::ErrorCode KEduVocPaukerReader::read(KEduVocDocument &doc)
0044 {
0045     m_doc = &doc;
0046 
0047     setDevice(&m_dev);
0048 
0049     while (!atEnd()) {
0050         readNext();
0051 
0052         if (isStartElement()) {
0053             if (name() == QStringView(QStringLiteral("Lesson")))
0054                 readPauker();
0055             else {
0056                 qWarning() << i18n("This is not a Pauker document");
0057                 return KEduVocDocument::FileTypeUnknown;
0058             }
0059         }
0060     }
0061 
0062     return error() ? KEduVocDocument::FileReaderFailed : KEduVocDocument::NoError;
0063 }
0064 
0065 void KEduVocPaukerReader::readUnknownElement()
0066 {
0067     while (!atEnd()) {
0068         readNext();
0069 
0070         if (isEndElement())
0071             break;
0072 
0073         if (isStartElement())
0074             readUnknownElement();
0075     }
0076 }
0077 
0078 void KEduVocPaukerReader::readPauker()
0079 {
0080     m_doc->setAuthor(QStringLiteral("http://pauker.sf.net"));
0081     /// Pauker does not provide any column titles
0082     m_doc->appendIdentifier();
0083     m_doc->appendIdentifier();
0084 
0085     while (!atEnd()) {
0086         readNext();
0087 
0088         if (isEndElement())
0089             break;
0090 
0091         if (isStartElement()) {
0092             if (name() == QStringView(QStringLiteral("Description")))
0093                 m_doc->setDocumentComment(readElementText());
0094             else if (name() == QStringView(QStringLiteral("Batch")))
0095                 readBatch();
0096             else
0097                 readUnknownElement();
0098         }
0099     }
0100 }
0101 
0102 void KEduVocPaukerReader::readBatch()
0103 {
0104     while (!atEnd()) {
0105         readNext();
0106 
0107         if (isEndElement())
0108             break;
0109 
0110         if (isStartElement()) {
0111             if (name() == QStringView(QStringLiteral("Card")))
0112                 readCard();
0113             else
0114                 readUnknownElement();
0115         }
0116     }
0117 }
0118 
0119 void KEduVocPaukerReader::readCard()
0120 {
0121     QString front;
0122     QString back;
0123 
0124     while (!atEnd()) {
0125         readNext();
0126 
0127         if (isEndElement())
0128             break;
0129 
0130         if (isStartElement()) {
0131             if (name() == QStringView(QStringLiteral("FrontSide")))
0132                 front = readText();
0133             else if (name() == QStringView(QStringLiteral("ReverseSide")))
0134                 back = readText();
0135             else
0136                 readUnknownElement();
0137         }
0138     }
0139 
0140     KEduVocLesson *lesson = new KEduVocLesson(i18n("Vocabulary"), m_doc->lesson());
0141     m_doc->lesson()->appendChildContainer(lesson);
0142 
0143     KEduVocExpression *expr = new KEduVocExpression(QStringList() << front << back);
0144     lesson->appendEntry(expr);
0145 }
0146 
0147 QString KEduVocPaukerReader::readText()
0148 {
0149     QString result;
0150 
0151     while (!atEnd()) {
0152         readNext();
0153 
0154         if (isEndElement())
0155             break;
0156 
0157         if (isStartElement()) {
0158             if (name() == QStringView(QStringLiteral("Text")))
0159                 result = readElementText();
0160             else
0161                 readUnknownElement();
0162         }
0163     }
0164     return result;
0165 }