source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/UTF8Control.java@ 36067

Last change on this file since 36067 was 33994, checked in by davidb, 4 years ago

The introduction of UTF8Control class means we can now work directly with resource-bundle/property files and assume they deliver UTF-8 strings to the Java code; this class is based on a StackOverflow posting; additional comments to how we then use this in Greenstone (relating to the 'initial' string) noted in Dictionary.java

File size: 2.3 KB
Line 
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
22package org.greenstone.gsdl3.util;
23
24import java.util.ResourceBundle;
25import java.util.ResourceBundle.Control;
26import java.util.Locale;
27import java.util.Enumeration;
28import java.util.PropertyResourceBundle;
29
30import java.io.*;
31import java.net.*;
32
33import org.apache.log4j.*;
34
35public 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}
Note: See TracBrowser for help on using the repository browser.