|
1 | 1 | package util;
|
2 | 2 |
|
3 | 3 | import java.io.BufferedReader;
|
4 |
| -import java.io.File; |
5 | 4 | import java.io.FileReader;
|
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStreamReader; |
| 7 | +import java.net.URI; |
6 | 8 | import java.net.URISyntaxException;
|
7 |
| -import java.nio.file.Path; |
| 9 | +import java.net.URL; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Paths; |
8 | 12 | import java.util.Arrays;
|
9 | 13 | import java.util.Collections;
|
| 14 | +import java.util.HashMap; |
10 | 15 | import java.util.List;
|
11 | 16 | import java.util.Map;
|
12 |
| -import java.util.Objects; |
13 | 17 | import java.util.stream.Collectors;
|
14 | 18 |
|
15 | 19 | /**
|
|
19 | 23 | public class ExamplesHandler {
|
20 | 24 |
|
21 | 25 | private static final String EXAMPLES_DIR = "/examples/";
|
| 26 | + private static final String EXAMPLES_LIST_FILE = EXAMPLES_DIR + "_example_list"; |
22 | 27 |
|
23 |
| - private static Map<String, Path> exampleMap = null; |
| 28 | + private static Map<String, String[]> exampleMap = null; |
24 | 29 |
|
25 | 30 | private ExamplesHandler() {}
|
26 | 31 |
|
27 |
| - private static Map<String, Path> getExampleMap() { |
| 32 | + private static Map<String, String[]> getExampleMap() { |
28 | 33 | if (exampleMap == null) {
|
| 34 | + // Read examples in one of two ways: |
| 35 | + // 1. If running from from the source via Gradle, e.g. in your IDE or terminal, list the files |
| 36 | + // in the examples dir and load them. |
| 37 | + |
29 | 38 | try {
|
30 |
| - exampleMap = |
31 |
| - Arrays.stream( |
32 |
| - Objects.requireNonNull( |
33 |
| - new File(ExamplesHandler.class.getResource(EXAMPLES_DIR).toURI()) |
34 |
| - .listFiles())) |
35 |
| - .map(File::toPath) // Convert File to Path |
36 |
| - .collect( |
37 |
| - Collectors.toMap( |
38 |
| - path -> { |
39 |
| - // Process the file name to create a readable name |
40 |
| - String name = path.getFileName().toString(); |
41 |
| - name = name.substring(0, name.lastIndexOf('.')); |
42 |
| - name = name.replace('_', ' '); |
43 |
| - name = name.substring(0, 1).toUpperCase() + name.substring(1); |
44 |
| - return name; |
45 |
| - }, |
46 |
| - path -> path // Use the Path object itself as the map value |
47 |
| - )); |
48 |
| - } catch (URISyntaxException e) { |
49 |
| - e.printStackTrace(); |
| 39 | + URI uri = ExamplesHandler.class.getResource(EXAMPLES_DIR).toURI(); |
| 40 | + // Check if uri points to a file (directory), or just a resource in a JAR |
| 41 | + if (uri.getScheme().equals("file")) { |
| 42 | + exampleMap = |
| 43 | + Files.list(Paths.get(uri)) |
| 44 | + .filter(Files::isRegularFile) |
| 45 | + .filter(path -> path.getFileName().toString().endsWith(".txt")) |
| 46 | + .collect( |
| 47 | + Collectors.toMap( |
| 48 | + path -> nameFromPath(path.getFileName().toString()), |
| 49 | + path -> { |
| 50 | + // Load the file from the file system |
| 51 | + try (BufferedReader reader = |
| 52 | + new BufferedReader(new FileReader(path.toFile()))) { |
| 53 | + return reader.lines().toArray(String[]::new); |
| 54 | + } catch (Exception e) { |
| 55 | + return new String[0]; |
| 56 | + } |
| 57 | + })); |
| 58 | + } |
| 59 | + } catch (URISyntaxException | IOException e) { |
| 60 | + // Ignore |
| 61 | + exampleMap = null; |
| 62 | + } |
| 63 | + |
| 64 | + // 2. If running from a JAR, files in examples dir cannot be listed. Instead, first read the |
| 65 | + // special file that contains their names, and then load them explicitly. |
| 66 | + if (exampleMap == null) { |
| 67 | + // Read the list of examples from the _example_list file using getResourceAsStream |
| 68 | + try (BufferedReader reader = |
| 69 | + new BufferedReader( |
| 70 | + new InputStreamReader( |
| 71 | + ExamplesHandler.class.getResourceAsStream(EXAMPLES_LIST_FILE)))) { |
| 72 | + Map<String, String[]> tmpMap = new HashMap<>(); |
| 73 | + String line; |
| 74 | + while ((line = reader.readLine()) != null) { |
| 75 | + URL fileURL = ExamplesHandler.class.getResource(EXAMPLES_DIR + line); |
| 76 | + if (fileURL != null) { |
| 77 | + try (BufferedReader fileReader = |
| 78 | + new BufferedReader(new InputStreamReader(fileURL.openStream()))) { |
| 79 | + tmpMap.put(nameFromPath(line), fileReader.lines().toArray(String[]::new)); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + exampleMap = tmpMap; |
| 84 | + } catch (Exception e) { |
| 85 | + // Ignore |
| 86 | + exampleMap = null; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (exampleMap == null) { |
50 | 91 | exampleMap = Collections.emptyMap();
|
51 | 92 | }
|
52 | 93 | }
|
53 | 94 | return exampleMap;
|
54 | 95 | }
|
55 | 96 |
|
| 97 | + private static String nameFromPath(final String filename) { |
| 98 | + // Process the file name to create a readable name |
| 99 | + String name = filename; |
| 100 | + name = name.substring(0, name.lastIndexOf('.')); |
| 101 | + name = name.replace('_', ' '); |
| 102 | + name = name.substring(0, 1).toUpperCase() + name.substring(1); |
| 103 | + return name; |
| 104 | + } |
| 105 | + |
56 | 106 | public static List<String> getExampleNames() {
|
57 | 107 | return getExampleMap().keySet().stream().sorted().collect(Collectors.toList());
|
58 | 108 | }
|
59 | 109 |
|
60 | 110 | public static String[] getExample(String name) {
|
61 |
| - Path path = getExampleMap().get(name); |
62 |
| - if (path == null) { |
63 |
| - return new String[0]; |
64 |
| - } |
65 |
| - try (BufferedReader reader = new BufferedReader(new FileReader(path.toFile()))) { |
66 |
| - String[] lines = |
67 |
| - reader |
68 |
| - .lines() |
69 |
| - .map(s -> s.split("(//|#|%)", 2)[0]) |
70 |
| - .map(s -> s.replace(" ", "")) |
71 |
| - .toArray(String[]::new); |
72 |
| - // verify that each line contains only 0s and 1s, and is 8 characters long |
73 |
| - for (int i = 0; i < lines.length; i++) { |
74 |
| - String line = lines[i]; |
75 |
| - if (line.length() != 8 || !line.matches("[01]+")) { |
76 |
| - throw new IllegalArgumentException( |
77 |
| - String.format("Invalid file format at line %d: '%s'", (i + 1), line)); |
78 |
| - } |
| 111 | + String[] lines = |
| 112 | + Arrays.stream(getExampleMap().get(name)) |
| 113 | + .map(s -> s.split("(//|#|%)", 2)[0]) |
| 114 | + .map(s -> s.replace(" ", "")) |
| 115 | + .toArray(String[]::new); |
| 116 | + // verify that each line contains only 0s and 1s, and is 8 characters long |
| 117 | + for (int i = 0; i < lines.length; i++) { |
| 118 | + String line = lines[i]; |
| 119 | + if (line.length() != 8 || !line.matches("[01]+")) { |
| 120 | + throw new IllegalArgumentException( |
| 121 | + String.format("Invalid file format at line %d: '%s'", (i + 1), line)); |
79 | 122 | }
|
80 |
| - return lines; |
81 |
| - } catch (Exception e) { |
82 |
| - return new String[0]; |
83 | 123 | }
|
| 124 | + return lines; |
84 | 125 | }
|
85 | 126 | }
|
0 commit comments