Skip to content

Commit

Permalink
Variable regrace fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sbellus committed May 18, 2016
1 parent 5839cee commit a78d2f0
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ public GraphicsVariableReplacer(VariableSource variableSource) {

public String replaceVariablesIn(String str) {
recursionDepth = 0;
return replaceVariablesRecursively(str);
if (str != null) {
return replaceVariablesRecursively(str);
}

return str;
}

private String replaceVariablesRecursively(String str) {
boolean isAtLeastOneVariableReplaced = false;
java.util.regex.Matcher m = VariablePattern.matcher(str);
while (m.find()) {
String var = m.group();
Expand All @@ -35,23 +38,24 @@ private String replaceVariablesRecursively(String str) {
varName = varName.replace("-REGRACE", "");
regrace = true;
}

Maybe<String> value = variableSource.findVariable(varName);
if (!value.isNothing()) {
isAtLeastOneVariableReplaced = true;
String varValue = value.getValue();
if (recursionDepth < MaxRecursionDepth) {
recursionDepth++;
varValue = replaceVariablesRecursively(varValue);
recursionDepth--;
}

if (regrace) {
varValue = GracefulNamer.regrace(varValue);
}

str = str.replace(var, varValue);
}
}

if (isAtLeastOneVariableReplaced && recursionDepth < MaxRecursionDepth) {
recursionDepth++;
return replaceVariablesRecursively(str);
}

return str;
}
}

0 comments on commit a78d2f0

Please sign in to comment.