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.util;
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 * @og.rev 5.9.3.3 (2015/12/26) fukurou.mailパッケージからutilに移動し、機能追加
036 *
037 * @version  4.0
038 * @author   Kazuhiko Hasegawa
039 * @since    JDK5.0,
040 */
041public final class UnicodeCorrecter {
042        private static final String[] ENC_LIST = new String[] { "Shift_JIS" , "SJIS", "Windows-31J", "CP932", "MS932" };
043
044        /**
045         * インスタンスの生成を抑止します。
046         */
047        private UnicodeCorrecter() {
048                // 何もありません。(PMD エラー回避)
049        }
050
051        /**
052         * Unicode 文字列の補正を行います。
053         * "MS932" コンバータでエンコードしようとした際に
054         * 正常に変換できない部分を補正します。
055         *
056         * @param       str 入力文字列
057         * @return      Unicode文字列の補正結果
058         */
059        public static String correctToCP932( final String str ) {
060                String rtn = "";
061
062                if( str != null ) {
063                        final int cnt = str.length();
064                        final StringBuilder buf = new StringBuilder( cnt );
065                        for(int i=0; i<cnt; i++) {
066                                buf.append(correctToCP932(str.charAt(i)));
067                        }
068                        rtn = buf.toString() ;
069                }
070                return rtn ;
071        }
072
073        /**
074         * Unicode 文字列の補正を行います。
075         * encodeがSJIS,Shift_JIS,Windows31J,CP932の場合のみ変換を適用します
076         *
077         * @param       str 入力文字列
078         * @param       enc エンコード
079         * @return      Unicode文字列の補正結果
080         */
081        public static String correctToCP932( final String str, final String enc ) {
082                if( enc == null ) { return str; }
083
084                for( int i=0; i<ENC_LIST.length; i++ ) {
085                        if( enc.equalsIgnoreCase( ENC_LIST[i] ) ) { return correctToCP932(str); }
086                }
087
088                return str;
089        }
090
091        /**
092         * キャラクタ単位に、Unicode 文字の補正を行います。
093         *
094         * 風間殿のページを参考にしています。
095         * @see <a href="http://www.ingrid.org/java/i18n/encoding/ja-conv.html" target="_blank">
096         * http://www.ingrid.org/java/i18n/encoding/ja-conv.html</a>
097         *
098         * @param       ch 入力文字
099         * @return      Unicode文字の補正結果
100         */
101        public static char correctToCP932( final char ch ) {
102                char rtn = ch;
103
104                switch (ch) {
105        //              case 0x00a2:    return 0xffe0;          // ≪
106        //              case 0x00a3:    return 0xffe1;          //  ̄
107        //              case 0x00ac:    return 0xffe2;          // μ
108        //              case 0x03bc:    return 0x00b5;          // ・
109        //              case 0x2014:    return 0x2015;          // ,
110        //              case 0x2016:    return 0x2225;          // ≫
111        //              case 0x2212:    return 0xff0d;          // ―
112        //              case 0x226a:    return 0x00ab;          // ∥
113        //              case 0x226b:    return 0x00bb;          // ヴ
114        //              case 0x301c:    return 0xff5e;          // -
115        //              case 0x30f4:    return 0x3094;          // ~
116        //              case 0x30fb:    return 0x00b7;          // ¢
117        //              case 0xff0c:    return 0x00b8;          // £
118        //              case 0xffe3:    return 0x00af;          // ¬
119
120                        case 0x00a2:    rtn = 0xffe0; break;            // ¢ (1-81, CENT SIGN)
121                        case 0x00a3:    rtn = 0xffe1; break;            // £ (1-82, POUND SIGN)
122                        case 0x00a5:    rtn = 0x005c; break;            // \ (D/12, YEN SIGN)
123                        case 0x00ac:    rtn = 0xffe2; break;            // ¬ (2-44, NOT SIGN)
124                        case 0x2016:    rtn = 0x2225; break;            // ∥ (1-34, DOUBLE VERTICAL LINE)
125                        case 0x203e:    rtn = 0x007e; break;            // ~ (F/14, OVERLINE)
126                        case 0x2212:    rtn = 0xff0d; break;            // - (1-61, MINUS SIGN)
127                        case 0x301c:    rtn = 0xff5e; break;            // ~ (1-33, WAVE DASH)
128
129        //              case 0x301c:    return 0xff5e;
130        //              case 0x2016:    return 0x2225;
131        //              case 0x2212:    return 0xff0d;
132                        default:                break;                  // 4.0.0 (2005/01/31)
133                }
134                return rtn;
135        }
136}