001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.resource.loader;
018
019import org.apache.wicket.Component;
020import org.apache.wicket.IInitializer;
021import org.apache.wicket.util.lang.Args;
022
023import java.util.List;
024import java.util.Locale;
025
026
027/**
028 * This is one of Wicket's default string resource loaders. It is designed to let wicket extension
029 * modules contribute default resource bundles for their components.
030 * <p>
031 * The initializer based string resource loader attempts to find the resource from a bundle that
032 * corresponds to the supplied wicket initializers.
033 * <p>
034 * This implementation is fully aware of both locale and style values when trying to obtain the
035 * appropriate resources.
036 * <p>
037 * 
038 * @author Bertrand Guay-Paquet
039 * @author Sven Meier
040 */
041public class InitializerStringResourceLoader extends ComponentStringResourceLoader
042{
043        private final List<IInitializer> initializers;
044
045        /**
046         * Create and initialize the resource loader.
047         * 
048         * @param initializers
049         *            initializers
050         */
051        public InitializerStringResourceLoader(List<IInitializer> initializers)
052        {
053                this.initializers = Args.notNull(initializers, "initializers");
054        }
055
056        @Override
057        public String loadStringResource(Class<?> clazz, final String key, final Locale locale,
058                final String style, final String variation)
059        {
060
061                for (IInitializer initializer : initializers)
062                {
063                        String string = super.loadStringResource(initializer.getClass(), key, locale, style,
064                                variation);
065                        if (string != null)
066                        {
067                                return string;
068                        }
069                }
070
071                // not found
072                return null;
073        }
074
075        @Override
076        public String loadStringResource(final Component component, final String key,
077                final Locale locale, final String style, final String variation)
078        {
079                return loadStringResource((Class<?>)null, key, locale, style, variation);
080        }
081}