From dadd943470ffbe1b51c7c8b3b78e16b63e646897 Mon Sep 17 00:00:00 2001 From: Bert Frees Date: Wed, 22 May 2019 21:55:07 +0200 Subject: [PATCH] Fix bug that was causing compilation errors for tables from file system see https://github.com/liblouis/liblouis-java/issues/15 --- pom.xml | 2 +- src/main/java/org/liblouis/Louis.java | 8 ++++--- .../java/org/liblouis/FindTranslatorTest.java | 15 +++++++++--- .../org/liblouis/TableFromFileSystemTest.java | 24 +++++++++++++++++++ src/test/resources/tables/foobar.cti | 2 -- src/test/resources/tables/foobar.tbl | 3 +++ 6 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 src/test/java/org/liblouis/TableFromFileSystemTest.java create mode 100644 src/test/resources/tables/foobar.tbl diff --git a/pom.xml b/pom.xml index b92c405..9e3bddd 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ 4.0.0 org.liblouis liblouis-java - 4.1.0 + 4.1.1-SNAPSHOT bundle liblouis-java JNA based Java bindings to liblouis, an open-source braille translator and back-translator. diff --git a/src/main/java/org/liblouis/Louis.java b/src/main/java/org/liblouis/Louis.java index 2869cea..586b49b 100644 --- a/src/main/java/org/liblouis/Louis.java +++ b/src/main/java/org/liblouis/Louis.java @@ -290,10 +290,12 @@ public URL resolve(String table, URL base) { } // try file system if (base != null && base.toString().startsWith("file:")) { - File f = new File(asFile(base), table); + File f = base.toString().endsWith("/") + ? new File(asFile(base), table) + : new File(asFile(base).getParentFile(), table); if (f.exists()) return asURL(f); - } else { + } else if (base == null) { File f = new File(table); if (f.exists()) return asURL(f); @@ -414,7 +416,7 @@ public Object fromNative(Object file, FromNativeContext context) { } } - private static File asFile(URL url) throws IllegalArgumentException { + static File asFile(URL url) throws IllegalArgumentException { try { if (!"file".equals(url.getProtocol())) throw new RuntimeException("expected file URL"); diff --git a/src/test/java/org/liblouis/FindTranslatorTest.java b/src/test/java/org/liblouis/FindTranslatorTest.java index 3eb4eab..47d5cdf 100644 --- a/src/test/java/org/liblouis/FindTranslatorTest.java +++ b/src/test/java/org/liblouis/FindTranslatorTest.java @@ -10,6 +10,7 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.liblouis.Louis.asFile; import static org.liblouis.Louis.asURL; public class FindTranslatorTest { @@ -49,9 +50,17 @@ public FindTranslatorTest() { public URL resolve(String table, URL base) { if (table == null) return null; - File tableFile = new File(table); - if (tableFile.exists()) - return asURL(tableFile); + if (base != null && base.toString().startsWith("file:")) { + File f = base.toString().endsWith("/") + ? new File(asFile(base), table) + : new File(asFile(base).getParentFile(), table); + if (f.exists()) + return asURL(f); + } else if (base == null) { + File f = new File(table); + if (f.exists()) + return asURL(f); + } return null; } public Set list() { diff --git a/src/test/java/org/liblouis/TableFromFileSystemTest.java b/src/test/java/org/liblouis/TableFromFileSystemTest.java new file mode 100644 index 0000000..871e91a --- /dev/null +++ b/src/test/java/org/liblouis/TableFromFileSystemTest.java @@ -0,0 +1,24 @@ +package org.liblouis; + +import java.io.File; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class TableFromFileSystemTest { + + @Test + public void testTableFromFileSystemAbsolutePath() throws Exception { + Translator translator = new Translator(new File(tablesDir, "foobar.tbl").getAbsolutePath()); + assertEquals( + "foobar", + translator.translate("foobar", null, null, null).getBraille()); + } + + private final File tablesDir; + + public TableFromFileSystemTest() { + File testRootDir = new File(this.getClass().getResource("/").getPath()); + tablesDir = new File(testRootDir, "tables"); + } +} diff --git a/src/test/resources/tables/foobar.cti b/src/test/resources/tables/foobar.cti index f54322c..9bcc92a 100644 --- a/src/test/resources/tables/foobar.cti +++ b/src/test/resources/tables/foobar.cti @@ -1,5 +1,3 @@ -#+locale: foo - uplow Aa 18,1 uplow Bb 128,12 uplow Cc 148,14 diff --git a/src/test/resources/tables/foobar.tbl b/src/test/resources/tables/foobar.tbl new file mode 100644 index 0000000..055bac5 --- /dev/null +++ b/src/test/resources/tables/foobar.tbl @@ -0,0 +1,3 @@ +#+locale: foo + +include foobar.cti