Here you can find the source of replaceGlobalTokensFromMap(Map
Parameter | Description |
---|---|
dataMap | - A map with keys that match the token names, such as "lna.firstName" |
message | - The message which requires data substitution |
public static String replaceGlobalTokensFromMap(Map<String, Object> dataMap, String message)
//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(); } }