AbstractMessageSource.java
001 /*
002  * Copyright 2010-2013 the original author or authors.
003  *
004  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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  */
016 
017 package org.codehaus.griffon.runtime.core.i18n;
018 
019 import griffon.core.i18n.MessageSource;
020 import griffon.core.i18n.NoSuchMessageException;
021 
022 import java.text.MessageFormat;
023 import java.util.List;
024 import java.util.Locale;
025 import java.util.Map;
026 import java.util.MissingResourceException;
027 
028 /**
029  @author Andres Almiray
030  @author Alexander Klein
031  @since 1.1.0
032  */
033 public abstract class AbstractMessageSource implements MessageSource {
034     protected static final Object[] EMPTY_OBJECT_ARGS = new Object[0];
035 
036     public String getMessage(String keythrows NoSuchMessageException {
037         return getMessage(key, EMPTY_OBJECT_ARGS, Locale.getDefault());
038     }
039 
040     public String getMessage(String key, Locale localethrows NoSuchMessageException {
041         return getMessage(key, EMPTY_OBJECT_ARGS, locale);
042     }
043 
044     public String getMessage(String key, Object[] argsthrows NoSuchMessageException {
045         return getMessage(key, args, Locale.getDefault());
046     }
047 
048     public String getMessage(String key, List argsthrows NoSuchMessageException {
049         return getMessage(key, toObjectArray(args), Locale.getDefault());
050     }
051 
052     public String getMessage(String key, List args, Locale localethrows NoSuchMessageException {
053         return getMessage(key, toObjectArray(args), locale);
054     }
055 
056     public String getMessage(String key, String defaultMessage) {
057         return getMessage(key, EMPTY_OBJECT_ARGS, defaultMessage, Locale.getDefault());
058     }
059 
060     public String getMessage(String key, String defaultMessage, Locale locale) {
061         return getMessage(key, EMPTY_OBJECT_ARGS, defaultMessage, locale);
062     }
063 
064     public String getMessage(String key, Object[] args, String defaultMessage) {
065         return getMessage(key, args, defaultMessage, Locale.getDefault());
066     }
067 
068     public String getMessage(String key, Object[] args, String defaultMessage, Locale locale) {
069         try {
070             return getMessage(key, args, locale);
071         catch (NoSuchMessageException nsme) {
072             return null == defaultMessage ? key : defaultMessage;
073         }
074     }
075 
076     public String getMessage(String key, Map<String, Object> argsthrows NoSuchMessageException {
077         return getMessage(key, args, Locale.getDefault());
078     }
079 
080     public String getMessage(String key, Map<String, Object> args, String defaultMessage) {
081         return getMessage(key, args, defaultMessage, Locale.getDefault());
082     }
083 
084     public String getMessage(String key, Map<String, Object> args, String defaultMessage, Locale locale) {
085         try {
086             return getMessage(key, args, locale);
087         catch (NoSuchMessageException nsme) {
088             return null == defaultMessage ? key : defaultMessage;
089         }
090     }
091 
092     public String getMessage(String key, Map<String, Object> args, Locale localethrows NoSuchMessageException {
093         String message = resolveMessage(key, locale);
094         return formatMessage(message, args);
095     }
096 
097     public String getMessage(String key, List args, String defaultMessage) {
098         return getMessage(key, toObjectArray(args), defaultMessage, Locale.getDefault());
099     }
100 
101     public String getMessage(String key, List args, String defaultMessage, Locale locale) {
102         return getMessage(key, toObjectArray(args), defaultMessage, locale);
103     }
104 
105     public String getMessage(String key, Object[] args, Locale localethrows NoSuchMessageException {
106         if (null == argsargs = EMPTY_OBJECT_ARGS;
107         if (null == localelocale = Locale.getDefault();
108         try {
109             String message = resolveMessage(key, locale);
110             return formatMessage(message, args);
111         catch (MissingResourceException e) {
112             throw new NoSuchMessageException(key, locale);
113         }
114     }
115 
116     protected abstract String resolveMessage(String key, Locale localethrows NoSuchMessageException;
117 
118     protected String formatMessage(String message, Object[] args) {
119         return MessageFormat.format(message, args);
120     }
121 
122     protected String formatMessage(String message, Map<String, Object> args) {
123         for (Map.Entry<String, Object> variable : args.entrySet()) {
124             String var = variable.getKey();
125             String value = variable.getValue() != null ? variable.getValue().toString() null;
126             if (value != nullmessage = message.replace("{:" + var + "}", value);
127         }
128         return message;
129     }
130 
131     protected Object[] toObjectArray(List args) {
132         if (null == args || args.isEmpty()) {
133             return EMPTY_OBJECT_ARGS;
134         }
135         return args.toArray(new Object[args.size()]);
136     }
137 }