1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-30 19:52:13 +01:00

Added StringReplacer.checkIfRequiredKeyIsMissingOrExcept helper method

This commit is contained in:
Cristian Maglie 2018-11-09 16:12:08 +01:00 committed by Cristian Maglie
parent 3f79d129bb
commit 2397e1e8c2

View File

@ -24,9 +24,50 @@ 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;