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
.streams
.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(infoFile
);
30 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 if (meta
.getCover() == null) {
65 // Second chance: try to check for a cover next to the info file
66 meta
.setCover(getCoverByName(sourceInfoFile
));
70 meta
.setWords(Long
.parseLong(getInfoTag(in
, "WORDCOUNT")));
71 } catch (NumberFormatException e
) {
74 meta
.setCreationDate(getInfoTag(in
, "CREATION_DATE"));
75 meta
.setFakeCover(Boolean
.parseBoolean(getInfoTag(in
, "FAKE_COVER")));
77 if (withCover
&& meta
.getCover() == null) {
78 meta
.setCover(BasicSupportHelper
.getDefaultCover(meta
.getSubject()));
85 * Return the cover image if it is next to the source file.
87 * @param sourceInfoFile
90 * @return the cover if present, NULL if not
92 public static Image
getCoverByName(URL sourceInfoFile
) {
95 File basefile
= new File(sourceInfoFile
.getFile());
98 + Instance
.getConfig().getString(Config
.FILE_FORMAT_IMAGE_FORMAT_COVER
)
101 // Without removing ext
102 cover
= BasicSupportHelper
.getImage(null, sourceInfoFile
,
103 basefile
.getAbsolutePath() + ext
);
106 String name
= basefile
.getName();
107 int pos
= name
.lastIndexOf(".");
108 if (cover
== null && pos
> 0) {
109 name
= name
.substring(0, pos
);
110 basefile
= new File(basefile
.getParent(), name
);
112 cover
= BasicSupportHelper
.getImage(null, sourceInfoFile
,
113 basefile
.getAbsolutePath() + ext
);
119 private static boolean getInfoTagBoolean(InputStream in
, String key
,
120 boolean def
) throws IOException
{
121 Boolean value
= getInfoTagBoolean(in
, key
);
122 return value
== null ? def
: value
;
125 private static Boolean
getInfoTagBoolean(InputStream in
, String key
)
127 String value
= getInfoTag(in
, key
);
128 if (value
!= null && !value
.trim().isEmpty()) {
129 value
= value
.toLowerCase().trim();
130 return value
.equals("1") || value
.equals("on")
131 || value
.equals("true") || value
.equals("yes");
137 private static List
<String
> getInfoTagList(InputStream in
, String key
,
138 String separator
) throws IOException
{
139 List
<String
> list
= new ArrayList
<String
>();
140 String tt
= getInfoTag(in
, key
);
142 for (String tag
: tt
.split(separator
)) {
143 list
.add(tag
.trim());
151 * Return the value of the given tag in the <tt>.info</tt> file if present.
156 * @return the value or NULL
158 * @throws IOException
159 * in case of I/O error
161 private static String
getInfoTag(InputStream in
, String key
)
163 key
= "^" + key
+ "=";
167 String value
= getLine(in
, key
, 0);
168 if (value
!= null && !value
.isEmpty()) {
169 value
= value
.trim().substring(key
.length() - 1).trim();
170 if (value
.startsWith("'") && value
.endsWith("'")
171 || value
.startsWith("\"") && value
.endsWith("\"")) {
172 value
= value
.substring(1, value
.length() - 1).trim();
183 * Return the first line from the given input which correspond to the given
189 * a string that must be found inside the target line (also
190 * supports "^" at start to say "only if it starts with" the
192 * @param relativeLine
193 * the line to return based upon the target line position (-1 =
194 * the line before, 0 = the target line...)
198 static private String
getLine(InputStream in
, String needle
,
200 return getLine(in
, needle
, relativeLine
, true);
204 * Return a line from the given input which correspond to the given
210 * a string that must be found inside the target line (also
211 * supports "^" at start to say "only if it starts with" the
213 * @param relativeLine
214 * the line to return based upon the target line position (-1 =
215 * the line before, 0 = the target line...)
217 * takes the first result (as opposed to the last one, which will
218 * also always spend the input)
222 static private String
getLine(InputStream in
, String needle
,
223 int relativeLine
, boolean first
) {
226 List
<String
> lines
= new ArrayList
<String
>();
227 @SuppressWarnings("resource")
228 Scanner scan
= new Scanner(in
, "UTF-8");
230 scan
.useDelimiter("\\n");
231 while (scan
.hasNext()) {
232 lines
.add(scan
.next());
235 if (needle
.startsWith("^")) {
236 if (lines
.get(lines
.size() - 1).startsWith(
237 needle
.substring(1))) {
238 index
= lines
.size() - 1;
242 if (lines
.get(lines
.size() - 1).contains(needle
)) {
243 index
= lines
.size() - 1;
248 if (index
>= 0 && index
+ relativeLine
< lines
.size()) {
249 rep
= lines
.get(index
+ relativeLine
);