1 | package org.expeditee.gui.management;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.nio.file.Path;
|
---|
6 | import java.nio.file.Paths;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import org.expeditee.core.Image;
|
---|
10 | import org.expeditee.gui.DisplayController;
|
---|
11 | import org.expeditee.gui.Frame;
|
---|
12 | import org.expeditee.gui.FrameIO;
|
---|
13 | import org.expeditee.items.Text;
|
---|
14 |
|
---|
15 | public class ResourceUtil {
|
---|
16 |
|
---|
17 | private static final char PATTERN_START_TWO = '{';
|
---|
18 | private static final char PATTERN_START_ONE = '$';
|
---|
19 | private static final char PATTERN_END = '}';
|
---|
20 | private static final char ESCAPE = '\\';
|
---|
21 |
|
---|
22 | public static final String CURRENT_FRAMESET_FLAG = "${CURRENT_FRAMESET}";
|
---|
23 | public static final String CURRENT_USER = "${CURRENT_USER}";
|
---|
24 | public static final String EXPEDITEE_HOME = "${EXPEDITEE_HOME}";
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Attempts to relativise a image path using expeditee.home (FrameIO.PARENT_FOLDER).
|
---|
28 | * @param path The path to attempt to relativise
|
---|
29 | * @return Returns the most relativised path if one exists, otherwise returns the ref parameter passed in.
|
---|
30 | */
|
---|
31 | public static Path relativiseImagePath(Path path) {
|
---|
32 | Path expediteeHome = Paths.get(FrameIO.PARENT_FOLDER);
|
---|
33 | if (path.startsWith(expediteeHome)) {
|
---|
34 | return expediteeHome.relativize(path);
|
---|
35 | } else {
|
---|
36 | return path;
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public static Path resolveImagePath(Path path, Frame context) {
|
---|
41 | List<String> directories = ResourceManager.images.getDirectories(context);
|
---|
42 | directories.add(FrameIO.PARENT_FOLDER);
|
---|
43 |
|
---|
44 | for (String dir: directories) {
|
---|
45 | Path canditate = Paths.get(dir).resolve(path);
|
---|
46 | if (canditate.toFile().exists()) {
|
---|
47 | return canditate;
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | return null;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public static Path getImageSaveFileLocation(Frame context) {
|
---|
55 | List<String> directories = ResourceManager.images.getDirectories(context);
|
---|
56 | if (directories.isEmpty()) {
|
---|
57 | return Paths.get(FrameIO.PARENT_FOLDER).toAbsolutePath();
|
---|
58 | } else {
|
---|
59 | return Paths.get(directories.get(0));
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public static Text newImageWithName(Image img, String name) throws IOException {
|
---|
64 | ResourceManager.images.getDirectories();// TODO: save this image out to the first directory in list instead of to IMAGES_PATH
|
---|
65 | File out = new File(FrameIO.IMAGES_PATH + name);
|
---|
66 | out.mkdirs();
|
---|
67 | img.writeToDisk("png", out);
|
---|
68 | Text item = DisplayController.getCurrentFrame().createNewText("@i: " + out.getName());
|
---|
69 | return item;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static Path resolveResourcePath(String unresolved, Frame context) {
|
---|
73 | Path expediteeHome = Paths.get(FrameIO.PARENT_FOLDER).toAbsolutePath();
|
---|
74 | Path absolutePath = Paths.get(context.getFramesetPath()).toAbsolutePath();
|
---|
75 | Path p = expediteeHome.relativize(absolutePath);
|
---|
76 | String resolved = ResourceUtil.substitute(unresolved, CURRENT_FRAMESET_FLAG, p.toString());
|
---|
77 | resolved = ResourceUtil.substitute(resolved, EXPEDITEE_HOME, expediteeHome.toString());
|
---|
78 | return Paths.get(resolved).toAbsolutePath();
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Utility function that transforms a input string to a output string.
|
---|
83 | * Any occurances of 'pattern' are replaced with 'replacement'.
|
---|
84 | * \ is used as the escape key.
|
---|
85 | * @param input The raw input
|
---|
86 | * @param pattern The pattern to replace.
|
---|
87 | * This will be prepended by in ${ and appended by } in the input string.
|
---|
88 | * @param replacement The replacement content to use.
|
---|
89 | * @return The transformed string.
|
---|
90 | */
|
---|
91 | protected static String substitute(String input, String pattern, String replacement) {
|
---|
92 | StringBuilder outputSB = new StringBuilder();
|
---|
93 | StringBuilder inputSB = new StringBuilder(input.replace("\\", "\\\\") + ResourceUtil.ESCAPE);
|
---|
94 | StringBuilder patternSB = new StringBuilder();
|
---|
95 | boolean inPattern = false;
|
---|
96 | boolean patternStartPossible = true;
|
---|
97 |
|
---|
98 | int index = 0;
|
---|
99 | int length = inputSB.length() - 1;
|
---|
100 | while (index < length) {
|
---|
101 | char c = inputSB.charAt(index);
|
---|
102 | boolean startingPattern = patternStartPossible &&
|
---|
103 | c == ResourceUtil.PATTERN_START_TWO &&
|
---|
104 | outputSB.charAt(outputSB.length() - 1) == ResourceUtil.PATTERN_START_ONE;
|
---|
105 | boolean endingPattern = c == ResourceUtil.PATTERN_END;
|
---|
106 |
|
---|
107 | if (c == ResourceUtil.ESCAPE && !inPattern) {
|
---|
108 | index++;
|
---|
109 | c = inputSB.charAt(index);
|
---|
110 | outputSB.append(c);
|
---|
111 | if (c == PATTERN_START_ONE) {
|
---|
112 | patternStartPossible = false;
|
---|
113 | }
|
---|
114 | } else if (inPattern && endingPattern) {
|
---|
115 | String patternFound = patternSB.toString();
|
---|
116 | if (("${" + patternFound + "}").equals(pattern)) {
|
---|
117 | outputSB.append(replacement);
|
---|
118 | } else {
|
---|
119 | outputSB.append("" + ResourceUtil.PATTERN_START_ONE + ResourceUtil.PATTERN_START_TWO);
|
---|
120 | outputSB.append(patternFound);
|
---|
121 | outputSB.append(ResourceUtil.PATTERN_END);
|
---|
122 | }
|
---|
123 | inPattern = false;
|
---|
124 | patternSB = new StringBuilder();
|
---|
125 | } else if (!inPattern && startingPattern) {
|
---|
126 | outputSB.deleteCharAt(outputSB.length() - 1);
|
---|
127 | inPattern = true;
|
---|
128 | } else if (inPattern) {
|
---|
129 | patternSB.append(c);
|
---|
130 | } else {
|
---|
131 | outputSB.append(c);
|
---|
132 | patternStartPossible = true;
|
---|
133 | }
|
---|
134 |
|
---|
135 | index++;
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (patternSB.length() > 0) {
|
---|
139 | outputSB.append("${");
|
---|
140 | }
|
---|
141 | outputSB.append(patternSB);
|
---|
142 | return outputSB.toString();
|
---|
143 | }
|
---|
144 | }
|
---|