| 1 | package be.nikiroo.fanfix.supported; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.FileInputStream; |
| 5 | import java.io.IOException; |
| 6 | import java.io.InputStream; |
| 7 | import java.net.MalformedURLException; |
| 8 | import java.net.URISyntaxException; |
| 9 | import java.net.URL; |
| 10 | import java.util.List; |
| 11 | import java.util.Map.Entry; |
| 12 | |
| 13 | import be.nikiroo.fanfix.data.MetaData; |
| 14 | import be.nikiroo.utils.MarkableFileInputStream; |
| 15 | |
| 16 | /** |
| 17 | * Support class for HTML files created with this program (as we need some |
| 18 | * metadata available in those we create). |
| 19 | * |
| 20 | * @author niki |
| 21 | */ |
| 22 | class Html extends InfoText { |
| 23 | private URL fakeSource; |
| 24 | |
| 25 | @Override |
| 26 | public String getSourceName() { |
| 27 | return "html"; |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | protected boolean supports(URL url) { |
| 32 | if (url.getPath().toLowerCase() |
| 33 | .endsWith(File.separatorChar + "index.html")) { |
| 34 | try { |
| 35 | File file = new File(url.toURI()).getParentFile(); |
| 36 | return super.supports(file.toURI().toURL()); |
| 37 | } catch (URISyntaxException e) { |
| 38 | } catch (MalformedURLException e) { |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | protected MetaData getMeta(URL source, InputStream in) throws IOException { |
| 47 | return super.getMeta(fakeSource, in); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected String getDesc(URL source, InputStream in) throws IOException { |
| 52 | return super.getDesc(fakeSource, in); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | protected List<Entry<String, URL>> getChapters(URL source, InputStream in) |
| 57 | throws IOException { |
| 58 | return super.getChapters(fakeSource, in); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | protected String getChapterContent(URL source, InputStream in, int number) |
| 63 | throws IOException { |
| 64 | return super.getChapterContent(fakeSource, in, number); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | protected InputStream openInput(URL source) throws IOException { |
| 69 | try { |
| 70 | File fakeFile = new File(source.toURI()); // "story/index.html" |
| 71 | fakeFile = new File(fakeFile.getParent()); // "story" |
| 72 | fakeFile = new File(fakeFile, fakeFile.getName()); // "story/story" |
| 73 | fakeSource = fakeFile.toURI().toURL(); |
| 74 | return new MarkableFileInputStream(new FileInputStream(fakeFile)); |
| 75 | } catch (URISyntaxException e) { |
| 76 | throw new IOException( |
| 77 | "file not supported (maybe not created with this program or corrupt)", |
| 78 | e); |
| 79 | } catch (MalformedURLException e) { |
| 80 | throw new IOException("file not supported (bad URL)", e); |
| 81 | } |
| 82 | } |
| 83 | } |