source: trunk/src/org/expeditee/gui/management/ResourceUtil.java@ 1441

Last change on this file since 1441 was 1441, checked in by bnemhaus, 5 years ago

Copying images in Expeditee now duplicates the associated file on the filesystem.
Also changed USER_NAME back to USER.NAME on David's direction.

File size: 3.6 KB
Line 
1package org.expeditee.gui.management;
2
3import java.nio.file.Path;
4import java.nio.file.Paths;
5import java.util.List;
6
7import org.expeditee.gui.Frame;
8import org.expeditee.gui.FrameIO;
9
10public class ResourceUtil {
11
12 private static final char PATTERN_START_TWO = '{';
13 private static final char PATTERN_START_ONE = '$';
14 private static final char PATTERN_END = '}';
15 private static final char ESCAPE = '\\';
16
17 public static final String CURRENT_FRAMESET_FLAG = "${CURRENT_FRAMESET}";
18 public static final String CURRENT_USER = "${CURRENT_USER}";
19
20 /**
21 * Attempts to relativise a image path using expeditee.home (FrameIO.PARENT_FOLDER).
22 * @param path The path to attempt to relativise
23 * @return Returns the most relativised path if one exists, otherwise returns a copy of the ref parameter passed in.
24 */
25 public static Path relativiseImagePath(Path path) {
26 Path expediteeHome = Paths.get(FrameIO.PARENT_FOLDER);
27 if (path.startsWith(expediteeHome)) {
28 return expediteeHome.relativize(path);
29 } else {
30 return path;
31 }
32 }
33
34 public static Path resolveImagePath(Path path, Frame context) {
35 List<String> directories = ResourceManager.images.getDirectories(context);
36 directories.add(FrameIO.PARENT_FOLDER);
37
38 for (String dir: directories) {
39 Path canditate = Paths.get(dir).resolve(path);
40 if (canditate.toFile().exists()) {
41 return canditate;
42 }
43 }
44
45 return null;
46 }
47
48 /**
49 * Utility function that transforms a input string to a output string.
50 * Any occurances of 'pattern' are replaced with 'replacement'.
51 * \ is used as the escape key.
52 * @param input The raw input
53 * @param pattern The pattern to replace.
54 * This will be prepended by in ${ and appended by } in the input string.
55 * @param replacement The replacement content to use.
56 * @return The transformed string.
57 */
58 protected static String substitute(String input, String pattern, String replacement) {
59 StringBuilder outputSB = new StringBuilder();
60 StringBuilder inputSB = new StringBuilder(input.replace("\\", "\\\\") + ResourceUtil.ESCAPE);
61 StringBuilder patternSB = new StringBuilder();
62 boolean inPattern = false;
63 boolean patternStartPossible = true;
64
65 int index = 0;
66 int length = inputSB.length() - 1;
67 while (index < length) {
68 char c = inputSB.charAt(index);
69 boolean startingPattern = patternStartPossible &&
70 c == ResourceUtil.PATTERN_START_TWO &&
71 outputSB.charAt(outputSB.length() - 1) == ResourceUtil.PATTERN_START_ONE;
72 boolean endingPattern = c == ResourceUtil.PATTERN_END;
73
74 if (c == ResourceUtil.ESCAPE && !inPattern) {
75 index++;
76 c = inputSB.charAt(index);
77 outputSB.append(c);
78 if (c == PATTERN_START_ONE) {
79 patternStartPossible = false;
80 }
81 } else if (inPattern && endingPattern) {
82 String patternFound = patternSB.toString();
83 if (("${" + patternFound + "}").equals(pattern)) {
84 outputSB.append(replacement);
85 } else {
86 outputSB.append("" + ResourceUtil.PATTERN_START_ONE + ResourceUtil.PATTERN_START_TWO);
87 outputSB.append(patternFound);
88 outputSB.append(ResourceUtil.PATTERN_END);
89 }
90 inPattern = false;
91 patternSB = new StringBuilder();
92 } else if (!inPattern && startingPattern) {
93 outputSB.deleteCharAt(outputSB.length() - 1);
94 inPattern = true;
95 } else if (inPattern) {
96 patternSB.append(c);
97 } else {
98 outputSB.append(c);
99 patternStartPossible = true;
100 }
101
102 index++;
103 }
104
105 if (patternSB.length() > 0) {
106 outputSB.append("${");
107 }
108 outputSB.append(patternSB);
109 return outputSB.toString();
110 }
111}
Note: See TracBrowser for help on using the repository browser.