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

0001 /*
0002  * create a KEduVocDocument from a XDXF file
0003  * SPDX-FileCopyrightText: 2007 Peter Hedlund <peter.hedlund@kdemail.net>
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "keduvocxdxfreader.h"
0008 
0009 #include <KLocalizedString>
0010 #include <QIODevice>
0011 
0012 #include "keduvocexpression.h"
0013 
0014 KEduVocXdxfReader::KEduVocXdxfReader(QIODevice &dev)
0015     : m_dev(dev)
0016 {
0017 }
0018 
0019 QString KEduVocXdxfReader::errorMessage() const
0020 {
0021     return i18n("Parse error at line %1, column %2:\n%3", lineNumber(), columnNumber(), errorString());
0022 }
0023 
0024 bool KEduVocXdxfReader::isParsable()
0025 {
0026     //@todo fix the xml isParsable to not expect lines as xml doesn't require lines
0027     QTextStream ts(&m_dev);
0028     QString line1(ts.readLine());
0029     QString line2(ts.readLine());
0030 
0031     m_dev.seek(0);
0032     return ((line1.startsWith(QLatin1String("<?xml"))) && (line2.indexOf(QLatin1String("xdxf"), 0) > 0));
0033 }
0034 
0035 KEduVocDocument::FileType KEduVocXdxfReader::fileTypeHandled()
0036 {
0037     return KEduVocDocument::Xdxf;
0038 }
0039 
0040 KEduVocDocument::ErrorCode KEduVocXdxfReader::read(KEduVocDocument &doc)
0041 {
0042     QIODevice *device(&m_dev);
0043     m_doc = &doc;
0044 
0045     setDevice(device);
0046 
0047     while (!atEnd()) {
0048         readNext();
0049 
0050         if (isStartElement()) {
0051             if (name() == QStringView(QStringLiteral("xdxf")))
0052                 readXdxf();
0053             else
0054                 raiseError(i18n("This is not a XDXF document"));
0055         }
0056     }
0057 
0058     return error() ? KEduVocDocument::FileReaderFailed : KEduVocDocument::NoError;
0059 }
0060 
0061 void KEduVocXdxfReader::readUnknownElement()
0062 {
0063     while (!atEnd()) {
0064         readNext();
0065 
0066         if (isEndElement())
0067             break;
0068 
0069         if (isStartElement())
0070             readUnknownElement();
0071     }
0072 }
0073 
0074 void KEduVocXdxfReader::readXdxf()
0075 {
0076     /// The language attributes are required and should be ISO 639-2 codes, but you never know...
0077 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0078     QStringRef
0079 #else
0080     QStringView
0081 #endif
0082         id1 = attributes().value(QStringLiteral("lang_from"));
0083     m_doc->appendIdentifier();
0084     if (!id1.isNull()) {
0085         m_doc->identifier(0).setLocale(id1.toString().toLower());
0086         m_doc->identifier(0).setName(id1.toString().toLower());
0087     }
0088 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0089     QStringRef
0090 #else
0091     QStringView
0092 #endif
0093         id2 = attributes().value(QStringLiteral("lang_to"));
0094     m_doc->appendIdentifier();
0095     if (!id2.isNull()) {
0096         m_doc->identifier(1).setLocale(id2.toString().toLower());
0097         m_doc->identifier(1).setName(id2.toString().toLower());
0098     }
0099 
0100     // Jam it all into one lesson
0101     KEduVocLesson *lesson = new KEduVocLesson(i18n("Lesson %1", 1), m_doc->lesson());
0102     m_doc->lesson()->appendChildContainer(lesson);
0103 
0104     while (!atEnd()) {
0105         readNext();
0106 
0107         if (isEndElement())
0108             break;
0109 
0110         if (isStartElement()) {
0111             if (name() == QStringView(QStringLiteral("description")))
0112                 m_doc->setDocumentComment(readElementText());
0113             else if (name() == QStringView(QStringLiteral("full_name")))
0114                 m_doc->setTitle(readElementText());
0115             else if (name() == QStringView(QStringLiteral("ar")))
0116                 readEntry();
0117             else
0118                 readUnknownElement();
0119         }
0120     }
0121 
0122     m_doc->setAuthor(QStringLiteral("http://xdxf.sf.net"));
0123 }
0124 
0125 void KEduVocXdxfReader::readEntry()
0126 {
0127     QString front;
0128     QString back;
0129 
0130     while (!(isEndElement() && name() == QStringView(QStringLiteral("ar")))) {
0131         readNext();
0132         if (isStartElement() && name() == QStringView(QStringLiteral("k"))) {
0133             front = readElementText();
0134         } else if (isCharacters() || isEntityReference()) {
0135             back.append(text().toString());
0136         }
0137     }
0138 
0139     KEduVocExpression *expr = new KEduVocExpression(front);
0140     expr->setTranslation(1, back);
0141 
0142     KEduVocLesson *lesson(dynamic_cast<KEduVocLesson *>(m_doc->lesson()->childContainer(0)));
0143     if (lesson) {
0144         lesson->appendEntry(expr);
0145     }
0146 }