CompositeMessageSource.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.util.List;
023 import java.util.Locale;
024 
025 /**
026  @author Andres Almiray
027  @since 1.1.0
028  */
029 public class CompositeMessageSource extends AbstractMessageSource {
030     private final MessageSource[] messageSources;
031 
032     public CompositeMessageSource(List<MessageSource> messageSources) {
033         this(toMessageSourceArray(messageSources));
034     }
035 
036     public CompositeMessageSource(MessageSource[] messageSources) {
037         this.messageSources = messageSources;
038     }
039 
040     private static MessageSource[] toMessageSourceArray(List<MessageSource> messageSources) {
041         if (null == messageSources || messageSources.isEmpty()) {
042             return new MessageSource[0];
043         }
044         return messageSources.toArray(new MessageSource[messageSources.size()]);
045     }
046 
047     public String getMessage(String key, String defaultMessage) {
048         return getMessageInternal(key, EMPTY_OBJECT_ARGS, defaultMessage, Locale.getDefault());
049     }
050 
051     public String getMessage(String key, String defaultMessage, Locale locale) {
052         return getMessageInternal(key, EMPTY_OBJECT_ARGS, defaultMessage, locale);
053     }
054 
055     public String getMessage(String key, Object[] args, String defaultMessage) {
056         return getMessageInternal(key, args, defaultMessage, Locale.getDefault());
057     }
058 
059     public String getMessage(String key, Object[] args, String defaultMessage, Locale locale) {
060         return getMessageInternal(key, args, defaultMessage, locale);
061     }
062 
063     public String getMessage(String key, List args, String defaultMessage) {
064         return getMessageInternal(key, toObjectArray(args), defaultMessage, Locale.getDefault());
065     }
066 
067     public String getMessage(String key, List args, String defaultMessage, Locale locale) {
068         return getMessageInternal(key, toObjectArray(args), defaultMessage, locale);
069     }
070 
071     public String resolveMessage(String key, Locale locale) {
072         if (null == localelocale = Locale.getDefault();
073         for (MessageSource messageSource : messageSources) {
074             try {
075                 if (messageSource instanceof AbstractMessageSource) {
076                     return ((AbstractMessageSourcemessageSource).resolveMessage(key, locale);
077                 }
078                 return messageSource.getMessage(key, locale);
079             catch (NoSuchMessageException nsme) {
080                 // ignore
081             }
082         }
083         throw new NoSuchMessageException(key, locale);
084     }
085 
086     private String getMessageInternal(String key, Object[] args, Locale localethrows NoSuchMessageException {
087         if (null == localelocale = Locale.getDefault();
088         for (MessageSource messageSource : messageSources) {
089             try {
090                 return messageSource.getMessage(key, args, locale);
091             catch (NoSuchMessageException nsme) {
092                 // ignore
093             }
094         }
095         throw new NoSuchMessageException(key, locale);
096     }
097 
098     private String getMessageInternal(String key, Object[] args, String defaultMessage, Locale locale) {
099         try {
100             return getMessageInternal(key, args, locale);
101         catch (NoSuchMessageException nsme) {
102             // ignore
103         }
104         return null == defaultMessage ? key : defaultMessage;
105     }
106 }