KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > http > LocaleToCharsetMap


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the "License"). You may not use this file except
7  * in compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * glassfish/bootstrap/legal/CDDLv1.0.txt or
11  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * HEADER in each file and include the License file at
17  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18  * add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your
20  * own identifying information: Portions Copyright [yyyy]
21  * [name of copyright owner]
22  *
23  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24  *
25  * Portions Copyright Apache Software Foundation.
26  */

27
28 /*
29  *
30  * This class was originally written by Jason Hunter <jhunter@acm.org>
31  * as part of the book "Java Servlet Programming" (O'Reilly).
32  * See http://www.servlets.com/book for more information.
33  * Used by Sun Microsystems with permission.
34  *
35  */

36
37 package org.apache.tomcat.util.http;
38
39 import java.util.*;
40
41 /**
42  * A mapping to determine the (somewhat arbitrarily) preferred charset for
43  * a given locale. Supports all locales recognized in JDK 1.1.
44  * This class was originally written by Jason Hunter [jhunter@acm.org]
45  * as part of the book "Java Servlet Programming" (O'Reilly).
46  * See <a HREF="http://www.servlets.com/book">
47  * http://www.servlets.com/book</a> for more information.
48  * Used by Sun Microsystems with permission.
49  */

50 public class LocaleToCharsetMap {
51
52   private static Hashtable map;
53
54   static {
55     map = new Hashtable();
56
57     map.put("ar", "ISO-8859-6");
58     map.put("be", "ISO-8859-5");
59     map.put("bg", "ISO-8859-5");
60     map.put("ca", "ISO-8859-1");
61     map.put("cs", "ISO-8859-2");
62     map.put("da", "ISO-8859-1");
63     map.put("de", "ISO-8859-1");
64     map.put("el", "ISO-8859-7");
65     map.put("en", "ISO-8859-1");
66     map.put("es", "ISO-8859-1");
67     map.put("et", "ISO-8859-1");
68     map.put("fi", "ISO-8859-1");
69     map.put("fr", "ISO-8859-1");
70     map.put("hr", "ISO-8859-2");
71     map.put("hu", "ISO-8859-2");
72     map.put("is", "ISO-8859-1");
73     map.put("it", "ISO-8859-1");
74     map.put("iw", "ISO-8859-8");
75     map.put("ja", "Shift_JIS");
76     map.put("ko", "EUC-KR"); // Requires JDK 1.1.6
77
map.put("lt", "ISO-8859-2");
78     map.put("lv", "ISO-8859-2");
79     map.put("mk", "ISO-8859-5");
80     map.put("nl", "ISO-8859-1");
81     map.put("no", "ISO-8859-1");
82     map.put("pl", "ISO-8859-2");
83     map.put("pt", "ISO-8859-1");
84     map.put("ro", "ISO-8859-2");
85     map.put("ru", "ISO-8859-5");
86     map.put("sh", "ISO-8859-5");
87     map.put("sk", "ISO-8859-2");
88     map.put("sl", "ISO-8859-2");
89     map.put("sq", "ISO-8859-2");
90     map.put("sr", "ISO-8859-5");
91     map.put("sv", "ISO-8859-1");
92     map.put("tr", "ISO-8859-9");
93     map.put("uk", "ISO-8859-5");
94     map.put("zh", "GB2312");
95     map.put("zh_TW", "Big5");
96
97   }
98
99   /**
100    * Gets the preferred charset for the given locale, or null if the locale
101    * is not recognized.
102    *
103    * @param loc the locale
104    * @return the preferred charset
105    */

106   public static String JavaDoc getCharset(Locale loc) {
107     String JavaDoc charset;
108
109     // Try for an full name match (may include country)
110
charset = (String JavaDoc) map.get(loc.toString());
111     if (charset != null) return charset;
112
113     // If a full name didn't match, try just the language
114
charset = (String JavaDoc) map.get(loc.getLanguage());
115     return charset; // may be null
116
}
117 }
118
Popular Tags