/** * 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()); }
/** * 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); }
public static String vsm(String materialFingerprint, String revision) { return StrSubstitutor.replace("/materials/value_stream_map/${material_fingerprint}/${revision}", of( "material_fingerprint", materialFingerprint, "revision", revision)); } }
public static String stageDetailTab(String pipelineName, int pipelineCounter, String stageName, int stageCounter) { return StrSubstitutor.replace("/pipelines/${pipeline_name}/${pipeline_counter}/${stage_name}/${stage_counter}", of( "pipeline_name", pipelineName, "pipeline_counter", pipelineCounter, "stage_name", stageName, "stage_counter", stageCounter)); } }
public static String vsm(String pipelineName, int pipelineCounter) { return StrSubstitutor.replace("/pipelines/value_stream_map/${pipeline_name}/${pipeline_counter}", of("pipeline_name", pipelineName, "pipeline_counter", pipelineCounter)); } }
/** * 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); }
/** * 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); }
@Test public void testSamePrefixAndSuffix() { final Map<String, String> map = new HashMap<>(); map.put("greeting", "Hello"); map.put(" there ", "XXX"); map.put("name", "commons"); assertEquals("Hi commons!", StrSubstitutor.replace("Hi @name@!", map, "@", "@")); assertEquals("Hello there commons!", StrSubstitutor.replace("@greeting@ there @name@!", map, "@", "@")); }
/** * Test the replace of a properties object */ @Test public void testSubstituteDefaultProperties(){ final String org = "${doesnotwork}"; System.setProperty("doesnotwork", "It works!"); // create a new Properties object with the System.getProperties as default final Properties props = new Properties(System.getProperties()); assertEquals("It works!", StrSubstitutor.replace(org, props)); }
/** * Tests static. */ @Test public void testStaticReplacePrefixSuffix() { final Map<String, String> map = new HashMap<>(); map.put("name", "commons"); assertEquals("Hi commons!", StrSubstitutor.replace("Hi <name>!", map, "<", ">")); }
/** * Tests static. */ @Test public void testStaticReplace() { final Map<String, String> map = new HashMap<>(); map.put("name", "commons"); assertEquals("Hi commons!", StrSubstitutor.replace("Hi ${name}!", map)); }
/** * 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 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 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)); }
@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)); }