KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > oreilly > servlet > LocaleToCharsetMap


1 // Copyright (C) 1998-2001 by Jason Hunter <jhunter_AT_acm_DOT_org>.
2
// All rights reserved. Use of this class is limited.
3
// Please see the LICENSE for more information.
4

5 package com.oreilly.servlet;
6
7 import java.util.*;
8
9 /**
10  * A mapping to determine the (somewhat arbitrarily) preferred charset for
11  * a given locale. Supports all locales recognized in JDK 1.1. This
12  * class is used by the LocaleNegotiator.
13  *
14  * @see com.oreilly.servlet.LocaleNegotiator
15  *
16  * @author <b>Jason Hunter</b>, Copyright &#169; 1998
17  * @version 1.0, 98/09/18
18  */

19 public class LocaleToCharsetMap {
20
21   private static Hashtable map;
22
23   static {
24     map = new Hashtable();
25
26     map.put("ar", "ISO-8859-6");
27     map.put("be", "ISO-8859-5");
28     map.put("bg", "ISO-8859-5");
29     map.put("ca", "ISO-8859-1");
30     map.put("cs", "ISO-8859-2");
31     map.put("da", "ISO-8859-1");
32     map.put("de", "ISO-8859-1");
33     map.put("el", "ISO-8859-7");
34     map.put("en", "ISO-8859-1");
35     map.put("es", "ISO-8859-1");
36     map.put("et", "ISO-8859-1");
37     map.put("fi", "ISO-8859-1");
38     map.put("fr", "ISO-8859-1");
39     map.put("hr", "ISO-8859-2");
40     map.put("hu", "ISO-8859-2");
41     map.put("is", "ISO-8859-1");
42     map.put("it", "ISO-8859-1");
43     map.put("iw", "ISO-8859-8");
44     map.put("ja", "Shift_JIS");
45     map.put("ko", "EUC-KR"); // Requires JDK 1.1.6
46
map.put("lt", "ISO-8859-2");
47     map.put("lv", "ISO-8859-2");
48     map.put("mk", "ISO-8859-5");
49     map.put("nl", "ISO-8859-1");
50     map.put("no", "ISO-8859-1");
51     map.put("pl", "ISO-8859-2");
52     map.put("pt", "ISO-8859-1");
53     map.put("ro", "ISO-8859-2");
54     map.put("ru", "ISO-8859-5");
55     map.put("sh", "ISO-8859-5");
56     map.put("sk", "ISO-8859-2");
57     map.put("sl", "ISO-8859-2");
58     map.put("sq", "ISO-8859-2");
59     map.put("sr", "ISO-8859-5");
60     map.put("sv", "ISO-8859-1");
61     map.put("tr", "ISO-8859-9");
62     map.put("uk", "ISO-8859-5");
63     map.put("zh", "GB2312");
64     map.put("zh_TW", "Big5");
65
66   }
67
68   /**
69    * Gets the preferred charset for the given locale, or null if the locale
70    * is not recognized.
71    *
72    * @param loc the locale
73    * @return the preferred charset
74    */

75   public static String JavaDoc getCharset(Locale loc) {
76     String JavaDoc charset;
77
78     // Try for an full name match (may include country)
79
charset = (String JavaDoc) map.get(loc.toString());
80     if (charset != null) return charset;
81
82     // If a full name didn't match, try just the language
83
charset = (String JavaDoc) map.get(loc.getLanguage());
84     return charset; // may be null
85
}
86 }
87
Popular Tags