LibKEduVocDocument

keduvocxdxfreader.cpp
1/*
2 * create a KEduVocDocument from a XDXF file
3 * SPDX-FileCopyrightText: 2007 Peter Hedlund <peter.hedlund@kdemail.net>
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "keduvocxdxfreader.h"
8
9#include <KLocalizedString>
10#include <QIODevice>
11
12#include "keduvocexpression.h"
13
15 : m_dev(dev)
16{
17}
18
20{
21 return i18n("Parse error at line %1, column %2:\n%3", lineNumber(), columnNumber(), errorString());
22}
23
25{
26 //@todo fix the xml isParsable to not expect lines as xml doesn't require lines
27 QTextStream ts(&m_dev);
28 QString line1(ts.readLine());
29 QString line2(ts.readLine());
30
31 m_dev.seek(0);
32 return ((line1.startsWith(QLatin1String("<?xml"))) && (line2.indexOf(QLatin1String("xdxf"), 0) > 0));
33}
34
39
41{
42 QIODevice *device(&m_dev);
43 m_doc = &doc;
44
46
47 while (!atEnd()) {
48 readNext();
49
50 if (isStartElement()) {
51 if (name() == QStringView(QStringLiteral("xdxf")))
52 readXdxf();
53 else
54 raiseError(i18n("This is not a XDXF document"));
55 }
56 }
57
59}
60
61void KEduVocXdxfReader::readUnknownElement()
62{
63 while (!atEnd()) {
64 readNext();
65
66 if (isEndElement())
67 break;
68
69 if (isStartElement())
70 readUnknownElement();
71 }
72}
73
74void KEduVocXdxfReader::readXdxf()
75{
76 /// The language attributes are required and should be ISO 639-2 codes, but you never know...
77 QStringView id1 = attributes().value(QStringLiteral("lang_from"));
78 m_doc->appendIdentifier();
79 if (!id1.isNull()) {
80 m_doc->identifier(0).setLocale(id1.toString().toLower());
81 m_doc->identifier(0).setName(id1.toString().toLower());
82 }
83 QStringView id2 = attributes().value(QStringLiteral("lang_to"));
84 m_doc->appendIdentifier();
85 if (!id2.isNull()) {
86 m_doc->identifier(1).setLocale(id2.toString().toLower());
87 m_doc->identifier(1).setName(id2.toString().toLower());
88 }
89
90 // Jam it all into one lesson
91 KEduVocLesson *lesson = new KEduVocLesson(i18n("Lesson %1", 1), m_doc->lesson());
92 m_doc->lesson()->appendChildContainer(lesson);
93
94 while (!atEnd()) {
95 readNext();
96
97 if (isEndElement())
98 break;
99
100 if (isStartElement()) {
101 if (name() == QStringView(QStringLiteral("description")))
103 else if (name() == QStringView(QStringLiteral("full_name")))
104 m_doc->setTitle(readElementText());
105 else if (name() == QStringView(QStringLiteral("ar")))
106 readEntry();
107 else
108 readUnknownElement();
109 }
110 }
111
112 m_doc->setAuthor(QStringLiteral("http://xdxf.sf.net"));
113}
114
115void KEduVocXdxfReader::readEntry()
116{
117 QString front;
119
120 while (!(isEndElement() && name() == QStringView(QStringLiteral("ar")))) {
121 readNext();
122 if (isStartElement() && name() == QStringView(QStringLiteral("k"))) {
123 front = readElementText();
124 } else if (isCharacters() || isEntityReference()) {
125 back.append(text().toString());
126 }
127 }
128
129 KEduVocExpression *expr = new KEduVocExpression(front);
130 expr->setTranslation(1, back);
131
132 KEduVocLesson *lesson(dynamic_cast<KEduVocLesson *>(m_doc->lesson()->childContainer(0)));
133 if (lesson) {
134 lesson->appendEntry(expr);
135 }
136}
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 setTitle(const QString &title)
Set the title of the file.
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
@ FileReaderFailed
file reader failed
KEduVocIdentifier & identifier(int index)
Returns the identifier of translation index.
FileType
known vocabulary file types
@ Xdxf
XDXF format.
This class contains one vocabulary expression as an original with one or more translations.
void setTranslation(int index, const QString &expression)
Add a translation to this expression.
void setLocale(const QString &name)
Set the locale.
void setName(const QString &name)
Set the name.
class to store information about a lesson
void appendEntry(KEduVocExpression *entry)
append an entry to the lesson
QString errorMessage() const override
an error message.
KEduVocDocument::FileType fileTypeHandled() override
returns the KEduVocDocument::FileType that this reader handles
bool isParsable() override
Can this reader parse this file.
KEduVocXdxfReader(QIODevice &file)
constructor
KEduVocDocument::ErrorCode read(KEduVocDocument &doc) override
Parse file and write into doc.
QString i18n(const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
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 toLower() const const
bool isNull() const const
QString toString() const const
QString readLine(qint64 maxlen)
QStringView value(QAnyStringView namespaceUri, QAnyStringView name) const const
bool atEnd() const const
QXmlStreamAttributes attributes() const const
qint64 columnNumber() const const
QIODevice * device() const const
Error error() const const
QString errorString() const const
bool isCharacters() const const
bool isEndElement() const const
bool isEntityReference() const const
bool isStartElement() const const
qint64 lineNumber() const const
QStringView name() const const
void raiseError(const QString &message)
QString readElementText(ReadElementTextBehaviour behaviour)
TokenType readNext()
void setDevice(QIODevice *device)
QStringView text() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.