AbstractCompositeResourceBundleBuilder.java
001 /*
002  * Copyright 2008-2014 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 package org.codehaus.griffon.runtime.util;
017 
018 import griffon.core.resources.ResourceHandler;
019 import griffon.util.CompositeResourceBundle;
020 import griffon.util.CompositeResourceBundleBuilder;
021 
022 import javax.annotation.Nonnull;
023 import javax.annotation.Nullable;
024 import javax.inject.Inject;
025 import java.net.URL;
026 import java.util.*;
027 
028 import static griffon.util.GriffonNameUtils.requireNonBlank;
029 import static java.util.Objects.requireNonNull;
030 
031 /**
032  @author Andres Almiray
033  @since 2.0.0
034  */
035 public abstract class AbstractCompositeResourceBundleBuilder implements CompositeResourceBundleBuilder {
036     protected static final String ERROR_FILENAME_BLANK = "Argument 'fileName' must not be blank";
037     protected static final String ERROR_SUFFIX_BLANK = "Argument 'suffix' must not be blank";
038     protected static final String ERROR_RESOURCE_HANDLER_NULL = "Argument 'resourceHandler' must not be null";
039     protected static final String ERROR_BASENAME_BLANK = "Argument 'basename' must not be blank";
040     protected static final String ERROR_LOCALE_NULL = "Argument 'locale' must not be null";
041 
042     protected final ResourceHandler resourceHandler;
043 
044     @Inject
045     public AbstractCompositeResourceBundleBuilder(@Nonnull ResourceHandler resourceHandler) {
046         this.resourceHandler = requireNonNull(resourceHandler, ERROR_RESOURCE_HANDLER_NULL);
047     }
048 
049     @Override
050     @Nonnull
051     public ResourceBundle create(@Nonnull String basename) {
052         return create(basename, Locale.getDefault());
053     }
054 
055     @Override
056     @Nonnull
057     public ResourceBundle create(@Nonnull String basename, @Nonnull Locale locale) {
058         requireNonBlank(basename, ERROR_BASENAME_BLANK);
059         requireNonNull(locale, ERROR_LOCALE_NULL);
060 
061         String[] combinations = {
062             locale.getLanguage() "_" + locale.getCountry() "_" + locale.getVariant(),
063             locale.getLanguage() "_" + locale.getCountry(),
064             locale.getLanguage()
065         };
066 
067         basename = basename.replace('.''/');
068         List<ResourceBundle> bundles = new ArrayList<>();
069         for (String suffix : combinations) {
070             if (suffix.endsWith("_")) continue;
071             bundles.addAll(loadBundlesFor(basename + "_" + suffix));
072         }
073         bundles.addAll(loadBundlesFor(basename));
074         if (bundles.size() == 0) {
075             throw new IllegalArgumentException("There are no ResourceBundle resources matching " + basename);
076         }
077 
078         return new CompositeResourceBundle(bundles);
079     }
080 
081     @Nonnull
082     protected ResourceHandler getResourceHandler() {
083         return resourceHandler;
084     }
085 
086     @Nullable
087     protected URL getResourceAsURL(@Nonnull String fileName, @Nonnull String suffix) {
088         requireNonBlank(fileName, ERROR_FILENAME_BLANK);
089         requireNonBlank(suffix, ERROR_SUFFIX_BLANK);
090         return resourceHandler.getResourceAsURL(fileName + suffix);
091     }
092 
093     @Nullable
094     protected List<URL> getResources(@Nonnull String fileName, @Nonnull String suffix) {
095         requireNonBlank(fileName, ERROR_FILENAME_BLANK);
096         requireNonBlank(suffix, ERROR_SUFFIX_BLANK);
097         return resourceHandler.getResources(fileName + suffix);
098     }
099 
100     @Nonnull
101     protected abstract Collection<ResourceBundle> loadBundlesFor(@Nonnull String basename);
102 }