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