FormatUtils { public static String replaceTokens(String text, Map<String, String> replacements, boolean matchUsingPrefixSuffix) { if (text != null && replacements != null && replacements.size() > 0) { if (matchUsingPrefixSuffix) { Matcher matcher = pattern.matcher(text); StringBuffer buffer = new StringBuffer(); while (matcher.find()) { String[] matchPipe = matcher.group(1).split("\\|"); String[] matchColon = matchPipe[0].split(":"); String replacement = replacements.get(matchColon[0]); if (replacement != null) { matcher.appendReplacement(buffer, ""); if (matchColon.length == 2) { int startIndex = Integer.parseInt(matchColon[1]); if (startIndex <= replacement.length()) { replacement = replacement.substring(Integer.parseInt(matchColon[1])); } } else if (matchColon.length == 3) { int startIndex = Integer.parseInt(matchColon[1]); int endIndex = Integer.parseInt(matchColon[2]); if (startIndex <= replacement.length() && endIndex <= replacement.length()) { replacement = replacement.substring(startIndex, endIndex); } } if (matchPipe.length == 2) { replacement = formatString(matchPipe[1], replacement); } buffer.append(replacement); } } matcher.appendTail(buffer); text = buffer.toString(); } else { for (Object key : replacements.keySet()) { text = text.replaceAll(key.toString(), replacements.get(key)); } } } return text; } private  FormatUtils(); static String replace(String prop, String replaceWith, String sourceString); static String replaceToken(String text, String tokenToReplace, String replaceWithText,
            boolean matchUsingPrefixSuffix); static String replaceTokens(String text, Map<String, String> replacements,
            boolean matchUsingPrefixSuffix); static String formatString(String format, String arg); static boolean toBoolean(String value); static boolean isMixedCase(String text); static boolean isWildCardMatch(String text, String pattern, boolean ignoreCase); static boolean isWildCardMatch(String text, String pattern); static String unescapeWildCards(String str); static String escapeWildCards(String str); static boolean isWildCarded(String str); static String[] wordWrap(String str, int firstLineSize, int nonFirstLineSize); static String[] wordWrap(String str, int lineSize); static String abbreviateForLogging(String value); @SuppressWarnings("rawtypes") static String abbreviateForLogging(List list, int maxCharsToLog); static Date parseDate(String str, String[] parsePatterns); static Date parseDate(String str, String[] parsePatterns, TimeZone timeZone); static Timestamp parseTimestampWithTimezone(String str); static Timestamp parseTimestampWithTimezone(String str, String[] parsePatterns); static String[] splitOnSpacePreserveQuotedStrings(String source); static String replaceCharsToShortenName(String name); static String lower(String str); static String upper(String str); static boolean isInfamousTurkey(); static String stripTurkeyDottedI(String str); static String formatDurationReadable(long duration); static String formatDateTimeISO(Date date); static boolean isInteger(String s); static final String[] TIMESTAMP_PATTERNS; static final String[] TIME_PATTERNS; static final String[] TIMESTAMP_WITH_TIMEZONE_PATTERNS; static final FastDateFormat TIMESTAMP_FORMATTER; static final FastDateFormat TIME_FORMATTER; final static String WILDCARD; final static String WILDCARD_SEPARATOR; final static String NEGATE_TOKEN; final static char WILDCARD_SEPARATOR_CHAR; final static char WILDCARD_CHAR; final static char NEGATE_TOKEN_CHAR; final static String WILDCARD_ESCAPED; final static String WILDCARD_SEPARATOR_ESCAPED; final static String NEGATE_TOKEN_ESCAPED; final static int MAX_CHARS_TO_LOG; }
@Test public void testReplaceTokens() { assertEquals("test", FormatUtils.replaceTokens("test", null, true)); assertEquals("test", FormatUtils.replaceTokens("test", new HashMap<String, String>(), true)); Map<String, String> params = new HashMap<String, String>(); params.put("test", "1"); assertEquals("test1", FormatUtils.replaceTokens("test$(test)", params, true)); assertEquals("test0001", FormatUtils.replaceTokens("test$(test|%04d)", params, true)); }
@Test public void testReplaceCurrentTimestamp() { String beforeSql = "insert into sym_node values ('00000', 'test-root-group', '00000', 1, null, null, '2.0', null, null, current_timestamp, null, 0, 0, '00000', 'engine')"; String afterSql = "insert into sym_node values ('00000', 'test-root-group', '00000', 1, null, null, '2.0', null, null, XXXX, null, 0, 0, '00000', 'engine')"; Map<String,String> replacementTokens = new HashMap<String, String>(); replacementTokens.put("current_timestamp", "XXXX"); assertEquals(afterSql, FormatUtils.replaceTokens(beforeSql, replacementTokens, false)); }