Debian Codegraph @

arduino-core/src/processing/app/helpers/StringReplacer.java

raw symbols SCIP
1 /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3 StringReplacer - Utility class for expression formatting
4 Part of the Arduino project - http://www.arduino.cc/
5
6 Copyright (c) 2011 Cristian Maglie
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 // If the key is not missing -> everything is OK
33 String checkedValue = inDict.get(key);
34 if (checkedValue != null && !checkedValue.isEmpty())
35 return;
36
37 PreferencesMap dict = new PreferencesMap(inDict);
38
39 // Find a random tag that is not contained in the dictionary and the src pattern
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 // Inject tag inside the dictionary
53 dict.put(key, tag);
54
55 // Recursive replace with a max depth of 10 levels.
56 String res;
57 for (int i = 0; i < 10; i++) {
58 // Do a replace with dictionary
59 res = StringReplacer.replaceFromMapping(src, dict);
60 if (res.equals(src))
61 break;
62 src = res;
63 }
64
65 // If the resulting string contains the tag, then the key is required
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 // Recursive replace with a max depth of 10 levels.
75 for (int i = 0; i < 10; i++) {
76 // Do a replace with dictionary
77 res = StringReplacer.replaceFromMapping(src, dict);
78 if (res.equals(src))
79 break;
80 src = res;
81 }
82
83 // Split the resulting string in arguments
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 // If the first char is not an escape char..
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 }