Commit | Line | Data |
---|---|---|
c3305350 NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
3 | import java.io.File; | |
c3305350 | 4 | import java.io.IOException; |
c3305350 | 5 | import java.net.MalformedURLException; |
c3305350 | 6 | import java.net.URL; |
c3305350 | 7 | |
0ffa4754 NR |
8 | import 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 | */ | |
16 | class Html extends InfoText { | |
c3305350 NR |
17 | @Override |
18 | public String getSourceName() { | |
19 | return "html"; | |
20 | } | |
21 | ||
22 | @Override | |
23 | protected boolean supports(URL url) { | |
41c3bba7 NR |
24 | File txt = getTxt(url); |
25 | return txt != null && txt.exists(); | |
c3305350 NR |
26 | } |
27 | ||
28 | @Override | |
0ffa4754 | 29 | public URL getCanonicalUrl(URL source) { |
41c3bba7 NR |
30 | File txt = getTxt(source); |
31 | if (txt != null) { | |
32 | try { | |
33 | source = txt.toURI().toURL(); | |
34 | } catch (MalformedURLException e) { | |
35 | Instance.getTraceHandler().error( | |
36 | new IOException("Cannot convert the right URL for " | |
37 | + source, e)); | |
38 | } | |
39 | } else { | |
40 | Instance.getTraceHandler().error( | |
41 | new IOException("Cannot find the right URL for " + source)); | |
42 | } | |
43 | ||
44 | return source; | |
45 | } | |
86d49dbc | 46 | |
41c3bba7 NR |
47 | /** |
48 | * Return the associated TXT source file if it can be found. | |
49 | * | |
50 | * @param source | |
51 | * the source URL | |
52 | * | |
53 | * @return the supported source text file or NULL | |
54 | */ | |
55 | private static File getTxt(URL source) { | |
86d49dbc NR |
56 | try { |
57 | File fakeFile = new File(source.toURI()); | |
58 | if (fakeFile.getName().equals("index.html")) { // "story/index.html" | |
59 | fakeFile = new File(fakeFile.getParent()); // -> "story/" | |
a4143cd7 | 60 | } |
86d49dbc NR |
61 | |
62 | if (fakeFile.isDirectory()) { // "story/" | |
63 | fakeFile = new File(fakeFile, fakeFile.getName() + ".txt"); // "story/story.txt" | |
64 | } | |
65 | ||
41c3bba7 NR |
66 | if (fakeFile.getName().endsWith(".txt")) { |
67 | return fakeFile; | |
68 | } | |
86d49dbc | 69 | } catch (Exception e) { |
c3305350 | 70 | } |
a4143cd7 | 71 | |
41c3bba7 | 72 | return null; |
c3305350 NR |
73 | } |
74 | } |