Continue conversion test + HTML.supports() fix
authorNiki Roo <niki@nikiroo.be>
Fri, 23 Mar 2018 14:37:58 +0000 (15:37 +0100)
committerNiki Roo <niki@nikiroo.be>
Fri, 23 Mar 2018 14:37:58 +0000 (15:37 +0100)
src/be/nikiroo/fanfix/supported/Html.java
src/be/nikiroo/fanfix/supported/InfoText.java
src/be/nikiroo/fanfix/supported/Text.java
src/be/nikiroo/fanfix/test/ConversionTest.java

index 8dec5f75d55eac093a8808df1a5023da81251dc0..f66032bfce20032f784b6df0ab4b51ce7883fce7 100644 (file)
@@ -22,14 +22,17 @@ 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;
@@ -37,17 +40,22 @@ class Html extends InfoText {
 
        @Override
        public URL getCanonicalUrl(URL source) {
-               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 (Exception e) {
-                               Instance.getTraceHandler().error(
-                                               new IOException("Cannot find the right URL for "
-                                                               + source, e));
+
+               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;
index 9ede84483aadbae697d2c04b19980fa682fbc945..5488ab0648f1e42c6ba476c921b9be9be6bf41d6 100644 (file)
@@ -6,7 +6,6 @@ import java.io.InputStream;
 import java.net.URISyntaxException;
 import java.net.URL;
 
-import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
 
 /**
@@ -27,9 +26,11 @@ class InfoText extends Text {
        @Override
        protected MetaData getMeta(URL source, InputStream in) throws IOException {
                try {
-                       MetaData meta = InfoReader.readMeta(
-                                       new File(new File(source.toURI()).getPath() + ".info"),
-                                       true);
+                       File sourceFile = new File(source.toURI());
+                       sourceFile = assureNoTxt(sourceFile);
+
+                       MetaData meta = InfoReader.readMeta(new File(sourceFile.getPath()
+                                       + ".info"), true);
 
                        // Some old .info files don't have those now required fields...
                        String test = meta.getTitle() == null ? "" : meta.getTitle();
@@ -61,19 +62,6 @@ class InfoText extends Text {
 
        @Override
        protected boolean supports(URL url) {
-               if ("file".equals(url.getProtocol())) {
-                       File file;
-                       try {
-                               file = new File(url.toURI());
-                               file = new File(file.getPath() + ".info");
-                       } catch (URISyntaxException e) {
-                               Instance.getTraceHandler().error(e);
-                               file = null;
-                       }
-
-                       return file != null && file.exists();
-               }
-
-               return false;
+               return supports(url, true);
        }
 }
index 25d7899a61ef1caf7f042053499b28dc87530fa3..fb4a0ad2a7d97c73dcbe37a025df58a0e8f931c4 100644 (file)
@@ -236,20 +236,55 @@ class Text extends BasicSupport_Deprecated {
 
        @Override
        protected boolean supports(URL url) {
+               return supports(url, false);
+       }
+
+       /**
+        * Check if we supports this {@link URL}, that is, if the info file can be
+        * found OR not found.
+        * 
+        * @param url
+        *            the {@link URL} to check
+        * @param info
+        *            TRUE to require the info file, FALSE to forbid the info file
+        * 
+        * @return TRUE if it is supported
+        */
+       protected boolean supports(URL url, boolean info) {
+               boolean infoPresent = false;
                if ("file".equals(url.getProtocol())) {
                        File file;
                        try {
                                file = new File(url.toURI());
+                               file = assureNoTxt(file);
                                file = new File(file.getPath() + ".info");
                        } catch (URISyntaxException e) {
                                Instance.getTraceHandler().error(e);
                                file = null;
                        }
 
-                       return file == null || !file.exists();
+                       infoPresent = (file != null && file.exists());
                }
 
-               return false;
+               return infoPresent == info;
+       }
+
+       /**
+        * Remove the ".txt" extension if it is present.
+        * 
+        * @param file
+        *            the file to process
+        * 
+        * @return the same file or a copy of it without the ".txt" extension if it
+        *         was present
+        */
+       protected File assureNoTxt(File file) {
+               if (file.getName().endsWith(".txt")) {
+                       file = new File(file.getPath().substring(0,
+                                       file.getPath().length() - 4));
+               }
+
+               return file;
        }
 
        /**
index 07ec7478006a1ea1c4487d11c4f0e99cd01ff074..92aca369d899eda7858828ce9378271478e654f5 100644 (file)
@@ -24,10 +24,19 @@ class ConversionTest extends TestLauncher {
        private File testFile;
        private File expectedDir;
        private File resultDir;
+       private List<BasicOutput.OutputType> realTypes;
 
        public ConversionTest(String[] args) {
                super("Conversion", args);
 
+               // Special mode SYSOUT is not a file type (System.out)
+               realTypes = new ArrayList<BasicOutput.OutputType>();
+               for (BasicOutput.OutputType type : BasicOutput.OutputType.values()) {
+                       if (!BasicOutput.OutputType.SYSOUT.equals(type)) {
+                               realTypes.add(type);
+                       }
+               }
+
                addTest(new TestCase("Read the test file") {
                        @Override
                        public void test() throws Exception {
@@ -48,11 +57,8 @@ class ConversionTest extends TestLauncher {
                        }
                });
 
-               for (BasicOutput.OutputType type : BasicOutput.OutputType.values()) {
-                       // NOT for special mode SYSOUT
-                       if (!BasicOutput.OutputType.SYSOUT.equals(type)) {
-                               addTest(getTestFor(type));
-                       }
+               for (BasicOutput.OutputType type : realTypes) {
+                       addTest(getTestFor(type));
                }
        }
 
@@ -81,21 +87,17 @@ class ConversionTest extends TestLauncher {
                                // Check conversion:
                                compareFiles(this, expectedDir, resultDir, type, null);
 
-                               // Cross-checks:
-
                                // LATEX not supported as input
-                               if (!BasicOutput.OutputType.LATEX.equals(type)) {
-                                       for (BasicOutput.OutputType crossType : BasicOutput.OutputType
-                                                       .values()) {
-                                               // NOT for special mode SYSOUT
-                                               if (!BasicOutput.OutputType.SYSOUT.equals(crossType)) {
-                                                       File crossDir = tempFiles
-                                                                       .createTempDir("cross-result");
-                                                       generate(this, target, crossDir, crossType);
-                                                       compareFiles(this, crossDir, resultDir, crossType,
-                                                                       crossType);
-                                               }
-                                       }
+                               if (BasicOutput.OutputType.LATEX.equals(type)) {
+                                       return;
+                               }
+
+                               // Cross-checks:
+                               for (BasicOutput.OutputType crossType : realTypes) {
+                                       File crossDir = tempFiles.createTempDir("cross-result");
+                                       generate(this, target, crossDir, crossType);
+                                       compareFiles(this, resultDir, crossDir, crossType,
+                                                       crossType);
                                }
                        }
                };