| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package processing.app.helpers; |
| 23 |
|
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.List; |
| 26 |
import java.util.Map; |
| 27 |
import java.util.UUID; |
| 28 |
|
| 29 |
public class StringReplacer { |
| 30 |
|
| 31 |
public static void checkIfRequiredKeyIsMissingOrExcept(String key, String src, PreferencesMap inDict) throws PreferencesMapException { |
| 32 |
|
| 33 |
String checkedValue = inDict.get(key); |
| 34 |
if (checkedValue != null && !checkedValue.isEmpty()) |
| 35 |
return; |
| 36 |
|
| 37 |
PreferencesMap dict = new PreferencesMap(inDict); |
| 38 |
|
| 39 |
|
| 40 |
String tag; |
| 41 |
while (true) { |
| 42 |
tag = UUID.randomUUID().toString(); |
| 43 |
if (src.contains(tag)) |
| 44 |
continue; |
| 45 |
if (dict.values().contains(tag)) |
| 46 |
continue; |
| 47 |
if (dict.keySet().contains(tag)) |
| 48 |
continue; |
| 49 |
break; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
dict.put(key, tag); |
| 54 |
|
| 55 |
|
| 56 |
String res; |
| 57 |
for (int i = 0; i < 10; i++) { |
| 58 |
|
| 59 |
res = StringReplacer.replaceFromMapping(src, dict); |
| 60 |
if (res.equals(src)) |
| 61 |
break; |
| 62 |
src = res; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
if (src.contains(tag)) { |
| 67 |
throw new PreferencesMapException(key); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public static String[] formatAndSplit(String src, Map<String, String> dict) throws Exception { |
| 72 |
String res; |
| 73 |
|
| 74 |
|
| 75 |
for (int i = 0; i < 10; i++) { |
| 76 |
|
| 77 |
res = StringReplacer.replaceFromMapping(src, dict); |
| 78 |
if (res.equals(src)) |
| 79 |
break; |
| 80 |
src = res; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
return quotedSplit(src, "\"'", false); |
| 85 |
} |
| 86 |
|
| 87 |
public static String[] quotedSplit(String src, String quoteChars, |
| 88 |
boolean acceptEmptyArguments) |
| 89 |
throws Exception { |
| 90 |
List<String> res = new ArrayList<>(); |
| 91 |
String escapedArg = null; |
| 92 |
String escapingChar = null; |
| 93 |
for (String i : src.split(" ")) { |
| 94 |
if (escapingChar == null) { |
| 95 |
|
| 96 |
String first = null; |
| 97 |
if (i.length() > 0) |
| 98 |
first = i.substring(0, 1); |
| 99 |
if (first == null || !quoteChars.contains(first)) { |
| 100 |
if (i.trim().length() != 0 || acceptEmptyArguments) |
| 101 |
res.add(i); |
| 102 |
continue; |
| 103 |
} |
| 104 |
|
| 105 |
escapingChar = first; |
| 106 |
i = i.substring(1); |
| 107 |
escapedArg = ""; |
| 108 |
} |
| 109 |
|
| 110 |
if (!i.endsWith(escapingChar)) { |
| 111 |
escapedArg += i + " "; |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
escapedArg += i.substring(0, i.length() - 1); |
| 116 |
if (escapedArg.trim().length() != 0 || acceptEmptyArguments) |
| 117 |
res.add(escapedArg); |
| 118 |
escapingChar = null; |
| 119 |
} |
| 120 |
if (escapingChar != null) |
| 121 |
throw new Exception("Invalid quoting: no closing [" + escapingChar + |
| 122 |
"] char found."); |
| 123 |
return res.toArray(new String[0]); |
| 124 |
} |
| 125 |
|
| 126 |
public static String replaceFromMapping(String src, Map<String, String> map) { |
| 127 |
return replaceFromMapping(src, map, "{", "}"); |
| 128 |
} |
| 129 |
|
| 130 |
public static String replaceFromMapping(String src, Map<String, String> map, |
| 131 |
String leftDelimiter, |
| 132 |
String rightDelimiter) { |
| 133 |
for (Map.Entry<String, String> entry : map.entrySet()) { |
| 134 |
String keyword = leftDelimiter + entry.getKey() + rightDelimiter; |
| 135 |
if (entry.getValue() != null && keyword != null) { |
| 136 |
src = src.replace(keyword, entry.getValue()); |
| 137 |
} |
| 138 |
} |
| 139 |
return src; |
| 140 |
} |
| 141 |
|
| 142 |
} |