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