Refine search
/** * 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); }
/** * Test for LANG-1055: StrSubstitutor.replaceSystemProperties does not work consistently */ @Test public void testLANG1055() { System.setProperty("test_key", "test_value"); final String expected = StrSubstitutor.replace("test_key=${test_key}", System.getProperties()); final String actual = StrSubstitutor.replaceSystemProperties("test_key=${test_key}"); assertEquals(expected, actual); }
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()); } }
/** * Tests get set. */ @Test public void testGetSetEscape() { final StrSubstitutor sub = new StrSubstitutor(); assertEquals('$', sub.getEscapeChar()); sub.setEscapeChar('<'); assertEquals('<', sub.getEscapeChar()); }
@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)); }
/** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source as a template. * The source is not altered by this method. * * @param source the buffer to use as a template, not changed, null returns null * @return the result of the replace operation * @since 3.2 */ public String replace(final CharSequence source) { if (source == null) { return null; } return replace(source, 0, source.length()); }
/** * 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}.")); }
@Test public void testDefaultValueDelimiters() { final Map<String, String> map = new HashMap<>(); map.put("animal", "fox"); map.put("target", "dog"); StrSubstitutor sub = new StrSubstitutor(map, "${", "}", '$'); assertEquals("The fox jumps over the lazy dog. 1234567890.", sub.replace("The ${animal} jumps over the lazy ${target}. ${undefined.number:-1234567890}.")); sub = new StrSubstitutor(map, "${", "}", '$', "?:"); assertEquals("The fox jumps over the lazy dog. 1234567890.", sub.replace("The ${animal} jumps over the lazy ${target}. ${undefined.number?:1234567890}.")); sub = new StrSubstitutor(map, "${", "}", '$', "||"); assertEquals("The fox jumps over the lazy dog. 1234567890.", sub.replace("The ${animal} jumps over the lazy ${target}. ${undefined.number||1234567890}.")); sub = new StrSubstitutor(map, "${", "}", '$', "!"); assertEquals("The fox jumps over the lazy dog. 1234567890.", sub.replace("The ${animal} jumps over the lazy ${target}. ${undefined.number!1234567890}.")); sub = new StrSubstitutor(map, "${", "}", '$', ""); sub.setValueDelimiterMatcher(null); assertEquals("The fox jumps over the lazy dog. ${undefined.number!1234567890}.", sub.replace("The ${animal} jumps over the lazy ${target}. ${undefined.number!1234567890}.")); sub = new StrSubstitutor(map, "${", "}", '$'); sub.setValueDelimiterMatcher(null); assertEquals("The fox jumps over the lazy dog. ${undefined.number!1234567890}.", sub.replace("The ${animal} jumps over the lazy ${target}. ${undefined.number!1234567890}.")); }
/** * Substitutes input and output file name in the command with given values * @param fin input file, must not be blank * @param fout output file, must not be blank * @return a string with the substitutions * @throws NullPointerException if any argument was null * @throws IllegalArgumentException if any argument was blank */ public String substitute(String fin, String fout){ Validate.notBlank(fin); Validate.notBlank(fout); Map<String, String> valuesMap = new HashMap<>(); valuesMap.put("fin", fin); valuesMap.put("fout", fout); StrSubstitutor sub = new StrSubstitutor(valuesMap); return sub.replace(this.cmd.toString()); }
/** * 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()); }
/** * Tests interpolation with system properties. */ @Test public void testStaticReplaceSystemProperties() { final StrBuilder buf = new StrBuilder(); buf.append("Hi ").append(System.getProperty("user.name")); buf.append(", you are working with "); buf.append(System.getProperty("os.name")); buf.append(", your home directory is "); buf.append(System.getProperty("user.home")).append('.'); assertEquals(buf.toString(), StrSubstitutor.replaceSystemProperties("Hi ${user.name}, you are " + "working with ${os.name}, your home " + "directory is ${user.home}.")); }
/** * Tests get set. */ @Test public void testGetSetPrefix() { final StrSubstitutor sub = new StrSubstitutor(); assertTrue(sub.getVariablePrefixMatcher() instanceof StrMatcher.StringMatcher); sub.setVariablePrefix('<'); assertTrue(sub.getVariablePrefixMatcher() instanceof StrMatcher.CharMatcher); sub.setVariablePrefix("<<"); assertTrue(sub.getVariablePrefixMatcher() instanceof StrMatcher.StringMatcher); try { sub.setVariablePrefix(null); fail(); } catch (final IllegalArgumentException ex) { // expected } assertTrue(sub.getVariablePrefixMatcher() instanceof StrMatcher.StringMatcher); final StrMatcher matcher = StrMatcher.commaMatcher(); sub.setVariablePrefixMatcher(matcher); assertSame(matcher, sub.getVariablePrefixMatcher()); try { sub.setVariablePrefixMatcher(null); fail(); } catch (final IllegalArgumentException ex) { // expected } assertSame(matcher, sub.getVariablePrefixMatcher()); }
/** * Tests get set. */ @Test public void testGetSetSuffix() { final StrSubstitutor sub = new StrSubstitutor(); assertTrue(sub.getVariableSuffixMatcher() instanceof StrMatcher.StringMatcher); sub.setVariableSuffix('<'); assertTrue(sub.getVariableSuffixMatcher() instanceof StrMatcher.CharMatcher); sub.setVariableSuffix("<<"); assertTrue(sub.getVariableSuffixMatcher() instanceof StrMatcher.StringMatcher); try { sub.setVariableSuffix(null); fail(); } catch (final IllegalArgumentException ex) { // expected } assertTrue(sub.getVariableSuffixMatcher() instanceof StrMatcher.StringMatcher); final StrMatcher matcher = StrMatcher.commaMatcher(); sub.setVariableSuffixMatcher(matcher); assertSame(matcher, sub.getVariableSuffixMatcher()); try { sub.setVariableSuffixMatcher(null); fail(); } catch (final IllegalArgumentException ex) { // expected } assertSame(matcher, sub.getVariableSuffixMatcher()); }
/** * Get a histogram for the time range by probing to break down large buckets. Use count instead of * querying if it is non-negative. */ private Histogram getHistogramByProbing(TableCountProbingContext probingContext, int count, long startEpoch, long endEpoch) { Histogram histogram = new Histogram(); Map<String, String> values = new HashMap<>(); values.put("table", probingContext.entity); values.put("column", probingContext.watermarkColumn); values.put("greater", ">="); values.put("less", "<"); StrSubstitutor sub = new StrSubstitutor(values); getHistogramRecursively(probingContext, histogram, sub, values, count, startEpoch, endEpoch); return histogram; }
/** * Tests protected. */ @Test public void testResolveVariable() { final StrBuilder builder = new StrBuilder("Hi ${name}!"); final Map<String, String> map = new HashMap<>(); map.put("name", "commons"); final StrSubstitutor sub = new StrSubstitutor(map) { @Override protected String resolveVariable(final String variableName, final StrBuilder buf, final int startPos, final int endPos) { assertEquals("name", variableName); assertSame(builder, buf); assertEquals(3, startPos); assertEquals(10, endPos); return "jakarta"; } }; sub.replaceIn(builder); assertEquals("Hi jakarta!", builder.toString()); }
public static String replacePlaceholdersWithWhiteSpace(final String templateContent) { return StrSubstitutor.replaceSystemProperties(templateContent); }
/** * Replaces all the occurrences of variables within the given source buffer * with their matching values from the resolver. * The buffer is updated with the result. * * @param source the buffer to replace in, updated, null returns zero * @return true if altered */ public boolean replaceIn(final StringBuffer source) { if (source == null) { return false; } return replaceIn(source, 0, source.length()); }
public static String vsm(String materialFingerprint, String revision) { return StrSubstitutor.replace("/materials/value_stream_map/${material_fingerprint}/${revision}", of( "material_fingerprint", materialFingerprint, "revision", revision)); } }
/** * 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}.")); }
public static String replacePlaceholdersWithWhiteSpace(final String templateContent) { return StrSubstitutor.replaceSystemProperties(templateContent); }