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