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
|
/*
* 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 <cstring>
#include <cstdlib>
#include <algorithm>
#include <ZLUnicodeUtil.h>
#include <ZLFileImage.h>
#include "ORBookReader.h"
#include "../xhtml/XHTMLReader.h"
#include "../util/MiscUtil.h"
#include "../../bookmodel/BookModel.h"
#include "../../library/Book.h"
ORBookReader::ORBookReader(BookModel &model) : myModelReader(model) {
}
void ORBookReader::characterDataHandler(const char *data, std::size_t len) {
if (myState == READ_TOCTITLE) {
myTOCTitle.append(data, len);
}
}
static const std::string TAG_RESOURCES = "resources";
static const std::string TAG_USERSET = "userset";
static const std::string TAG_NAVIGATION = "primarynav";
static const std::string TAG_SPINE = "spine";
static const std::string TAG_COVER = "cover";
static const std::string TAG_ITEM = "item";
static const std::string TAG_ITEMREF = "itemref";
static const std::string TAG_POINTER = "pointer";
static const std::string TAG_TITLE = "title";
static const std::string xhtmlMediaType = "application/x-orp-bcd1+xml";
void ORBookReader::startElementHandler(const char *tag, const char **xmlattributes) {
const std::string tagString = ZLUnicodeUtil::toLower(tag);
if (TAG_RESOURCES == tagString) {
myState = READ_RESOURCES;
} else if (TAG_USERSET == tagString) {
myState = READ_USERSET;
} else if ((myState == READ_RESOURCES) && (TAG_ITEM == tagString)) {
const char *resid = attributeValue(xmlattributes, "resid");
const char *resource = attributeValue(xmlattributes, "resource");
shared_ptr<ZLMimeType> mediaType = ZLMimeType::get(attributeValue(xmlattributes, "media-type"));
if ((resid != 0) && (resource != 0)) {
myResources[resid] = resource;
if (!mediaType.isNull() && mediaType != ZLMimeType::EMPTY) {
if (ZLMimeType::APPLICATION_OR_XML == mediaType) {
myHtmlFileIDs.insert(resid);
} else if (ZLMimeType::isImage(mediaType)) {
myImageIDs[resid] = mediaType;
}
}
}
} else if (myState == READ_USERSET) {
if (TAG_NAVIGATION == tagString) {
myState = READ_NAVIGATION;
} else if (TAG_SPINE == tagString) {
const char *residrefs = attributeValue(xmlattributes, "residrefs");
if (residrefs != 0) {
while (1) {
const char *nextSpace = std::strchr(residrefs, ' ');
if (nextSpace == 0) {
if (*residrefs != '\0') {
myHtmlFilesOrder.push_back(residrefs);
}
break;
}
if (nextSpace != residrefs) {
myHtmlFilesOrder.push_back(std::string(residrefs, nextSpace - residrefs));
}
residrefs = nextSpace + 1;
}
}
} else if (TAG_COVER == tagString) {
const char *residrefs = attributeValue(xmlattributes, "residrefs");
if (residrefs != 0) {
myCoverReference = residrefs;
}
}
} else if (myState == READ_NAVIGATION && TAG_POINTER == tagString) {
const char *ref = attributeValue(xmlattributes, "elemrefs");
const char *level = attributeValue(xmlattributes, "level");
if (ref != 0 && level != 0) {
myTOCReference = ref;
myTOCLevel = std::atoi(level);
myState = READ_POINTER;
}
} else if (myState == READ_POINTER && TAG_TITLE == tagString) {
myState = READ_TOCTITLE;
}
}
void ORBookReader::endElementHandler(const char *tag) {
const std::string tagString = ZLUnicodeUtil::toLower(tag);
if (TAG_RESOURCES == tagString || TAG_USERSET == tagString) {
myState = READ_NONE;
} else if (myState == READ_NAVIGATION && TAG_NAVIGATION == tagString) {
myState = READ_USERSET;
} else if (myState == READ_POINTER && TAG_POINTER == tagString) {
myState = READ_NAVIGATION;
} else if (myState == READ_TOCTITLE && TAG_TITLE == tagString) {
myTOC.push_back(TOCItem(myTOCReference, myTOCTitle, myTOCLevel));
myTOCTitle.erase();
myState = READ_POINTER;
}
}
bool ORBookReader::readBook() {
const ZLFile &file = myModelReader.model().book()->file();
myFilePrefix = MiscUtil::htmlDirectoryPrefix(file.path());
myResources.clear();
myCoverReference.erase();
myHtmlFileIDs.clear();
myImageIDs.clear();
myHtmlFilesOrder.clear();
myTOC.clear();
myState = READ_NONE;
if (!readDocument(file)) {
return false;
}
myModelReader.setMainTextModel();
myModelReader.pushKind(REGULAR);
if (!myCoverReference.empty()) {
myModelReader.addImageReference(myCoverReference);
}
for (std::vector<std::string>::const_iterator it = myHtmlFilesOrder.begin(); it != myHtmlFilesOrder.end(); ++it) {
myHtmlFileIDs.erase(*it);
XHTMLReader(myModelReader).readFile(ZLFile(myFilePrefix + myResources[*it]), *it);
}
int level = 1;
for (std::vector<TOCItem>::const_iterator it = myTOC.begin(); it != myTOC.end(); ++it) {
int index = myModelReader.model().label(it->Reference).ParagraphNumber;
if (index != -1) {
for (; level > it->Level; --level) {
myModelReader.endContentsParagraph();
}
++level;
myModelReader.beginContentsParagraph(index);
myModelReader.addContentsData(it->Text);
}
}
for (; level > 1; --level) {
myModelReader.endContentsParagraph();
}
for (std::set<std::string>::const_iterator it = myHtmlFileIDs.begin(); it != myHtmlFileIDs.end(); ++it) {
myModelReader.setFootnoteTextModel(*it);
myModelReader.pushKind(REGULAR);
XHTMLReader(myModelReader).readFile(ZLFile(myFilePrefix + myResources[*it]), *it);
}
for (std::map<std::string,shared_ptr<ZLMimeType> >::const_iterator it = myImageIDs.begin(); it != myImageIDs.end(); ++it) {
myModelReader.addImage(it->first, new ZLFileImage(ZLFile(myFilePrefix + myResources[it->first], it->second), 0));
}
return true;
}
|