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