Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
3 | import java.io.File; | |
08fe2e33 NR |
4 | import java.io.IOException; |
5 | import java.io.InputStream; | |
08fe2e33 NR |
6 | import java.net.URISyntaxException; |
7 | import java.net.URL; | |
08fe2e33 NR |
8 | |
9 | import be.nikiroo.fanfix.Instance; | |
68686a37 | 10 | import be.nikiroo.fanfix.data.MetaData; |
08fe2e33 NR |
11 | |
12 | /** | |
13 | * Support class for <tt>.info</tt> text files ({@link Text} files with a | |
14 | * <tt>.info</tt> metadata file next to them). | |
15 | * <p> | |
16 | * The <tt>.info</tt> file is supposed to be written by this program, or | |
17 | * compatible. | |
18 | * | |
19 | * @author niki | |
20 | */ | |
21 | class InfoText extends Text { | |
22 | @Override | |
23 | public String getSourceName() { | |
24 | return "info-text"; | |
25 | } | |
26 | ||
27 | @Override | |
68686a37 | 28 | protected MetaData getMeta(URL source, InputStream in) throws IOException { |
08fe2e33 | 29 | try { |
68686a37 NR |
30 | MetaData meta = InfoReader.readMeta(new File(new File(source |
31 | .toURI()).getPath() + ".info")); | |
32 | ||
a7d266e6 | 33 | // Some old .info files don't have those now required fields... |
68686a37 NR |
34 | String test = meta.getTitle() == null ? "" : meta.getTitle(); |
35 | test += meta.getAuthor() == null ? "" : meta.getAuthor(); | |
36 | test += meta.getDate() == null ? "" : meta.getDate(); | |
a7d266e6 | 37 | test += meta.getUrl() == null ? "" : meta.getUrl(); |
68686a37 NR |
38 | if (test.isEmpty()) { |
39 | MetaData superMeta = super.getMeta(source, reset(in)); | |
40 | if (meta.getTitle() == null || meta.getTitle().isEmpty()) { | |
41 | meta.setTitle(superMeta.getTitle()); | |
42 | } | |
43 | if (meta.getAuthor() == null || meta.getAuthor().isEmpty()) { | |
44 | meta.setAuthor(superMeta.getAuthor()); | |
45 | } | |
46 | if (meta.getDate() == null || meta.getDate().isEmpty()) { | |
47 | meta.setDate(superMeta.getDate()); | |
08fe2e33 | 48 | } |
a7d266e6 NR |
49 | if (meta.getUrl() == null || meta.getUrl().isEmpty()) { |
50 | meta.setUrl(superMeta.getUrl()); | |
51 | } | |
08fe2e33 | 52 | } |
08fe2e33 | 53 | |
68686a37 | 54 | return meta; |
08fe2e33 | 55 | |
68686a37 NR |
56 | } catch (URISyntaxException e) { |
57 | throw new IOException("Cannot parse URL to file: " + source, e); | |
58 | } | |
08fe2e33 NR |
59 | } |
60 | ||
61 | @Override | |
62 | protected boolean supports(URL url) { | |
63 | if ("file".equals(url.getProtocol())) { | |
64 | File file; | |
65 | try { | |
66 | file = new File(url.toURI()); | |
67 | file = new File(file.getPath() + ".info"); | |
68 | } catch (URISyntaxException e) { | |
69 | Instance.syserr(e); | |
70 | file = null; | |
71 | } | |
72 | ||
73 | return file != null && file.exists(); | |
74 | } | |
75 | ||
76 | return false; | |
77 | } | |
08fe2e33 | 78 | } |