/*
 * (C) Copyright IBM Corp. 1998  All rights reserved.
 *
 * US Government Users Restricted Rights Use, duplication or
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *
 * The program is provided "as is" without any warranty express or
 * implied, including the warranty of non-infringement and the implied
 * warranties of merchantibility and fitness for a particular purpose.
 * IBM will not be liable for any damages suffered by you as a result
 * of using the Program. In no event will IBM be liable for any
 * special, indirect or consequential damages or lost profits even if
 * IBM has been advised of the possibility of their occurrence. IBM
 * will not be liable for any third party claims against you.
 */

package com.ibm.xml.parser;

import java.util.*;

/**
 * MIME2Java is a convenience class which handles conversions between MIME charset names
 * and Java encoding names.
 * <p>The supported XML encodings are the intersection of XML-supported code sets and those 
 * supported in JDK 1.1.
 * <p>MIME charset names are used on <var>xmlEncoding</var> parameters to methods such
 * as <code>TXDocument#setEncoding</code> and <code>DTD#setEncoding</code>.
 * <p>Java encoding names are used on <var>enc</var> or <var>encoding</var> parameters to
 * methods such as <code>TXDocument#printWithFormat</code> and <code>DTD#printExternal</code>. 
 *
 * @version 1.1
 * @author TAMURA Kent &lt;kent@trl.ibm.co.jp&gt;
 * @see com.ibm.xml.parser.TXDocument#setEncoding
 * @see com.ibm.xml.parser.DTD#setEncoding
 * @see com.ibm.xml.parser.TXDocument#printWithFormat
 * @see com.ibm.xml.parser.DTD#printExternal
 */
public class MIME2Java {
    
    static private Hashtable s_enchash;
    static private Hashtable s_revhash;
    
    static {
        s_enchash = new Hashtable();
        //    <preferred MIME name>, <Java encoding name>
        s_enchash.put("UTF-8", "UTF8");
        s_enchash.put("US-ASCII", "8859_1");    // ?
        s_enchash.put("ISO-8859-1", "8859_1");
        s_enchash.put("ISO-8859-2", "8859_2");
        s_enchash.put("ISO-8859-3", "8859_3");
        s_enchash.put("ISO-8859-4", "8859_4");
        s_enchash.put("ISO-8859-5", "8859_5");
        s_enchash.put("ISO-8859-6", "8859_6");
        s_enchash.put("ISO-8859-7", "8859_7");
        s_enchash.put("ISO-8859-8", "8859_8");
        s_enchash.put("ISO-8859-9", "8859_9");
        s_enchash.put("ISO-2022-JP", "JIS");
        s_enchash.put("SHIFT_JIS", "SJIS");	// "Shift_JIS" is correct.
                                                // Toupper for case-insensitive matching
        s_enchash.put("EUC-JP", "EUCJIS");
        s_enchash.put("GB2312", "GB2312");
        s_enchash.put("BIG5", "Big5");          // "Big5" is correct

                                                // j:CNS11643
                                                // ISO-2022-CN? ISO-2022-CN-EXT?
                                                // j:KSC5601 ISO-2022-KR??
                                                // KOI8-R?
                                                
        s_revhash = new Hashtable();
        //    <Java encoding name>, <preferred MIME name>
        s_revhash.put("UTF8", "UTF-8");
        //s_revhash.put("8859_1", "US-ASCII");    // ?
        s_revhash.put("8859_1", "ISO-8859-1");
        s_revhash.put("8859_2", "ISO-8859-2");
        s_revhash.put("8859_3", "ISO-8859-3");
        s_revhash.put("8859_4", "ISO-8859-4");
        s_revhash.put("8859_5", "ISO-8859-5");
        s_revhash.put("8859_6", "ISO-8859-6");
        s_revhash.put("8859_7", "ISO-8859-7");
        s_revhash.put("8859_8", "ISO-8859-8");
        s_revhash.put("8859_9", "ISO-8859-9");
        s_revhash.put("JIS", "ISO-2022-JP");
        s_revhash.put("SJIS", "Shift_JIS");
        s_revhash.put("EUCJIS", "EUC-JP");
        s_revhash.put("GB2312", "GB2312");
        s_revhash.put("BIG5", "Big5");
    }

    private MIME2Java() {
    }

    /**
     * Convert a MIME charset name, also known as an XML encoding name, to a Java encoding name.
     * @param   mimeCharsetName Case insensitive MIME charset name: <code>UTF-8, US-ASCII, ISO-8859-1,
     *                          ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6,
     *                          ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-2022-JP, Shift_JIS, 
     *                          EUC-JP, GB2312, and Big5</code>.
     * @return                  Java encoding name, or <var>null</var> if <var>mimeCharsetName</var>
     *                          is unknown.
     * @see #reverse
     */
    public static String convert(String mimeCharsetName) {
        return (String)s_enchash.get(mimeCharsetName.toUpperCase());
    }

    /**
     * Convert a Java encoding name to MIME charset name.
     * Available values of <i>enc</i> are "UTF8", "8859_1", "8859_2", "8859_3", "8859_4",
     * "8859_5", "8859_6", "8859_7", "8859_8", "8859_9", "JIS", "SJIS", "EUCJIS",
     * "GB2312" and "BIG5".
     * @param   enc Case insensitive Java encoding name: <code>UTF8, 8859_1, 8859_2, 8859_3,
     *              8859_4, 8859_5, 8859_6, 8859_7, 8859_8, 8859_9, JIS, SJIS, EUCJIS,
     *              GB2312, and BIG5</code>.
     * @return      MIME charset name, or <var>null</var> if <var>enc</var> is unknown.
     * @see #convert
     */
    public static String reverse(String enc) {
        return (String)s_revhash.get(enc.toUpperCase());
    }
}
