@Test public void testExpand() { Map<String,String> props = new HashMap<String, String>(); props.put("MOVIE", "Amelie"); PropertyExpander expander = new PropertyExpander(props); String expected = "My favorite movie is Amelie"; String actual = expander.expand("My favorite movie is @MOVIE@"); Assert.assertEquals(expected, actual); }
public String expand(String str) { if (StringUtils.isEmpty(str)) { return str; } if (str.indexOf("@") < 0) { return str; } Matcher mat = pat.matcher(str); int offset = 0; String expression; String value; StringBuffer expanded = new StringBuffer(); while (mat.find(offset)) { expression = mat.group(1); expanded.append(str.substring(offset, mat.start(1))); value = props.get(mat.group(2)); if (value != null) { expanded.append(value); } else { expanded.append(expression); } offset = mat.end(1); } expanded.append(str.substring(offset)); if (expanded.indexOf("@@") >= 0) { return expanded.toString().replaceAll("\\@\\@", "\\@"); } else { return expanded.toString(); } }
PropertyExpander { public String expand(String str) { if (StringUtils.isEmpty(str)) { return str; } if (str.indexOf("@") < 0) { return str; } Matcher mat = pat.matcher(str); int offset = 0; String expression; String value; StringBuffer expanded = new StringBuffer(); while (mat.find(offset)) { expression = mat.group(1); expanded.append(str.substring(offset, mat.start(1))); value = props.get(mat.group(2)); if (value != null) { expanded.append(value); } else { expanded.append(expression); } offset = mat.end(1); } expanded.append(str.substring(offset)); if (expanded.indexOf("@@") >= 0) { return expanded.toString().replaceAll("\\@\\@", "\\@"); } else { return expanded.toString(); } } }
PropertyExpander { public String expand(String str) { if (StringUtils.isEmpty(str)) { return str; } if (str.indexOf("@") < 0) { return str; } Matcher mat = pat.matcher(str); int offset = 0; String expression; String value; StringBuffer expanded = new StringBuffer(); while (mat.find(offset)) { expression = mat.group(1); expanded.append(str.substring(offset, mat.start(1))); value = props.get(mat.group(2)); if (value != null) { expanded.append(value); } else { expanded.append(expression); } offset = mat.end(1); } expanded.append(str.substring(offset)); if (expanded.indexOf("@@") >= 0) { return expanded.toString().replaceAll("\\@\\@", "\\@"); } else { return expanded.toString(); } } PropertyExpander(Map<String, String> props); }
PropertyExpander { public String expand(String str) { if (StringUtils.isEmpty(str)) { return str; } if (str.indexOf("@") < 0) { return str; } Matcher mat = pat.matcher(str); int offset = 0; String expression; String value; StringBuffer expanded = new StringBuffer(); while (mat.find(offset)) { expression = mat.group(1); expanded.append(str.substring(offset, mat.start(1))); value = props.get(mat.group(2)); if (value != null) { expanded.append(value); } else { expanded.append(expression); } offset = mat.end(1); } expanded.append(str.substring(offset)); if (expanded.indexOf("@@") >= 0) { return expanded.toString().replaceAll("\\@\\@", "\\@"); } else { return expanded.toString(); } } PropertyExpander(Map<String, String> props); String expand(String str); }
PropertyExpander { public String expand(String str) { if (StringUtils.isEmpty(str)) { return str; } if (str.indexOf("@") < 0) { return str; } Matcher mat = pat.matcher(str); int offset = 0; String expression; String value; StringBuffer expanded = new StringBuffer(); while (mat.find(offset)) { expression = mat.group(1); expanded.append(str.substring(offset, mat.start(1))); value = props.get(mat.group(2)); if (value != null) { expanded.append(value); } else { expanded.append(expression); } offset = mat.end(1); } expanded.append(str.substring(offset)); if (expanded.indexOf("@@") >= 0) { return expanded.toString().replaceAll("\\@\\@", "\\@"); } else { return expanded.toString(); } } PropertyExpander(Map<String, String> props); String expand(String str);  }