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
.MarkableFileInputStream
;
19 import be
.nikiroo
.utils
.Progress
;
22 * Support class for EPUB files created with this program (as we need some
23 * metadata available in those we create).
27 class Epub
extends InfoText
{
29 protected MetaData meta
;
31 private URL fakeSource
;
32 private InputStream fakeIn
;
35 public String
getSourceName() {
40 protected boolean supports(URL url
) {
41 if (url
.getPath().toLowerCase().endsWith(".epub")) {
49 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
54 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
57 return super.getDesc(fakeSource
, fakeIn
);
64 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
,
65 Progress pg
) throws IOException
{
68 return super.getChapters(fakeSource
, fakeIn
, pg
);
75 protected String
getChapterContent(URL source
, InputStream in
, int number
,
76 Progress pg
) throws IOException
{
79 return super.getChapterContent(fakeSource
, fakeIn
, number
, pg
);
86 protected void preprocess(URL source
, InputStream in
) throws IOException
{
87 // Note: do NOT close this stream, as it would also close "in"
88 ZipInputStream zipIn
= new ZipInputStream(in
);
89 tmp
= File
.createTempFile("fanfic-reader-parser_", ".tmp");
90 File tmpInfo
= new File(tmp
+ ".info");
91 fakeSource
= tmp
.toURI().toURL();
92 BufferedImage cover
= null;
94 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
96 if (!entry
.isDirectory()
97 && entry
.getName().startsWith(getDataPrefix())) {
98 String entryLName
= entry
.getName().toLowerCase();
100 boolean imageEntry
= false;
101 for (String ext
: getImageExt(false)) {
102 if (entryLName
.endsWith(ext
)) {
107 if (entry
.getName().equals(getDataPrefix() + "version")) {
108 // Nothing to do for now ("first"
110 } else if (entryLName
.endsWith(".info")) {
112 IOUtils
.write(zipIn
, tmpInfo
);
113 } else if (imageEntry
) {
117 cover
= IOUtils
.toImage(zipIn
);
118 } catch (Exception e
) {
122 } else if (entry
.getName().equals(getDataPrefix() + "URL")) {
124 } else if (entry
.getName().equals(getDataPrefix() + "SUMMARY")) {
127 // Hopefully the data file
128 IOUtils
.write(zipIn
, tmp
);
133 if (requireInfo() && (!tmp
.exists() || !tmpInfo
.exists())) {
134 throw new IOException(
135 "file not supported (maybe not created with this program or corrupt)");
139 this.fakeIn
= new MarkableFileInputStream(new FileInputStream(tmp
));
142 if (tmpInfo
.exists()) {
143 meta
= InfoReader
.readMeta(tmpInfo
, true);
145 meta
.setCover(cover
);
149 meta
= new MetaData();
150 meta
.setUuid(source
.toString());
152 meta
.setTags(new ArrayList
<String
>());
153 meta
.setSource(getSourceName());
154 meta
.setUrl(source
.toString());
159 protected void close() throws IOException
{
160 if (tmp
!= null && tmp
.exists()) {
172 protected String
getDataPrefix() {
176 protected boolean requireInfo() {
180 protected boolean getCover() {