fix cache dir for new remote libs
[fanfix.git] / src / be / nikiroo / fanfix / supported / Html.java
CommitLineData
c3305350
NR
1package be.nikiroo.fanfix.supported;
2
3import java.io.File;
c3305350 4import java.io.IOException;
c3305350 5import java.net.MalformedURLException;
c3305350 6import java.net.URL;
c3305350 7
0ffa4754
NR
8import be.nikiroo.fanfix.Instance;
9
c3305350
NR
10/**
11 * Support class for HTML files created with this program (as we need some
12 * metadata available in those we create).
13 *
14 * @author niki
15 */
16class Html extends InfoText {
c3305350
NR
17 @Override
18 protected boolean supports(URL url) {
0ec78557
NR
19 try {
20 File txt = getTxt(url);
21 if (txt != null) {
22 return super.supports(txt.toURI().toURL());
23 }
24 } catch (MalformedURLException e) {
25 }
26
27 return false;
c3305350
NR
28 }
29
60f72311
NR
30 @Override
31 protected File getInfoFile() {
32 File source = getSourceFile();
33 if ("index.html".equals(source.getName())) {
34 source = source.getParentFile();
35 }
36
37 String src = source.getPath();
38 File infoFile = new File(src + ".info");
39 if (!infoFile.exists() && src.endsWith(".txt")) {
40 infoFile = new File(
41 src.substring(0, src.length() - ".txt".length()) + ".info");
42 }
43
44 return infoFile;
45 }
46
c3305350 47 @Override
0ffa4754 48 public URL getCanonicalUrl(URL source) {
41c3bba7
NR
49 File txt = getTxt(source);
50 if (txt != null) {
51 try {
52 source = txt.toURI().toURL();
53 } catch (MalformedURLException e) {
d66deb8d
NR
54 Instance.getInstance().getTraceHandler()
55 .error(new IOException("Cannot convert the right URL for " + source, e));
41c3bba7 56 }
41c3bba7
NR
57 }
58
59 return source;
60 }
86d49dbc 61
41c3bba7
NR
62 /**
63 * Return the associated TXT source file if it can be found.
64 *
65 * @param source
66 * the source URL
67 *
68 * @return the supported source text file or NULL
69 */
70 private static File getTxt(URL source) {
86d49dbc
NR
71 try {
72 File fakeFile = new File(source.toURI());
73 if (fakeFile.getName().equals("index.html")) { // "story/index.html"
74 fakeFile = new File(fakeFile.getParent()); // -> "story/"
a4143cd7 75 }
86d49dbc
NR
76
77 if (fakeFile.isDirectory()) { // "story/"
78 fakeFile = new File(fakeFile, fakeFile.getName() + ".txt"); // "story/story.txt"
79 }
80
41c3bba7
NR
81 if (fakeFile.getName().endsWith(".txt")) {
82 return fakeFile;
83 }
86d49dbc 84 } catch (Exception e) {
c3305350 85 }
a4143cd7 86
41c3bba7 87 return null;
c3305350
NR
88 }
89}