2012-01-02 16:45:01 +01:00
|
|
|
/* -*- 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
|
|
|
|
*/
|
2011-12-31 14:32:48 +01:00
|
|
|
package processing.app.helpers;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
public class StringReplacer {
|
|
|
|
|
2012-01-13 14:10:34 +01:00
|
|
|
public static String[] formatAndSplit(String src, Map<String, String> dict,
|
|
|
|
boolean recursive) throws Exception {
|
|
|
|
String res;
|
|
|
|
|
|
|
|
// Recursive replace with a max depth of 10 levels.
|
2013-07-17 14:36:20 +02:00
|
|
|
for (int i = 0; i < 10; i++) {
|
2012-01-13 14:10:34 +01:00
|
|
|
// Do a replace with dictionary
|
|
|
|
res = StringReplacer.replaceFromMapping(src, dict);
|
|
|
|
if (!recursive)
|
|
|
|
break;
|
|
|
|
if (res.equals(src))
|
|
|
|
break;
|
|
|
|
src = res;
|
|
|
|
}
|
2011-12-31 14:32:48 +01:00
|
|
|
|
|
|
|
// Split the resulting string in arguments
|
2013-07-17 14:36:20 +02:00
|
|
|
return quotedSplit(src, "\"'", false);
|
2011-12-31 14:32:48 +01:00
|
|
|
}
|
|
|
|
|
2013-07-17 14:36:20 +02:00
|
|
|
public static String[] quotedSplit(String src, String quoteChars,
|
2012-01-13 14:10:34 +01:00
|
|
|
boolean acceptEmptyArguments)
|
|
|
|
throws Exception {
|
2016-09-29 19:32:30 +02:00
|
|
|
List<String> res = new ArrayList<>();
|
2011-12-31 14:32:48 +01:00
|
|
|
String escapedArg = null;
|
2013-07-17 14:36:20 +02:00
|
|
|
String escapingChar = null;
|
2011-12-31 14:32:48 +01:00
|
|
|
for (String i : src.split(" ")) {
|
2013-07-17 14:36:20 +02:00
|
|
|
if (escapingChar == null) {
|
|
|
|
// If the first char is not an escape char..
|
2013-07-19 16:08:36 +02:00
|
|
|
String first = null;
|
|
|
|
if (i.length() > 0)
|
|
|
|
first = i.substring(0, 1);
|
|
|
|
if (first == null || !quoteChars.contains(first)) {
|
2012-06-25 15:30:43 +02:00
|
|
|
if (i.trim().length() != 0 || acceptEmptyArguments)
|
2011-12-31 14:32:48 +01:00
|
|
|
res.add(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-07-17 14:36:20 +02:00
|
|
|
escapingChar = first;
|
2011-12-31 14:32:48 +01:00
|
|
|
i = i.substring(1);
|
|
|
|
escapedArg = "";
|
|
|
|
}
|
|
|
|
|
2013-07-17 14:36:20 +02:00
|
|
|
if (!i.endsWith(escapingChar)) {
|
2011-12-31 14:32:48 +01:00
|
|
|
escapedArg += i + " ";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
escapedArg += i.substring(0, i.length() - 1);
|
2012-06-25 15:30:43 +02:00
|
|
|
if (escapedArg.trim().length() != 0 || acceptEmptyArguments)
|
2011-12-31 14:32:48 +01:00
|
|
|
res.add(escapedArg);
|
2013-07-17 14:36:20 +02:00
|
|
|
escapingChar = null;
|
2011-12-31 14:32:48 +01:00
|
|
|
}
|
2013-07-17 14:36:20 +02:00
|
|
|
if (escapingChar != null)
|
|
|
|
throw new Exception("Invalid quoting: no closing [" + escapingChar +
|
|
|
|
"] char found.");
|
2011-12-31 14:32:48 +01:00
|
|
|
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,
|
2012-01-13 14:10:34 +01:00
|
|
|
String leftDelimiter,
|
|
|
|
String rightDelimiter) {
|
2015-06-18 12:22:55 +02:00
|
|
|
for (Map.Entry<String, String> entry : map.entrySet()) {
|
|
|
|
String keyword = leftDelimiter + entry.getKey() + rightDelimiter;
|
2016-01-25 18:29:25 +01:00
|
|
|
if (entry.getValue() != null && keyword != null) {
|
|
|
|
src = src.replace(keyword, entry.getValue());
|
|
|
|
}
|
2011-12-31 14:32:48 +01:00
|
|
|
}
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|