1 /** 2 * Copyright 2005-2013 The Kuali Foundation 3 * 4 * Licensed under the Educational Community License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.opensource.org/licenses/ecl2.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.kuali.rice.krad.util; 17 18 import java.util.HashSet; 19 import java.util.Map; 20 import java.util.Set; 21 22 public class MessageUtils { 23 /** 24 * Interpolates a message based on values contained in the data map, assuming message is formatted using a ${key} syntax. 25 * 26 * @param message 27 * the message to be interpolated 28 * @param data 29 * Map<String, String> containing the data to be used for interpolation 30 * @return the interpolated message 31 */ 32 public static String interpolate(String message, String... data) { 33 if (message != null) { 34 for (int i = 0; i < data.length; i++) { 35 message = message.replaceAll("\\$\\{" + i + "\\}", "" + escape(data[i])); 36 } 37 } 38 return message; 39 } 40 41 /** 42 * Interpolates a message based on parameter index, assuming message is formatted using a ${0}..${n} syntax 43 * 44 * @param message 45 * the message to be interpolated 46 * @param data 47 * varargs to be used for interpolation 48 * @return the interpolated message 49 */ 50 public static String interpolate(String message, Map<String, Object> data) { 51 if (message != null && data != null) { 52 Set<String> fields = findFields(message); 53 for (String s : fields) { 54 if(data.get(s) != null){ 55 message = message.replaceAll("\\$\\{" + s + "\\}", "" + escape(data.get(s).toString())); 56 } 57 } 58 } 59 return message; 60 } 61 62 /** 63 * Interpolates a message which requires only a single property replacement. 64 * 65 * @param message 66 * @param parameter 67 * @param value 68 * @return the interpolated message 69 */ 70 public static String interpolate(String message, String parameter, Object value){ 71 message = message.replaceAll("\\$\\{" + parameter + "\\}", "" + escape(value.toString())); 72 return message; 73 } 74 75 /** 76 * Adds an escape to all characters requiring an escape. 77 * 78 * @param input 79 * @return the input string with characters escaped. 80 */ 81 private static String escape(String input) { 82 char[] toEscape = {'\\', '$', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}'}; 83 for (char c : toEscape) { 84 input = input.replaceAll("\\" + c, "\\\\\\" + c); 85 } 86 return input; 87 } 88 89 /** 90 * Returns a Set<String> of all interpolation targets (fields) within a String. 91 * 92 * @param input 93 * the String from which to extract the interpolation targets 94 * @return Set<String> containing the field names of the interpolation targets 95 */ 96 public static Set<String> findFields(String input) { 97 Set<String> result = new HashSet<String>(); 98 int begin = input.indexOf("${"); 99 while (begin != -1) { 100 int end = input.indexOf("}", begin); 101 result.add(input.substring(begin + 2, end)); 102 begin = input.indexOf("${", end); 103 } 104 return result; 105 } 106 107 }