Lot of fixes + first (bad, ugly) working GUI
[fanfix.git] / src / be / nikiroo / fanfix / supported / InfoReader.java
CommitLineData
68686a37
NR
1package be.nikiroo.fanfix.supported;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.io.InputStream;
333f0e7b 8import java.net.URL;
68686a37
NR
9import java.util.ArrayList;
10import java.util.List;
11
12import be.nikiroo.fanfix.data.MetaData;
13import be.nikiroo.utils.MarkableFileInputStream;
14
15// not complete: no "description" tag
16public class InfoReader {
17 public static MetaData readMeta(File infoFile) throws IOException {
18 if (infoFile == null) {
19 throw new IOException("File is null");
20 }
21
22 if (infoFile.exists()) {
23 InputStream in = new MarkableFileInputStream(new FileInputStream(
24 infoFile));
25 try {
333f0e7b 26 return createMeta(infoFile.toURI().toURL(), in);
68686a37
NR
27 } finally {
28 in.close();
29 in = null;
30 }
31 } else {
32 throw new FileNotFoundException(
33 "File given as argument does not exists: "
34 + infoFile.getAbsolutePath());
35 }
36 }
37
333f0e7b
NR
38 private static MetaData createMeta(URL sourceInfoFile, InputStream in)
39 throws IOException {
68686a37
NR
40 MetaData meta = new MetaData();
41
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"));
2206ef66 47 meta.setUrl(getInfoTag(in, "URL"));
68686a37
NR
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));
333f0e7b
NR
55 meta.setCover(BasicSupport.getImage(null, sourceInfoFile,
56 getInfoTag(in, "COVER")));
68686a37
NR
57
58 if (meta.getCover() == null) {
59 meta.setCover(BasicSupport.getDefaultCover(meta.getSubject()));
60 }
61
62 return meta;
63 }
64
65 private static boolean getInfoTagBoolean(InputStream in, String key,
66 boolean def) throws IOException {
67 Boolean value = getInfoTagBoolean(in, key);
68 return value == null ? def : value;
69 }
70
71 private static Boolean getInfoTagBoolean(InputStream in, String key)
72 throws IOException {
73 String value = getInfoTag(in, key);
74 if (value != null && !value.trim().isEmpty()) {
75 value = value.toLowerCase().trim();
76 return value.equals("1") || value.equals("on")
77 || value.equals("true") || value.equals("yes");
78 }
79
80 return null;
81 }
82
83 private static List<String> getInfoTagList(InputStream in, String key,
84 String separator) throws IOException {
85 List<String> list = new ArrayList<String>();
86 String tt = getInfoTag(in, key);
87 if (tt != null) {
88 for (String tag : tt.split(separator)) {
89 list.add(tag.trim());
90 }
91 }
92
93 return list;
94 }
95
96 /**
97 * Return the value of the given tag in the <tt>.info</tt> file if present.
98 *
99 * @param key
100 * the tag key
101 *
102 * @return the value or NULL
103 *
104 * @throws IOException
105 * in case of I/O error
106 */
107 private static String getInfoTag(InputStream in, String key)
108 throws IOException {
109 key = "^" + key + "=";
110
111 if (in != null) {
112 in.reset();
113 String value = BasicSupport.getLine(in, key, 0);
114 if (value != null && !value.isEmpty()) {
115 value = value.trim().substring(key.length() - 1).trim();
116 if (value.startsWith("'") && value.endsWith("'")
117 || value.startsWith("\"") && value.endsWith("\"")) {
118 value = value.substring(1, value.length() - 1).trim();
119 }
120
121 return value;
122 }
123 }
124
125 return null;
126 }
127}