Remove or move java.awt dependencies
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / Cbz.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.zip.ZipEntry;
12 import java.util.zip.ZipInputStream;
13
14 import be.nikiroo.fanfix.Instance;
15 import be.nikiroo.fanfix.data.Chapter;
16 import be.nikiroo.fanfix.data.Paragraph;
17 import be.nikiroo.fanfix.data.Story;
18 import be.nikiroo.utils.Image;
19 import be.nikiroo.utils.Progress;
20
21 /**
22 * Support class for CBZ files (works better with CBZ created with this program,
23 * as they have some metadata available).
24 *
25 * @author niki
26 */
27 class Cbz extends Epub {
28 @Override
29 protected boolean supports(URL url) {
30 return url.toString().toLowerCase().endsWith(".cbz");
31 }
32
33 @Override
34 public String getSourceName() {
35 return "cbz";
36 }
37
38 @Override
39 protected String getDataPrefix() {
40 return "";
41 }
42
43 @Override
44 protected boolean requireInfo() {
45 return false;
46 }
47
48 @Override
49 protected boolean getCover() {
50 return false;
51 }
52
53 @Override
54 protected void preprocess(URL source, InputStream in) throws IOException {
55 super.preprocess(source, in);
56 meta.setImageDocument(true);
57 }
58
59 @Override
60 public Story process(URL url, Progress pg) throws IOException {
61 if (pg == null) {
62 pg = new Progress();
63 } else {
64 pg.setMinMax(0, 100);
65 }
66
67 Progress pgMeta = new Progress();
68 pg.addProgress(pgMeta, 10);
69 Story story = processMeta(url, false, true, pgMeta);
70 pgMeta.done(); // 10%
71
72 story.setChapters(new ArrayList<Chapter>());
73 Chapter chap = new Chapter(1, null);
74 story.getChapters().add(chap);
75
76 ZipInputStream zipIn = new ZipInputStream(getInput());
77
78 Map<String, Image> images = new HashMap<String, Image>();
79 for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn
80 .getNextEntry()) {
81 if (!entry.isDirectory()
82 && entry.getName().startsWith(getDataPrefix())) {
83 String entryLName = entry.getName().toLowerCase();
84 boolean imageEntry = false;
85 for (String ext : getImageExt(false)) {
86 if (entryLName.endsWith(ext)) {
87 imageEntry = true;
88 }
89 }
90
91 if (imageEntry) {
92 String uuid = meta.getUuid() + "_" + entry.getName();
93 try {
94 images.put(uuid, new Image(zipIn));
95 } catch (Exception e) {
96 Instance.getTraceHandler().error(e);
97 }
98
99 if (pg.getProgress() < 85) {
100 pg.add(1);
101 }
102 }
103 }
104 }
105
106 pg.setProgress(85);
107
108 // ZIP order is not correct for us
109 List<String> imagesList = new ArrayList<String>(images.keySet());
110 Collections.sort(imagesList);
111
112 pg.setProgress(90);
113
114 for (String uuid : imagesList) {
115 try {
116 chap.getParagraphs().add(new Paragraph(images.get(uuid)));
117 } catch (Exception e) {
118 Instance.getTraceHandler().error(e);
119 }
120 }
121
122 if (meta.getCover() == null && !images.isEmpty()) {
123 meta.setCover(images.get(imagesList.get(0)));
124 meta.setFakeCover(true);
125 }
126
127 pg.setProgress(100);
128 return story;
129 }
130 }