GenericTokenParser { public String parse(String text) { StringBuilder builder = new StringBuilder(); if (text != null && text.length() > 0) { char[] src = text.toCharArray(); int offset = 0; int start = text.indexOf(openToken, offset); while (start > -1) { if (start > 0 && src[start - 1] == '\\') { builder.append(src, offset, start - offset - 1).append(openToken); offset = start + openToken.length(); } else { int end = text.indexOf(closeToken, start); if (end == -1) { builder.append(src, offset, src.length - offset); offset = src.length; } else { builder.append(src, offset, start - offset); offset = start + openToken.length(); String content = new String(src, offset, end - offset); builder.append(handler.handleToken(content)); offset = end + closeToken.length(); } } start = text.indexOf(openToken, offset); } if (offset < src.length) { builder.append(src, offset, src.length - offset); } } return builder.toString(); } GenericTokenParser(String openToken, String closeToken, TokenHandler handler); String parse(String text);  }
@Test public void shouldDemonstrateGenericTokenReplacement() { GenericTokenParser parser = new GenericTokenParser("${", "}", new VariableTokenHandler(new HashMap<String, String>() { { put("first_name", "James"); put("initial", "T"); put("last_name", "Kirk"); put("", ""); } })); assertEquals("James T Kirk reporting.", parser.parse("${first_name} ${initial} ${last_name} reporting.")); assertEquals("Hello captain James T Kirk", parser.parse("Hello captain ${first_name} ${initial} ${last_name}")); assertEquals("James T Kirk", parser.parse("${first_name} ${initial} ${last_name}")); assertEquals("JamesTKirk", parser.parse("${first_name}${initial}${last_name}")); assertEquals("{}JamesTKirk", parser.parse("{}${first_name}${initial}${last_name}")); assertEquals("}JamesTKirk", parser.parse("}${first_name}${initial}${last_name}")); assertEquals("}James{{T}}Kirk", parser.parse("}${first_name}{{${initial}}}${last_name}")); assertEquals("}James}T{Kirk", parser.parse("}${first_name}}${initial}{${last_name}")); assertEquals("}James}T{Kirk", parser.parse("}${first_name}}${initial}{${last_name}")); assertEquals("}James}T{Kirk{{}}", parser.parse("}${first_name}}${initial}{${last_name}{{}}")); assertEquals("}James}T{Kirk{{}}", parser.parse("}${first_name}}${initial}{${last_name}{{}}${}")); assertEquals("{$$something}JamesTKirk", parser.parse("{$$something}${first_name}${initial}${last_name}")); assertEquals("${", parser.parse("${")); assertEquals("}", parser.parse("}")); assertEquals("Hello ${ this is a test.", parser.parse("Hello ${ this is a test.")); assertEquals("Hello } this is a test.", parser.parse("Hello } this is a test.")); assertEquals("Hello } ${ this is a test.", parser.parse("Hello } ${ this is a test.")); }
@Test public void shallNotInterpolateSkippedVaiables() { GenericTokenParser parser = new GenericTokenParser("${", "}", new VariableTokenHandler(new HashMap<String, String>())); assertEquals("${skipped} variable", parser.parse("\\${skipped} variable")); assertEquals("This is a ${skipped} variable", parser.parse("This is a \\${skipped} variable")); assertEquals("null ${skipped} variable", parser.parse("${skipped} \\${skipped} variable")); assertEquals("The null is ${skipped} variable", parser.parse("The ${skipped} is \\${skipped} variable")); }
@Test(timeout = 1000) public void shouldParseFastOnJdk7u6() { GenericTokenParser parser = new GenericTokenParser("${", "}", new VariableTokenHandler(new HashMap<String, String>() { { put("first_name", "James"); put("initial", "T"); put("last_name", "Kirk"); put("", ""); } })); StringBuilder input = new StringBuilder(); for (int i = 0; i < 10000; i++) { input.append("${first_name} ${initial} ${last_name} reporting. "); } StringBuilder expected = new StringBuilder(); for (int i = 0; i < 10000; i++) { expected.append("James T Kirk reporting. "); } assertEquals(expected.toString(), parser.parse(input.toString())); }