/** * Replaces all the occurrences of variables in the given source object with * their matching values from the map. * * @param <V> the type of the values in the map * @param source the source text containing the variables to substitute, null returns null * @param valueMap the map with the values, may be null * @return the result of the replace operation */ public static <V> String replace(final Object source, final Map<String, V> valueMap) { return new StrSubstitutor(valueMap).replace(source); }
/** * Replaces all the occurrences of variables in the given source object with * their matching values from the map. This method allows to specify a * custom variable prefix and suffix * * @param <V> the type of the values in the map * @param source the source text containing the variables to substitute, null returns null * @param valueMap the map with the values, may be null * @param prefix the prefix of variables, not null * @param suffix the suffix of variables, not null * @return the result of the replace operation * @throws IllegalArgumentException if the prefix or suffix is null */ public static <V> String replace(final Object source, final Map<String, V> valueMap, final String prefix, final String suffix) { return new StrSubstitutor(valueMap, prefix, suffix).replace(source); }
Map<String, String> values = new HashMap<String, String>(); values.put("value", x); values.put("column", y); StrSubstitutor sub = new StrSubstitutor(values, "%(", ")"); String result = sub.replace("There's an incorrect value '%(value)' in column # %(column)");
Map<String, String> valuesMap = new HashMap<String, String>(); valuesMap.put("animal", "quick brown fox"); valuesMap.put("target", "lazy dog"); String templateString = "The ${animal} jumped over the ${target}."; StrSubstitutor sub = new StrSubstitutor(valuesMap); String resolvedString = sub.replace(templateString);
String buildVariableSubstitute(String str) { return new StrSubstitutor(buildVariables).replace(str); }
/** * Replaces all the occurrences of variables in the given source object with * their matching values from the system properties. * * @param source the source text containing the variables to substitute, null returns null * @return the result of the replace operation */ public static String replaceSystemProperties(final Object source) { return new StrSubstitutor(StrLookup.systemPropertiesLookup()).replace(source); }
/** * Tests adjacent keys. */ @Test public void testReplaceAdjacentAtStart() { values.put("code", "GBP"); values.put("amount", "12.50"); final StrSubstitutor sub = new StrSubstitutor(values); assertEquals("GBP12.50 charged", sub.replace("${code}${amount} charged")); }
/** * Tests adjacent keys. */ @Test public void testReplaceAdjacentAtEnd() { values.put("code", "GBP"); values.put("amount", "12.50"); final StrSubstitutor sub = new StrSubstitutor(values); assertEquals("Amount is GBP12.50", sub.replace("Amount is ${code}${amount}")); }
/** * Tests constructor. */ @Test public void testConstructorMapPrefixSuffix() { final Map<String, String> map = new HashMap<>(); map.put("name", "commons"); final StrSubstitutor sub = new StrSubstitutor(map, "<", ">"); assertEquals("Hi < commons", sub.replace("Hi $< <name>")); }
/** * Tests constructor. */ @Test public void testConstructorMapFull() { final Map<String, String> map = new HashMap<>(); map.put("name", "commons"); StrSubstitutor sub = new StrSubstitutor(map, "<", ">", '!'); assertEquals("Hi < commons", sub.replace("Hi !< <name>")); sub = new StrSubstitutor(map, "<", ">", '!', "||"); assertEquals("Hi < commons", sub.replace("Hi !< <name2||commons>")); }
/** * Tests key replace changing map after initialization (not recommended). */ @Test public void testReplaceChangedMap() { final StrSubstitutor sub = new StrSubstitutor(values); values.put("target", "moon"); assertEquals("The quick brown fox jumps over the moon.", sub.replace("The ${animal} jumps over the ${target}.")); }
/** * Tests constructor. */ @Test public void testConstructorNoArgs() { final StrSubstitutor sub = new StrSubstitutor(); assertEquals("Hi ${name}", sub.replace("Hi ${name}")); }
/** * Tests simple key replace. */ @Test public void testReplacePartialString_noReplace() { final StrSubstitutor sub = new StrSubstitutor(); assertEquals("${animal} jumps", sub.replace("The ${animal} jumps over the ${target}.", 4, 15)); }
/** * Tests whether substitution in variable names is disabled per default. */ @Test public void testReplaceInVariableDisabled() { values.put("animal.1", "fox"); values.put("animal.2", "mouse"); values.put("species", "2"); final StrSubstitutor sub = new StrSubstitutor(values); assertEquals( "Wrong result (1)", "The ${animal.${species}} jumps over the lazy dog.", sub.replace("The ${animal.${species}} jumps over the ${target}.")); assertEquals( "Wrong result (2)", "The ${animal.${species:-1}} jumps over the lazy dog.", sub.replace("The ${animal.${species:-1}} jumps over the ${target}.")); }
/** * Tests complex and recursive substitution in variable names. */ @Test public void testReplaceInVariableRecursive() { values.put("animal.2", "brown fox"); values.put("animal.1", "white mouse"); values.put("color", "white"); values.put("species.white", "1"); values.put("species.brown", "2"); final StrSubstitutor sub = new StrSubstitutor(values); sub.setEnableSubstitutionInVariables(true); assertEquals( "Wrong result (1)", "The white mouse jumps over the lazy dog.", sub.replace("The ${animal.${species.${color}}} jumps over the ${target}.")); assertEquals( "Wrong result (2)", "The brown fox jumps over the lazy dog.", sub.replace("The ${animal.${species.${unknownColor:-brown}}} jumps over the ${target}.")); }
/** * Tests whether a variable can be replaced in a variable name. */ @Test public void testReplaceInVariable() { values.put("animal.1", "fox"); values.put("animal.2", "mouse"); values.put("species", "2"); final StrSubstitutor sub = new StrSubstitutor(values); sub.setEnableSubstitutionInVariables(true); assertEquals( "Wrong result (1)", "The mouse jumps over the lazy dog.", sub.replace("The ${animal.${species}} jumps over the ${target}.")); values.put("species", "1"); assertEquals( "Wrong result (2)", "The fox jumps over the lazy dog.", sub.replace("The ${animal.${species}} jumps over the ${target}.")); assertEquals( "Wrong result (3)", "The fox jumps over the lazy dog.", sub.replace("The ${unknown.animal.${unknown.species:-1}:-fox} jumps over the ${unknown.target:-lazy dog}.")); }
@Test public void testSubstitutePreserveEscape() { final String org = "${not-escaped} $${escaped}"; final Map<String, String> map = new HashMap<>(); map.put("not-escaped", "value"); final StrSubstitutor sub = new StrSubstitutor(map, "${", "}", '$'); assertFalse(sub.isPreserveEscapes()); assertEquals("value ${escaped}", sub.replace(org)); sub.setPreserveEscapes(true); assertTrue(sub.isPreserveEscapes()); assertEquals("value $${escaped}", sub.replace(org)); }
/** * Tests get set. */ @Test public void testGetSetEscape() { final StrSubstitutor sub = new StrSubstitutor(); assertEquals('$', sub.getEscapeChar()); sub.setEscapeChar('<'); assertEquals('<', sub.getEscapeChar()); }
/** * Tests get set. */ @Test public void testGetSetValueDelimiter() { final StrSubstitutor sub = new StrSubstitutor(); assertTrue(sub.getValueDelimiterMatcher() instanceof StrMatcher.StringMatcher); sub.setValueDelimiter(':'); assertTrue(sub.getValueDelimiterMatcher() instanceof StrMatcher.CharMatcher); sub.setValueDelimiter("||"); assertTrue(sub.getValueDelimiterMatcher() instanceof StrMatcher.StringMatcher); sub.setValueDelimiter(null); assertNull(sub.getValueDelimiterMatcher()); final StrMatcher matcher = StrMatcher.commaMatcher(); sub.setValueDelimiterMatcher(matcher); assertSame(matcher, sub.getValueDelimiterMatcher()); sub.setValueDelimiterMatcher(null); assertNull(sub.getValueDelimiterMatcher()); }
private void doTestNoReplace(final String replaceTemplate) { final StrSubstitutor sub = new StrSubstitutor(values); if (replaceTemplate == null) { assertNull(sub.replace((String) null)); assertNull(sub.replace((String) null, 0, 100)); assertNull(sub.replace((char[]) null)); assertNull(sub.replace((char[]) null, 0, 100)); assertNull(sub.replace((StringBuffer) null)); assertNull(sub.replace((StringBuffer) null, 0, 100)); assertNull(sub.replace((StrBuilder) null)); assertNull(sub.replace((StrBuilder) null, 0, 100)); assertNull(sub.replace((Object) null)); assertFalse(sub.replaceIn((StringBuffer) null)); assertFalse(sub.replaceIn((StringBuffer) null, 0, 100)); assertFalse(sub.replaceIn((StrBuilder) null)); assertFalse(sub.replaceIn((StrBuilder) null, 0, 100)); } else { assertEquals(replaceTemplate, sub.replace(replaceTemplate)); final StrBuilder bld = new StrBuilder(replaceTemplate); assertFalse(sub.replaceIn(bld)); assertEquals(replaceTemplate, bld.toString()); } }