KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > util > LocaleCharSet


1 /*
2  * $Id: LocaleCharSet.java,v 1.5 2005/05/13 15:47:08 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.util;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19 import java.io.InputStream JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 /**
24  * Map {@link java.util.Locale} to html/iso character set via <code>org/wings/util/charset.properties</code>.
25  *
26  * @author <a HREF="mailto:andre@lison.de">Andre Lison</a>
27  */

28 public class LocaleCharSet {
29     /**
30      * The default character encoding "ISO-8859-1".
31      */

32     public final static String JavaDoc DEFAULT_ENCODING = "ISO-8859-1";
33     private static LocaleCharSet fInstance = null;
34     private final transient static Log log = LogFactory.getLog(LocaleCharSet.class);
35     private Properties JavaDoc fCharSet;
36     private final static String JavaDoc CHARSET_PROPERTIES = "org/wings/util/charset.properties";
37
38     protected LocaleCharSet() {
39         fCharSet = new Properties JavaDoc();
40         try {
41             InputStream JavaDoc in = this.getClass().getClassLoader().getResourceAsStream(CHARSET_PROPERTIES);
42             fCharSet.load(in);
43             in.close();
44         } catch (Exception JavaDoc e) {
45             log.warn("Unexpected error on loading " + CHARSET_PROPERTIES + " via class path.", e);
46         }
47     }
48
49     /**
50      * Get a instance of LocaleCharSet.
51      *
52      * @return Instance of LocaleCharset
53      */

54     public static LocaleCharSet getInstance() {
55         if (fInstance == null) {
56             fInstance = new LocaleCharSet();
57         }
58         return fInstance;
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 JavaDoc getCharSet(Locale JavaDoc aLocale) {
68         String JavaDoc cs = null;
69         if (aLocale == null) {
70             return DEFAULT_ENCODING;
71         }
72
73         cs = fCharSet.getProperty(aLocale.getCountry() + "_" + aLocale.getLanguage());
74
75         if (cs == null) {
76             cs = fCharSet.getProperty(aLocale.getCountry());
77         }
78
79         if (cs == null) {
80             cs = fCharSet.getProperty(aLocale.getLanguage());
81         }
82
83         return cs != null ? cs : DEFAULT_ENCODING;
84     }
85 }
Popular Tags