package org.expeditee.gui.management; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import org.expeditee.gui.Frame; import org.expeditee.gui.FrameIO; public class ResourceUtil { private static final char PATTERN_START_TWO = '{'; private static final char PATTERN_START_ONE = '$'; private static final char PATTERN_END = '}'; private static final char ESCAPE = '\\'; public static final String CURRENT_FRAMESET_FLAG = "${CURRENT_FRAMESET}"; public static final String CURRENT_USER = "${CURRENT_USER}"; /** * Attempts to relativise a image path using expeditee.home (FrameIO.PARENT_FOLDER). * @param path The path to attempt to relativise * @return Returns the most relativised path if one exists, otherwise returns a copy of the ref parameter passed in. */ public static Path relativiseImagePath(Path path) { Path expediteeHome = Paths.get(FrameIO.PARENT_FOLDER); if (path.startsWith(expediteeHome)) { return expediteeHome.relativize(path); } else { return path; } } public static Path resolveImagePath(Path path, Frame context) { List directories = ResourceManager.images.getDirectories(context); directories.add(FrameIO.PARENT_FOLDER); for (String dir: directories) { Path canditate = Paths.get(dir).resolve(path); if (canditate.toFile().exists()) { return canditate; } } return null; } /** * Utility function that transforms a input string to a output string. * Any occurances of 'pattern' are replaced with 'replacement'. * \ is used as the escape key. * @param input The raw input * @param pattern The pattern to replace. * This will be prepended by in ${ and appended by } in the input string. * @param replacement The replacement content to use. * @return The transformed string. */ protected static String substitute(String input, String pattern, String replacement) { StringBuilder outputSB = new StringBuilder(); StringBuilder inputSB = new StringBuilder(input.replace("\\", "\\\\") + ResourceUtil.ESCAPE); StringBuilder patternSB = new StringBuilder(); boolean inPattern = false; boolean patternStartPossible = true; int index = 0; int length = inputSB.length() - 1; while (index < length) { char c = inputSB.charAt(index); boolean startingPattern = patternStartPossible && c == ResourceUtil.PATTERN_START_TWO && outputSB.charAt(outputSB.length() - 1) == ResourceUtil.PATTERN_START_ONE; boolean endingPattern = c == ResourceUtil.PATTERN_END; if (c == ResourceUtil.ESCAPE && !inPattern) { index++; c = inputSB.charAt(index); outputSB.append(c); if (c == PATTERN_START_ONE) { patternStartPossible = false; } } else if (inPattern && endingPattern) { String patternFound = patternSB.toString(); if (("${" + patternFound + "}").equals(pattern)) { outputSB.append(replacement); } else { outputSB.append("" + ResourceUtil.PATTERN_START_ONE + ResourceUtil.PATTERN_START_TWO); outputSB.append(patternFound); outputSB.append(ResourceUtil.PATTERN_END); } inPattern = false; patternSB = new StringBuilder(); } else if (!inPattern && startingPattern) { outputSB.deleteCharAt(outputSB.length() - 1); inPattern = true; } else if (inPattern) { patternSB.append(c); } else { outputSB.append(c); patternStartPossible = true; } index++; } if (patternSB.length() > 0) { outputSB.append("${"); } outputSB.append(patternSB); return outputSB.toString(); } }