001    /*******************************************************************************
002     * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
003     * Thomschke.
004     * 
005     * All Rights Reserved. This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     * 
010     * Contributors:
011     *     Sebastian Thomschke - initial implementation.
012     *******************************************************************************/
013    package net.sf.oval.internal;
014    
015    import java.util.Map;
016    import java.util.Map.Entry;
017    
018    import net.sf.oval.Validator;
019    import net.sf.oval.internal.util.StringUtils;
020    import net.sf.oval.localization.value.MessageValueFormatter;
021    
022    /**
023     * @author Sebastian Thomschke
024     *
025     */
026    public final class MessageRenderer
027    {
028            public static String renderMessage(final String messageKey, final Map<String, ? > messageValues)
029            {
030                    String message = Validator.getMessageResolver().getMessage(messageKey);
031                    if (message == null) message = messageKey;
032    
033                    final MessageValueFormatter formatter = Validator.getMessageValueFormatter();
034    
035                    // if there are no place holders in the message simply return it
036                    if (message.indexOf('{') == -1) return message;
037    
038                    if (messageValues != null && messageValues.size() > 0)
039                            for (final Entry<String, ? > entry : messageValues.entrySet())
040                                    message = StringUtils.replaceAll(message, "{" + entry.getKey() + "}",
041                                                    formatter.format(entry.getValue()));
042                    return message;
043            }
044    
045            public static String renderMessage(final String messageKey, final String messageValueName, final String messageValue)
046            {
047                    String message = Validator.getMessageResolver().getMessage(messageKey);
048                    if (message == null) message = messageKey;
049    
050                    // if there are no place holders in the message simply return it
051                    if (message.indexOf('{') == -1) return message;
052    
053                    message = StringUtils.replaceAll(message, "{" + messageValueName + "}", messageValue);
054    
055                    return message;
056            }
057    
058            private MessageRenderer()
059            {
060                    super();
061            }
062    }