/*
 * MosP - Mind Open Source Project    http://www.mosp.jp/
 * Copyright (C) MIND Co., Ltd.       http://www.e-mind.co.jp/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package jp.mosp.common.common;

import java.util.HashMap;

/**
 * 仮名変換ユーティリティ。
 * <p>2008/12/05</p>
 * <p>
 * 全角カナ→半角カナ、半角カナ→全角カナ変換<br>
 * ひらがな→半角カナ、半角カナ→ひらがな変換<br>
 * ひらがな→全角カナ、全角カナ→ひらがな変換
 * </p>
 * @author m.yoshida
 * @since
 * @version 0.0.1
 */
public final class KanaConvertUtility {

	/**
	 * 全角カナ
	 */
	private static String[] aryZenkakuKana = {
			"ア","イ","ウ","エ","オ",
			"カ","キ","ク","ケ","コ",
			"サ","シ","ス","セ","ソ",
			"タ","チ","ツ","テ","ト",
			"ナ","ニ","ヌ","ネ","ノ",
			"ハ","ヒ","フ","ヘ","ホ",
			"マ","ミ","ム","メ","モ",
			"ヤ","ユ","ヨ",
			"ラ","リ","ル","レ","ロ",
			"ワ","ヲ","ン",
			"ガ","ギ","グ","ゲ","ゴ",
			"ザ","ジ","ズ","ゼ","ゾ",
			"ダ","ヂ","ヅ","デ","ド",
			"バ","ビ","ブ","ベ","ボ",
			"パ","ピ","プ","ペ","ポ",
			"ァ","ィ","ゥ","ェ","ォ",
			"ャ","ュ","ョ","ッ","ー",
			"。","「","」","、","・",
			"゛","゜",
			"ヴ",
			"０","１","２","３","４",
			"５","６","７","８","９",
			"ａ","ｂ","ｃ","ｄ","ｅ",
			"ｆ","ｇ","ｈ","Ｉ","ｊ",
			"ｋ","ｌ","ｍ","ｎ","ｏ",
			"ｐ","ｑ","ｒ","ｓ","ｔ",
			"ｕ","ｖ","ｗ","ｘ","ｙ",
			"ｚ",
			"Ａ","Ｂ","Ｃ","Ｄ","Ｅ",
			"Ｆ","Ｇ","Ｈ","Ｉ","Ｊ",
			"Ｋ","Ｌ","Ｍ","Ｎ","Ｏ",
			"Ｐ","Ｑ","Ｒ","Ｓ","Ｔ",
			"Ｕ","Ｖ","Ｗ","Ｘ","Ｙ",
			"Ｚ",		
	};

	/**
	 * 半角カナ
	 */
	private static String[] aryHankakuKana = {
			"ｱ","ｲ","ｳ","ｴ","ｵ",
			"ｶ","ｷ","ｸ","ｹ","ｺ",
			"ｻ","ｼ","ｽ","ｾ","ｿ",
			"ﾀ","ﾁ","ﾂ","ﾃ","ﾄ",
			"ﾅ","ﾆ","ﾇ","ﾈ","ﾉ",
			"ﾊ","ﾋ","ﾌ","ﾍ","ﾎ",
			"ﾏ","ﾐ","ﾑ","ﾒ","ﾓ",
			"ﾔ","ﾕ","ﾖ",
			"ﾗ","ﾘ","ﾙ","ﾚ","ﾛ",
			"ﾜ","ｦ","ﾝ",
			"ｶﾞ","ｷﾞ","ｸﾞ","ｹﾞ","ｺﾞ",
			"ｻﾞ","ｼﾞ","ｽﾞ","ｾﾞ","ｿﾞ",
			"ﾀﾞ","ﾁﾞ","ﾂﾞ","ﾃﾞ","ﾄﾞ",
			"ﾊﾞ","ﾋﾞ","ﾌﾞ","ﾍﾞ","ﾎﾞ",
			"ﾊﾟ","ﾋﾟ","ﾌﾟ","ﾍﾟ","ﾎﾟ",
			"ｧ","ｨ","ｩ","ｪ","ｫ",
			"ｬ","ｭ","ｮ","ｯ","ｰ",
			"｡","｢","｣","､","･",
			"ﾞ","ﾟ",
			"ｳﾞ",
			"0","1","2","3","4",
			"5","6","7","8","9",
			"a","b","c","d","e",
			"f","g","h","I","j",
			"k","l","m","n","o",
			"p","q","r","s","t",
			"u","v","w","x","y",
			"z",
			"A","B","C","D","E",
			"F","G","H","I","J",
			"K","L","M","N","O",
			"P","Q","R","S","T",
			"U","V","W","X","Y",
			"Z",		
	};

	/**
	 * ひらがな
	 */
	private static String[] aryHiraGana = {
			"あ","い","う","え","お",
			"か","き","く","け","こ",
			"さ","し","す","せ","そ",
			"た","ち","つ","て","と",
			"な","に","ぬ","ね","の",
			"は","ひ","ふ","へ","ほ",
			"ま","み","む","め","も",
			"や","ゆ","よ",
			"ら","り","る","れ","ろ",
			"わ","を","ん",
			"が","ぎ","ぐ","げ","ご",
			"ざ","じ","ず","ぜ","ぞ",
			"だ","ぢ","づ","で","ど",
			"ば","び","ぶ","べ","ぼ",
			"ぱ","ぴ","ぷ","ぺ","ぽ",
			"ぁ","ぃ","ぅ","ぇ","ぉ",
			"ゃ","ゅ","ょ","っ","ー",
			"。","「","」","、","・",
			"゛","゜",
			"う゛",
			"０","１","２","３","４",
			"５","６","７","８","９",
			"ａ","ｂ","ｃ","ｄ","ｅ",
			"ｆ","ｇ","ｈ","Ｉ","ｊ",
			"ｋ","ｌ","ｍ","ｎ","ｏ",
			"ｐ","ｑ","ｒ","ｓ","ｔ",
			"ｕ","ｖ","ｗ","ｘ","ｙ",
			"ｚ",
			"Ａ","Ｂ","Ｃ","Ｄ","Ｅ",
			"Ｆ","Ｇ","Ｈ","Ｉ","Ｊ",
			"Ｋ","Ｌ","Ｍ","Ｎ","Ｏ",
			"Ｐ","Ｑ","Ｒ","Ｓ","Ｔ",
			"Ｕ","Ｖ","Ｗ","Ｘ","Ｙ",
			"Ｚ",		
	};

