WIP: convert local "supports" to new BasicSupport
[fanfix.git] / src / be / nikiroo / fanfix / supported / Html.java
index 8dec5f75d55eac093a8808df1a5023da81251dc0..c7db5d83ce3c0f16d1525dbb591f9453571cd420 100644 (file)
@@ -3,7 +3,6 @@ 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;
@@ -22,14 +21,12 @@ 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 txt = getTxt(url);
+                       if (txt != null) {
+                               return super.supports(txt.toURI().toURL());
                        }
+               } catch (MalformedURLException e) {
                }
 
                return false;
@@ -37,19 +34,45 @@ class Html extends InfoText {
 
        @Override
        public URL getCanonicalUrl(URL source) {
-               if (source.toString().endsWith(File.separator + "index.html")) {
+               File txt = getTxt(source);
+               if (txt != null) {
                        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 (Exception e) {
+                               source = txt.toURI().toURL();
+                       } catch (MalformedURLException e) {
                                Instance.getTraceHandler().error(
-                                               new IOException("Cannot find the right URL for "
+                                               new IOException("Cannot convert the right URL for "
                                                                + source, e));
                        }
                }
 
                return source;
        }
+
+       /**
+        * 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 null;
+       }
 }