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

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

org.expeditee.gui.managment.ResourceUtil.newImageWithName(Image, String) now handles the generation of new images when copy/pasting images that do not have a file (for example, from a web page or paint program)

File size: 4.2 KB
Line 
1package org.expeditee.gui.management;
2
3import java.io.File;
4import java.io.IOException;
5import java.nio.file.Path;
6import java.nio.file.Paths;
7import java.util.List;
8
9import org.expeditee.core.Image;
10import org.expeditee.gui.DisplayController;
11import org.expeditee.gui.Frame;
12import org.expeditee.gui.FrameIO;
13import org.expeditee.items.Text;
14
15public 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 /**
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
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
62 /**
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();
97 if (("${" + patternFound + "}").equals(pattern)) {
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}
Note: See TracBrowser for help on using the repository browser.