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