Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / fanfix / output / InfoCover.java
1 package be.nikiroo.fanfix.output;
2
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.OutputStreamWriter;
8 import java.util.Arrays;
9
10 import be.nikiroo.fanfix.Instance;
11 import be.nikiroo.fanfix.bundles.Config;
12 import be.nikiroo.fanfix.data.Chapter;
13 import be.nikiroo.fanfix.data.MetaData;
14 import be.nikiroo.fanfix.data.Story;
15
16 /**
17 * Helper class to write info, cover and summary (resume) files.
18 *
19 * @author niki
20 */
21 public class InfoCover {
22 /**
23 * Write both the <tt>.info<tt> and the <tt>.summary</tt> files by taking
24 * the information from the {@link MetaData}.
25 *
26 * @param targetDir
27 * the directory where to write the 2 files
28 * @param targetName
29 * the target name (no extension) to use (so you will get
30 * <tt>targetName.info</tt> and <tt>targetName.summary</tt>)
31 * @param meta
32 * the {@link MetaData} to get the data out of
33 *
34 * @throws IOException
35 * in case of I/O error
36 */
37 public static void writeInfo(File targetDir, String targetName,
38 MetaData meta) throws IOException {
39 File info = new File(targetDir, targetName + ".info");
40
41 if (meta != null) {
42 BufferedWriter infoWriter = null;
43 try {
44 infoWriter = new BufferedWriter(new OutputStreamWriter(
45 new FileOutputStream(info), "UTF-8"));
46
47 String tags = "";
48 if (meta.getTags() != null) {
49 for (String tag : meta.getTags()) {
50 if (!tags.isEmpty()) {
51 tags += ", ";
52 }
53 tags += tag;
54 }
55 }
56
57 writeMeta(infoWriter, "TITLE", meta.getTitle());
58 writeMeta(infoWriter, "AUTHOR", meta.getAuthor());
59 writeMeta(infoWriter, "DATE", meta.getDate());
60 writeMeta(infoWriter, "SUBJECT", meta.getSubject());
61 writeMeta(infoWriter, "SOURCE", meta.getSource());
62 writeMeta(infoWriter, "URL", meta.getUrl());
63 writeMeta(infoWriter, "TAGS", tags);
64 writeMeta(infoWriter, "UUID", meta.getUuid());
65 writeMeta(infoWriter, "LUID", meta.getLuid());
66 writeMeta(infoWriter, "LANG", meta.getLang() == null ? ""
67 : meta.getLang().toLowerCase());
68 writeMeta(infoWriter, "IMAGES_DOCUMENT",
69 meta.isImageDocument() ? "true" : "false");
70 writeMeta(infoWriter, "TYPE", meta.getType());
71 if (meta.getCover() != null) {
72 String format = Instance.getInstance().getConfig()
73 .getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER)
74 .toLowerCase();
75 writeMeta(infoWriter, "COVER", targetName + "." + format);
76 } else {
77 writeMeta(infoWriter, "COVER", "");
78 }
79 writeMeta(infoWriter, "EPUBCREATOR", BasicOutput.EPUB_CREATOR);
80 writeMeta(infoWriter, "PUBLISHER", meta.getPublisher());
81 writeMeta(infoWriter, "WORDCOUNT",
82 Long.toString(meta.getWords()));
83 writeMeta(infoWriter, "CREATION_DATE", meta.getCreationDate());
84 writeMeta(infoWriter, "FAKE_COVER",
85 Boolean.toString(meta.isFakeCover()));
86 } finally {
87 if (infoWriter != null) {
88 infoWriter.close();
89 }
90 }
91
92 if (meta.getResume() != null) {
93 Story fakeStory = new Story();
94 fakeStory.setMeta(meta);
95 fakeStory.setChapters(Arrays.asList(meta.getResume()));
96
97 Text summaryText = new Text() {
98 @Override
99 protected boolean isWriteCover() {
100 return false;
101 }
102
103 @Override
104 protected boolean isWriteInfo() {
105 return false; // infinite loop if not!
106 }
107
108 @Override
109 public String getDefaultExtension(boolean readerTarget) {
110 return ".summary";
111 }
112
113 @Override
114 protected void writeStoryHeader(Story story)
115 throws IOException {
116 }
117
118 @Override
119 protected void writeStoryFooter(Story story)
120 throws IOException {
121 }
122
123 @Override
124 protected void writeChapterHeader(Chapter chap)
125 throws IOException {
126 }
127
128 @Override
129 protected void writeChapterFooter(Chapter chap)
130 throws IOException {
131 }
132 };
133
134 summaryText.process(fakeStory, targetDir, targetName);
135 }
136 }
137 }
138
139 /**
140 * Write the cover file.
141 *
142 * @param targetDir
143 * the target directory
144 * @param targetName
145 * the target name for the cover (the extension will be added)
146 * @param meta
147 * the meta to get the information out of
148 */
149 public static void writeCover(File targetDir, String targetName,
150 MetaData meta) {
151 if (meta != null && meta.getCover() != null) {
152 try {
153 Instance.getInstance().getCache().saveAsImage(meta.getCover(), new File(targetDir, targetName), true);
154 } catch (IOException e) {
155 // Allow to continue without cover
156 Instance.getInstance().getTraceHandler().error(new IOException("Failed to save the cover image", e));
157 }
158 }
159 }
160
161 private static void writeMeta(BufferedWriter writer, String key,
162 String value) throws IOException {
163 if (value == null) {
164 value = "";
165 }
166
167 writer.write(String.format("%s=\"%s\"\n", key, value.replace("\"", "'")));
168 }
169 }