1 | /*
|
---|
2 | * UTF8Control.java
|
---|
3 | * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 |
|
---|
20 | // Based on: https://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle
|
---|
21 |
|
---|
22 | package org.greenstone.gsdl3.util;
|
---|
23 |
|
---|
24 | import java.util.ResourceBundle;
|
---|
25 | import java.util.ResourceBundle.Control;
|
---|
26 | import java.util.Locale;
|
---|
27 | import java.util.Enumeration;
|
---|
28 | import java.util.PropertyResourceBundle;
|
---|
29 |
|
---|
30 | import java.io.*;
|
---|
31 | import java.net.*;
|
---|
32 |
|
---|
33 | import org.apache.log4j.*;
|
---|
34 |
|
---|
35 | public class UTF8Control extends Control
|
---|
36 | {
|
---|
37 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.UTF8Control.class.getName());
|
---|
38 |
|
---|
39 | public ResourceBundle newBundle
|
---|
40 | (String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
|
---|
41 | throws IllegalAccessException, InstantiationException, IOException
|
---|
42 | {
|
---|
43 | // The below is a copy of the default implementation.
|
---|
44 | String bundleName = toBundleName(baseName, locale);
|
---|
45 | String resourceName = toResourceName(bundleName, "properties");
|
---|
46 | ResourceBundle bundle = null;
|
---|
47 | InputStream stream = null;
|
---|
48 | if (reload) {
|
---|
49 | URL url = loader.getResource(resourceName);
|
---|
50 | if (url != null) {
|
---|
51 | URLConnection connection = url.openConnection();
|
---|
52 | if (connection != null) {
|
---|
53 | connection.setUseCaches(false);
|
---|
54 | stream = connection.getInputStream();
|
---|
55 | }
|
---|
56 | }
|
---|
57 | } else {
|
---|
58 | stream = loader.getResourceAsStream(resourceName);
|
---|
59 | }
|
---|
60 | if (stream != null) {
|
---|
61 | try {
|
---|
62 | // Only this line is changed to make it to read properties files as UTF-8.
|
---|
63 | bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
|
---|
64 | } finally {
|
---|
65 | stream.close();
|
---|
66 | }
|
---|
67 | }
|
---|
68 | return bundle;
|
---|
69 | }
|
---|
70 | }
|
---|