X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Fsupported%2FHtml.java;h=1e960bf84978bf1125aececd6d59a47eff38f9f9;hb=41c3bba7f6fc6f5ec1fa7fe35643c6aace94240d;hp=fffbcd7f0ad6507ccb026f7fd49fcc83aa987b1a;hpb=a4143cd74a90e17a811a4581cbeb213fed1f6304;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/supported/Html.java b/src/be/nikiroo/fanfix/supported/Html.java index fffbcd7..1e960bf 100644 --- a/src/be/nikiroo/fanfix/supported/Html.java +++ b/src/be/nikiroo/fanfix/supported/Html.java @@ -3,9 +3,10 @@ package be.nikiroo.fanfix.supported; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; -import java.net.URISyntaxException; import java.net.URL; +import be.nikiroo.fanfix.Instance; + /** * Support class for HTML files created with this program (as we need some * metadata available in those we create). @@ -20,34 +21,54 @@ class Html extends InfoText { @Override protected boolean supports(URL url) { - if (url.getPath().toLowerCase() - .endsWith(File.separatorChar + "index.html")) { + File txt = getTxt(url); + return txt != null && txt.exists(); + } + + @Override + public URL getCanonicalUrl(URL source) { + File txt = getTxt(source); + if (txt != null) { try { - File file = new File(url.toURI()).getParentFile(); - return super.supports(file.toURI().toURL()); - } catch (URISyntaxException e) { + source = txt.toURI().toURL(); } catch (MalformedURLException e) { + Instance.getTraceHandler().error( + new IOException("Cannot convert the right URL for " + + source, e)); } + } else { + Instance.getTraceHandler().error( + new IOException("Cannot find the right URL for " + source)); } - return false; + return source; } - @Override - public URL getCanonicalUrl(URL source) throws IOException { - if (source.toString().endsWith(File.separator + "index.html")) { - try { - File fakeFile = new File(source.toURI()); // "story/index.html" - fakeFile = new File(fakeFile.getParent()); // "story" - fakeFile = new File(fakeFile, fakeFile.getName()); // "story/story" - return fakeFile.toURI().toURL(); - } catch (URISyntaxException e) { - throw new IOException( - "file not supported (maybe not created with this program or corrupt)", - e); + /** + * Return the associated TXT source file if it can be found. + * + * @param source + * the source URL + * + * @return the supported source text file or NULL + */ + private static File getTxt(URL source) { + try { + File fakeFile = new File(source.toURI()); + if (fakeFile.getName().equals("index.html")) { // "story/index.html" + fakeFile = new File(fakeFile.getParent()); // -> "story/" + } + + if (fakeFile.isDirectory()) { // "story/" + fakeFile = new File(fakeFile, fakeFile.getName() + ".txt"); // "story/story.txt" } + + if (fakeFile.getName().endsWith(".txt")) { + return fakeFile; + } + } catch (Exception e) { } - return source; + return null; } }