GUI: add search menu
[nikiroo-utils.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) {
54 Instance.getTraceHandler().error(
55 new IOException("Cannot convert the right URL for "
56 + source, e));
57 }
41c3bba7
NR
58 }
59
60 return source;
61 }
86d49dbc 62
41c3bba7
NR
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) {
86d49dbc
NR
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/"
a4143cd7 76 }
86d49dbc
NR
77
78 if (fakeFile.isDirectory()) { // "story/"
79 fakeFile = new File(fakeFile, fakeFile.getName() + ".txt"); // "story/story.txt"
80 }
81
41c3bba7
NR
82 if (fakeFile.getName().endsWith(".txt")) {
83 return fakeFile;
84 }
86d49dbc 85 } catch (Exception e) {
c3305350 86 }
a4143cd7 87
41c3bba7 88 return null;
c3305350
NR
89 }
90}