manage remote and io exception in fanfix
[fanfix.git] / src / be / nikiroo / fanfix / supported / Html.java
... / ...
CommitLineData
1package be.nikiroo.fanfix.supported;
2
3import java.io.File;
4import java.io.IOException;
5import java.net.MalformedURLException;
6import java.net.URL;
7
8import 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 */
16class Html extends InfoText {
17 @Override
18 protected boolean supports(URL url) {
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;
28 }
29
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
47 @Override
48 public URL getCanonicalUrl(URL source) {
49 File txt = getTxt(source);
50 if (txt != null) {
51 try {
52 source = txt.toURI().toURL();
53 } catch (MalformedURLException e) {
54 Instance.getTraceHandler().error(
55 new IOException("Cannot convert the right URL for "
56 + source, e));
57 }
58 }
59
60 return source;
61 }
62
63 /**
64 * Return the associated TXT source file if it can be found.
65 *
66 * @param source
67 * the source URL
68 *
69 * @return the supported source text file or NULL
70 */
71 private static File getTxt(URL source) {
72 try {
73 File fakeFile = new File(source.toURI());
74 if (fakeFile.getName().equals("index.html")) { // "story/index.html"
75 fakeFile = new File(fakeFile.getParent()); // -> "story/"
76 }
77
78 if (fakeFile.isDirectory()) { // "story/"
79 fakeFile = new File(fakeFile, fakeFile.getName() + ".txt"); // "story/story.txt"
80 }
81
82 if (fakeFile.getName().endsWith(".txt")) {
83 return fakeFile;
84 }
85 } catch (Exception e) {
86 }
87
88 return null;
89 }
90}