001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.common.i18n;
025    
026    import java.net.URL;
027    import java.util.Locale;
028    
029    /**
030     * Implementation of a {@link LocalizationRepository} that loads a properties file from the classpath of the supplied
031     * {@link ClassLoader class loader}.
032     * <p>
033     * This repository for a property file by building locations of the form "path/to/class_locale.properties", where "path/to/class"
034     * is created from the fully-qualified classname and all "." replaced with "/" characters, "locale" is the a variant of the locale
035     * (first the full locale, then subsequently with the last segment removed). As soon as a property file is found, its URL is
036     * returned immediately.
037     * </p>
038     * named with a name that matches
039     * @author Randall Hauch
040     */
041    public class ClasspathLocalizationRepository implements LocalizationRepository {
042    
043        private final ClassLoader classLoader;
044    
045        /**
046         * Create a repository using the current thread's {@link Thread#getContextClassLoader() context class loader} or, if that is
047         * null, the same class loader that loaded this class.
048         */
049        public ClasspathLocalizationRepository() {
050            this(null);
051        }
052    
053        /**
054         * Create a repository using the supplied class loader. Null may be passed if the class loader should be obtained from the
055         * current thread's {@link Thread#getContextClassLoader() context class loader} or, if that is null, the same class loader
056         * that loaded this class.
057         * @param classLoader the class loader to use; may be null
058         */
059        public ClasspathLocalizationRepository( ClassLoader classLoader ) {
060            if (classLoader == null) classLoader = Thread.currentThread().getContextClassLoader();
061            if (classLoader == null) classLoader = this.getClass().getClassLoader();
062            this.classLoader = classLoader;
063        }
064    
065        /**
066         * {@inheritDoc}
067         */
068        public URL getLocalizationBundle( String bundleName, Locale locale ) {
069            URL url = null;
070            String pathPfx = bundleName.replaceAll("\\.", "/");
071            String variant = '_' + locale.toString();
072            do {
073                url = this.classLoader.getResource(pathPfx + variant + ".properties");
074                if (url == null) {
075                    int ndx = variant.lastIndexOf('_');
076                    if (ndx < 0) {
077                        break;
078                    }
079                    variant = variant.substring(0, ndx);
080                }
081            } while (url == null);
082            return url;
083        }
084    
085    }