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
) throws IOException
{
18 if (infoFile
== null) {
19 throw new IOException("File is null");
22 if (infoFile
.exists()) {
23 InputStream in
= new MarkableFileInputStream(new FileInputStream(
26 return createMeta(infoFile
.toURI().toURL(), in
);
32 throw new FileNotFoundException(
33 "File given as argument does not exists: "
34 + infoFile
.getAbsolutePath());
38 private static MetaData
createMeta(URL sourceInfoFile
, InputStream in
)
40 MetaData meta
= new MetaData();
42 meta
.setTitle(getInfoTag(in
, "TITLE"));
43 meta
.setAuthor(getInfoTag(in
, "AUTHOR"));
44 meta
.setDate(getInfoTag(in
, "DATE"));
45 meta
.setTags(getInfoTagList(in
, "TAGS", ","));
46 meta
.setSource(getInfoTag(in
, "SOURCE"));
47 meta
.setUrl(getInfoTag(in
, "URL"));
48 meta
.setPublisher(getInfoTag(in
, "PUBLISHER"));
49 meta
.setUuid(getInfoTag(in
, "UUID"));
50 meta
.setLuid(getInfoTag(in
, "LUID"));
51 meta
.setLang(getInfoTag(in
, "LANG"));
52 meta
.setSubject(getInfoTag(in
, "SUBJECT"));
53 meta
.setType(getInfoTag(in
, "TYPE"));
54 meta
.setImageDocument(getInfoTagBoolean(in
, "IMAGES_DOCUMENT", false));
55 meta
.setCover(BasicSupport
.getImage(null, sourceInfoFile
,
56 getInfoTag(in
, "COVER")));
58 meta
.setWords(Long
.parseLong(getInfoTag(in
, "WORDCOUNT")));
59 } catch (NumberFormatException e
) {
62 meta
.setCreationDate(getInfoTag(in
, "CREATION_DATE"));
63 meta
.setFakeCover(Boolean
.parseBoolean(getInfoTag(in
, "FAKE_COVER")));
65 if (meta
.getCover() == null) {
66 meta
.setCover(BasicSupport
.getDefaultCover(meta
.getSubject()));
72 private static boolean getInfoTagBoolean(InputStream in
, String key
,
73 boolean def
) throws IOException
{
74 Boolean value
= getInfoTagBoolean(in
, key
);
75 return value
== null ? def
: value
;
78 private static Boolean
getInfoTagBoolean(InputStream in
, String key
)
80 String value
= getInfoTag(in
, key
);
81 if (value
!= null && !value
.trim().isEmpty()) {
82 value
= value
.toLowerCase().trim();
83 return value
.equals("1") || value
.equals("on")
84 || value
.equals("true") || value
.equals("yes");
90 private static List
<String
> getInfoTagList(InputStream in
, String key
,
91 String separator
) throws IOException
{
92 List
<String
> list
= new ArrayList
<String
>();
93 String tt
= getInfoTag(in
, key
);
95 for (String tag
: tt
.split(separator
)) {
104 * Return the value of the given tag in the <tt>.info</tt> file if present.
109 * @return the value or NULL
111 * @throws IOException
112 * in case of I/O error
114 private static String
getInfoTag(InputStream in
, String key
)
116 key
= "^" + key
+ "=";
120 String value
= BasicSupport
.getLine(in
, key
, 0);
121 if (value
!= null && !value
.isEmpty()) {
122 value
= value
.trim().substring(key
.length() - 1).trim();
123 if (value
.startsWith("'") && value
.endsWith("'")
124 || value
.startsWith("\"") && value
.endsWith("\"")) {
125 value
= value
.substring(1, value
.length() - 1).trim();