1 package be
.nikiroo
.jvcard
.resources
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
7 import java
.io
.InputStreamReader
;
9 import java
.net
.URLConnection
;
10 import java
.util
.Locale
;
11 import java
.util
.PropertyResourceBundle
;
12 import java
.util
.ResourceBundle
;
13 import java
.util
.ResourceBundle
.Control
;
16 * Fixed ResourceBundle.Control class. It will use UTF-8 for the files to load.
18 * Also support an option to first check into the given path before looking into
24 class FixedResourceBundleControl
extends Control
{
25 private String outsideWorld
= null;
28 * Create a new {@link FixedResourceBundleControl}.
31 * NULL if you are only interested into the resources, a path to
32 * first check into it before looking at the actual resources
34 public FixedResourceBundleControl(String outsideWorld
) {
35 this.outsideWorld
= outsideWorld
;
38 public ResourceBundle
newBundle(String baseName
, Locale locale
,
39 String format
, ClassLoader loader
, boolean reload
)
40 throws IllegalAccessException
, InstantiationException
, IOException
{
41 // The below is a copy of the default implementation.
42 String bundleName
= toBundleName(baseName
, locale
);
43 String resourceName
= toResourceName(bundleName
, "properties");
45 ResourceBundle bundle
= null;
46 InputStream stream
= null;
48 URL url
= loader
.getResource(resourceName
);
50 URLConnection connection
= url
.openConnection();
51 if (connection
!= null) {
52 connection
.setUseCaches(false);
53 stream
= connection
.getInputStream();
57 // New code to support outside resources:
58 if (outsideWorld
!= null) {
59 String pkg
= this.getClass().getPackage().getName()
60 .replaceAll("\\.", File
.separator
)
63 if (resourceName
.startsWith(pkg
)) {
65 String file
= outsideWorld
+ File
.separator
66 + resourceName
.substring(pkg
.length());
67 stream
= new FileInputStream(file
);
68 } catch (Exception e
) {
69 // file not in priority directory,
70 // fallback to default resource
76 stream
= loader
.getResourceAsStream(resourceName
);
81 // This line is changed to make it to read properties files
83 // How can someone use an archaic encoding such as ISO 8859-1 by
84 // *DEFAULT* is beyond me...
85 bundle
= new PropertyResourceBundle(new InputStreamReader(