1 package be
.nikiroo
.fanfix
.supported
;
3 import java
.awt
.image
.BufferedImage
;
5 import java
.io
.FileInputStream
;
6 import java
.io
.IOException
;
7 import java
.io
.InputStream
;
9 import java
.util
.ArrayList
;
10 import java
.util
.List
;
11 import java
.util
.Map
.Entry
;
12 import java
.util
.zip
.ZipEntry
;
13 import java
.util
.zip
.ZipInputStream
;
15 import be
.nikiroo
.fanfix
.Instance
;
16 import be
.nikiroo
.fanfix
.data
.MetaData
;
17 import be
.nikiroo
.utils
.IOUtils
;
18 import be
.nikiroo
.utils
.ImageUtils
;
19 import be
.nikiroo
.utils
.MarkableFileInputStream
;
20 import be
.nikiroo
.utils
.Progress
;
23 * Support class for EPUB files created with this program (as we need some
24 * metadata available in those we create).
28 class Epub
extends InfoText
{
30 protected MetaData meta
;
32 private URL fakeSource
;
33 private InputStream fakeIn
;
36 public String
getSourceName() {
41 protected boolean supports(URL url
) {
42 if (url
.getPath().toLowerCase().endsWith(".epub")) {
50 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
55 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
58 return super.getDesc(fakeSource
, fakeIn
);
65 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
,
66 Progress pg
) throws IOException
{
69 return super.getChapters(fakeSource
, fakeIn
, pg
);
76 protected String
getChapterContent(URL source
, InputStream in
, int number
,
77 Progress pg
) throws IOException
{
80 return super.getChapterContent(fakeSource
, fakeIn
, number
, pg
);
87 protected void preprocess(URL source
, InputStream in
) throws IOException
{
88 // Note: do NOT close this stream, as it would also close "in"
89 ZipInputStream zipIn
= new ZipInputStream(in
);
90 tmp
= File
.createTempFile("fanfic-reader-parser_", ".tmp");
91 File tmpInfo
= new File(tmp
+ ".info");
92 fakeSource
= tmp
.toURI().toURL();
93 BufferedImage cover
= null;
95 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
97 if (!entry
.isDirectory()
98 && entry
.getName().startsWith(getDataPrefix())) {
99 String entryLName
= entry
.getName().toLowerCase();
101 boolean imageEntry
= false;
102 for (String ext
: getImageExt(false)) {
103 if (entryLName
.endsWith(ext
)) {
108 if (entry
.getName().equals(getDataPrefix() + "version")) {
109 // Nothing to do for now ("first"
111 } else if (entryLName
.endsWith(".info")) {
113 IOUtils
.write(zipIn
, tmpInfo
);
114 } else if (imageEntry
) {
118 cover
= ImageUtils
.fromStream(zipIn
);
119 } catch (Exception e
) {
123 } else if (entry
.getName().equals(getDataPrefix() + "URL")) {
125 } else if (entry
.getName().equals(getDataPrefix() + "SUMMARY")) {
128 // Hopefully the data file
129 IOUtils
.write(zipIn
, tmp
);
134 if (requireInfo() && (!tmp
.exists() || !tmpInfo
.exists())) {
135 throw new IOException(
136 "file not supported (maybe not created with this program or corrupt)");
140 this.fakeIn
= new MarkableFileInputStream(new FileInputStream(tmp
));
143 if (tmpInfo
.exists()) {
144 meta
= InfoReader
.readMeta(tmpInfo
, true);
146 meta
.setCover(cover
);
150 meta
= new MetaData();
151 meta
.setUuid(source
.toString());
153 meta
.setTags(new ArrayList
<String
>());
154 meta
.setSource(getSourceName());
155 meta
.setUrl(source
.toString());
160 protected void close() throws IOException
{
161 if (tmp
!= null && tmp
.exists()) {
169 if (fakeIn
!= null) {
176 protected String
getDataPrefix() {
180 protected boolean requireInfo() {
184 protected boolean getCover() {