in src/main/java/org/apache/commons/exec/util/StringUtils.java [140:220]
public static StringBuffer stringSubstitution(final String argStr, final Map<? super String, ?> vars, final boolean isLenient) {
final StringBuffer argBuf = new StringBuffer();
if (argStr == null || argStr.isEmpty()) {
return argBuf;
}
if (vars == null || vars.isEmpty()) {
return argBuf.append(argStr);
}
final int argStrLength = argStr.length();
for (int cIdx = 0; cIdx < argStrLength;) {
char ch = argStr.charAt(cIdx);
char del = ' ';
switch (ch) {
case '$':
final StringBuilder nameBuf = new StringBuilder();
del = argStr.charAt(cIdx + 1);
if (del == '{') {
cIdx++;
for (++cIdx; cIdx < argStr.length(); ++cIdx) {
ch = argStr.charAt(cIdx);
if (ch != '_' && ch != '.' && ch != '-' && ch != '+' && !Character.isLetterOrDigit(ch)) {
break;
}
nameBuf.append(ch);
}
if (nameBuf.length() >= 0) {
String value;
final Object temp = vars.get(nameBuf.toString());
if (temp instanceof File) {
// for a file we have to fix the separator chars to allow
// cross-platform compatibility
value = fixFileSeparatorChar(((File) temp).getAbsolutePath());
} else {
value = Objects.toString(temp, null);
}
if (value != null) {
argBuf.append(value);
} else {
if (!isLenient) {
// complain that no variable was found
throw new IllegalArgumentException("No value found for : " + nameBuf);
}
// just append the unresolved variable declaration
argBuf.append("${").append(nameBuf.toString()).append("}");
}
del = argStr.charAt(cIdx);
if (del != '}') {
throw new IllegalArgumentException("Delimiter not found for : " + nameBuf);
}
}
} else {
argBuf.append(ch);
}
cIdx++;
break;
default:
argBuf.append(ch);
++cIdx;
break;
}
}
return argBuf;
}