| 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.net.URL; |
| 9 | import java.util.ArrayList; |
| 10 | import java.util.List; |
| 11 | |
| 12 | import be.nikiroo.fanfix.Instance; |
| 13 | import be.nikiroo.fanfix.bundles.Config; |
| 14 | import be.nikiroo.fanfix.data.MetaData; |
| 15 | import be.nikiroo.utils.MarkableFileInputStream; |
| 16 | |
| 17 | // not complete: no "description" tag |
| 18 | public class InfoReader { |
| 19 | public static MetaData readMeta(File infoFile, boolean withCover) |
| 20 | throws IOException { |
| 21 | if (infoFile == null) { |
| 22 | throw new IOException("File is null"); |
| 23 | } |
| 24 | |
| 25 | if (infoFile.exists()) { |
| 26 | InputStream in = new MarkableFileInputStream(new FileInputStream( |
| 27 | infoFile)); |
| 28 | try { |
| 29 | return createMeta(infoFile.toURI().toURL(), in, withCover); |
| 30 | } finally { |
| 31 | in.close(); |
| 32 | in = null; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | throw new FileNotFoundException( |
| 37 | "File given as argument does not exists: " |
| 38 | + infoFile.getAbsolutePath()); |
| 39 | } |
| 40 | |
| 41 | private static MetaData createMeta(URL sourceInfoFile, InputStream in, |
| 42 | boolean withCover) throws IOException { |
| 43 | MetaData meta = new MetaData(); |
| 44 | |
| 45 | meta.setTitle(getInfoTag(in, "TITLE")); |
| 46 | meta.setAuthor(getInfoTag(in, "AUTHOR")); |
| 47 | meta.setDate(getInfoTag(in, "DATE")); |
| 48 | meta.setTags(getInfoTagList(in, "TAGS", ",")); |
| 49 | meta.setSource(getInfoTag(in, "SOURCE")); |
| 50 | meta.setUrl(getInfoTag(in, "URL")); |
| 51 | meta.setPublisher(getInfoTag(in, "PUBLISHER")); |
| 52 | meta.setUuid(getInfoTag(in, "UUID")); |
| 53 | meta.setLuid(getInfoTag(in, "LUID")); |
| 54 | meta.setLang(getInfoTag(in, "LANG")); |
| 55 | meta.setSubject(getInfoTag(in, "SUBJECT")); |
| 56 | meta.setType(getInfoTag(in, "TYPE")); |
| 57 | meta.setImageDocument(getInfoTagBoolean(in, "IMAGES_DOCUMENT", false)); |
| 58 | if (withCover) { |
| 59 | meta.setCover(BasicSupport.getImage(null, sourceInfoFile, |
| 60 | getInfoTag(in, "COVER"))); |
| 61 | // Second chance: try to check for a cover next to the info file |
| 62 | if (meta.getCover() == null) { |
| 63 | String info = sourceInfoFile.getFile().toString(); |
| 64 | if (info.endsWith(".info")) { |
| 65 | info = info.substring(0, info.length() - ".info".length()); |
| 66 | String ext = "." |
| 67 | + Instance.getConfig().getString( |
| 68 | Config.IMAGE_FORMAT_COVER); |
| 69 | meta.setCover(BasicSupport.getImage(null, sourceInfoFile, |
| 70 | info + ext)); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | try { |
| 75 | meta.setWords(Long.parseLong(getInfoTag(in, "WORDCOUNT"))); |
| 76 | } catch (NumberFormatException e) { |
| 77 | meta.setWords(0); |
| 78 | } |
| 79 | meta.setCreationDate(getInfoTag(in, "CREATION_DATE")); |
| 80 | meta.setFakeCover(Boolean.parseBoolean(getInfoTag(in, "FAKE_COVER"))); |
| 81 | |
| 82 | if (withCover && meta.getCover() == null) { |
| 83 | meta.setCover(BasicSupport.getDefaultCover(meta.getSubject())); |
| 84 | } |
| 85 | |
| 86 | return meta; |
| 87 | } |
| 88 | |
| 89 | private static boolean getInfoTagBoolean(InputStream in, String key, |
| 90 | boolean def) throws IOException { |
| 91 | Boolean value = getInfoTagBoolean(in, key); |
| 92 | return value == null ? def : value; |
| 93 | } |
| 94 | |
| 95 | private static Boolean getInfoTagBoolean(InputStream in, String key) |
| 96 | throws IOException { |
| 97 | String value = getInfoTag(in, key); |
| 98 | if (value != null && !value.trim().isEmpty()) { |
| 99 | value = value.toLowerCase().trim(); |
| 100 | return value.equals("1") || value.equals("on") |
| 101 | || value.equals("true") || value.equals("yes"); |
| 102 | } |
| 103 | |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | private static List<String> getInfoTagList(InputStream in, String key, |
| 108 | String separator) throws IOException { |
| 109 | List<String> list = new ArrayList<String>(); |
| 110 | String tt = getInfoTag(in, key); |
| 111 | if (tt != null) { |
| 112 | for (String tag : tt.split(separator)) { |
| 113 | list.add(tag.trim()); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return list; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Return the value of the given tag in the <tt>.info</tt> file if present. |
| 122 | * |
| 123 | * @param key |
| 124 | * the tag key |
| 125 | * |
| 126 | * @return the value or NULL |
| 127 | * |
| 128 | * @throws IOException |
| 129 | * in case of I/O error |
| 130 | */ |
| 131 | private static String getInfoTag(InputStream in, String key) |
| 132 | throws IOException { |
| 133 | key = "^" + key + "="; |
| 134 | |
| 135 | if (in != null) { |
| 136 | in.reset(); |
| 137 | String value = BasicSupport.getLine(in, key, 0); |
| 138 | if (value != null && !value.isEmpty()) { |
| 139 | value = value.trim().substring(key.length() - 1).trim(); |
| 140 | if (value.startsWith("'") && value.endsWith("'") |
| 141 | || value.startsWith("\"") && value.endsWith("\"")) { |
| 142 | value = value.substring(1, value.length() - 1).trim(); |
| 143 | } |
| 144 | |
| 145 | return value; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return null; |
| 150 | } |
| 151 | } |