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
.net
.URLDecoder
;
10 import java
.util
.ArrayList
;
11 import java
.util
.List
;
12 import java
.util
.Map
.Entry
;
13 import java
.util
.zip
.ZipEntry
;
14 import java
.util
.zip
.ZipInputStream
;
16 import be
.nikiroo
.fanfix
.Instance
;
17 import be
.nikiroo
.fanfix
.data
.MetaData
;
18 import be
.nikiroo
.utils
.IOUtils
;
19 import be
.nikiroo
.utils
.ImageUtils
;
20 import be
.nikiroo
.utils
.MarkableFileInputStream
;
21 import be
.nikiroo
.utils
.Progress
;
22 import be
.nikiroo
.utils
.StringUtils
;
25 * Support class for EPUB files created with this program (as we need some
26 * metadata available in those we create).
30 class Epub
extends InfoText
{
31 protected MetaData meta
;
35 private URL fakeSource
;
36 private InputStream fakeIn
;
39 public String
getSourceName() {
44 protected boolean supports(URL url
) {
45 if (url
.getPath().toLowerCase().endsWith(".epub")) {
53 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
58 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
65 return super.getDesc(fakeSource
, fakeIn
);
72 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
,
73 Progress pg
) throws IOException
{
76 return super.getChapters(fakeSource
, fakeIn
, pg
);
83 protected String
getChapterContent(URL source
, InputStream in
, int number
,
84 Progress pg
) throws IOException
{
87 return super.getChapterContent(fakeSource
, fakeIn
, number
, pg
);
94 protected void preprocess(URL source
, InputStream in
) throws IOException
{
95 // Note: do NOT close this stream, as it would also close "in"
96 ZipInputStream zipIn
= new ZipInputStream(in
);
97 tmp
= File
.createTempFile("fanfic-reader-parser_", ".tmp");
98 File tmpInfo
= new File(tmp
+ ".info");
99 fakeSource
= tmp
.toURI().toURL();
100 BufferedImage cover
= null;
102 String url
= source
.toString();
104 String author
= null;
106 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
108 if (!entry
.isDirectory()
109 && entry
.getName().startsWith(getDataPrefix())) {
110 String entryLName
= entry
.getName().toLowerCase();
112 boolean imageEntry
= false;
113 for (String ext
: getImageExt(false)) {
114 if (entryLName
.endsWith(ext
)) {
119 if (entry
.getName().equals(getDataPrefix() + "version")) {
120 // Nothing to do for now ("first"
122 } else if (entryLName
.endsWith(".info")) {
124 IOUtils
.write(zipIn
, tmpInfo
);
125 } else if (imageEntry
) {
129 cover
= ImageUtils
.fromStream(zipIn
);
130 } catch (Exception e
) {
134 } else if (entry
.getName().equals(getDataPrefix() + "URL")) {
135 String
[] descArray
= StringUtils
136 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
138 if (descArray
.length
> 0) {
139 url
= descArray
[0].trim();
141 } else if (entry
.getName().equals(getDataPrefix() + "SUMMARY")) {
142 String
[] descArray
= StringUtils
143 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
146 if (descArray
.length
> 1) {
147 title
= descArray
[0].trim();
149 if (descArray
.length
> 2
150 && descArray
[1].startsWith("©")) {
151 author
= descArray
[1].substring(1).trim();
156 for (int i
= skip
; i
< descArray
.length
; i
++) {
157 this.desc
+= descArray
[i
].trim() + "\n";
160 this.desc
= this.desc
.trim();
162 // Hopefully the data file
163 IOUtils
.write(zipIn
, tmp
);
168 if (requireInfo() && (!tmp
.exists() || !tmpInfo
.exists())) {
169 throw new IOException(
170 "file not supported (maybe not created with this program or corrupt)");
174 this.fakeIn
= new MarkableFileInputStream(new FileInputStream(tmp
));
177 if (tmpInfo
.exists()) {
178 meta
= InfoReader
.readMeta(tmpInfo
, true);
180 meta
.setCover(cover
);
184 if (title
== null || title
.isEmpty()) {
185 title
= new File(source
.getPath()).getName();
186 if (title
.toLowerCase().endsWith(".cbz")) {
187 title
= title
.substring(0, title
.length() - 4);
189 title
= URLDecoder
.decode(title
, "UTF-8").trim();
192 meta
= new MetaData();
194 meta
.setTags(new ArrayList
<String
>());
195 meta
.setSource(getSourceName());
198 meta
.setTitle(title
);
199 meta
.setAuthor(author
);
204 protected void close() throws IOException
{
205 if (tmp
!= null && tmp
.exists()) {
213 if (fakeIn
!= null) {
220 protected String
getDataPrefix() {
224 protected boolean requireInfo() {
228 protected boolean getCover() {