Example usage for org.apache.commons.lang3.text StrSubstitutor replace

List of usage examples for org.apache.commons.lang3.text StrSubstitutor replace

Introduction

In this page you can find the example usage for org.apache.commons.lang3.text StrSubstitutor replace.

Prototype

public static <V> String replace(final Object source, final Map<String, V> valueMap, final String prefix,
        final String suffix) 

Source Link

Document

Replaces all the occurrences of variables in the given source object with their matching values from the map.

Usage

From source file:com.etsy.arbiter.util.NamedArgumentInterpolator.java

/**
 * Performs variable interpolation using the named arguments from an Action
 * This will create a new map if any interpolation is performed
 *
 * @param input The positional arguments possibly containing keys to be interpolated
 * @param namedArgs The key/value pairs used for interpolation
 * @param defaultArgs Default values for the named args, used if an interpolation key has no value given
 * @param listArgs The key/value list pairs used for list interpolation. List interpolation allows for interpolating a list of values in place of a single key
 *                 If any interpolation is performed this map is modified to remove the key that was interpolated
 *
 * @return A copy of input with variable interpolation performed
 *//*from   www  . j  a  v  a2  s .c o  m*/
public static Map<String, List<String>> interpolate(Map<String, List<String>> input,
        final Map<String, String> namedArgs, final Map<String, String> defaultArgs,
        final Map<String, List<String>> listArgs) {
    if (namedArgs == null || input == null) {
        return input;
    }

    final Map<String, String> interpolationArgs = createFinalInterpolationMap(namedArgs, defaultArgs);

    return Maps.transformValues(input, new Function<List<String>, List<String>>() {
        @Override
        public List<String> apply(List<String> input) {
            List<String> result = new ArrayList<>(input.size());
            for (String s : input) {
                String interpolated = StrSubstitutor.replace(s, interpolationArgs, PREFIX, SUFFIX);
                String listInterpolationKey = interpolated.replace(PREFIX, "").replace(SUFFIX, ""); // Strip out the prefix/suffix to get the actual key

                // If we have a standalone key we can use it for list interpolation
                // We only support standalone entries as it does not make sense to interpolate a list as part of a string
                if (listArgs != null && listArgs.containsKey(listInterpolationKey)) {
                    result.addAll(listArgs.get(listInterpolationKey));
                    listArgs.remove(listInterpolationKey);
                } else {
                    result.add(interpolated);
                }
            }

            return result;
        }
    });
}

From source file:com.buildria.mocking.builder.action.ActionSpec.java

protected void resolvePath(String name, String value) {
    Map<String, String> map = new HashMap<>();
    map.put(name, value);/*from  ww  w . j  av  a  2  s.  c  om*/
    this.path = StrSubstitutor.replace(path, map, "{", "}");
}

From source file:com.buildria.mocking.builder.rule.MethodRuleSpec.java

public MethodRuleSpec withPathParam(String name, Object value) {
    Map<String, String> map = new HashMap<>();
    map.put(name, String.valueOf(value));
    this.path = StrSubstitutor.replace(path, map, "{", "}");
    return this;
}

From source file:info.mikaelsvensson.devtools.analysis.shared.AbstractAnalyzer.java

public static String getFormattedString(String pattern, File patternArgumentSourceFile) {
    // LinkedHashMap since the values should be returned in the order of insertion (for backwards compatibility)
    final LinkedHashMap<String, Object> values = new LinkedHashMap<String, Object>();
    values.put("logFileName", patternArgumentSourceFile.getName());
    values.put("logFileNameWithoutExt",
            StringUtils.substringBeforeLast(patternArgumentSourceFile.getName(), "."));
    values.put("logFilePath",
            StringUtils.substringBeforeLast(patternArgumentSourceFile.getAbsolutePath(), "."));
    values.put("logFilePathWithoutExt",
            StringUtils.substringBeforeLast(patternArgumentSourceFile.getAbsolutePath(), "."));
    values.put("parentName", patternArgumentSourceFile.getParentFile().getName());
    values.put("parentPath", patternArgumentSourceFile.getParentFile().getAbsolutePath());

    // Add numeric "key" for each "parameter" (for backwards compability)
    int i = 0;/*from  w  ww  . ja va  2  s  . com*/
    for (Object value : values.values().toArray()) {
        values.put(String.valueOf(i++), value);
    }

    // Use {key} format instead of ${key}, thus providing backwards compatibility with previously used MessageFormat.
    return StrSubstitutor.replace(pattern, values, "{", "}");
}

From source file:edu.nps.moves.mmowgli.db.Pages.java

public static String replaceTokens(String source, String gameUrl, String uname, String gameAcronym,
        String gameHandle, String dateTime, String gameName, String confirmLink, String troubleLink,
        String troubleMailto, String portalLink, String how2Link, String apTitle) {
    HashMap<String, String> hm = new HashMap<String, String>();
    hm.put(gmurlT, gameUrl);// www. j ava2 s.  co  m
    hm.put(unameT, uname);
    hm.put(acronT, gameAcronym);
    hm.put(handlT, gameHandle);
    hm.put(dtimeT, dateTime);
    hm.put(gnameT, gameName);
    hm.put(troubT, troubleLink);
    hm.put(tmailT, troubleMailto);
    hm.put(portlT, portalLink);
    hm.put(cnfrmT, confirmLink);
    hm.put(howToT, how2Link);
    hm.put(apTtlT, apTitle);
    return StrSubstitutor.replace(source, hm, prefix, suffix);
}

From source file:edu.nps.moves.mmowgli.db.Pages.java

public static String replaceTokens(String source, PagesData data) {
    return StrSubstitutor.replace(source, data.map, prefix, suffix);
}

From source file:com.etsy.arbiter.util.NamedArgumentInterpolator.java

/**
 * Performs variable interpolation using the named arguments from an Action on a single String
 *
 * @param input The string possibly containing keys to be interpolated
 * @param namedArgs The key/value pairs used for interpolation
 * @param defaultArgs Default values for the named args, used if an interpolation key has no value given
 *
 * @return A copy of input with variable interpolation performed
 *//*from  w w  w. ja  v  a  2  s .co m*/
public static String interpolate(String input, final Map<String, String> namedArgs,
        final Map<String, String> defaultArgs) {
    if (namedArgs == null || input == null) {
        return input;
    }

    final Map<String, String> interpolationArgs = createFinalInterpolationMap(namedArgs, defaultArgs);

    return StrSubstitutor.replace(input, interpolationArgs, PREFIX, SUFFIX);
}

From source file:com.google.mr4c.sources.ConfiguredDiffSource.java

private DatasetSource createOutputSource(DatasetConfig config, DiffOutput output) throws IOException {
    Map<String, String> props = new HashMap<String, String>();
    props.put(m_diffConfig.getDiffParam(), output.toString());
    ConfigSerializer ser = SerializerFactories.getSerializerFactory("application/json")
            .createConfigSerializer(); // assume json config for now
    ser = new ParameterizedConfigSerializer(ser);
    StringWriter sw = new StringWriter();
    ser.serializeDatasetConfig(config, sw);
    String json = StrSubstitutor.replace(sw.toString(), props, "!(", ")");
    Reader reader = new StringReader(json);
    config = ser.deserializeDatasetConfig(reader);
    return DatasetSources.getDatasetSource(config);
}