From ed2f84864a7e841249e183a44178fa2c1b38de3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francis=20Gagn=C3=A9?= Date: Sat, 13 Sep 2014 21:49:30 -0400 Subject: Initial version --- .../java/org/reasm/batch/LocalFileFetcher.java | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/main/java/org/reasm/batch/LocalFileFetcher.java (limited to 'src/main/java/org/reasm/batch/LocalFileFetcher.java') diff --git a/src/main/java/org/reasm/batch/LocalFileFetcher.java b/src/main/java/org/reasm/batch/LocalFileFetcher.java new file mode 100644 index 0000000..2d36791 --- /dev/null +++ b/src/main/java/org/reasm/batch/LocalFileFetcher.java @@ -0,0 +1,57 @@ +package org.reasm.batch; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.reasm.FileFetcher; +import org.reasm.source.SourceFile; + +/** + * Fetches files from the local file system, relative to the directory of the main source file. + * + * @author Francis Gagné + */ +public class LocalFileFetcher implements FileFetcher { + + private final Path mainFileDirectoryPath; + + /** + * Initializes a new LocalFileFetcher. + * + * @param mainFilePath + * the path to the main source file + */ + public LocalFileFetcher(String mainFilePath) { + final Path mainFileDirectoryPath = Paths.get(mainFilePath).getParent(); + if (mainFileDirectoryPath != null && !Files.isDirectory(mainFileDirectoryPath)) { + throw new IllegalArgumentException("mainFilePath"); + } + + this.mainFileDirectoryPath = mainFileDirectoryPath; + } + + @Override + public byte[] fetchBinaryFile(String filePath) throws IOException { + return this.fetchFile(filePath); + } + + @Override + public SourceFile fetchSourceFile(String filePath) throws IOException { + return new SourceFile(new String(this.fetchFile(filePath), "UTF-8"), filePath); + } + + private byte[] fetchFile(String filePath) throws IOException { + return Files.readAllBytes(this.resolveInclude(filePath)); + } + + private Path resolveInclude(String filePath) { + if (this.mainFileDirectoryPath != null) { + return this.mainFileDirectoryPath.resolve(filePath).toAbsolutePath(); + } + + return Paths.get(filePath).toAbsolutePath(); + } + +} -- cgit v1.2.3