1 package be
.nikiroo
.fanfix
.output
;
3 import java
.io
.BufferedWriter
;
5 import java
.io
.FileOutputStream
;
6 import java
.io
.IOException
;
7 import java
.io
.OutputStreamWriter
;
8 import java
.util
.Arrays
;
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
;
17 * Helper class to write info, cover and summary (resume) files.
21 public class InfoCover
{
23 * Write both the <tt>.info<tt> and the <tt>.summary</tt> files by taking
24 * the information from the {@link MetaData}.
27 * the directory where to write the 2 files
29 * the target name (no extension) to use (so you will get
30 * <tt>targetName.info</tt> and <tt>targetName.summary</tt>)
32 * the {@link MetaData} to get the data out of
35 * in case of I/O error
37 public static void writeInfo(File targetDir
, String targetName
,
38 MetaData meta
) throws IOException
{
39 File info
= new File(targetDir
, targetName
+ ".info");
42 BufferedWriter infoWriter
= null;
44 infoWriter
= new BufferedWriter(new OutputStreamWriter(
45 new FileOutputStream(info
), "UTF-8"));
48 if (meta
.getTags() != null) {
49 for (String tag
: meta
.getTags()) {
50 if (!tags
.isEmpty()) {
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
)
75 writeMeta(infoWriter
, "COVER", targetName
+ "." + format
);
77 writeMeta(infoWriter
, "COVER", "");
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()));
87 if (infoWriter
!= null) {
92 if (meta
.getResume() != null) {
93 Story fakeStory
= new Story();
94 fakeStory
.setMeta(meta
);
95 fakeStory
.setChapters(Arrays
.asList(meta
.getResume()));
97 Text summaryText
= new Text() {
99 protected boolean isWriteCover() {
104 protected boolean isWriteInfo() {
105 return false; // infinite loop if not!
109 public String
getDefaultExtension(boolean readerTarget
) {
114 protected void writeStoryHeader(Story story
)
119 protected void writeStoryFooter(Story story
)
124 protected void writeChapterHeader(Chapter chap
)
129 protected void writeChapterFooter(Chapter chap
)
134 summaryText
.process(fakeStory
, targetDir
, targetName
);
140 * Write the cover file.
143 * the target directory
145 * the target name for the cover (the extension will be added)
147 * the meta to get the information out of
149 public static void writeCover(File targetDir
, String targetName
,
151 if (meta
!= null && meta
.getCover() != null) {
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
));
161 private static void writeMeta(BufferedWriter writer
, String key
,
162 String value
) throws IOException
{
167 writer
.write(String
.format("%s=\"%s\"\n", key
, value
.replace("\"", "'")));