Skip to content

Commit

Permalink
First shot a DirectoryTableResolver.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander committed Jun 7, 2019
1 parent 0424afa commit 18dde01
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/main/java/org/liblouis/DirectoryTableResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.liblouis;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashSet;
import java.util.Set;

/** A simple table resolver that resolves tables in one specific local directory. */
public class DirectoryTableResolver implements TableResolver {

private Path dir;

public DirectoryTableResolver(Path dir) {
BasicFileAttributes a;
try {
a = Files.getFileAttributeView(dir, BasicFileAttributeView.class).readAttributes();
} catch (NoSuchFileException e) {
throw new RuntimeException("directory does not exist: "+dir);
} catch (FileSystemNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
if (!a.isDirectory())
throw new RuntimeException("Not a directory: "+dir);
this.dir = dir;
}

@Override
public URL resolve(String table, URL base) {
try {
return dir.resolve(table).toFile().toURI().toURL();
} catch (MalformedURLException e) {
return null;
}
}

@Override
public Set<String> list() {
File[] files = dir.toFile().listFiles(new FileFilter() { //NOSONAR
@Override
public boolean accept(File file) {
return file.isFile();
}
});

Set<String> result = new HashSet<>();
for (File it : files) {
result.add(it.getName());
}
return result;
}

}

0 comments on commit 18dde01

Please sign in to comment.