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
.data
.MetaData
;
13 import be
.nikiroo
.utils
.MarkableFileInputStream
;
15 // not complete: no "description" tag
16 public class InfoReader
{
17 public static MetaData
readMeta(File infoFile
, boolean withCover
)
19 if (infoFile
== null) {
20 throw new IOException("File is null");
23 if (infoFile
.exists()) {
24 InputStream in
= new MarkableFileInputStream(new FileInputStream(
27 return createMeta(infoFile
.toURI().toURL(), in
, withCover
);
33 throw new FileNotFoundException(
34 "File given as argument does not exists: "
35 + infoFile
.getAbsolutePath());
39 private static MetaData
createMeta(URL sourceInfoFile
, InputStream in
,
40 boolean withCover
) throws IOException
{
41 MetaData meta
= new MetaData();
43 meta
.setTitle(getInfoTag(in
, "TITLE"));
44 meta
.setAuthor(getInfoTag(in
, "AUTHOR"));
45 meta
.setDate(getInfoTag(in
, "DATE"));
46 meta
.setTags(getInfoTagList(in
, "TAGS", ","));
47 meta
.setSource(getInfoTag(in
, "SOURCE"));
48 meta
.setUrl(getInfoTag(in
, "URL"));
49 meta
.setPublisher(getInfoTag(in
, "PUBLISHER"));
50 meta
.setUuid(getInfoTag(in
, "UUID"));
51 meta
.setLuid(getInfoTag(in
, "LUID"));
52 meta
.setLang(getInfoTag(in
, "LANG"));
53 meta
.setSubject(getInfoTag(in
, "SUBJECT"));
54 meta
.setType(getInfoTag(in
, "TYPE"));
55 meta
.setImageDocument(getInfoTagBoolean(in
, "IMAGES_DOCUMENT", false));
57 meta
.setCover(BasicSupport
.getImage(null, sourceInfoFile
,
58 getInfoTag(in
, "COVER")));
61 meta
.setWords(Long
.parseLong(getInfoTag(in
, "WORDCOUNT")));
62 } catch (NumberFormatException e
) {
65 meta
.setCreationDate(getInfoTag(in
, "CREATION_DATE"));
66 meta
.setFakeCover(Boolean
.parseBoolean(getInfoTag(in
, "FAKE_COVER")));
68 if (withCover
&& meta
.getCover() == null) {
69 meta
.setCover(BasicSupport
.getDefaultCover(meta
.getSubject()));
75 private static boolean getInfoTagBoolean(InputStream in
, String key
,
76 boolean def
) throws IOException
{
77 Boolean value
= getInfoTagBoolean(in
, key
);
78 return value
== null ? def
: value
;
81 private static Boolean
getInfoTagBoolean(InputStream in
, String key
)
83 String value
= getInfoTag(in
, key
);
84 if (value
!= null && !value
.trim().isEmpty()) {
85 value
= value
.toLowerCase().trim();
86 return value
.equals("1") || value
.equals("on")
87 || value
.equals("true") || value
.equals("yes");
93 private static List
<String
> getInfoTagList(InputStream in
, String key
,
94 String separator
) throws IOException
{
95 List
<String
> list
= new ArrayList
<String
>();
96 String tt
= getInfoTag(in
, key
);
98 for (String tag
: tt
.split(separator
)) {
107 * Return the value of the given tag in the <tt>.info</tt> file if present.
112 * @return the value or NULL
114 * @throws IOException
115 * in case of I/O error
117 private static String
getInfoTag(InputStream in
, String key
)
119 key
= "^" + key
+ "=";
123 String value
= BasicSupport
.getLine(in
, key
, 0);
124 if (value
!= null && !value
.isEmpty()) {
125 value
= value
.trim().substring(key
.length() - 1).trim();
126 if (value
.startsWith("'") && value
.endsWith("'")
127 || value
.startsWith("\"") && value
.endsWith("\"")) {
128 value
= value
.substring(1, value
.length() - 1).trim();