-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflextextimporter.cpp
142 lines (130 loc) · 5.44 KB
/
flextextimporter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "flextextimporter.h"
#include "phrase.h"
#include "text.h"
#include "glossitem.h"
#include "morphologicalanalysis.h"
#include "databaseadapter.h"
#include "project.h"
#include "paragraph.h"
#include <QFile>
#include <QFileInfo>
#include <QtGlobal>
#include <QXmlStreamReader>
#include <QList>
#include <QMessageBox>
FlexTextImporter::FlexTextImporter(Text *text) : FlexTextReader(text)
{
}
FlexTextReader::Result FlexTextImporter::readFile(const QString & filepath)
{
QFile file(filepath);
file.open(QFile::ReadOnly);
QXmlStreamReader stream(&file);
mText->removeAllParagraphs();
bool inWord = false;
bool inPhrase = false;
TextBitHash textForms;
TextBitHash glossForms;
while (!stream.atEnd())
{
stream.readNext();
QString name = stream.name().toString();
if( stream.tokenType() == QXmlStreamReader::StartElement )
{
if( name == "word" )
{
inWord = true;
textForms.clear();
glossForms.clear();
}
else if ( name == "paragraph" ) // <paragraph>
{
mText->mParagraphs.append( new Paragraph(mText->mBaselineWritingSystem) );
QObject::connect( mText->mParagraphs.last(), SIGNAL(changed()), mText, SLOT(markAsChanged()) );
}
else if ( name == "phrase" )
{
inPhrase = true;
mText->mParagraphs.last()->mPhrases.append( new Phrase(mText, mText->mProject) );
mText->mParagraphs.last()->mPhrases.last()->connectToText();
QXmlStreamAttributes attr = stream.attributes();
if( attr.hasAttribute("http://www.adambaker.org/gloss.php","annotation-start") && attr.hasAttribute("http://www.adambaker.org/gloss.php","annotation-end") )
{
qlonglong start = attr.value("http://www.adambaker.org/gloss.php","annotation-start").toString().toLongLong();
qlonglong end = attr.value("http://www.adambaker.org/gloss.php","annotation-end").toString().toLongLong();
mText->mParagraphs.last()->phrases()->last()->setInterval( Interval(start, end) );
}
}
else if ( name == "item" )
{
QXmlStreamAttributes attr = stream.attributes();
if( attr.hasAttribute("type") )
{
QString type = attr.value("type").toString();
WritingSystem lang;
if( attr.hasAttribute("lang") )
lang = mText->mProject->dbAdapter()->writingSystem( attr.value("lang").toString() );
QString text = stream.readElementText();
if( inWord )
{
qlonglong itemId = attr.hasAttribute("http://www.adambaker.org/gloss.php","id") ? attr.value("http://www.adambaker.org/gloss.php","id").toString().toLongLong() : -1;
// there's no handling here for the case where itemId == -1
// that is handled in GlossItem::loadStringsFromDatabase()
if( type == "txt" )
{
TextBit textForm( text , lang, itemId);
textForms.insert( lang, textForm );
}
else if( type == "gls" )
{
glossForms.insert( lang, TextBit( text , lang, itemId) );
}
else if ( type == "punct" )
{
TextBit textForm( text , lang, itemId);
textForms.insert( lang, textForm );
}
}
else if ( inPhrase && type == "gls" )
{
mText->mParagraphs.last()->phrases()->last()->setPhrasalGloss( TextBit( text , lang ) );
}
}
}
else if(name == "interlinear-text")
{
QXmlStreamAttributes attr = stream.attributes();
if( attr.hasAttribute("http://www.adambaker.org/gloss.php","audio-file") )
{
mText->setSound( QUrl::fromEncoded( attr.value("http://www.adambaker.org/gloss.php","audio-file").toString().toUtf8() ) );
}
}
else if(name == "morphemes")
{
/// Skip over any <morphemes> element
while( !( stream.name().toString() == "morphemes" && stream.tokenType() == QXmlStreamReader::EndElement ) )
{
stream.readNext();
}
}
}
else if( stream.tokenType() == QXmlStreamReader::EndElement )
{
if(name == "word")
{
mText->mParagraphs.last()->phrases()->last()->appendGlossItem(new GlossItem( mText->mBaselineWritingSystem, textForms, glossForms, mText->mProject ));
inWord = false;
}
else if(name == "phrase")
{
inPhrase = false;
}
}
}
file.close();
if (stream.hasError()) {
qWarning() << "FlexTextImporter::readTextFromFlexText error with xml reading";
return FlexTextImporter::FlexTextReadXmlReadError;
}
return FlexTextImporter::FlexTextReadSuccess;
}