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