fix see word count on source/author, step 2
[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 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
35 @Override
0ffa4754 36 public URL getCanonicalUrl(URL source) {
41c3bba7
NR
37 File txt = getTxt(source);
38 if (txt != null) {
39 try {
40 source = txt.toURI().toURL();
41 } catch (MalformedURLException e) {
42 Instance.getTraceHandler().error(
43 new IOException("Cannot convert the right URL for "
44 + source, e));
45 }
41c3bba7
NR
46 }
47
48 return source;
49 }
86d49dbc 50
41c3bba7
NR
51 /**
52 * Return the associated TXT source file if it can be found.
53 *
54 * @param source
55 * the source URL
56 *
57 * @return the supported source text file or NULL
58 */
59 private static File getTxt(URL source) {
86d49dbc
NR
60 try {
61 File fakeFile = new File(source.toURI());
62 if (fakeFile.getName().equals("index.html")) { // "story/index.html"
63 fakeFile = new File(fakeFile.getParent()); // -> "story/"
a4143cd7 64 }
86d49dbc
NR
65
66 if (fakeFile.isDirectory()) { // "story/"
67 fakeFile = new File(fakeFile, fakeFile.getName() + ".txt"); // "story/story.txt"
68 }
69
41c3bba7
NR
70 if (fakeFile.getName().endsWith(".txt")) {
71 return fakeFile;
72 }
86d49dbc 73 } catch (Exception e) {
c3305350 74 }
a4143cd7 75
41c3bba7 76 return null;
c3305350
NR
77 }
78}