-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First shot a DirectoryTableResolver.java
- Loading branch information
Alexander
committed
Jun 7, 2019
1 parent
0424afa
commit 18dde01
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |