Perf improvement: covers
[fanfix.git] / src / be / nikiroo / fanfix / supported / InfoText.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.supported;
2
3import java.io.File;
08fe2e33
NR
4import java.io.IOException;
5import java.io.InputStream;
08fe2e33
NR
6import java.net.URISyntaxException;
7import java.net.URL;
08fe2e33
NR
8
9import be.nikiroo.fanfix.Instance;
68686a37 10import 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 */
21class 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 {
57f02339
NR
30 MetaData meta = InfoReader.readMeta(
31 new File(new File(source.toURI()).getPath() + ".info"),
32 true);
68686a37 33
a7d266e6 34 // Some old .info files don't have those now required fields...
68686a37
NR
35 String test = meta.getTitle() == null ? "" : meta.getTitle();
36 test += meta.getAuthor() == null ? "" : meta.getAuthor();
37 test += meta.getDate() == null ? "" : meta.getDate();
a7d266e6 38 test += meta.getUrl() == null ? "" : meta.getUrl();
68686a37
NR
39 if (test.isEmpty()) {
40 MetaData superMeta = super.getMeta(source, reset(in));
41 if (meta.getTitle() == null || meta.getTitle().isEmpty()) {
42 meta.setTitle(superMeta.getTitle());
43 }
44 if (meta.getAuthor() == null || meta.getAuthor().isEmpty()) {
45 meta.setAuthor(superMeta.getAuthor());
46 }
47 if (meta.getDate() == null || meta.getDate().isEmpty()) {
48 meta.setDate(superMeta.getDate());
08fe2e33 49 }
a7d266e6
NR
50 if (meta.getUrl() == null || meta.getUrl().isEmpty()) {
51 meta.setUrl(superMeta.getUrl());
52 }
08fe2e33 53 }
08fe2e33 54
68686a37 55 return meta;
08fe2e33 56
68686a37
NR
57 } catch (URISyntaxException e) {
58 throw new IOException("Cannot parse URL to file: " + source, e);
59 }
08fe2e33
NR
60 }
61
62 @Override
63 protected boolean supports(URL url) {
64 if ("file".equals(url.getProtocol())) {
65 File file;
66 try {
67 file = new File(url.toURI());
68 file = new File(file.getPath() + ".info");
69 } catch (URISyntaxException e) {
70 Instance.syserr(e);
71 file = null;
72 }
73
74 return file != null && file.exists();
75 }
76
77 return false;
78 }
08fe2e33 79}