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
);
36 throw new FileNotFoundException(
37 "File given as argument does not exists: "
38 + 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 String infoTag
= getInfoTag(in
, "COVER");
60 if (infoTag
!= null && !infoTag
.trim().isEmpty()) {
61 meta
.setCover(BasicSupportHelper
.getImage(null, sourceInfoFile
,
64 // Second chance: try to check for a cover next to the info file
65 if (meta
.getCover() == null) {
66 String info
= sourceInfoFile
.getFile().toString();
67 if (info
.endsWith(".info")) {
68 info
= info
.substring(0, info
.length() - ".info".length());
70 + Instance
.getConfig()
71 .getString(Config
.IMAGE_FORMAT_COVER
)
73 meta
.setCover(BasicSupportHelper
.getImage(null,
74 sourceInfoFile
, info
+ ext
));
79 meta
.setWords(Long
.parseLong(getInfoTag(in
, "WORDCOUNT")));
80 } catch (NumberFormatException e
) {
83 meta
.setCreationDate(getInfoTag(in
, "CREATION_DATE"));
84 meta
.setFakeCover(Boolean
.parseBoolean(getInfoTag(in
, "FAKE_COVER")));
86 if (withCover
&& meta
.getCover() == null) {
87 meta
.setCover(BasicSupportHelper
.getDefaultCover(meta
.getSubject()));
93 private static boolean getInfoTagBoolean(InputStream in
, String key
,
94 boolean def
) throws IOException
{
95 Boolean value
= getInfoTagBoolean(in
, key
);
96 return value
== null ? def
: value
;
99 private static Boolean
getInfoTagBoolean(InputStream in
, String key
)
101 String value
= getInfoTag(in
, key
);
102 if (value
!= null && !value
.trim().isEmpty()) {
103 value
= value
.toLowerCase().trim();
104 return value
.equals("1") || value
.equals("on")
105 || value
.equals("true") || value
.equals("yes");
111 private static List
<String
> getInfoTagList(InputStream in
, String key
,
112 String separator
) throws IOException
{
113 List
<String
> list
= new ArrayList
<String
>();
114 String tt
= getInfoTag(in
, key
);
116 for (String tag
: tt
.split(separator
)) {
117 list
.add(tag
.trim());
125 * Return the value of the given tag in the <tt>.info</tt> file if present.
130 * @return the value or NULL
132 * @throws IOException
133 * in case of I/O error
135 private static String
getInfoTag(InputStream in
, String key
)
137 key
= "^" + key
+ "=";
141 String value
= BasicSupport_Deprecated
.getLine(in
, key
, 0);
142 if (value
!= null && !value
.isEmpty()) {
143 value
= value
.trim().substring(key
.length() - 1).trim();
144 if (value
.startsWith("'") && value
.endsWith("'")
145 || value
.startsWith("\"") && value
.endsWith("\"")) {
146 value
= value
.substring(1, value
.length() - 1).trim();