blob: 2d36791225073a2f453a9301b34e218742553a64 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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();
}
}
|