Java Map Substitute substitute(String aString, Map variablesValues, String open, String close)

Here you can find the source of substitute(String aString, Map variablesValues, String open, String close)

Description

Substitutes variables in aString.

License

Open Source License

Parameter

Parameter Description
aString the string on which to apply the substitution.
variablesValues a map containing the variable values. The keys are the variable names, the values are the variable values.
open the open delimiter for variables.
close the close delimiter for variables.

Return

the string with the substitution applied.

Declaration

public static String substitute(String aString, Map<String, String> variablesValues, String open,
        String close) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Map;

public class Main {
    /**// w ww.j  a v a 2  s  .  c  o m
     * Substitutes variables in <code>aString</code>. Variable names are
     * delimited by open and close strings. The values are retrieved from the
     * given map.
     * 
     * @param aString
     *            the string on which to apply the substitution.
     * @param variablesValues
     *            a map containing the variable values. The keys are the
     *            variable names, the values are the variable values.
     * @param open
     *            the open delimiter for variables.
     * @param close
     *            the close delimiter for variables.
     * @return the string with the substitution applied.
     */
    public static String substitute(String aString, Map<String, String> variablesValues, String open,
            String close) {
        return substitute(aString, variablesValues, open, close, 0);
    }

    /**
     * Substitutes variables in <code>aString</code>. Variable names are
     * delimited by open and close strings. The values are retrieved from the
     * given map.
     * 
     * @param aString
     *            the string on which to apply the substitution.
     * @param variablesValues
     *            a map containg the variable values. The keys are the variable
     *            names, the values are the variable values.
     * @param open
     *            the open delimiter for variables.
     * @param close
     *            the close delimiter for variables.
     * @param recursion
     *            the number of recursion (internal counter to avoid endless
     *            loops)
     * @return the string with the substitution applied.
     */
    public static String substitute(String aString, Map<String, String> variablesValues, String open, String close,
            int recursion) {
        if (aString == null)
            return null;

        StringBuffer buffer = new StringBuffer();

        String rest = aString;

        // search for opening string
        int i = rest.indexOf(open);
        while (i > -1) {
            int j = rest.indexOf(close, i + open.length());
            // search for closing string
            if (j > -1) {
                String varName = rest.substring(i + open.length(), j);
                Object value = variablesValues.get(varName);
                if (value == null) {
                    value = open + varName + close;
                } else {
                    // check for another variable inside this value
                    int another = ((String) value).indexOf(open); // check
                    // here
                    // first for
                    // speed
                    if (another > -1) {
                        if (recursion > 50) // for safety: avoid recursive
                        // endless loops with stack overflow
                        {
                            throw new RuntimeException(
                                    "Endless loop detected for substitution of variable: " + (String) value);
                        }
                        value = substitute((String) value, variablesValues, open, close, ++recursion);
                    }
                }
                buffer.append(rest.substring(0, i));
                buffer.append(value);
                rest = rest.substring(j + close.length());
            } else {
                // no closing tag found; end the search
                buffer.append(rest);
                rest = "";
            }
            // keep searching
            i = rest.indexOf(open);
        }
        buffer.append(rest);
        return buffer.toString();
    }
}

Related

  1. subsituteAttr(Map attrMap, String text)
  2. substitute(String page, String payRef, String name, String emailAddress, String when, String license, Map map)
  3. substitute(String s, Map map)
  4. substitute(String s, Map map)
  5. substitute(String str, Map subs)