View Javadoc

1   /*
2    * Copyright (c) 2009 QOS.ch All rights reserved.
3    * 
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   * 
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   * 
14   * THE SOFTWARE IS PROVIDED "AS  IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  package ch.qos.cal10n.util;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.net.URL;
27  import java.net.URLConnection;
28  import java.util.Locale;
29  import java.util.ResourceBundle;
30  
31  public class PropertyResourceBundleFinder {
32  
33    public static ResourceBundle getBundle(ClassLoader classLoader, String baseName,
34        Locale locale)  {
35  
36      // same as the JDK convention
37      //
38      // It generates a path name from the candidate bundle name by replacing all "." 
39      // characters with "/" and appending the string ".properties".
40      /// see also   http: // tinyurl.com/ldgej8
41      baseName = baseName.replace('.', '/');
42  
43      String languageAndCountryCandidate = computeLanguageAndCountryCandidate(
44          baseName, locale);
45      String languageOnlyCandidate = computeLanguageOnlyCandidate(baseName,
46          locale);
47  
48      CAL10NPropertyResourceBundle cprbLanguageOnly = makePropertyResourceBundle(
49          classLoader, languageOnlyCandidate);
50      CAL10NPropertyResourceBundle cprbLanguageAndCountry = null;
51  
52      if (languageAndCountryCandidate != null) {
53        cprbLanguageAndCountry = makePropertyResourceBundle(classLoader,
54            languageAndCountryCandidate);
55      }
56  
57      if (cprbLanguageAndCountry != null) {
58        cprbLanguageAndCountry.setParent(cprbLanguageOnly);
59        return cprbLanguageAndCountry;
60      }
61      return cprbLanguageOnly;
62    }
63  
64    private static CAL10NPropertyResourceBundle makePropertyResourceBundle(
65        ClassLoader classLoader, String resourceCandiate) {
66  
67      CAL10NPropertyResourceBundle prb = null;
68  
69      URL url = classLoader.getResource(resourceCandiate);
70      if (url != null) {
71        try {
72          InputStream in = openConnectionForUrl(url);
73          prb = new CAL10NPropertyResourceBundle(in);
74          in.close();
75        } catch (IOException e) {
76        }
77      }
78      return prb;
79    }
80  
81    private static String computeLanguageAndCountryCandidate(String baseName,
82        Locale locale) {
83      String language = locale.getLanguage();
84      String country = locale.getCountry();
85      if (country != null && country.length() > 0) {
86        return baseName + "_" + language + "_" + country + ".properties";
87      } else {
88        return null;
89      }
90    }
91  
92    private static String computeLanguageOnlyCandidate(String baseName, Locale locale) {
93      String language = locale.getLanguage();
94      return baseName + "_" + language + ".properties";
95    }
96  
97    private static InputStream openConnectionForUrl(URL url) throws IOException {
98      URLConnection urlConnection = url.openConnection();
99      urlConnection.setDefaultUseCaches(false);
100     InputStream in = urlConnection.getInputStream();
101     return in;
102   }
103 }