X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Fsupported%2FHtml.java;h=900fa0a1772f0ffd5ecfcaf2fecfbd8b248168a0;hb=d66deb8d8b30cff6b54db352eef34a3508939f84;hp=036479d21758797c403bbf17686222880832f157;hpb=c330535057e64f195ee0d6e3955f3a65731e39a2;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/supported/Html.java b/src/be/nikiroo/fanfix/supported/Html.java index 036479d..900fa0a 100644 --- a/src/be/nikiroo/fanfix/supported/Html.java +++ b/src/be/nikiroo/fanfix/supported/Html.java @@ -1,17 +1,11 @@ package be.nikiroo.fanfix.supported; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.net.MalformedURLException; -import java.net.URISyntaxException; import java.net.URL; -import java.util.List; -import java.util.Map.Entry; -import be.nikiroo.fanfix.data.MetaData; -import be.nikiroo.utils.MarkableFileInputStream; +import be.nikiroo.fanfix.Instance; /** * Support class for HTML files created with this program (as we need some @@ -20,64 +14,76 @@ import be.nikiroo.utils.MarkableFileInputStream; * @author niki */ class Html extends InfoText { - private URL fakeSource; - - @Override - public String getSourceName() { - return "html"; - } - @Override protected boolean supports(URL url) { - if (url.getPath().toLowerCase() - .endsWith(File.separatorChar + "index.html")) { - try { - File file = new File(url.toURI()).getParentFile(); - return super.supports(file.toURI().toURL()); - } catch (URISyntaxException e) { - } catch (MalformedURLException e) { + try { + File txt = getTxt(url); + if (txt != null) { + return super.supports(txt.toURI().toURL()); } + } catch (MalformedURLException e) { } return false; } @Override - protected MetaData getMeta(URL source, InputStream in) throws IOException { - return super.getMeta(fakeSource, in); - } + protected File getInfoFile() { + File source = getSourceFile(); + if ("index.html".equals(source.getName())) { + source = source.getParentFile(); + } - @Override - protected String getDesc(URL source, InputStream in) throws IOException { - return super.getDesc(fakeSource, in); - } + String src = source.getPath(); + File infoFile = new File(src + ".info"); + if (!infoFile.exists() && src.endsWith(".txt")) { + infoFile = new File( + src.substring(0, src.length() - ".txt".length()) + ".info"); + } - @Override - protected List> getChapters(URL source, InputStream in) - throws IOException { - return super.getChapters(fakeSource, in); + return infoFile; } @Override - protected String getChapterContent(URL source, InputStream in, int number) - throws IOException { - return super.getChapterContent(fakeSource, in, number); + public URL getCanonicalUrl(URL source) { + File txt = getTxt(source); + if (txt != null) { + try { + source = txt.toURI().toURL(); + } catch (MalformedURLException e) { + Instance.getInstance().getTraceHandler() + .error(new IOException("Cannot convert the right URL for " + source, e)); + } + } + + return source; } - @Override - protected InputStream openInput(URL source) throws IOException { + /** + * 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()); // "story/index.html" - fakeFile = new File(fakeFile.getParent()); // "story" - fakeFile = new File(fakeFile, fakeFile.getName()); // "story/story" - fakeSource = fakeFile.toURI().toURL(); - return new MarkableFileInputStream(new FileInputStream(fakeFile)); - } catch (URISyntaxException e) { - throw new IOException( - "file not supported (maybe not created with this program or corrupt)", - e); - } catch (MalformedURLException e) { - throw new IOException("file not supported (bad URL)", e); + 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 null; } }