Merge branch 'master' into subtree
[nikiroo-utils.git] / 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 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.getInstance().getTraceHandler()
55 .error(new IOException("Cannot convert the right URL for " + source, e));
56 }
57 }
58
59 return source;
60 }
61
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) {
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/"
75 }
76
77 if (fakeFile.isDirectory()) { // "story/"
78 fakeFile = new File(fakeFile, fakeFile.getName() + ".txt"); // "story/story.txt"
79 }
80
81 if (fakeFile.getName().endsWith(".txt")) {
82 return fakeFile;
83 }
84 } catch (Exception e) {
85 }
86
87 return null;
88 }
89 }