001/**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.util;
017
018import java.util.HashSet;
019import java.util.Map;
020import java.util.Set;
021
022public class MessageUtils {
023    /**
024     * Interpolates a message based on values contained in the data map, assuming message is formatted using a ${key} syntax.
025     *
026     * @param message
027     *            the message to be interpolated
028     * @param data
029     *            Map<String, String> containing the data to be used for interpolation
030     * @return the interpolated message
031     */
032    public static String interpolate(String message, String... data) {
033        if (message != null) {
034            for (int i = 0; i < data.length; i++) {
035                message = message.replaceAll("\\$\\{" + i + "\\}", "" + escape(data[i]));
036            }
037        }
038        return message;
039    }
040
041    /**
042     * Interpolates a message based on parameter index, assuming message is formatted using a ${0}..${n} syntax
043     *
044     * @param message
045     *            the message to be interpolated
046     * @param data
047     *            varargs to be used for interpolation
048     * @return the interpolated message
049     */
050    public static String interpolate(String message, Map<String, Object> data) {
051        if (message != null && data != null) {
052            Set<String> fields = findFields(message);
053            for (String s : fields) {
054                if(data.get(s) != null){
055                        message = message.replaceAll("\\$\\{" + s + "\\}", "" + escape(data.get(s).toString()));
056                }
057            }
058        }
059        return message;
060    }
061
062    /**
063     * Interpolates a message which requires only a single property replacement.
064     *
065     * @param message
066     * @param parameter
067     * @param value
068     * @return the interpolated message
069     */
070    public static String interpolate(String message, String parameter, Object value){
071        message = message.replaceAll("\\$\\{" + parameter + "\\}", "" + escape(value.toString()));
072        return message;
073    }
074
075    /**
076     * Adds an escape to all characters requiring an escape.
077     *
078     * @param input
079     * @return the input string with characters escaped.
080     */
081    private static String escape(String input) {
082        char[] toEscape = {'\\', '$', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}'};
083        for (char c : toEscape) {
084            input = input.replaceAll("\\" + c, "\\\\\\" + c);
085        }
086        return input;
087    }
088
089    /**
090     * Returns a Set<String> of all interpolation targets (fields) within a String.
091     *
092     * @param input
093     *            the String from which to extract the interpolation targets
094     * @return Set<String> containing the field names of the interpolation targets
095     */
096    public static Set<String> findFields(String input) {
097        Set<String> result = new HashSet<String>();
098        int begin = input.indexOf("${");
099        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}