1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.exec.util;
19
20 import java.io.File;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Objects;
25 import java.util.StringTokenizer;
26
27
28
29
30
31
32 public class StringUtils {
33
34 private static final String[] EMPTY_STRING_ARRAY = {};
35 private static final String SINGLE_QUOTE = "\'";
36 private static final String DOUBLE_QUOTE = "\"";
37 private static final char SLASH_CHAR = '/';
38 private static final char BACKSLASH_CHAR = '\\';
39
40
41
42
43
44
45
46
47
48
49
50 public static String fixFileSeparatorChar(final String arg) {
51 return arg.replace(SLASH_CHAR, File.separatorChar).replace(BACKSLASH_CHAR, File.separatorChar);
52 }
53
54
55
56
57
58
59
60 public static boolean isQuoted(final String argument) {
61 return argument.startsWith(SINGLE_QUOTE) && argument.endsWith(SINGLE_QUOTE) || argument.startsWith(DOUBLE_QUOTE) && argument.endsWith(DOUBLE_QUOTE);
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75 public static String quoteArgument(final String argument) {
76
77 String cleanedArgument = argument.trim();
78
79
80 while (cleanedArgument.startsWith(SINGLE_QUOTE) || cleanedArgument.startsWith(DOUBLE_QUOTE)) {
81 cleanedArgument = cleanedArgument.substring(1);
82 }
83
84 while (cleanedArgument.endsWith(SINGLE_QUOTE) || cleanedArgument.endsWith(DOUBLE_QUOTE)) {
85 cleanedArgument = cleanedArgument.substring(0, cleanedArgument.length() - 1);
86 }
87
88 final StringBuilder buf = new StringBuilder();
89 if (cleanedArgument.indexOf(DOUBLE_QUOTE) > -1) {
90 if (cleanedArgument.indexOf(SINGLE_QUOTE) > -1) {
91 throw new IllegalArgumentException("Can't handle single and double quotes in same argument");
92 }
93 return buf.append(SINGLE_QUOTE).append(cleanedArgument).append(SINGLE_QUOTE).toString();
94 }
95 if (cleanedArgument.indexOf(SINGLE_QUOTE) > -1 || cleanedArgument.indexOf(" ") > -1) {
96 return buf.append(DOUBLE_QUOTE).append(cleanedArgument).append(DOUBLE_QUOTE).toString();
97 }
98 return cleanedArgument;
99 }
100
101
102
103
104
105
106
107
108 public static String[] split(final String input, final String splitChar) {
109 final StringTokenizer tokens = new StringTokenizer(input, splitChar);
110 final List<String> strList = new ArrayList<>();
111 while (tokens.hasMoreTokens()) {
112 strList.add(tokens.nextToken());
113 }
114 return strList.toArray(EMPTY_STRING_ARRAY);
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 public static StringBuffer stringSubstitution(final String argStr, final Map<? super String, ?> vars, final boolean isLenient) {
141
142 final StringBuffer argBuf = new StringBuffer();
143
144 if (argStr == null || argStr.isEmpty()) {
145 return argBuf;
146 }
147
148 if (vars == null || vars.isEmpty()) {
149 return argBuf.append(argStr);
150 }
151
152 final int argStrLength = argStr.length();
153
154 for (int cIdx = 0; cIdx < argStrLength;) {
155
156 char ch = argStr.charAt(cIdx);
157 char del = ' ';
158
159 switch (ch) {
160
161 case '$':
162 final StringBuilder nameBuf = new StringBuilder();
163 del = argStr.charAt(cIdx + 1);
164 if (del == '{') {
165 cIdx++;
166
167 for (++cIdx; cIdx < argStr.length(); ++cIdx) {
168 ch = argStr.charAt(cIdx);
169 if (ch != '_' && ch != '.' && ch != '-' && ch != '+' && !Character.isLetterOrDigit(ch)) {
170 break;
171 }
172 nameBuf.append(ch);
173 }
174
175 if (nameBuf.length() >= 0) {
176
177 String value;
178 final Object temp = vars.get(nameBuf.toString());
179
180 if (temp instanceof File) {
181
182
183 value = fixFileSeparatorChar(((File) temp).getAbsolutePath());
184 } else {
185 value = Objects.toString(temp, null);
186 }
187
188 if (value != null) {
189 argBuf.append(value);
190 } else {
191 if (!isLenient) {
192
193 throw new IllegalArgumentException("No value found for : " + nameBuf);
194 }
195
196 argBuf.append("${").append(nameBuf.toString()).append("}");
197 }
198
199 del = argStr.charAt(cIdx);
200
201 if (del != '}') {
202 throw new IllegalArgumentException("Delimiter not found for : " + nameBuf);
203 }
204 }
205 } else {
206 argBuf.append(ch);
207 }
208 cIdx++;
209
210 break;
211
212 default:
213 argBuf.append(ch);
214 ++cIdx;
215 break;
216 }
217 }
218
219 return argBuf;
220 }
221
222
223
224
225
226
227
228
229
230 @Deprecated
231 public static String toString(final String[] strings, final String separator) {
232 return String.join(separator, strings);
233 }
234 }