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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/*
* Copyright (C) 2004-2012 Geometer Plus <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <cstdlib>
#include <ZLInputStream.h>
#include <ZLUnicodeUtil.h>
#include "FB2MetaInfoReader.h"
#include "FB2TagManager.h"
#include "../../library/Book.h"
FB2MetaInfoReader::FB2MetaInfoReader(Book &book) : myBook(book) {
myBook.removeAllAuthors();
myBook.setTitle(std::string());
myBook.setLanguage(std::string());
myBook.removeAllTags();
}
void FB2MetaInfoReader::characterDataHandler(const char *text, std::size_t len) {
switch (myReadState) {
case READ_TITLE:
myBuffer.append(text, len);
break;
case READ_LANGUAGE:
myBuffer.append(text, len);
break;
case READ_AUTHOR_NAME_0:
myAuthorNames[0].append(text, len);
break;
case READ_AUTHOR_NAME_1:
myAuthorNames[1].append(text, len);
break;
case READ_AUTHOR_NAME_2:
myAuthorNames[2].append(text, len);
break;
case READ_GENRE:
myBuffer.append(text, len);
break;
default:
break;
}
}
void FB2MetaInfoReader::startElementHandler(int tag, const char **attributes) {
switch (tag) {
case _BODY:
myReturnCode = true;
interrupt();
break;
case _TITLE_INFO:
myReadState = READ_SOMETHING;
break;
case _BOOK_TITLE:
if (myReadState == READ_SOMETHING) {
myReadState = READ_TITLE;
}
break;
case _GENRE:
if (myReadState == READ_SOMETHING) {
myReadState = READ_GENRE;
}
break;
case _AUTHOR:
if (myReadState == READ_SOMETHING) {
myReadState = READ_AUTHOR;
}
break;
case _LANG:
if (myReadState == READ_SOMETHING) {
myReadState = READ_LANGUAGE;
}
break;
case _FIRST_NAME:
if (myReadState == READ_AUTHOR) {
myReadState = READ_AUTHOR_NAME_0;
}
break;
case _MIDDLE_NAME:
if (myReadState == READ_AUTHOR) {
myReadState = READ_AUTHOR_NAME_1;
}
break;
case _LAST_NAME:
if (myReadState == READ_AUTHOR) {
myReadState = READ_AUTHOR_NAME_2;
}
break;
case _SEQUENCE:
if (myReadState == READ_SOMETHING) {
const char *name = attributeValue(attributes, "name");
if (name != 0) {
std::string seriesTitle = name;
ZLUnicodeUtil::utf8Trim(seriesTitle);
const char *number = attributeValue(attributes, "number");
myBook.setSeries(seriesTitle, number != 0 ? std::string(number) : std::string());
}
}
break;
default:
break;
}
}
void FB2MetaInfoReader::endElementHandler(int tag) {
switch (tag) {
case _TITLE_INFO:
myReadState = READ_NOTHING;
break;
case _BOOK_TITLE:
if (myReadState == READ_TITLE) {
myBook.setTitle(myBuffer);
myBuffer.erase();
myReadState = READ_SOMETHING;
}
break;
case _GENRE:
if (myReadState == READ_GENRE) {
ZLUnicodeUtil::utf8Trim(myBuffer);
if (!myBuffer.empty()) {
const std::vector<std::string> &tags =
FB2TagManager::Instance().humanReadableTags(myBuffer);
if (!tags.empty()) {
for (std::vector<std::string>::const_iterator it = tags.begin(); it != tags.end(); ++it) {
myBook.addTag(*it);
}
} else {
myBook.addTag(myBuffer);
}
myBuffer.erase();
}
myReadState = READ_SOMETHING;
}
break;
case _AUTHOR:
if (myReadState == READ_AUTHOR) {
ZLUnicodeUtil::utf8Trim(myAuthorNames[0]);
ZLUnicodeUtil::utf8Trim(myAuthorNames[1]);
ZLUnicodeUtil::utf8Trim(myAuthorNames[2]);
std::string fullName = myAuthorNames[0];
if (!fullName.empty() && !myAuthorNames[1].empty()) {
fullName += ' ';
}
fullName += myAuthorNames[1];
if (!fullName.empty() && !myAuthorNames[2].empty()) {
fullName += ' ';
}
fullName += myAuthorNames[2];
myBook.addAuthor(fullName, myAuthorNames[2]);
myAuthorNames[0].erase();
myAuthorNames[1].erase();
myAuthorNames[2].erase();
myReadState = READ_SOMETHING;
}
break;
case _LANG:
if (myReadState == READ_LANGUAGE) {
myBook.setLanguage(myBuffer);
myBuffer.erase();
myReadState = READ_SOMETHING;
}
break;
case _FIRST_NAME:
if (myReadState == READ_AUTHOR_NAME_0) {
myReadState = READ_AUTHOR;
}
break;
case _MIDDLE_NAME:
if (myReadState == READ_AUTHOR_NAME_1) {
myReadState = READ_AUTHOR;
}
break;
case _LAST_NAME:
if (myReadState == READ_AUTHOR_NAME_2) {
myReadState = READ_AUTHOR;
}
break;
default:
break;
}
}
bool FB2MetaInfoReader::readMetaInfo() {
myReadState = READ_NOTHING;
for (int i = 0; i < 3; ++i) {
myAuthorNames[i].erase();
}
return readDocument(myBook.file());
}
|