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

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

Fixed a bug causing ${CURRENT_FRAMESET} flag not to work.

File size: 2.6 KB
Line 
1package org.expeditee.gui.management;
2
3public class ResourceUtil {
4
5 private static final char PATTERN_START_TWO = '{';
6 private static final char PATTERN_START_ONE = '$';
7 private static final char PATTERN_END = '}';
8 private static final char ESCAPE = '\\';
9
10 public static final String CURRENT_FRAMESET_FLAG = "${CURRENT_FRAMESET}";
11 public static final String CURRENT_USER = "${CURRENT_USER}";
12
13 /**
14 * Utility function that transforms a input string to a output string.
15 * Any occurances of 'pattern' are replaced with 'replacement'.
16 * \ is used as the escape key.
17 * @param input The raw input
18 * @param pattern The pattern to replace.
19 * This will be prepended by in ${ and appended by } in the input string.
20 * @param replacement The replacement content to use.
21 * @return The transformed string.
22 */
23 protected static String substitute(String input, String pattern, String replacement) {
24 StringBuilder outputSB = new StringBuilder();
25 StringBuilder inputSB = new StringBuilder(input.replace("\\", "\\\\") + ResourceUtil.ESCAPE);
26 StringBuilder patternSB = new StringBuilder();
27 boolean inPattern = false;
28 boolean patternStartPossible = true;
29
30 int index = 0;
31 int length = inputSB.length() - 1;
32 while (index < length) {
33 char c = inputSB.charAt(index);
34 boolean startingPattern = patternStartPossible &&
35 c == ResourceUtil.PATTERN_START_TWO &&
36 outputSB.charAt(outputSB.length() - 1) == ResourceUtil.PATTERN_START_ONE;
37 boolean endingPattern = c == ResourceUtil.PATTERN_END;
38
39 if (c == ResourceUtil.ESCAPE && !inPattern) {
40 index++;
41 c = inputSB.charAt(index);
42 outputSB.append(c);
43 if (c == PATTERN_START_ONE) {
44 patternStartPossible = false;
45 }
46 } else if (inPattern && endingPattern) {
47 String patternFound = patternSB.toString();
48 if (("${" + patternFound + "}").equals(pattern)) {
49 outputSB.append(replacement);
50 } else {
51 outputSB.append("" + ResourceUtil.PATTERN_START_ONE + ResourceUtil.PATTERN_START_TWO);
52 outputSB.append(patternFound);
53 outputSB.append(ResourceUtil.PATTERN_END);
54 }
55 inPattern = false;
56 patternSB = new StringBuilder();
57 } else if (!inPattern && startingPattern) {
58 outputSB.deleteCharAt(outputSB.length() - 1);
59 inPattern = true;
60 } else if (inPattern) {
61 patternSB.append(c);
62 } else {
63 outputSB.append(c);
64 patternStartPossible = true;
65 }
66
67 index++;
68 }
69
70 if (patternSB.length() > 0) {
71 outputSB.append("${");
72 }
73 outputSB.append(patternSB);
74 return outputSB.toString();
75 }
76}
Note: See TracBrowser for help on using the repository browser.