cached lib can now getStory()
[fanfix.git] / src / be / nikiroo / fanfix / supported / Html.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7
8 import be.nikiroo.fanfix.Instance;
9
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 */
16 class Html extends InfoText {
17 @Override
18 public String getSourceName() {
19 return "html";
20 }
21
22 @Override
23 protected boolean supports(URL url) {
24 try {
25 File txt = getTxt(url);
26 if (txt != null) {
27 return super.supports(txt.toURI().toURL());
28 }
29 } catch (MalformedURLException e) {
30 }
31
32 return false;
33 }
34
35 @Override
36 protected File getInfoFile() {
37 File source = getSourceFile();
38 if ("index.html".equals(source.getName())) {
39 source = source.getParentFile();
40 }
41
42 String src = source.getPath();
43 File infoFile = new File(src + ".info");
44 if (!infoFile.exists() && src.endsWith(".txt")) {
45 infoFile = new File(
46 src.substring(0, src.length() - ".txt".length()) + ".info");
47 }
48
49 return infoFile;
50 }
51
52 @Override
53 public URL getCanonicalUrl(URL source) {
54 File txt = getTxt(source);
55 if (txt != null) {
56 try {
57 source = txt.toURI().toURL();
58 } catch (MalformedURLException e) {
59 Instance.getTraceHandler().error(
60 new IOException("Cannot convert the right URL for "
61 + source, e));
62 }
63 }
64
65 return source;
66 }
67
68 /**
69 * Return the associated TXT source file if it can be found.
70 *
71 * @param source
72 * the source URL
73 *
74 * @return the supported source text file or NULL
75 */
76 private static File getTxt(URL source) {
77 try {
78 File fakeFile = new File(source.toURI());
79 if (fakeFile.getName().equals("index.html")) { // "story/index.html"
80 fakeFile = new File(fakeFile.getParent()); // -> "story/"
81 }
82
83 if (fakeFile.isDirectory()) { // "story/"
84 fakeFile = new File(fakeFile, fakeFile.getName() + ".txt"); // "story/story.txt"
85 }
86
87 if (fakeFile.getName().endsWith(".txt")) {
88 return fakeFile;
89 }
90 } catch (Exception e) {
91 }
92
93 return null;
94 }
95 }