001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.fukurou.mail;
017
018/**
019 * unicode と、JIS との文字コードの関係で、変換しています。
020 * <pre>
021 * http://www.ingrid.org/java/i18n/encoding/ja-conv.html
022 *
023 * 0x00a2 ⇒ 0xffe0       ¢ (1-81, CENT SIGN)
024 * 0x00a3 ⇒ 0xffe1       £ (1-82, POUND SIGN)
025 * 0x00a5 ⇒ 0x005c       \ (D/12, YEN SIGN)
026 * 0x00ac ⇒ 0xffe2       ¬ (2-44, NOT SIGN)
027 * 0x2016 ⇒ 0x2225       ∥ (1-34, DOUBLE VERTICAL LINE)
028 * 0x203e ⇒ 0x007e       ~ (F/14, OVERLINE)
029 * 0x2212 ⇒ 0xff0d       - (1-61, MINUS SIGN)
030 * 0x301c ⇒ 0xff5e       ~ (1-33, WAVE DASH)
031 *
032 * それぞれコード変換します。
033 * </pre>
034 *
035 * @version  4.0
036 * @author   Kazuhiko Hasegawa
037 * @since    JDK5.0,
038 */
039final class UnicodeCorrecter {
040
041        /**
042         * インスタンスの生成を抑止します。
043         */
044        private UnicodeCorrecter() {
045                // 何もありません。(PMD エラー回避)
046        }
047
048        /**
049         * Unicode 文字列の補正を行います。
050         * "MS932" コンバータでエンコードしようとした際に
051         * 正常に変換できない部分を補正します。
052         *
053         * @param       str 入力文字列
054         * @return      Unicode 文字列の補正結果
055         */
056        public static String correctToCP932( final String str ) {
057                String rtn = "";
058
059                if( str != null ) {
060                        int cnt = str.length();
061                        StringBuilder buf = new StringBuilder( cnt );
062                        for(int i=0; i<cnt; i++) {
063                                buf.append(correctToCP932(str.charAt(i)));
064                        }
065                        rtn = buf.toString() ;
066                }
067                return rtn ;
068        }
069
070        /**
071         * キャラクタ単位に、Unicode 文字の補正を行います。
072         *
073         * 風間殿のページを参考にしています。
074         * @see <a href="http://www.ingrid.org/java/i18n/encoding/ja-conv.html" target="_blank">
075         * http://www.ingrid.org/java/i18n/encoding/ja-conv.html</a>
076         *
077         * @param       ch 入力文字
078         * @return      Unicode 文字の補正結果
079         */
080        public static char correctToCP932( final char ch ) {
081                char rtn = ch;
082
083                switch (ch) {
084        //              case 0x00a2:    return 0xffe0;          // ≪
085        //              case 0x00a3:    return 0xffe1;          //  ̄
086        //              case 0x00ac:    return 0xffe2;          // μ
087        //              case 0x03bc:    return 0x00b5;          // ・
088        //              case 0x2014:    return 0x2015;          // ,
089        //              case 0x2016:    return 0x2225;          // ≫
090        //              case 0x2212:    return 0xff0d;          // ―
091        //              case 0x226a:    return 0x00ab;          // ∥
092        //              case 0x226b:    return 0x00bb;          // ヴ
093        //              case 0x301c:    return 0xff5e;          // -
094        //              case 0x30f4:    return 0x3094;          // ~
095        //              case 0x30fb:    return 0x00b7;          // ¢
096        //              case 0xff0c:    return 0x00b8;          // £
097        //              case 0xffe3:    return 0x00af;          // ¬
098
099                        case 0x00a2:    rtn = 0xffe0; break;            // ¢ (1-81, CENT SIGN)
100                        case 0x00a3:    rtn = 0xffe1; break;            // £ (1-82, POUND SIGN)
101                        case 0x00a5:    rtn = 0x005c; break;            // \ (D/12, YEN SIGN)
102                        case 0x00ac:    rtn = 0xffe2; break;            // ¬ (2-44, NOT SIGN)
103                        case 0x2016:    rtn = 0x2225; break;            // ∥ (1-34, DOUBLE VERTICAL LINE)
104                        case 0x203e:    rtn = 0x007e; break;            // ~ (F/14, OVERLINE)
105                        case 0x2212:    rtn = 0xff0d; break;            // - (1-61, MINUS SIGN)
106                        case 0x301c:    rtn = 0xff5e; break;            // ~ (1-33, WAVE DASH)
107
108        //              case 0x301c:    return 0xff5e;
109        //              case 0x2016:    return 0x2225;
110        //              case 0x2212:    return 0xff0d;
111                        default:                break;                  // 4.0.0 (2005/01/31)
112                }
113                return rtn;
114        }
115}