Commit bbe9b8d2 authored by Andreas Cord-Landwehr's avatar Andreas Cord-Landwehr
Browse files

Switch to XmlStreamParser

parent 49280ea5
......@@ -25,46 +25,64 @@
#include "artikulate_debug.h"
#include <KLocalizedString>
#include <QQmlEngine>
#include <QXmlSchema>
#include <QDomDocument>
#include "resources/courseparser.h"
#include <QXmlStreamReader>
#include <QFile>
#include <QIODevice>
std::shared_ptr<Language> Language::create(QUrl file)
{
QXmlSchema schema = CourseParser::loadXmlSchema(QStringLiteral("language"));
if (!schema.isValid()) {
return nullptr;
}
QDomDocument document = CourseParser::loadDomDocument(file, schema);
if (document.isNull()) {
qCWarning(ARTIKULATE_LOG) << "Could not parse document " << file.toLocalFile() << ", aborting.";
return nullptr;
}
QDomElement root(document.documentElement());
auto language = std::shared_ptr<Language>(new Language());
language->setFile(file);
language->setId(root.firstChildElement(QStringLiteral("id")).text());
language->setTitle(root.firstChildElement(QStringLiteral("title")).text());
language->seti18nTitle(root.firstChildElement(QStringLiteral("i18nTitle")).text());
// create phoneme groups
for (QDomElement groupNode = root.firstChildElement(QStringLiteral("phonemeGroups")).firstChildElement();
!groupNode.isNull();
groupNode = groupNode.nextSiblingElement())
{
auto group = language->addPhonemeGroup(
groupNode.firstChildElement(QStringLiteral("id")).text(),
groupNode.firstChildElement(QStringLiteral("title")).text());
group->setDescription(groupNode.attribute(QStringLiteral("description")));
// register phonemes
for (QDomElement phonemeNode = groupNode.firstChildElement(QStringLiteral("phonemes")).firstChildElement();
!phonemeNode.isNull();
phonemeNode = phonemeNode.nextSiblingElement())
{
group->addPhoneme(phonemeNode.firstChildElement(QStringLiteral("id")).text(), phonemeNode.firstChildElement(QStringLiteral("title")).text());
// load basic information from language file, but does not parse everything
QXmlStreamReader xml;
QFile handle(file.toLocalFile());
if (handle.open(QIODevice::ReadOnly)) {
xml.setDevice(&handle);
xml.readNextStartElement();
while (xml.readNext() && !xml.atEnd()) {
if (xml.name() == "id") {
language->setId(xml.readElementText());
}
if (xml.name() == "title") {
language->setTitle(xml.readElementText());
}
if (xml.name() == "i18nTitle") {
language->seti18nTitle(xml.readElementText());
}
if (xml.name() == "phonemeGroup") {
std::shared_ptr<PhonemeGroup> group = std::shared_ptr<PhonemeGroup>(new PhonemeGroup);
while (xml.readNext() && !xml.atEnd() && xml.name() != "phonemeGroup") {
if (xml.name() == "id") {
group->setId(xml.readElementText());
}
if (xml.name() == "title") {
group->setTitle(xml.readElementText());
}
if (xml.name() == "description") {
group->setDescription(xml.readElementText());
}
if (xml.name() == "phoneme") {
std::unique_ptr<Phoneme> phoneme = std::unique_ptr<Phoneme>(new Phoneme);
while (xml.readNext() && !xml.atEnd() && xml.name() != "phoneme") {
if (xml.name() == "id") {
phoneme->setId(xml.readElementText());
}
if (xml.name() == "title") {
phoneme->setTitle(xml.readElementText());
}
}
group->addPhoneme(std::move(phoneme));
}
}
language->m_phonemeGroups.append(group);
}
}
if (xml.hasError()) {
qCritical() << "Error occurred when reading Language XML file:" << file.toLocalFile();
}
}
xml.clear();
handle.close();
return language;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment