LibKEduVocDocument

keduvocpaukerreader.cpp
1/*
2 * create a KEduVocDocument from a Pauker file
3 * SPDX-FileCopyrightText: 2004, 2007 Peter Hedlund <peter.hedlund@kdemail.net>
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "keduvocpaukerreader.h"
8
9#include <KLocalizedString>
10#include <QIODevice>
11#include <QTextStream>
12
13#include "keduvocexpression.h"
14#include "keduvoclesson.h"
15#include <QDebug>
16
21
23{
24 return i18n("Parse error at line %1, column %2:\n%3", lineNumber(), columnNumber(), errorString());
25}
26
28{
29 //@todo fix the xml isParsable to not expect lines as xml doesn't require lines
30 QTextStream ts(&m_dev);
31 QString line1(ts.readLine());
32 QString line2(ts.readLine());
33
34 m_dev.seek(0);
35 return ((line1.startsWith(QLatin1String("<?xml"))) && (line2.indexOf(QLatin1String("pauker"), 0) > 0));
36}
37
42
44{
45 m_doc = &doc;
46
47 setDevice(&m_dev);
48
49 while (!atEnd()) {
50 readNext();
51
52 if (isStartElement()) {
53 if (name() == QStringView(QStringLiteral("Lesson")))
54 readPauker();
55 else {
56 qWarning() << i18n("This is not a Pauker document");
58 }
59 }
60 }
61
63}
64
65void KEduVocPaukerReader::readUnknownElement()
66{
67 while (!atEnd()) {
68 readNext();
69
70 if (isEndElement())
71 break;
72
73 if (isStartElement())
74 readUnknownElement();
75 }
76}
77
78void KEduVocPaukerReader::readPauker()
79{
80 m_doc->setAuthor(QStringLiteral("http://pauker.sf.net"));
81 /// Pauker does not provide any column titles
82 m_doc->appendIdentifier();
83 m_doc->appendIdentifier();
84
85 while (!atEnd()) {
86 readNext();
87
88 if (isEndElement())
89 break;
90
91 if (isStartElement()) {
92 if (name() == QStringView(QStringLiteral("Description")))
94 else if (name() == QStringView(QStringLiteral("Batch")))
95 readBatch();
96 else
97 readUnknownElement();
98 }
99 }
100}
101
102void KEduVocPaukerReader::readBatch()
103{
104 while (!atEnd()) {
105 readNext();
106
107 if (isEndElement())
108 break;
109
110 if (isStartElement()) {
111 if (name() == QStringView(QStringLiteral("Card")))
112 readCard();
113 else
114 readUnknownElement();
115 }
116 }
117}
118
119void KEduVocPaukerReader::readCard()
120{
121 QString front;
123
124 while (!atEnd()) {
125 readNext();
126
127 if (isEndElement())
128 break;
129
130 if (isStartElement()) {
131 if (name() == QStringView(QStringLiteral("FrontSide")))
132 front = readText();
133 else if (name() == QStringView(QStringLiteral("ReverseSide")))
134 back = readText();
135 else
136 readUnknownElement();
137 }
138 }
139
140 KEduVocLesson *lesson = new KEduVocLesson(i18n("Vocabulary"), m_doc->lesson());
141 m_doc->lesson()->appendChildContainer(lesson);
142
143 KEduVocExpression *expr = new KEduVocExpression(QStringList() << front << back);
144 lesson->appendEntry(expr);
145}
146
147QString KEduVocPaukerReader::readText()
148{
149 QString result;
150
151 while (!atEnd()) {
152 readNext();
153
154 if (isEndElement())
155 break;
156
157 if (isStartElement()) {
158 if (name() == QStringView(QStringLiteral("Text")))
159 result = readElementText();
160 else
161 readUnknownElement();
162 }
163 }
164 return result;
165}
The primary entry point to the hierarchy of objects describing vocabularies.
int appendIdentifier(const KEduVocIdentifier &identifier=KEduVocIdentifier())
Appends a new identifier (usually a language)
void setDocumentComment(const QString &comment)
Set the comment of the file.
void setAuthor(const QString &author)
Set the author of the file.
KEduVocLesson * lesson()
Get the lesson root object.
ErrorCode
the return code when opening/saving
@ FileTypeUnknown
unknown file type
@ FileReaderFailed
file reader failed
FileType
known vocabulary file types
@ Pauker
Pauker format.
This class contains one vocabulary expression as an original with one or more translations.
class to store information about a lesson
void appendEntry(KEduVocExpression *entry)
append an entry to the lesson
bool isParsable() override
Can this reader parse this file.
KEduVocDocument::FileType fileTypeHandled() override
returns the KEduVocDocument::FileType that this reader handles
KEduVocPaukerReader(QIODevice &file)
constructor
KEduVocDocument::ErrorCode read(KEduVocDocument &doc) override
Parse file and write into doc.
QString errorMessage() const override
an error message.
QString i18n(const char *text, const TYPE &arg...)
const QList< QKeySequence > & back()
virtual bool seek(qint64 pos)
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString readLine(qint64 maxlen)
bool atEnd() const const
qint64 columnNumber() const const
Error error() const const
QString errorString() const const
bool isEndElement() const const
bool isStartElement() const const
qint64 lineNumber() const const
QStringView name() const const
QString readElementText(ReadElementTextBehaviour behaviour)
TokenType readNext()
void setDevice(QIODevice *device)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.