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) { | |
0ec78557 NR |
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; | |
c3305350 NR |
33 | } |
34 | ||
60f72311 NR |
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 | ||
c3305350 | 52 | @Override |
0ffa4754 | 53 | public URL getCanonicalUrl(URL source) { |
41c3bba7 NR |
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 | } | |
41c3bba7 NR |
63 | } |
64 | ||
65 | return source; | |
66 | } | |
86d49dbc | 67 | |
41c3bba7 NR |
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) { | |
86d49dbc NR |
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/" | |
a4143cd7 | 81 | } |
86d49dbc NR |
82 | |
83 | if (fakeFile.isDirectory()) { // "story/" | |
84 | fakeFile = new File(fakeFile, fakeFile.getName() + ".txt"); // "story/story.txt" | |
85 | } | |
86 | ||
41c3bba7 NR |
87 | if (fakeFile.getName().endsWith(".txt")) { |
88 | return fakeFile; | |
89 | } | |
86d49dbc | 90 | } catch (Exception e) { |
c3305350 | 91 | } |
a4143cd7 | 92 | |
41c3bba7 | 93 | return null; |
c3305350 NR |
94 | } |
95 | } |