@Test public void substituteTemplateKey() { String string = "foo bar @{var1} baz @{var2}"; Function<String, String> keyToValueFunction = s -> { switch (s) { case "var1": return "val1"; case "var2": return "val2"; default: throw new IllegalArgumentException("Undefined variable " + s); } }; String result = TemplateUtils.substituteTemplateVariable(string, keyToValueFunction); assertEquals("foo bar val1 baz val2", result); }
public static String substituteTemplateVariable(final String text, final Function<String, String> keyToValueFunction) { if (text == null || text.isEmpty()) { return text; } StringBuilder resultBuilder = new StringBuilder(text); int pos; while ((pos = resultBuilder.indexOf("@{")) != -1) { int end = resultBuilder.indexOf("}", pos + 2); if (end != -1) { final String varName = resultBuilder.substring(pos + 2, end); final String value = keyToValueFunction.apply(varName); if (value == null) { return text; } resultBuilder.replace(pos, end + 1, value); } else { break; } } return resultBuilder.toString(); }
TemplateUtils { public static String substituteTemplateVariable(final String text, final Function<String, String> keyToValueFunction) { if (text == null || text.isEmpty()) { return text; } StringBuilder resultBuilder = new StringBuilder(text); int pos; while ((pos = resultBuilder.indexOf("@{")) != -1) { int end = resultBuilder.indexOf("}", pos + 2); if (end != -1) { final String varName = resultBuilder.substring(pos + 2, end); final String value = keyToValueFunction.apply(varName); if (value == null) { return text; } resultBuilder.replace(pos, end + 1, value); } else { break; } } return resultBuilder.toString(); } }
TemplateUtils { public static String substituteTemplateVariable(final String text, final Function<String, String> keyToValueFunction) { if (text == null || text.isEmpty()) { return text; } StringBuilder resultBuilder = new StringBuilder(text); int pos; while ((pos = resultBuilder.indexOf("@{")) != -1) { int end = resultBuilder.indexOf("}", pos + 2); if (end != -1) { final String varName = resultBuilder.substring(pos + 2, end); final String value = keyToValueFunction.apply(varName); if (value == null) { return text; } resultBuilder.replace(pos, end + 1, value); } else { break; } } return resultBuilder.toString(); } private  TemplateUtils(); }
TemplateUtils { public static String substituteTemplateVariable(final String text, final Function<String, String> keyToValueFunction) { if (text == null || text.isEmpty()) { return text; } StringBuilder resultBuilder = new StringBuilder(text); int pos; while ((pos = resultBuilder.indexOf("@{")) != -1) { int end = resultBuilder.indexOf("}", pos + 2); if (end != -1) { final String varName = resultBuilder.substring(pos + 2, end); final String value = keyToValueFunction.apply(varName); if (value == null) { return text; } resultBuilder.replace(pos, end + 1, value); } else { break; } } return resultBuilder.toString(); } private  TemplateUtils(); static Collection<InterpolationVariable> extractInterpolationVariables(final String text); static String substituteTemplateVariable(final String text,
                                                    final Function<String, String> keyToValueFunction); }
TemplateUtils { public static String substituteTemplateVariable(final String text, final Function<String, String> keyToValueFunction) { if (text == null || text.isEmpty()) { return text; } StringBuilder resultBuilder = new StringBuilder(text); int pos; while ((pos = resultBuilder.indexOf("@{")) != -1) { int end = resultBuilder.indexOf("}", pos + 2); if (end != -1) { final String varName = resultBuilder.substring(pos + 2, end); final String value = keyToValueFunction.apply(varName); if (value == null) { return text; } resultBuilder.replace(pos, end + 1, value); } else { break; } } return resultBuilder.toString(); } private  TemplateUtils(); static Collection<InterpolationVariable> extractInterpolationVariables(final String text); static String substituteTemplateVariable(final String text,
                                                    final Function<String, String> keyToValueFunction);  }