@Test public void testFilter() { TokenFilter tokenFilter = new TokenFilter(); tokenFilter.setToken("TOKEN_A", "value-of-a"); tokenFilter.setToken("TOKEN_B", "value-of-b"); tokenFilter.setToken("TOKEN_C", "Value-of-C"); assertEquals( "$${NOPE}hellovalue-of-aworldvalue-of-b${NOT_A_TOKEN}", tokenFilter.filter("$$${NOPE}hello${TOKEN_A}world${TOKEN_B}$${NOT_A_TOKEN}") ); assertEquals( "${NOPE}hellovalue-of-aworld${TOKEN_D}", tokenFilter.filter("${NOPE}hello${TOKEN_A}world${TOKEN_D}") ); assertEquals( "Value-of-C", tokenFilter.filter("${TOKEN_C}") ); assertEquals( "value-of-c", tokenFilter.filter("${TOKEN_C:lower}") ); assertEquals( "VALUE-OF-C", tokenFilter.filter("${TOKEN_C:upper}") ); }
public String filter(String input) { StringBuilder output = new StringBuilder(); Matcher tokenMatcher = tokenPattern.matcher(input); int endOfLastMatch = 0; while (tokenMatcher.find()) { String literal = tokenMatcher.group(LEADING_TEXT_GROUP); String escape = tokenMatcher.group(ESCAPE_CHAR_GROUP); String modifier = tokenMatcher.group(TOKEN_MODIFIER); output.append(literal); if ("$".equals(escape)) { String notToken = tokenMatcher.group(TOKEN_GROUP); output.append(notToken); } else { output.append(escape); String tokenName = tokenMatcher.group(TOKEN_NAME_GROUP); String tokenValue = getToken(tokenName); if (tokenValue == null) { String notToken = tokenMatcher.group(TOKEN_GROUP); output.append(notToken); } else { if (modifier != null && !modifier.isEmpty()) { switch (modifier) { case "upper": output.append(tokenValue.toUpperCase()); break; case "lower": output.append(tokenValue.toLowerCase()); break; default: output.append(tokenValue); } } else output.append(tokenValue); } } endOfLastMatch = tokenMatcher.end(); } output.append(input.substring(endOfLastMatch)); return output.toString(); }
TokenFilter { public String filter(String input) { StringBuilder output = new StringBuilder(); Matcher tokenMatcher = tokenPattern.matcher(input); int endOfLastMatch = 0; while (tokenMatcher.find()) { String literal = tokenMatcher.group(LEADING_TEXT_GROUP); String escape = tokenMatcher.group(ESCAPE_CHAR_GROUP); String modifier = tokenMatcher.group(TOKEN_MODIFIER); output.append(literal); if ("$".equals(escape)) { String notToken = tokenMatcher.group(TOKEN_GROUP); output.append(notToken); } else { output.append(escape); String tokenName = tokenMatcher.group(TOKEN_NAME_GROUP); String tokenValue = getToken(tokenName); if (tokenValue == null) { String notToken = tokenMatcher.group(TOKEN_GROUP); output.append(notToken); } else { if (modifier != null && !modifier.isEmpty()) { switch (modifier) { case "upper": output.append(tokenValue.toUpperCase()); break; case "lower": output.append(tokenValue.toLowerCase()); break; default: output.append(tokenValue); } } else output.append(tokenValue); } } endOfLastMatch = tokenMatcher.end(); } output.append(input.substring(endOfLastMatch)); return output.toString(); } }
TokenFilter { public String filter(String input) { StringBuilder output = new StringBuilder(); Matcher tokenMatcher = tokenPattern.matcher(input); int endOfLastMatch = 0; while (tokenMatcher.find()) { String literal = tokenMatcher.group(LEADING_TEXT_GROUP); String escape = tokenMatcher.group(ESCAPE_CHAR_GROUP); String modifier = tokenMatcher.group(TOKEN_MODIFIER); output.append(literal); if ("$".equals(escape)) { String notToken = tokenMatcher.group(TOKEN_GROUP); output.append(notToken); } else { output.append(escape); String tokenName = tokenMatcher.group(TOKEN_NAME_GROUP); String tokenValue = getToken(tokenName); if (tokenValue == null) { String notToken = tokenMatcher.group(TOKEN_GROUP); output.append(notToken); } else { if (modifier != null && !modifier.isEmpty()) { switch (modifier) { case "upper": output.append(tokenValue.toUpperCase()); break; case "lower": output.append(tokenValue.toLowerCase()); break; default: output.append(tokenValue); } } else output.append(tokenValue); } } endOfLastMatch = tokenMatcher.end(); } output.append(input.substring(endOfLastMatch)); return output.toString(); } TokenFilter();  TokenFilter(Map<String, String> tokenValues); }
TokenFilter { public String filter(String input) { StringBuilder output = new StringBuilder(); Matcher tokenMatcher = tokenPattern.matcher(input); int endOfLastMatch = 0; while (tokenMatcher.find()) { String literal = tokenMatcher.group(LEADING_TEXT_GROUP); String escape = tokenMatcher.group(ESCAPE_CHAR_GROUP); String modifier = tokenMatcher.group(TOKEN_MODIFIER); output.append(literal); if ("$".equals(escape)) { String notToken = tokenMatcher.group(TOKEN_GROUP); output.append(notToken); } else { output.append(escape); String tokenName = tokenMatcher.group(TOKEN_NAME_GROUP); String tokenValue = getToken(tokenName); if (tokenValue == null) { String notToken = tokenMatcher.group(TOKEN_GROUP); output.append(notToken); } else { if (modifier != null && !modifier.isEmpty()) { switch (modifier) { case "upper": output.append(tokenValue.toUpperCase()); break; case "lower": output.append(tokenValue.toLowerCase()); break; default: output.append(tokenValue); } } else output.append(tokenValue); } } endOfLastMatch = tokenMatcher.end(); } output.append(input.substring(endOfLastMatch)); return output.toString(); } TokenFilter();  TokenFilter(Map<String, String> tokenValues); void setToken(String name, String value); String getToken(String name); void unsetToken(String name); Map<String, String> getTokens(); void setTokens(Map<String, String> tokens); String filter(String input); void filterValues(Map<?, String> map); }
TokenFilter { public String filter(String input) { StringBuilder output = new StringBuilder(); Matcher tokenMatcher = tokenPattern.matcher(input); int endOfLastMatch = 0; while (tokenMatcher.find()) { String literal = tokenMatcher.group(LEADING_TEXT_GROUP); String escape = tokenMatcher.group(ESCAPE_CHAR_GROUP); String modifier = tokenMatcher.group(TOKEN_MODIFIER); output.append(literal); if ("$".equals(escape)) { String notToken = tokenMatcher.group(TOKEN_GROUP); output.append(notToken); } else { output.append(escape); String tokenName = tokenMatcher.group(TOKEN_NAME_GROUP); String tokenValue = getToken(tokenName); if (tokenValue == null) { String notToken = tokenMatcher.group(TOKEN_GROUP); output.append(notToken); } else { if (modifier != null && !modifier.isEmpty()) { switch (modifier) { case "upper": output.append(tokenValue.toUpperCase()); break; case "lower": output.append(tokenValue.toLowerCase()); break; default: output.append(tokenValue); } } else output.append(tokenValue); } } endOfLastMatch = tokenMatcher.end(); } output.append(input.substring(endOfLastMatch)); return output.toString(); } TokenFilter();  TokenFilter(Map<String, String> tokenValues); void setToken(String name, String value); String getToken(String name); void unsetToken(String name); Map<String, String> getTokens(); void setTokens(Map<String, String> tokens); String filter(String input); void filterValues(Map<?, String> map);  }