Java Map Replace replaceGlobalTokensFromMap(Map dataMap, String message)

Here you can find the source of replaceGlobalTokensFromMap(Map dataMap, String message)

Description

Replace global tokens with their map correspondents.

License

Creative Commons License

Parameter

Parameter Description
dataMap - A map with keys that match the token names, such as "lna.firstName"
message - The message which requires data substitution

Return

- A String after parameter substitution has taken place. However, if any/some substitutions did not took place, then the tokens will be returned with pound signs around them instead of '%'

Declaration

public static String replaceGlobalTokensFromMap(Map<String, Object> dataMap, String message) 

Method Source Code

//package com.java2s;
/**/*w  ww .j  a  v a2s.c  om*/
 * StringHelper is a class with static methods to manipulate String objects 
 * 
 * This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 United States License. To 
 * view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/us/ or send a letter to Creative 
 * Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
 *
 * @package edu.psu.iam.cpr.ip.ui.action 
 * @author $Author: jal55 $
 * @version $Rev: 5888 $
 * @lastrevision $Date: 2012-12-11 21:14:04 -0500 (Tue, 11 Dec 2012) $
 */

import java.util.Map;

public class Main {
    /**
     * Replace global tokens with their map correspondents. 
     * 
     * @param dataMap - A map with keys that match the token names, such as "lna.firstName"
     * @param message - The message which requires data substitution
     * @return        - A String after parameter substitution has taken place.
     *                  However, if any/some substitutions did not took place, then the tokens will be returned 
     *                  with pound signs around them instead of '%'
     */
    public static String replaceGlobalTokensFromMap(Map<String, Object> dataMap, String message) {
        String result = message;
        int beginDelim = 0;
        while ((beginDelim = result.indexOf("%gbl.")) >= 0) {
            int endDelim = result.indexOf("%", beginDelim + 1);
            String varName = result.substring(beginDelim + 1, endDelim);

            if (dataMap.containsKey(varName)) {
                result = result.replaceAll(result.substring(beginDelim, endDelim + 1),
                        (String) dataMap.get(varName));
            } else {
                result = result.replaceAll(result.substring(beginDelim, endDelim + 1), "#" + varName + "#");
            }

        }

        return result.toString();
    }
}

Related

  1. replaceCharactersInArray(char[] characters, Map replacements)
  2. replaceElements(Map destinationMap, Map sourceMap)
  3. replaceEntities(final Map replacements, final String xml)
  4. replaceFromMap(final String string, final Map replacements)
  5. replaceFromMap(String string, Map replacements)
  6. replaceInvalid(String uri, Map reps)
  7. replaceKey(final Map map, final TKey oldkey, final TKey newkey)
  8. replaceKey(Map map, K oldKey, K newKey)
  9. replaceKeyCharacter(Map map, char oldChar, char newChar)