5409 |
16 Sep 10 |
nicklas |
1 |
/** |
5409 |
16 Sep 10 |
nicklas |
$Id$ |
5409 |
16 Sep 10 |
nicklas |
3 |
|
5409 |
16 Sep 10 |
nicklas |
Copyright (C) 2010 Nicklas Nordborg |
5409 |
16 Sep 10 |
nicklas |
5 |
|
5409 |
16 Sep 10 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5409 |
16 Sep 10 |
nicklas |
Available at http://base.thep.lu.se/ |
5409 |
16 Sep 10 |
nicklas |
8 |
|
5409 |
16 Sep 10 |
nicklas |
BASE is free software; you can redistribute it and/or |
5409 |
16 Sep 10 |
nicklas |
modify it under the terms of the GNU General Public License |
5409 |
16 Sep 10 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5409 |
16 Sep 10 |
nicklas |
of the License, or (at your option) any later version. |
5409 |
16 Sep 10 |
nicklas |
13 |
|
5409 |
16 Sep 10 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5409 |
16 Sep 10 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5409 |
16 Sep 10 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5409 |
16 Sep 10 |
nicklas |
GNU General Public License for more details. |
5409 |
16 Sep 10 |
nicklas |
18 |
|
5409 |
16 Sep 10 |
nicklas |
You should have received a copy of the GNU General Public License |
5409 |
16 Sep 10 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5409 |
16 Sep 10 |
nicklas |
21 |
*/ |
5409 |
16 Sep 10 |
nicklas |
22 |
package net.sf.basedb.util.resources; |
5409 |
16 Sep 10 |
nicklas |
23 |
|
5409 |
16 Sep 10 |
nicklas |
24 |
import java.io.IOException; |
5409 |
16 Sep 10 |
nicklas |
25 |
import java.io.InputStream; |
5409 |
16 Sep 10 |
nicklas |
26 |
import java.io.InputStreamReader; |
7714 |
21 May 19 |
nicklas |
27 |
import java.nio.charset.StandardCharsets; |
5409 |
16 Sep 10 |
nicklas |
28 |
import java.util.List; |
5409 |
16 Sep 10 |
nicklas |
29 |
import java.util.Locale; |
5409 |
16 Sep 10 |
nicklas |
30 |
import java.util.PropertyResourceBundle; |
5409 |
16 Sep 10 |
nicklas |
31 |
import java.util.ResourceBundle; |
5409 |
16 Sep 10 |
nicklas |
32 |
|
5409 |
16 Sep 10 |
nicklas |
33 |
import net.sf.basedb.util.FileUtil; |
5409 |
16 Sep 10 |
nicklas |
34 |
|
5409 |
16 Sep 10 |
nicklas |
35 |
/** |
5409 |
16 Sep 10 |
nicklas |
ResourceBundle.Control implementation that open property files in UTF-8 format |
5409 |
16 Sep 10 |
nicklas |
instead of ISO-8859-1 format. Use {@link #INSTANCE} as the control parameter |
5409 |
16 Sep 10 |
nicklas |
when calling {@link ResourceBundle#getBundle(String, java.util.ResourceBundle.Control)}. |
5409 |
16 Sep 10 |
nicklas |
39 |
|
5409 |
16 Sep 10 |
nicklas |
@author Nicklas |
5409 |
16 Sep 10 |
nicklas |
@since 2.16 |
5409 |
16 Sep 10 |
nicklas |
@base.modified $Date$ |
5409 |
16 Sep 10 |
nicklas |
43 |
*/ |
5409 |
16 Sep 10 |
nicklas |
44 |
public class Utf8PropertiesResourceBundleControl |
5409 |
16 Sep 10 |
nicklas |
45 |
extends ResourceBundle.Control |
5409 |
16 Sep 10 |
nicklas |
46 |
{ |
5409 |
16 Sep 10 |
nicklas |
47 |
/** |
5409 |
16 Sep 10 |
nicklas |
The one and only instance. |
5409 |
16 Sep 10 |
nicklas |
49 |
*/ |
5409 |
16 Sep 10 |
nicklas |
50 |
public static final Utf8PropertiesResourceBundleControl INSTANCE = new Utf8PropertiesResourceBundleControl(); |
5409 |
16 Sep 10 |
nicklas |
51 |
|
5409 |
16 Sep 10 |
nicklas |
52 |
private Utf8PropertiesResourceBundleControl() |
5409 |
16 Sep 10 |
nicklas |
53 |
{} |
5409 |
16 Sep 10 |
nicklas |
54 |
|
5409 |
16 Sep 10 |
nicklas |
55 |
/* |
5409 |
16 Sep 10 |
nicklas |
From the ResourceBundle.Control class |
5409 |
16 Sep 10 |
nicklas |
57 |
------------------------------------- |
5409 |
16 Sep 10 |
nicklas |
58 |
*/ |
5409 |
16 Sep 10 |
nicklas |
59 |
/** |
5409 |
16 Sep 10 |
nicklas |
We don't need a fallback locale. |
5409 |
16 Sep 10 |
nicklas |
@return Always null |
5409 |
16 Sep 10 |
nicklas |
62 |
*/ |
5409 |
16 Sep 10 |
nicklas |
63 |
@Override |
5409 |
16 Sep 10 |
nicklas |
64 |
public Locale getFallbackLocale(String baseName, Locale locale) |
5409 |
16 Sep 10 |
nicklas |
65 |
{ |
5409 |
16 Sep 10 |
nicklas |
66 |
return null; |
5409 |
16 Sep 10 |
nicklas |
67 |
} |
5409 |
16 Sep 10 |
nicklas |
68 |
|
5409 |
16 Sep 10 |
nicklas |
69 |
/** |
5409 |
16 Sep 10 |
nicklas |
This control only handle property files. |
5823 |
24 Oct 11 |
nicklas |
@return Always {@link java.util.ResourceBundle.Control#FORMAT_PROPERTIES}. |
5409 |
16 Sep 10 |
nicklas |
72 |
*/ |
5409 |
16 Sep 10 |
nicklas |
73 |
@Override |
5409 |
16 Sep 10 |
nicklas |
74 |
public List<String> getFormats(String baseName) |
5409 |
16 Sep 10 |
nicklas |
75 |
{ |
5409 |
16 Sep 10 |
nicklas |
76 |
return FORMAT_PROPERTIES; |
5409 |
16 Sep 10 |
nicklas |
77 |
} |
5409 |
16 Sep 10 |
nicklas |
78 |
|
5409 |
16 Sep 10 |
nicklas |
79 |
/** |
5409 |
16 Sep 10 |
nicklas |
Load property files using UTF-8. |
5409 |
16 Sep 10 |
nicklas |
81 |
*/ |
5409 |
16 Sep 10 |
nicklas |
82 |
@Override |
5409 |
16 Sep 10 |
nicklas |
83 |
public ResourceBundle newBundle(String baseName, Locale locale, |
5409 |
16 Sep 10 |
nicklas |
84 |
String format, ClassLoader loader, boolean reload) |
5409 |
16 Sep 10 |
nicklas |
85 |
throws IllegalAccessException, InstantiationException, IOException |
5409 |
16 Sep 10 |
nicklas |
86 |
{ |
5409 |
16 Sep 10 |
nicklas |
// Let the superclass handle non-property bundles, but this |
5409 |
16 Sep 10 |
nicklas |
// *should* never happen since getFormats() only return "java.properties" |
5409 |
16 Sep 10 |
nicklas |
89 |
if (!format.equals("java.properties")) |
5409 |
16 Sep 10 |
nicklas |
90 |
{ |
5409 |
16 Sep 10 |
nicklas |
91 |
return super.newBundle(baseName, locale, format, loader, reload); |
5409 |
16 Sep 10 |
nicklas |
92 |
} |
5409 |
16 Sep 10 |
nicklas |
93 |
|
5409 |
16 Sep 10 |
nicklas |
94 |
String bundleName = toBundleName(baseName, locale); |
5409 |
16 Sep 10 |
nicklas |
95 |
String resourceName = toResourceName(bundleName, "properties"); |
5409 |
16 Sep 10 |
nicklas |
96 |
ResourceBundle bundle = null; |
5409 |
16 Sep 10 |
nicklas |
97 |
InputStream in = null; |
5409 |
16 Sep 10 |
nicklas |
98 |
try |
5409 |
16 Sep 10 |
nicklas |
99 |
{ |
5409 |
16 Sep 10 |
nicklas |
100 |
in = loader.getResourceAsStream(resourceName); |
5409 |
16 Sep 10 |
nicklas |
101 |
if (in != null) |
5409 |
16 Sep 10 |
nicklas |
102 |
{ |
7714 |
21 May 19 |
nicklas |
103 |
bundle = new PropertyResourceBundle(new InputStreamReader(in, StandardCharsets.UTF_8)); |
5409 |
16 Sep 10 |
nicklas |
104 |
} |
5409 |
16 Sep 10 |
nicklas |
105 |
} |
5409 |
16 Sep 10 |
nicklas |
106 |
finally |
5409 |
16 Sep 10 |
nicklas |
107 |
{ |
5409 |
16 Sep 10 |
nicklas |
108 |
FileUtil.close(in); |
5409 |
16 Sep 10 |
nicklas |
109 |
} |
5409 |
16 Sep 10 |
nicklas |
110 |
return bundle; |
5409 |
16 Sep 10 |
nicklas |
111 |
} |
5409 |
16 Sep 10 |
nicklas |
112 |
// --------------------------------------------------- |
5409 |
16 Sep 10 |
nicklas |
113 |
|
5409 |
16 Sep 10 |
nicklas |
114 |
} |