Continue conversion test + HTML.supports() fix
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / Html.java
index fffbcd7f0ad6507ccb026f7fd49fcc83aa987b1a..f66032bfce20032f784b6df0ab4b51ce7883fce7 100644 (file)
@@ -6,6 +6,8 @@ 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,32 +22,40 @@ class Html extends InfoText {
 
        @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 file = new File(url.toURI());
+                       if (file.getName().equals("index.html")) {
+                               file = file.getParentFile();
                        }
+
+                       file = new File(file, file.getName());
+
+                       return super.supports(file.toURI().toURL());
+               } catch (URISyntaxException e) {
+               } catch (MalformedURLException e) {
                }
 
                return false;
        }
 
        @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);
+       public URL getCanonicalUrl(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"
+                       }
+
+                       return fakeFile.toURI().toURL();
+               } catch (Exception e) {
+                       Instance.getTraceHandler().error(
+                                       new IOException("Cannot find the right URL for " + source,
+                                                       e));
                }
 
                return source;