	/**
	 * 全角カナ→半角カナ変換
	 * @param input 全角カナを含む文字列
	 * @return 全角カナを半角カナに変換した文字列
	 */
	public static String convZenkakuToHankaku(String input) {
		return convertKana(input, getAryZenkakuKana(), getAryHankakuKana());
	}

	/**
	 * ひらがな→半角カナ変換
	 * @param input		ひらがな文字列
	 * @return	半角カナに変換された文字列
	 */
	public static String convHiraGanaToHankaku(String input) {
		return convertKana(input, getAryHiraGana(), getAryHankakuKana());
	}

	/**
	 * 全角カナ→ひらがな変換
	 * @param input		カタカナ文字列
	 * @return ひらがなに変換された文字列
	 */
	public static String convKataKanaToHiraGana(String input) {
		return convertKana(input, getAryZenkakuKana(), getAryHiraGana());
	}

	/**
	 * ひらがな→全角カナ変換
	 * @param input		ひらがな文字列
	 * @return	全角カナに変換された文字列
	 */
	public static String convHiraGanaToKataKana(String input) {
		return convertKana(input, getAryHiraGana(), getAryZenkakuKana());
	}

	/**
	 * 仮名変換メソッド
	 * @param input			入力
	 * @param aryContain	入力対象仮名配列
	 * @param aryTarget		変換対象仮名配列
	 * @return	output		変換文字列
	 */
	private static String convertKana(String input, String[] aryContain, String[] aryTarget) {
		StringBuffer output = new StringBuffer();
		for (int i = 0; i < input.length(); i++) {
			char ch = input.charAt(i);
			for (int j = 0; j < aryContain.length; j++) {
				if (String.valueOf(ch).equals(aryContain[j])) {
					output.append(aryTarget[j]);
					break;
				} else if (!String.valueOf(ch).equals(aryContain[j])
						&& (j == aryContain.length -1)) {
					output.append(ch);
				}
			}
		}
		return output.toString();
	}

	/**
	 * ひらがな配列取得
	 * @return	ひらがな配列
	 */
	public static String[] getAryHiraGana() {
		return aryHiraGana.clone();
	}

	/**
	 * 半角カナ配列取得
	 * @return	半角カナ配列
	 */
	public static String[] getAryHankakuKana() {
		return aryHankakuKana.clone();
	}

	/**
	 * 全角カナ配列取得
	 * @return	全角カナ配列
	 */
	public static String[] getAryZenkakuKana() {
		return aryZenkakuKana.clone();
	}

	/**
	 * 半角カナ→全角カナ変換
	 * @param input 半角カナを含む文字列
	 * @return 半角カナを全角カナに変換した文字列
	 */
	public static String convHankakuToZenkaku(String input) {
		return convertZenkaku(input, getKataKanaMap());
	}
	
	/**
	 * 半角カナ→ひらがな変換
	 * @param input		半角カナ文字列
	 * @return	ひらがなに変換された文字列
	 */
	public static String convHankakuToHiraGana(String input) {
		return convertZenkaku(input, getHiraGanaMap());
	}

	/**
	 * 半角カナ変換メソッド
	 * @param input			半角カナ文字列
	 * @param targetMap		変換対象マップ
	 * @return	変換対象仮名に変換された文字列
	 */
	private static String convertZenkaku(String input, HashMap<String, String> targetMap) {
		StringBuffer output = new StringBuffer();
		for (int i = 0; i < input.length(); i++) {
			char ch = input.charAt(i);
			int next = i + 1;
			char p = ' ';
			String han = String.valueOf(ch);
			if (next < input.length()) {
				p = input.charAt(next);
				han += p;
			}
			String kana = targetMap.get(han);
			if (kana == null) {
				if (targetMap.get(String.valueOf(ch)) != null) {
					output.append(targetMap.get(String.valueOf(ch)));
				} else {
					output.append(ch);
				}
			} else {
				output.append(kana);
				i++;
			}
		}
		return output.toString();
	}

	/**
	 * 半角カナ→全角カナ変換用マップ
	 * @return	変換用マップ
	 */
	private static HashMap<String, String> getKataKanaMap() {
		HashMap<String, String> kataKanaMap = new HashMap<String, String>();
		for (int i = 0; i < aryHankakuKana.length; i++) {
			kataKanaMap.put(aryHankakuKana[i], aryZenkakuKana[i]);
		}
		return kataKanaMap;
	}

	/**
	 * 半角カナ→ひらがな変換用マップ
	 * @return	変換用マップ
	 */
	private static HashMap<String, String> getHiraGanaMap() {
		HashMap<String, String> hiraGanaMap = new HashMap<String, String>();
		for (int i = 0; i < aryHankakuKana.length; i++) {
			hiraGanaMap.put(aryHankakuKana[i], aryHiraGana[i]);
		}
		return hiraGanaMap;
	}

}
