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
;
8 import java
.util
.ArrayList
;
11 import be
.nikiroo
.fanfix
.data
.MetaData
;
12 import be
.nikiroo
.utils
.MarkableFileInputStream
;
14 // not complete: no "description" tag
15 public class InfoReader
{
16 public static MetaData
readMeta(File infoFile
) throws IOException
{
17 if (infoFile
== null) {
18 throw new IOException("File is null");
21 if (infoFile
.exists()) {
22 InputStream in
= new MarkableFileInputStream(new FileInputStream(
25 return createMeta(in
);
31 throw new FileNotFoundException(
32 "File given as argument does not exists: "
33 + infoFile
.getAbsolutePath());
37 private static MetaData
createMeta(InputStream in
) throws IOException
{
38 MetaData meta
= new MetaData();
40 meta
.setTitle(getInfoTag(in
, "TITLE"));
41 meta
.setAuthor(getInfoTag(in
, "AUTHOR"));
42 meta
.setDate(getInfoTag(in
, "DATE"));
43 meta
.setTags(getInfoTagList(in
, "TAGS", ","));
44 meta
.setSource(getInfoTag(in
, "SOURCE"));
45 meta
.setUrl(getInfoTag(in
, "URL"));
46 meta
.setPublisher(getInfoTag(in
, "PUBLISHER"));
47 meta
.setUuid(getInfoTag(in
, "UUID"));
48 meta
.setLuid(getInfoTag(in
, "LUID"));
49 meta
.setLang(getInfoTag(in
, "LANG"));
50 meta
.setSubject(getInfoTag(in
, "SUBJECT"));
51 meta
.setType(getInfoTag(in
, "TYPE"));
52 meta
.setImageDocument(getInfoTagBoolean(in
, "IMAGES_DOCUMENT", false));
53 meta
.setCover(BasicSupport
.getImage(null, getInfoTag(in
, "COVER")));
55 if (meta
.getCover() == null) {
56 meta
.setCover(BasicSupport
.getDefaultCover(meta
.getSubject()));
62 private static boolean getInfoTagBoolean(InputStream in
, String key
,
63 boolean def
) throws IOException
{
64 Boolean value
= getInfoTagBoolean(in
, key
);
65 return value
== null ? def
: value
;
68 private static Boolean
getInfoTagBoolean(InputStream in
, String key
)
70 String value
= getInfoTag(in
, key
);
71 if (value
!= null && !value
.trim().isEmpty()) {
72 value
= value
.toLowerCase().trim();
73 return value
.equals("1") || value
.equals("on")
74 || value
.equals("true") || value
.equals("yes");
80 private static List
<String
> getInfoTagList(InputStream in
, String key
,
81 String separator
) throws IOException
{
82 List
<String
> list
= new ArrayList
<String
>();
83 String tt
= getInfoTag(in
, key
);
85 for (String tag
: tt
.split(separator
)) {
94 * Return the value of the given tag in the <tt>.info</tt> file if present.
99 * @return the value or NULL
101 * @throws IOException
102 * in case of I/O error
104 private static String
getInfoTag(InputStream in
, String key
)
106 key
= "^" + key
+ "=";
110 String value
= BasicSupport
.getLine(in
, key
, 0);
111 if (value
!= null && !value
.isEmpty()) {
112 value
= value
.trim().substring(key
.length() - 1).trim();
113 if (value
.startsWith("'") && value
.endsWith("'")
114 || value
.startsWith("\"") && value
.endsWith("\"")) {
115 value
= value
.substring(1, value
.length() - 1).trim();