summaryrefslogtreecommitdiffstats
path: root/reader/src/formats/oeb/OEBCoverReader.cpp
blob: 842de30b40451e264cb69fbab6ffeeedce297401 (plain)
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
/*
 * Copyright (C) 2009-2012 Geometer Plus <contact@geometerplus.com>
 *
 * 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 <ZLFile.h>
#include <ZLFileImage.h>
#include <ZLXMLNamespace.h>

#include "OEBCoverReader.h"
#include "XHTMLImageFinder.h"

#include "../util/MiscUtil.h"

OEBCoverReader::OEBCoverReader() {
}

shared_ptr<const ZLImage> OEBCoverReader::readCover(const ZLFile &file) {
	myPathPrefix = MiscUtil::htmlDirectoryPrefix(file.path());
	myReadState = READ_NOTHING;
	myImage.reset();
	myCoverXHTML.erase();
	readDocument(file);
	if (myImage.isNull() && !myCoverXHTML.empty()) {
		const ZLFile coverFile(myCoverXHTML);
		const std::string ext = coverFile.extension();
		if (ext == "gif" || ext == "jpeg" || ext == "jpg") {
			myImage = new ZLFileImage(coverFile, 0);
		} else {
			myImage = XHTMLImageFinder().readImage(coverFile);
		}
	}
	return myImage;
}

static const std::string METADATA = "metadata";
static const std::string META = "meta";
static const std::string MANIFEST = "manifest";
static const std::string ITEM = "item";
static const std::string GUIDE = "guide";
static const std::string REFERENCE = "reference";
static const std::string COVER = "cover";
static const std::string COVER_IMAGE = "other.ms-coverimage-standard";

bool OEBCoverReader::processNamespaces() const {
	return true;
}

void OEBCoverReader::startElementHandler(const char *tag, const char **attributes) {
	switch (myReadState) {
		case READ_NOTHING:
			if (GUIDE == tag) {
				myReadState = READ_GUIDE;
			} else if (MANIFEST == tag && !myCoverId.empty()) {
				myReadState = READ_MANIFEST;
			} else if (testTag(ZLXMLNamespace::OpenPackagingFormat, METADATA, tag)) {
				myReadState = READ_METADATA;
			}
			break;
		case READ_GUIDE:
			if (REFERENCE == tag) {
				const char *type = attributeValue(attributes, "type");
				if (type != 0) {
					if (COVER == type) {
						const char *href = attributeValue(attributes, "href");
						if (href != 0) {
							myCoverXHTML = myPathPrefix + MiscUtil::decodeHtmlURL(href);
							interrupt();
						}
					} else if (COVER_IMAGE == type) {
						createImage(attributeValue(attributes, "href"));
					}
				}
			}
			break;
		case READ_METADATA:
			if (testTag(ZLXMLNamespace::OpenPackagingFormat, META, tag)) {
				const char *name = attributeValue(attributes, "name");
				if (name != 0 && COVER == name) {
					myCoverId = attributeValue(attributes, "content");
				}
			}
			break;
		case READ_MANIFEST:
			if (ITEM == tag) {
				const char *id = attributeValue(attributes, "id");
				if (id != 0 && myCoverId == id) {
					createImage(attributeValue(attributes, "href"));
				}
			}
			break;
	}
}

void OEBCoverReader::createImage(const char *href) {
	if (href != 0) {
		myImage = new ZLFileImage(ZLFile(myPathPrefix + MiscUtil::decodeHtmlURL(href)), 0);
		interrupt();
	}
}

void OEBCoverReader::endElementHandler(const char *tag) {
	switch (myReadState) {
		case READ_NOTHING:
			break;
		case READ_GUIDE:
			if (GUIDE == tag) {
				myReadState = READ_NOTHING;
			}
			break;
		case READ_METADATA:
			if (testTag(ZLXMLNamespace::OpenPackagingFormat, METADATA, tag)) {
				myReadState = READ_NOTHING;
			}
			break;
		case READ_MANIFEST:
			if (MANIFEST == tag) {
				myReadState = READ_NOTHING;
			}
			break;
	}
}