01: /*
02: * Copyright 2000,2005 wingS development team.
03: *
04: * This file is part of wingS (http://wingsframework.org).
05: *
06: * wingS is free software; you can redistribute it and/or modify
07: * it under the terms of the GNU Lesser General Public License
08: * as published by the Free Software Foundation; either version 2.1
09: * of the License, or (at your option) any later version.
10: *
11: * Please see COPYING for the complete licence.
12: */
13: package org.wings.util;
14:
15: import org.apache.commons.logging.Log;
16: import org.apache.commons.logging.LogFactory;
17:
18: import java.util.Locale;
19: import java.util.Properties;
20:
21: /**
22: * Map {@link java.util.Locale} to html/iso character set via <code>org/wings/util/charset.properties</code>.
23: *
24: * @author <a href="mailto:andre@lison.de">Andre Lison</a>
25: */
26: public class LocaleCharSet {
27: /**
28: * The default character encoding "UTF-8".
29: */
30: public final static String DEFAULT_ENCODING = "UTF-8";
31:
32: private final static String CHARSET_PROPERTIES = "org/wings/util/charset.properties";
33: private final static Log log = LogFactory
34: .getLog(LocaleCharSet.class);
35: private Properties charsetMap;
36: private static LocaleCharSet instance = null;
37:
38: protected LocaleCharSet() {
39: try {
40: charsetMap = PropertyDiscovery
41: .loadRequiredProperties(CHARSET_PROPERTIES);
42: } catch (Exception e) {
43: log.warn("Unexpected error on loading "
44: + CHARSET_PROPERTIES + " via class path.", e);
45: charsetMap = new Properties();
46: }
47: }
48:
49: /**
50: * Get a instance of LocaleCharSet.
51: *
52: * @return Instance of LocaleCharset
53: */
54: public static LocaleCharSet getInstance() {
55: if (instance == null) {
56: instance = new LocaleCharSet();
57: }
58: return instance;
59: }
60:
61: /**
62: * Try to find a matching character set for this locale.
63: *
64: * @param aLocale The Locale to retrieve the default charset for.
65: * @return if found the charset, DEFAULT_ENCODING otherwise
66: */
67: public String getCharSet(Locale aLocale) {
68: String charset = null;
69: if (aLocale == null) {
70: return DEFAULT_ENCODING;
71: }
72:
73: charset = charsetMap.getProperty(aLocale.getCountry() + "_"
74: + aLocale.getLanguage());
75:
76: if (charset == null) {
77: charset = charsetMap.getProperty(aLocale.getCountry());
78: }
79:
80: if (charset == null) {
81: charset = charsetMap.getProperty(aLocale.getLanguage());
82: }
83:
84: return charset != null ? charset : DEFAULT_ENCODING;
85: }
86: }
|