View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.internal;
14  
15  import java.util.Map;
16  import java.util.Map.Entry;
17  
18  import net.sf.oval.Validator;
19  import net.sf.oval.internal.util.StringUtils;
20  import net.sf.oval.localization.value.MessageValueFormatter;
21  
22  /**
23   * @author Sebastian Thomschke
24   *
25   */
26  public final class MessageRenderer
27  {
28  	public static String renderMessage(final String messageKey, final Map<String, ? > messageValues)
29  	{
30  		String message = Validator.getMessageResolver().getMessage(messageKey);
31  		if (message == null) message = messageKey;
32  
33  		final MessageValueFormatter formatter = Validator.getMessageValueFormatter();
34  
35  		// if there are no place holders in the message simply return it
36  		if (message.indexOf('{') == -1) return message;
37  
38  		if (messageValues != null && messageValues.size() > 0)
39  			for (final Entry<String, ? > entry : messageValues.entrySet())
40  				message = StringUtils.replaceAll(message, "{" + entry.getKey() + "}",
41  						formatter.format(entry.getValue()));
42  		return message;
43  	}
44  
45  	public static String renderMessage(final String messageKey, final String messageValueName, final String messageValue)
46  	{
47  		String message = Validator.getMessageResolver().getMessage(messageKey);
48  		if (message == null) message = messageKey;
49  
50  		// if there are no place holders in the message simply return it
51  		if (message.indexOf('{') == -1) return message;
52  
53  		message = StringUtils.replaceAll(message, "{" + messageValueName + "}", messageValue);
54  
55  		return message;
56  	}
57  
58  	private MessageRenderer()
59  	{
60  		super();
61  	}
62  }