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
|
/*
* 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 "NCXReader.h"
#include "../util/MiscUtil.h"
#include "../util/EntityFilesCollector.h"
NCXReader::NCXReader(BookReader &modelReader) : myModelReader(modelReader), myReadState(READ_NONE), myPlayIndex(-65535) {
}
static const std::string TAG_NAVMAP = "navMap";
static const std::string TAG_NAVPOINT = "navPoint";
static const std::string TAG_NAVLABEL = "navLabel";
static const std::string TAG_CONTENT = "content";
static const std::string TAG_TEXT = "text";
void NCXReader::startElementHandler(const char *fullTag, const char **attributes) {
std::string tag = fullTag;
const std::size_t index = tag.rfind(':');
if (index != std::string::npos) {
tag = tag.substr(index + 1);
}
switch (myReadState) {
case READ_NONE:
if (TAG_NAVMAP == tag) {
myReadState = READ_MAP;
}
break;
case READ_MAP:
if (TAG_NAVPOINT == tag) {
const char *order = attributeValue(attributes, "playOrder");
myPointStack.push_back(NavPoint(order != 0 ? std::atoi(order) : myPlayIndex++, myPointStack.size()));
myReadState = READ_POINT;
}
break;
case READ_POINT:
if (TAG_NAVPOINT == tag) {
const char *order = attributeValue(attributes, "playOrder");
myPointStack.push_back(NavPoint(order != 0 ? std::atoi(order) : myPlayIndex++, myPointStack.size()));
} else if (TAG_NAVLABEL == tag) {
myReadState = READ_LABEL;
} else if (TAG_CONTENT == tag) {
const char *src = attributeValue(attributes, "src");
if (src != 0) {
myPointStack.back().ContentHRef = MiscUtil::decodeHtmlURL(src);
}
}
break;
case READ_LABEL:
if (TAG_TEXT == tag) {
myReadState = READ_TEXT;
}
break;
case READ_TEXT:
break;
}
}
void NCXReader::endElementHandler(const char *fullTag) {
std::string tag = fullTag;
const std::size_t index = tag.rfind(':');
if (index != std::string::npos) {
tag = tag.substr(index + 1);
}
switch (myReadState) {
case READ_NONE:
break;
case READ_MAP:
if (TAG_NAVMAP == tag) {
myReadState = READ_NONE;
}
break;
case READ_POINT:
if (TAG_NAVPOINT == tag) {
if (myPointStack.back().Text.empty()) {
myPointStack.back().Text = "...";
}
myNavigationMap[myPointStack.back().Order] = myPointStack.back();
myPointStack.pop_back();
myReadState = myPointStack.empty() ? READ_MAP : READ_POINT;
}
case READ_LABEL:
if (TAG_NAVLABEL == tag) {
myReadState = READ_POINT;
}
break;
case READ_TEXT:
if (TAG_TEXT == tag) {
myReadState = READ_LABEL;
}
break;
}
}
void NCXReader::characterDataHandler(const char *text, std::size_t len) {
if (myReadState == READ_TEXT) {
myPointStack.back().Text.append(text, len);
}
}
const std::vector<std::string> &NCXReader::externalDTDs() const {
return EntityFilesCollector::Instance().externalDTDs("xhtml");
}
const std::map<int,NCXReader::NavPoint> &NCXReader::navigationMap() const {
return myNavigationMap;
}
NCXReader::NavPoint::NavPoint() {
}
NCXReader::NavPoint::NavPoint(int order, std::size_t level) : Order(order), Level(level) {
}
|