Java Map Replace replaceVars(String s, Map m)

Here you can find the source of replaceVars(String s, Map m)

Description

Simple utility for replacing variables of the form "{key}" with values in the specified map.

License

Apache License

Parameter

Parameter Description
s The string containing variables to replace.
m The map containing the variable values.

Return

The new string with variables replaced, or the original string if it didn't have variables in it.

Declaration

public static String replaceVars(String s, Map<String, Object> m) 

Method Source Code

//package com.java2s;
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *

import java.util.*;

public class Main {
    /**/*w  w  w  . j  a v a 2s. c  om*/
     * Simple utility for replacing variables of the form <js>"{key}"</js> with values
     *    in the specified map.
     * <p>
     * Nested variables are supported in both the input string and map values.
     * <p>
     * If the map does not contain the specified value, the variable is not replaced.
     * <p>
     * <jk>null</jk> values in the map are treated as blank strings.
     *
     * @param s The string containing variables to replace.
     * @param m The map containing the variable values.
     * @return The new string with variables replaced, or the original string if it didn't have variables in it.
     */
    public static String replaceVars(String s, Map<String, Object> m) {

        if (s.indexOf('{') == -1)
            return s;

        int S1 = 1; // Not in variable, looking for {
        int S2 = 2; // Found {, Looking for }

        int state = S1;
        boolean hasInternalVar = false;
        int x = 0;
        int depth = 0;
        int length = s.length();
        StringBuilder out = new StringBuilder();
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (state == S1) {
                if (c == '{') {
                    state = S2;
                    x = i;
                } else {
                    out.append(c);
                }
            } else /* state == S2 */ {
                if (c == '{') {
                    depth++;
                    hasInternalVar = true;
                } else if (c == '}') {
                    if (depth > 0) {
                        depth--;
                    } else {
                        String key = s.substring(x + 1, i);
                        key = (hasInternalVar ? replaceVars(key, m) : key);
                        hasInternalVar = false;
                        if (!m.containsKey(key))
                            out.append('{').append(key).append('}');
                        else {
                            Object val = m.get(key);
                            if (val == null)
                                val = "";
                            String v = val.toString();
                            // If the replacement also contains variables, replace them now.
                            if (v.indexOf('{') != -1)
                                v = replaceVars(v, m);
                            out.append(v);
                        }
                        state = 1;
                    }
                }
            }
        }
        return out.toString();
    }

    /**
     * Returns the character at the specified index in the string without throwing exceptions.
     *
     * @param s The string.
     * @param i The index position.
     * @return The character at the specified index, or <code>0</code> if the index is out-of-range or the string
     *    is <jk>null</jk>.
     */
    public static char charAt(String s, int i) {
        if (s == null)
            return 0;
        if (i < 0 || i >= s.length())
            return 0;
        return s.charAt(i);
    }

    /**
     * Calls {@link #toString()} on the specified object if it's not null.
     *
     * @param o The object to convert to a string.
     * @return The object converted to a string, or <jk>null</jk> if the object was null.
     */
    public static String toString(Object o) {
        return (o == null ? null : o.toString());
    }
}

Related

  1. replaceValues(String s, String begin, String end, Map values)
  2. replaceValuesToSynonyms(Map dic, Map synonims)
  3. replaceVariable(final String src, final Map value)
  4. replaceVariables(String chart, Map variables)
  5. replaceVars(String origString, Map vars)
  6. stringReplace(Map map, String line)
  7. unescape(String cooked, char escapeChar, Map replacements)
  8. zipToMap(String[] searchStrings, String[] replacements)