Skip to content

Commit

Permalink
Fix bug that was causing compilation errors for tables from file system
Browse files Browse the repository at this point in the history
see #15
  • Loading branch information
bertfrees committed May 22, 2019
1 parent 4e6b05d commit dadd943
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.liblouis</groupId>
<artifactId>liblouis-java</artifactId>
<version>4.1.0</version>
<version>4.1.1-SNAPSHOT</version>
<packaging>bundle</packaging>
<name>liblouis-java</name>
<description>JNA based Java bindings to liblouis, an open-source braille translator and back-translator.</description>
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/liblouis/Louis.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down
15 changes: 12 additions & 3 deletions src/test/java/org/liblouis/FindTranslatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String> list() {
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/liblouis/TableFromFileSystemTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
2 changes: 0 additions & 2 deletions src/test/resources/tables/foobar.cti
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#+locale: foo

uplow Aa 18,1
uplow Bb 128,12
uplow Cc 148,14
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/tables/foobar.tbl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#+locale: foo

include foobar.cti

0 comments on commit dadd943

Please sign in to comment.