1 package be
.nikiroo
.fanfix
.supported
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.FileNotFoundException
;
6 import java
.io
.IOException
;
7 import java
.io
.InputStream
;
9 import java
.util
.ArrayList
;
10 import java
.util
.List
;
11 import java
.util
.Scanner
;
13 import be
.nikiroo
.fanfix
.Instance
;
14 import be
.nikiroo
.fanfix
.bundles
.Config
;
15 import be
.nikiroo
.fanfix
.data
.MetaData
;
16 import be
.nikiroo
.utils
.Image
;
17 import be
.nikiroo
.utils
.MarkableFileInputStream
;
19 // not complete: no "description" tag
20 public class InfoReader
{
21 public static MetaData
readMeta(File infoFile
, boolean withCover
)
23 if (infoFile
== null) {
24 throw new IOException("File is null");
27 if (infoFile
.exists()) {
28 InputStream in
= new MarkableFileInputStream(new FileInputStream(
31 return createMeta(infoFile
.toURI().toURL(), in
, withCover
);
37 throw new FileNotFoundException(
38 "File given as argument does not exists: "
39 + infoFile
.getAbsolutePath());
42 private static MetaData
createMeta(URL sourceInfoFile
, InputStream in
,
43 boolean withCover
) throws IOException
{
44 MetaData meta
= new MetaData();
46 meta
.setTitle(getInfoTag(in
, "TITLE"));
47 meta
.setAuthor(getInfoTag(in
, "AUTHOR"));
48 meta
.setDate(getInfoTag(in
, "DATE"));
49 meta
.setTags(getInfoTagList(in
, "TAGS", ","));
50 meta
.setSource(getInfoTag(in
, "SOURCE"));
51 meta
.setUrl(getInfoTag(in
, "URL"));
52 meta
.setPublisher(getInfoTag(in
, "PUBLISHER"));
53 meta
.setUuid(getInfoTag(in
, "UUID"));
54 meta
.setLuid(getInfoTag(in
, "LUID"));
55 meta
.setLang(getInfoTag(in
, "LANG"));
56 meta
.setSubject(getInfoTag(in
, "SUBJECT"));
57 meta
.setType(getInfoTag(in
, "TYPE"));
58 meta
.setImageDocument(getInfoTagBoolean(in
, "IMAGES_DOCUMENT", false));
60 String infoTag
= getInfoTag(in
, "COVER");
61 if (infoTag
!= null && !infoTag
.trim().isEmpty()) {
62 meta
.setCover(BasicSupportHelper
.getImage(null, sourceInfoFile
,
65 if (meta
.getCover() == null) {
66 // Second chance: try to check for a cover next to the info file
67 meta
.setCover(getCoverByName(sourceInfoFile
));
71 meta
.setWords(Long
.parseLong(getInfoTag(in
, "WORDCOUNT")));
72 } catch (NumberFormatException e
) {
75 meta
.setCreationDate(getInfoTag(in
, "CREATION_DATE"));
76 meta
.setFakeCover(Boolean
.parseBoolean(getInfoTag(in
, "FAKE_COVER")));
78 if (withCover
&& meta
.getCover() == null) {
79 meta
.setCover(BasicSupportHelper
.getDefaultCover(meta
.getSubject()));
86 * Return the cover image if it is next to the source file.
88 * @param sourceInfoFile
91 * @return the cover if present, NULL if not
93 public static Image
getCoverByName(URL sourceInfoFile
) {
94 String info
= sourceInfoFile
.getFile().toString();
95 if (info
.endsWith(".info")) {
96 info
= info
.substring(0, info
.length() - ".info".length());
98 + Instance
.getConfig().getString(Config
.IMAGE_FORMAT_COVER
)
100 return BasicSupportHelper
101 .getImage(null, sourceInfoFile
, info
+ ext
);
107 private static boolean getInfoTagBoolean(InputStream in
, String key
,
108 boolean def
) throws IOException
{
109 Boolean value
= getInfoTagBoolean(in
, key
);
110 return value
== null ? def
: value
;
113 private static Boolean
getInfoTagBoolean(InputStream in
, String key
)
115 String value
= getInfoTag(in
, key
);
116 if (value
!= null && !value
.trim().isEmpty()) {
117 value
= value
.toLowerCase().trim();
118 return value
.equals("1") || value
.equals("on")
119 || value
.equals("true") || value
.equals("yes");
125 private static List
<String
> getInfoTagList(InputStream in
, String key
,
126 String separator
) throws IOException
{
127 List
<String
> list
= new ArrayList
<String
>();
128 String tt
= getInfoTag(in
, key
);
130 for (String tag
: tt
.split(separator
)) {
131 list
.add(tag
.trim());
139 * Return the value of the given tag in the <tt>.info</tt> file if present.
144 * @return the value or NULL
146 * @throws IOException
147 * in case of I/O error
149 private static String
getInfoTag(InputStream in
, String key
)
151 key
= "^" + key
+ "=";
155 String value
= getLine(in
, key
, 0);
156 if (value
!= null && !value
.isEmpty()) {
157 value
= value
.trim().substring(key
.length() - 1).trim();
158 if (value
.startsWith("'") && value
.endsWith("'")
159 || value
.startsWith("\"") && value
.endsWith("\"")) {
160 value
= value
.substring(1, value
.length() - 1).trim();
171 * Return the first line from the given input which correspond to the given
177 * a string that must be found inside the target line (also
178 * supports "^" at start to say "only if it starts with" the
180 * @param relativeLine
181 * the line to return based upon the target line position (-1 =
182 * the line before, 0 = the target line...)
186 static private String
getLine(InputStream in
, String needle
,
188 return getLine(in
, needle
, relativeLine
, true);
192 * Return a line from the given input which correspond to the given
198 * a string that must be found inside the target line (also
199 * supports "^" at start to say "only if it starts with" the
201 * @param relativeLine
202 * the line to return based upon the target line position (-1 =
203 * the line before, 0 = the target line...)
205 * takes the first result (as opposed to the last one, which will
206 * also always spend the input)
210 static private String
getLine(InputStream in
, String needle
,
211 int relativeLine
, boolean first
) {
214 List
<String
> lines
= new ArrayList
<String
>();
215 @SuppressWarnings("resource")
216 Scanner scan
= new Scanner(in
, "UTF-8");
218 scan
.useDelimiter("\\n");
219 while (scan
.hasNext()) {
220 lines
.add(scan
.next());
223 if (needle
.startsWith("^")) {
224 if (lines
.get(lines
.size() - 1).startsWith(
225 needle
.substring(1))) {
226 index
= lines
.size() - 1;
230 if (lines
.get(lines
.size() - 1).contains(needle
)) {
231 index
= lines
.size() - 1;
236 if (index
>= 0 && index
+ relativeLine
< lines
.size()) {
237 rep
= lines
.get(index
+ relativeLine
);