-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenexportparser.cpp
More file actions
119 lines (106 loc) · 4.08 KB
/
enexportparser.cpp
File metadata and controls
119 lines (106 loc) · 4.08 KB
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
#include "enexportparser.h"
#include "ennote.h"
EnExportParser::EnExportParser() :
mIsInNote(false),
mIsInResource(false)
{
}
QList<EnNote*> EnExportParser::readFromIO(QIODevice* sourceDevice){
if(sourceDevice->isReadable()){
QXmlInputSource source(sourceDevice);
EnExportParser handler;
QXmlSimpleReader reader;
reader.setContentHandler(&handler);
reader.parse(source);
foreach(EnNote* note, handler.getNotes()){
note->prepareContent();
}
qDebug() << "closing device";
sourceDevice->close();
return handler.getNotes();
}
return QList<EnNote*>();
}
bool EnExportParser::startElement(const QString &,
const QString &,
const QString &qName,
const QXmlAttributes &attrs){
// äëÿ êàæäîãî íîâîã òåãà ïîìåùàåì â ñòåê ñòðîêó, â êîòîðóþ áóäåì çàïèñûâàòü åãî ñîäåðæèìîå
mTagContent.push(QString());
if(qName == "note"){
Q_ASSERT_X(!mIsInNote, "EnExportParser::startElement", "starting new note inside note");
// ñîçäà¸ì íîâóþ çàìåòêó
mEnNotes << new EnNote;
mIsInNote = true;
}else if(qName == "resource"){
Q_ASSERT_X(!mIsInResource, "EnExportParser::startElement", "starting new note inside note");
// äîáàâëÿåì â çàìåòêó ïóñòîé ðåñóðñ
mEnNotes.last()->pushEmptyResource();
mIsInResource = true;
}
return true;
}
bool EnExportParser::characters(const QString& characters){
// ïðèáàâëÿåì ê ïîñëåäåíìó ýëåìåíòó ñòåêà ïðî÷èòàííûå ñèìâîëû
mTagContent.last() += characters;
return true;
}
bool EnExportParser::endElement(const QString&, const QString&, const QString& qName){
//qDebug() << qName;
if(qName == "note"){
Q_ASSERT_X(mIsInNote, "EnExportParser::endElement", "ending note outside note");
mIsInNote = false;
}else if(mIsInNote){
// çàïîëíÿåì ñâîéñòâà çàìåòêè
if(qName == "title"){
mEnNotes.last()->title = mTagContent.last();
}else if(qName == "content"){
mEnNotes.last()->content = new EnContent(mTagContent.last());
}else if(qName == "created"){
mEnNotes.last()->created = mTagContent.last();
}else if(qName == "updated"){
mEnNotes.last()->updated = mTagContent.last();
}else if(qName == "tag"){
mEnNotes.last()->tags << mTagContent.last();
}else if(qName == "source"){
mEnNotes.last()->source = mTagContent.last();
}else if(qName == "source-url"){
mEnNotes.last()->sourceUrl = mTagContent.last();
}else if(qName == "resource"){
mIsInResource = false;
//qDebug() << mTagContent.last();
}else if(mIsInResource){
// çàïîëíÿåì ñâîéñòâà ðåñóðñà
if(qName == "data"){
mEnNotes.last()->resources.last()->setData(mTagContent.last());
}else if(qName == "mime"){
mEnNotes.last()->resources.last()->setType(mTagContent.last());
}else if(qName == "width"){
mEnNotes.last()->resources.last()->setWidth(mTagContent.last().toInt());
}else if(qName == "height"){
mEnNotes.last()->resources.last()->setHeight(mTagContent.last().toInt());
}/*else if(qName == "recognition"){
mEnNotes.last()->resources.last()->setHash(getResourceHash(mTagContent.last()));
}*/
}
}
if(!mTagContent.isEmpty())
mTagContent.pop();
return true;
}
bool EnExportParser::fatalError(const QXmlParseException& exception){
qDebug() << "Line:" << exception.lineNumber()
<< ", Column:" << exception.columnNumber()
<< ", Message:" << exception.message();
return false;
}
QList<EnNote*> EnExportParser::getNotes() const{
return mEnNotes;
}
QString EnExportParser::getResourceHash(const QString& str){
//qDebug() << str;
int from = str.indexOf("objID=\"");
from += 7;
int to = str.indexOf('"', from);
return str.mid(from, to - from);
}