Add URL into .info and MetaData, work on Library
[fanfix.git] / src / be / nikiroo / fanfix / supported / InfoReader.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.io.File;
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;
9 import java.util.List;
10
11 import be.nikiroo.fanfix.data.MetaData;
12 import be.nikiroo.utils.MarkableFileInputStream;
13
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");
19 }
20
21 if (infoFile.exists()) {
22 InputStream in = new MarkableFileInputStream(new FileInputStream(
23 infoFile));
24 try {
25 return createMeta(in);
26 } finally {
27 in.close();
28 in = null;
29 }
30 } else {
31 throw new FileNotFoundException(
32 "File given as argument does not exists: "
33 + infoFile.getAbsolutePath());
34 }
35 }
36
37 private static MetaData createMeta(InputStream in) throws IOException {
38 MetaData meta = new MetaData();
39
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")));
54
55 if (meta.getCover() == null) {
56 meta.setCover(BasicSupport.getDefaultCover(meta.getSubject()));
57 }
58
59 return meta;
60 }
61
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;
66 }
67
68 private static Boolean getInfoTagBoolean(InputStream in, String key)
69 throws IOException {
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");
75 }
76
77 return null;
78 }
79
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);
84 if (tt != null) {
85 for (String tag : tt.split(separator)) {
86 list.add(tag.trim());
87 }
88 }
89
90 return list;
91 }
92
93 /**
94 * Return the value of the given tag in the <tt>.info</tt> file if present.
95 *
96 * @param key
97 * the tag key
98 *
99 * @return the value or NULL
100 *
101 * @throws IOException
102 * in case of I/O error
103 */
104 private static String getInfoTag(InputStream in, String key)
105 throws IOException {
106 key = "^" + key + "=";
107
108 if (in != null) {
109 in.reset();
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();
116 }
117
118 return value;
119 }
120 }
121
122 return null;
123 }
124 }