/** * 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()); }
/** * 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 * @since 3.2 */ public boolean replaceIn(final StringBuilder source) { if (source == null) { return false; } return replaceIn(source, 0, source.length()); }
/** * 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()); }
assertTrue(sub.replaceIn(buf)); assertEquals(expectedResult, buf.toString()); if (substring) { buf = new StringBuffer(replaceTemplate); assertTrue(sub.replaceIn(buf, 1, buf.length() - 2)); assertTrue(sub.replaceIn(builder)); assertEquals(expectedResult, builder.toString()); if (substring) { builder = new StringBuilder(replaceTemplate); assertTrue(sub.replaceIn(builder, 1, builder.length() - 2)); assertTrue(sub.replaceIn(bld)); assertEquals(expectedResult, bld.toString()); if (substring) { bld = new StrBuilder(replaceTemplate); assertTrue(sub.replaceIn(bld, 1, bld.length() - 2));
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()); } }
@Override public boolean replaceIn(StringBuilder value) { return substitutor.replaceIn(value); }
/** * 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()); }
/** * 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()); }
/** * 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()); }
/** * 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 * @since 3.2 */ public boolean replaceIn(final StringBuilder source) { if (source == null) { return false; } return replaceIn(source, 0, source.length()); }
/** * 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 * @since 3.2 */ public boolean replaceIn(final StringBuilder source) { if (source == null) { return false; } return replaceIn(source, 0, source.length()); }
/** * 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 * @since 3.2 */ public boolean replaceIn(final StringBuilder source) { if (source == null) { return false; } return replaceIn(source, 0, source.length()); }
@Override public boolean replaceAll(Map<String, Object> dictionary) { int replacedCount = 0; for(String key : dictionary.keySet()) { final Object val = dictionary.get(key); if (val instanceof String) { StringBuilder sb = Support.acquireStringBuilder((String)val); if (substitutor.replaceIn(sb)) { replacedCount++; dictionary.put(key, sb.toString()); } } } return replacedCount > 0; }
@Override public boolean replaceAll(Dictionary<String, Object> dictionary) { int replacedCount = 0; Enumeration<String> keys = dictionary.keys(); while(keys.hasMoreElements()) { final String key = keys.nextElement(); final Object val = dictionary.get(key); if (val instanceof String) { StringBuilder sb = Support.acquireStringBuilder((String)val); if (substitutor.replaceIn(sb)) { replacedCount++; dictionary.put(key, sb.toString()); } } } return replacedCount > 0; }