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
;
21 * Support class for EPUB files created with this program (as we need some
22 * metadata available in those we create).
26 class Epub
extends InfoText
{
28 protected MetaData meta
;
30 private URL fakeSource
;
31 private InputStream fakeIn
;
34 public String
getSourceName() {
39 protected boolean supports(URL url
) {
40 if (url
.getPath().toLowerCase().endsWith(".epub")) {
48 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
53 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
56 return super.getDesc(fakeSource
, fakeIn
);
63 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
)
67 return super.getChapters(fakeSource
, fakeIn
);
74 protected String
getChapterContent(URL source
, InputStream in
, int number
)
78 return super.getChapterContent(fakeSource
, fakeIn
, number
);
85 protected void preprocess(URL source
, InputStream in
) throws IOException
{
86 // Note: do NOT close this stream, as it would also close "in"
87 ZipInputStream zipIn
= new ZipInputStream(in
);
88 tmp
= File
.createTempFile("fanfic-reader-parser_", ".tmp");
89 File tmpInfo
= new File(tmp
+ ".info");
90 fakeSource
= tmp
.toURI().toURL();
91 BufferedImage cover
= null;
93 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
95 if (!entry
.isDirectory()
96 && entry
.getName().startsWith(getDataPrefix())) {
97 String entryLName
= entry
.getName().toLowerCase();
99 boolean imageEntry
= false;
100 for (String ext
: getImageExt(false)) {
101 if (entryLName
.endsWith(ext
)) {
106 if (entry
.getName().equals(getDataPrefix() + "version")) {
107 // Nothing to do for now ("first"
109 } else if (entryLName
.endsWith(".info")) {
111 IOUtils
.write(zipIn
, tmpInfo
);
112 } else if (imageEntry
) {
116 cover
= IOUtils
.toImage(zipIn
);
117 } catch (Exception e
) {
121 } else if (entry
.getName().equals(getDataPrefix() + "URL")) {
123 } else if (entry
.getName().equals(getDataPrefix() + "SUMMARY")) {
126 // Hopefully the data file
127 IOUtils
.write(zipIn
, tmp
);
132 if (requireInfo() && (!tmp
.exists() || !tmpInfo
.exists())) {
133 throw new IOException(
134 "file not supported (maybe not created with this program or corrupt)");
138 this.fakeIn
= new MarkableFileInputStream(new FileInputStream(tmp
));
141 if (tmpInfo
.exists()) {
142 meta
= InfoReader
.readMeta(tmpInfo
);
144 meta
.setCover(cover
);
148 meta
= new MetaData();
149 meta
.setUuid(source
.toString());
151 meta
.setTags(new ArrayList
<String
>());
152 meta
.setSource(getSourceName());
153 meta
.setUrl(source
.toString());
158 protected void close() throws IOException
{
159 if (tmp
!= null && tmp
.exists()) {
171 protected String
getDataPrefix() {
175 protected boolean requireInfo() {
179 protected boolean getCover() {