forked from arduino/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStringReplacer.java
142 lines (120 loc) · 4.46 KB
/
StringReplacer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
StringReplacer - Utility class for expression formatting
Part of the Arduino project - http://www.arduino.cc/
Copyright (c) 2011 Cristian Maglie
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.app.helpers;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class StringReplacer {
public static void checkIfRequiredKeyIsMissingOrExcept(String key, String src, PreferencesMap inDict) throws PreferencesMapException {
// If the key is not missing -> everything is OK
String checkedValue = inDict.get(key);
if (checkedValue != null && !checkedValue.isEmpty())
return;
PreferencesMap dict = new PreferencesMap(inDict);
// Find a random tag that is not contained in the dictionary and the src pattern
String tag;
while (true) {
tag = UUID.randomUUID().toString();
if (src.contains(tag))
continue;
if (dict.values().contains(tag))
continue;
if (dict.keySet().contains(tag))
continue;
break;
}
// Inject tag inside the dictionary
dict.put(key, tag);
// Recursive replace with a max depth of 10 levels.
String res;
for (int i = 0; i < 10; i++) {
// Do a replace with dictionary
res = StringReplacer.replaceFromMapping(src, dict);
if (res.equals(src))
break;
src = res;
}
// If the resulting string contains the tag, then the key is required
if (src.contains(tag)) {
throw new PreferencesMapException(key);
}
}
public static String[] formatAndSplit(String src, Map<String, String> dict) throws Exception {
String res;
// Recursive replace with a max depth of 10 levels.
for (int i = 0; i < 10; i++) {
// Do a replace with dictionary
res = StringReplacer.replaceFromMapping(src, dict);
if (res.equals(src))
break;
src = res;
}
// Split the resulting string in arguments
return quotedSplit(src, "\"'", false);
}
public static String[] quotedSplit(String src, String quoteChars,
boolean acceptEmptyArguments)
throws Exception {
List<String> res = new ArrayList<>();
String escapedArg = null;
String escapingChar = null;
for (String i : src.split(" ")) {
if (escapingChar == null) {
// If the first char is not an escape char..
String first = null;
if (i.length() > 0)
first = i.substring(0, 1);
if (first == null || !quoteChars.contains(first)) {
if (i.trim().length() != 0 || acceptEmptyArguments)
res.add(i);
continue;
}
escapingChar = first;
i = i.substring(1);
escapedArg = "";
}
if (!i.endsWith(escapingChar)) {
escapedArg += i + " ";
continue;
}
escapedArg += i.substring(0, i.length() - 1);
if (escapedArg.trim().length() != 0 || acceptEmptyArguments)
res.add(escapedArg);
escapingChar = null;
}
if (escapingChar != null)
throw new Exception("Invalid quoting: no closing [" + escapingChar +
"] char found.");
return res.toArray(new String[0]);
}
public static String replaceFromMapping(String src, Map<String, String> map) {
return replaceFromMapping(src, map, "{", "}");
}
public static String replaceFromMapping(String src, Map<String, String> map,
String leftDelimiter,
String rightDelimiter) {
for (Map.Entry<String, String> entry : map.entrySet()) {
String keyword = leftDelimiter + entry.getKey() + rightDelimiter;
if (entry.getValue() != null && keyword != null) {
src = src.replace(keyword, entry.getValue());
}
}
return src;
}
}