001// Copyright (C) 1998-2001 by Jason Hunter <jhunter_AT_acm_DOT_org>.
002// All rights reserved.  Use of this class is limited.
003// Please see the LICENSE for more information.
004
005package com.oreilly.servlet;
006
007import java.util.*;
008
009/** 
010 * A mapping to determine the (somewhat arbitrarily) preferred charset for 
011 * a given locale.  Supports all locales recognized in JDK 1.1.  This
012 * class is used by the LocaleNegotiator.
013 *
014 * @see com.oreilly.servlet.LocaleNegotiator
015 *
016 * @author <b>Jason Hunter</b>, Copyright &#169; 1998
017 * @version 1.0, 98/09/18
018 */
019public class LocaleToCharsetMap {
020
021  private static Hashtable map;
022
023  static {
024    map = new Hashtable();
025
026    map.put("ar", "ISO-8859-6");
027    map.put("be", "ISO-8859-5");
028    map.put("bg", "ISO-8859-5");
029    map.put("ca", "ISO-8859-1");
030    map.put("cs", "ISO-8859-2");
031    map.put("da", "ISO-8859-1");
032    map.put("de", "ISO-8859-1");
033    map.put("el", "ISO-8859-7");
034    map.put("en", "ISO-8859-1");
035    map.put("es", "ISO-8859-1");
036    map.put("et", "ISO-8859-1");
037    map.put("fi", "ISO-8859-1");
038    map.put("fr", "ISO-8859-1");
039    map.put("hr", "ISO-8859-2");
040    map.put("hu", "ISO-8859-2");
041    map.put("is", "ISO-8859-1");
042    map.put("it", "ISO-8859-1");
043    map.put("iw", "ISO-8859-8");
044    map.put("ja", "Shift_JIS");
045    map.put("ko", "EUC-KR");     // Requires JDK 1.1.6
046    map.put("lt", "ISO-8859-2");
047    map.put("lv", "ISO-8859-2");
048    map.put("mk", "ISO-8859-5");
049    map.put("nl", "ISO-8859-1");
050    map.put("no", "ISO-8859-1");
051    map.put("pl", "ISO-8859-2");
052    map.put("pt", "ISO-8859-1");
053    map.put("ro", "ISO-8859-2");
054    map.put("ru", "ISO-8859-5");
055    map.put("sh", "ISO-8859-5");
056    map.put("sk", "ISO-8859-2");
057    map.put("sl", "ISO-8859-2");
058    map.put("sq", "ISO-8859-2");
059    map.put("sr", "ISO-8859-5");
060    map.put("sv", "ISO-8859-1");
061    map.put("tr", "ISO-8859-9");
062    map.put("uk", "ISO-8859-5");
063    map.put("zh", "GB2312");
064    map.put("zh_TW", "Big5");
065
066  }
067
068  /**
069   * Gets the preferred charset for the given locale, or null if the locale
070   * is not recognized.
071   *
072   * @param loc the locale
073   * @return the preferred charset
074   */
075  public static String getCharset(Locale loc) {
076    String charset;
077
078    // Try for an full name match (may include country)
079    charset = (String) map.get(loc.toString());
080    if (charset != null) return charset;
081
082    // If a full name didn't match, try just the language
083    charset = (String) map.get(loc.getLanguage());
084    return charset;  // may be null
085  }
086}