summaryrefslogtreecommitdiff
path: root/src/main/java/org/reasm/batch/LocalFileFetcher.java
diff options
context:
space:
mode:
authorFrancis Gagné <fragag1@gmail.com>2014-09-13 21:49:30 -0400
committerFrancis Gagné <fragag1@gmail.com>2014-09-13 21:59:04 -0400
commited2f84864a7e841249e183a44178fa2c1b38de3b (patch)
tree073577d5e4319a5fba38204e8e8743eb702c0b5d /src/main/java/org/reasm/batch/LocalFileFetcher.java
parent72bfd73989cd2d73863fc31146c7b173febfe879 (diff)
Initial version
Diffstat (limited to 'src/main/java/org/reasm/batch/LocalFileFetcher.java')
-rw-r--r--src/main/java/org/reasm/batch/LocalFileFetcher.java57
1 files changed, 57 insertions, 0 deletions
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();
+ }
+
+}