View Javadoc

    1   package com.ozacc.mail.impl;
    2
    3   import java.util.Locale;
    4
    5   /***
    6    * The Cp932 class contains a utility method for converting Microsoft's
    7    * Cp 932 into JIS.
    8    * <p>
    9    * <strong>注:</strong> このクラスはpublicですが、ozacc-mail library外からは使用しないでください。
    10   *
    11   * @author Kazuhiro Kazama
    12   * @version 1.0 01/06/97
    13   *
    14   * @since 1.0.2
    15   */
    16  public class Cp932 {
    17
    18  	private static boolean isCp932 = false;
    19
    20  	static {
    21  		String p = System.getProperty("iscp932");
    22  		String os = System.getProperty("os.name");
    23  		if (Locale.getDefault().getLanguage().equals("ja")) {
    24  			if (p != null && Boolean.getBoolean(p))
    25  				isCp932 = true;
    26  			else if (os != null && (os.startsWith("Win")))
    27  				isCp932 = true;
    28  		}
    29  	}
    30
    31  	/*
    32  	 * You can't use this constructor.
    33  	 */
    34  	private Cp932() {}
    35
    36  	/*
    37  	 * This method converts Cp932 to JIS.
    38  	 */
    39  	public static String toJIS(String s) {
    40
    41  		/* commented by otsuka
    42  		 *
    43  		 * if (!isCp932)
    44  		 * return s;
    45  		 */
    46
    47  		StringBuffer sb = new StringBuffer();
    48  		char c;
    49  		for (int i = 0; i < s.length(); i++) {
    50  			c = s.charAt(i);
    51
    52  			switch (c) {
    53  				case 0xff3c: // FULLWIDTH REVERSE SOLIDUS ->
    54  					c = 0x005c; // REVERSE SOLIDUS
    55  					break;
    56  				case 0xff5e: // FULLWIDTH TILDE ->
    57  					c = 0x301c; // WAVE DASH
    58  					break;
    59  				case 0x2225: // PARALLEL TO ->
    60  					c = 0x2016; // DOUBLE VERTICAL LINE
    61  					break;
    62  				case 0xff0d: // FULLWIDTH HYPHEN-MINUS ->
    63  					c = 0x2212; // MINUS SIGN
    64  					break;
    65  				case 0xffe0: // FULLWIDTH CENT SIGN ->
    66  					c = 0x00a2; // CENT SIGN
    67  					break;
    68  				case 0xffe1: // FULLWIDTH POUND SIGN ->
    69  					c = 0x00a3; // POUND SIGN
    70  					break;
    71  				case 0xffe2: // FULLWIDTH NOT SIGN ->
    72  					c = 0x00ac; // NOT SIGN
    73  					break;
    74  			}
    75  			sb.append(c);
    76  		}
    77  		return new String(sb);
    78  	}
    79
    80  	/*
    81  	 * This method convert JIS to Cp932.
    82  	 */
    83  	public static String toCp932(String s) {
    84  		if (!isCp932)
    85  			return s;
    86  		StringBuffer sb = new StringBuffer();
    87  		char c;
    88  		for (int i = 0; i < s.length(); i++) {
    89  			c = s.charAt(i);
    90
    91  			switch (c) {
    92  				case 0x005c: // REVERSE SOLIDUS ->
    93  					c = 0xff3c; // FULLWIDTH REVERSE SOLIDUS
    94  					break;
    95  				case 0x301c: // WAVE DASH ->
    96  					c = 0xff5e; // FULLWIDTH TILDE
    97  					break;
    98  				case 0x2016: // DOUBLE VERTICAL LINE ->
    99  					c = 0x2225; // PARALLEL TO
    100 					break;
    101 				case 0x2212: // MINUS SIGN ->
    102 					c = 0xff0d; // FULLWIDTH HYPHEN-MINUS
    103 					break;
    104 				case 0x00a2: // CENT SIGN ->
    105 					c = 0xffe0; // FULLWIDTH CENT SIGN
    106 					break;
    107 				case 0x00a3: // POUND SIGN ->
    108 					c = 0xffe1; // FULLWIDTH POUND SIGN
    109 					break;
    110 				case 0x00ac: // NOT SIGN ->
    111 					c = 0xffe2; // FULLWIDTH NOT SIGN
    112 					break;
    113 			}
    114 			sb.append(c);
    115 		}
    116 		return new String(sb);
    117 	}
    118 }