Improvement for SubstituteTemplate Java Action in Communicty Commons - Mendix Forum

Improvement for SubstituteTemplate Java Action in Communicty Commons

0

When the template contains a {parameter} that is not available in the Substitute object then an error is raised and nothing is returned.

My use case: I have a string with some {parameters}. There are two substitute objects. I would like to call the action twice : the first time using the template string and the first object. The second time using the output from the first call as template string and the second object.

My proposal: modify the code for substituteTemplate from

return regexReplaceAll(template, "\\{(@)?([\\w./]+)\\}", (MatchResult match) -> {
String value;
String path = match.group(2);
if (match.group(1) != null) {
value = String.valueOf(Core.getConfiguration().getConstantValue(path));
} else {
try {
value = ORM.getValueOfPath(context, substitute, path, datetimeformat);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return HTMLEncode ? HTMLEncode(value) : value;
});

to

return regexReplaceAll(template, "\\{(@)?([\\w./]+)\\}", (MatchResult match) -> {
String value;
String path = match.group(2);
if (match.group(1) != null) {
try {
value = String.valueOf(Core.getConfiguration().getConstantValue(path));
} catch (Exception e) {
value = match.group(0);
}

} else {
try {
value = ORM.getValueOfPath(context, substitute, path, datetimeformat);
} catch (Exception e) {
value = match.group(0);
}
}
return HTMLEncode ? HTMLEncode(value) : value;
});

This would make the module much more useful.

Regards

Wim Stevens

asked
0 answers