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
;
12 import be
.nikiroo
.fanfix
.Instance
;
13 import be
.nikiroo
.fanfix
.bundles
.Config
;
14 import be
.nikiroo
.fanfix
.data
.MetaData
;
15 import be
.nikiroo
.utils
.MarkableFileInputStream
;
17 // not complete: no "description" tag
18 public class InfoReader
{
19 public static MetaData
readMeta(File infoFile
, boolean withCover
)
21 if (infoFile
== null) {
22 throw new IOException("File is null");
25 if (infoFile
.exists()) {
26 InputStream in
= new MarkableFileInputStream(new FileInputStream(
29 return createMeta(infoFile
.toURI().toURL(), in
, withCover
);
35 throw new FileNotFoundException(
36 "File given as argument does not exists: "
37 + infoFile
.getAbsolutePath());
41 private static MetaData
createMeta(URL sourceInfoFile
, InputStream in
,
42 boolean withCover
) throws IOException
{
43 MetaData meta
= new MetaData();
45 meta
.setTitle(getInfoTag(in
, "TITLE"));
46 meta
.setAuthor(getInfoTag(in
, "AUTHOR"));
47 meta
.setDate(getInfoTag(in
, "DATE"));
48 meta
.setTags(getInfoTagList(in
, "TAGS", ","));
49 meta
.setSource(getInfoTag(in
, "SOURCE"));
50 meta
.setUrl(getInfoTag(in
, "URL"));
51 meta
.setPublisher(getInfoTag(in
, "PUBLISHER"));
52 meta
.setUuid(getInfoTag(in
, "UUID"));
53 meta
.setLuid(getInfoTag(in
, "LUID"));
54 meta
.setLang(getInfoTag(in
, "LANG"));
55 meta
.setSubject(getInfoTag(in
, "SUBJECT"));
56 meta
.setType(getInfoTag(in
, "TYPE"));
57 meta
.setImageDocument(getInfoTagBoolean(in
, "IMAGES_DOCUMENT", false));
59 meta
.setCover(BasicSupport
.getImage(null, sourceInfoFile
,
60 getInfoTag(in
, "COVER")));
61 // Second chance: try to check for a cover next to the info file
62 if (meta
.getCover() == null) {
63 String info
= sourceInfoFile
.getFile().toString();
64 if (info
.endsWith(".info")) {
65 info
= info
.substring(0, info
.length() - ".info".length());
67 + Instance
.getConfig().getString(
68 Config
.IMAGE_FORMAT_COVER
);
69 meta
.setCover(BasicSupport
.getImage(null, sourceInfoFile
,
75 meta
.setWords(Long
.parseLong(getInfoTag(in
, "WORDCOUNT")));
76 } catch (NumberFormatException e
) {
79 meta
.setCreationDate(getInfoTag(in
, "CREATION_DATE"));
80 meta
.setFakeCover(Boolean
.parseBoolean(getInfoTag(in
, "FAKE_COVER")));
82 if (withCover
&& meta
.getCover() == null) {
83 meta
.setCover(BasicSupport
.getDefaultCover(meta
.getSubject()));
89 private static boolean getInfoTagBoolean(InputStream in
, String key
,
90 boolean def
) throws IOException
{
91 Boolean value
= getInfoTagBoolean(in
, key
);
92 return value
== null ? def
: value
;
95 private static Boolean
getInfoTagBoolean(InputStream in
, String key
)
97 String value
= getInfoTag(in
, key
);
98 if (value
!= null && !value
.trim().isEmpty()) {
99 value
= value
.toLowerCase().trim();
100 return value
.equals("1") || value
.equals("on")
101 || value
.equals("true") || value
.equals("yes");
107 private static List
<String
> getInfoTagList(InputStream in
, String key
,
108 String separator
) throws IOException
{
109 List
<String
> list
= new ArrayList
<String
>();
110 String tt
= getInfoTag(in
, key
);
112 for (String tag
: tt
.split(separator
)) {
113 list
.add(tag
.trim());
121 * Return the value of the given tag in the <tt>.info</tt> file if present.
126 * @return the value or NULL
128 * @throws IOException
129 * in case of I/O error
131 private static String
getInfoTag(InputStream in
, String key
)
133 key
= "^" + key
+ "=";
137 String value
= BasicSupport
.getLine(in
, key
, 0);
138 if (value
!= null && !value
.isEmpty()) {
139 value
= value
.trim().substring(key
.length() - 1).trim();
140 if (value
.startsWith("'") && value
.endsWith("'")
141 || value
.startsWith("\"") && value
.endsWith("\"")) {
142 value
= value
.substring(1, value
.length() - 1).trim();