//===========================================================================
// package
//===========================================================================
package jp.gr.java_conf.bugslife.util;

//===========================================================================
// import
//===========================================================================
import java.text.*;
import java.util.*;
import java.io.*;

import org.apache.struts.util.*;

import java.util.regex.*; 

import jp.gr.java_conf.bugslife.system.*;
/**
 *<PRE>
 * 文字列変換クラス
 *
 *
 *</PRE>
 * @version	0.1, 2000/11/30
 * @author	鰐川 正寛
 */
//===========================================================================
// version	0.0, 2000/11/30	新規作成
// version	0.0, 2001/06/12	notNull, argumentの追加(末廣)
//===========================================================================
public class Converter
{
//===========================================================================
// attributes
//===========================================================================
	/**
	 * 日付フォーマットタイプ
	 */
	/** 日時 */
	public static final int DATE_TIME = 1;
	public static final String DATE_TIME_PATTAN = "yyyy/MM/dd HH:mm:ss";
	/** 日付 */
	public static final int DATE = 2;
	public static final String DATE_PATTAN = "yyyy/MM/dd";
	/** 日付のみ（年２桁）*/
	public static final int SHORT_DATE	 = 3;	//日付のみ（年２桁）
	public static final String SHORT_DATE_PATTAN = "yy/MM/dd";
	/** ＭＭＤＤ形式 */
	public static final int MMDD = 4;
	public static final String MMDD_PATTAN = "MM/dd";
	/** ＭＭＤＤＨＨＭＭ形式 */
	public static final int MMDDHHMM = 5;
	public static final String MMDDHHMM_PATTAN = "MM/dd HH:mm";
	/** 日時(短形式) */
	public static final int SHORT_DATE_TIME	 = 6;
	public static final String SHORT_DATE_TIME_PATTAN = "yy/MM/dd HH:mm";
	
	/**
	 * 日付フォーマット１
	 */
	private static final DateFormat DATE_TIME_FORMAT =  new SimpleDateFormat(DATE_TIME_PATTAN);
	/**
	 * 日付フォーマット２
	 */
	private static final DateFormat DATE_FORMAT =  new SimpleDateFormat(DATE_PATTAN);
	/**
	 * 日付フォーマット３
	 */
	private static final DateFormat SHORT_DATE_FORMAT =  new SimpleDateFormat(SHORT_DATE_PATTAN);
	/**
	 * 日付フォーマット４
	 */
	private static final DateFormat MMDD_FORMAT =  new SimpleDateFormat(MMDD_PATTAN);
	/**
	 * 日付フォーマット５
	 */
	private static final DateFormat MMDDHHMM_FORMAT =  new SimpleDateFormat(MMDDHHMM_PATTAN);
	/**
	 * 日付フォーマット６
	 */
	private static final DateFormat SHORT_DATE_TIME_FORMAT =  new SimpleDateFormat(SHORT_DATE_TIME_PATTAN);
//===========================================================================
// methods
//===========================================================================
	/**
	 *	String to HTML String
	 *
	 * @param	s	文字
	 */ 
	public static final String StringToHtml(String s) {
		
		if ((s == null) || (s.length() == 0)) {
			return (new String());
		}
		
		String LF = "\n";
		String BR = "<br>";
		int sPos = 0, ePos;
		
		StringBuffer sb = new StringBuffer(s);
		do {
			sPos = sb.indexOf(LF, sPos);
			
			if (sPos != -1) {
				sb.replace(sPos, sPos+1, BR);
				sPos += BR.length();
			}
		} while(sPos != -1);
		
		return new String(sb);
	}
	/**
	 *	String to HTML String2
	 *
	 * @param	s	文字
	 */ 
	public static final String StringToHtml2(String s) {
		
		if ((s == null) || (s.length() == 0)) {
			return (new String());
		}
		
		//クロスサイトスクリプト対策
		String newS = ResponseUtils.filter(s);
		
		//故障番号にリンクをつける
		StringBuffer sb = new StringBuffer();
		String pattan = "(故障|故障番号|障害|障害番号|bug|#)(:|：|#|＃)?(\\d{1,4})";
		Pattern p = Pattern.compile(pattan);
		Matcher reg = p.matcher(newS);
		int offset = 0;
		int sPos = 0;
		int ePos = 0;
		int sPos2 = 0;
		int ePos2 = 0;
		while (reg.find()) {
			//マッチした文字の取得
			sPos = reg.start();
			ePos = reg.end();
			sPos2 = reg.start(3);
			ePos2 = reg.end(3);
			
			//先頭でマッチした以外
			if (sPos != 0) {
				//マッチした部分までをコピー
				sb.append(newS.substring(offset, sPos));
			}
			
//			System.out.println("s = " + sPos + " e = " + ePos + " " + newS.substring(sPos, ePos));
//			System.out.println("s2 = " + sPos2 + " e2 = " + ePos2 + " " + newS.substring(sPos2, ePos2));
			
			String word = newS.substring(sPos, ePos);	//マッチした単語を抜き出す
			String bno = newS.substring(sPos2, ePos2);	//マッチした単語を抜き出す
			String url = "<A href=\"../showAll.do?bno=" + bno + "\">" + word + "<A>";//URLをつける
			
			sb.append(url);
			offset = ePos;
			
		}
		if(offset < newS.length()) {
			//マッチした部分までをコピー
			sb.append(newS.substring(offset));
		}
		
		
		//改行コードを<br>に
		String LF = "\n";
		String BR = "<br>";
		sPos = 0; 
		ePos = 0;;
		
		do {
			sPos = sb.indexOf(LF, sPos);
			
			if (sPos != -1) {
				sb.replace(sPos, sPos+1, BR);
				sPos += BR.length();
			}
		} while(sPos != -1);
		
		return new String(sb);
	}
	/**
	 * 日付変換（デフォルトフォーマット
	 *
	 * param	date	日付
	 @ return 変換文字列
	 */
	public static String getDateString(Date date) {
		
		return getDateString(date, DATE_TIME);
	}
	/**
	 * 日付変換
	 *
	 * param	date	日付
	 @ return 変換文字列
	 */
	public static String getDateString(Date date, int format) {
		
		String s = null;
		if (date == null) {
			s = "";
		} else {
			switch(format) {
			case DATE_TIME :
				s = DATE_TIME_FORMAT.format(date);
				break;
			case DATE :
				s = DATE_FORMAT.format(date);
				break;
			case SHORT_DATE :
				s = SHORT_DATE_FORMAT.format(date);
				break;
			case MMDD :
				s = MMDD_FORMAT.format(date);
				break;
			case MMDDHHMM :
				s = MMDDHHMM_FORMAT.format(date);
				break;
			case SHORT_DATE_TIME :
				s = SHORT_DATE_TIME_FORMAT.format(date);
				break;
			default :
				s = DATE_TIME_FORMAT.format(date);
				break;
			}
		}
		return s;
	}
	/**
	 * 数値文字列取得
	 *
	 * param	num		数値
	 * param	format	フォーマット
	 @ return 変換文字列
	 */
	public static String getNumberString(long num, String pattan) {
		
		NumberFormat nf = new DecimalFormat(pattan);
		
		return (nf.format(num));
	}
	/**
	 * エンコードされた文字列取得
	 *
	 * param	s		文字列
	 @ return 変換文字列
	 */
	public static String getEncodingString(String s) {
		
		String ss = null;
		try {
			ss = new String(s.getBytes("ISO-8859-1"), "SJIS");
		} catch(IOException e) {
			throw new BLError("Encodeing error", e);
		}
		return ss;
	}
	/**
	 * 指定した長さ分だけスペースを返す
	 *
	 * param	num		個数
	 @ return 文字列
	 */
	public static String space(int num) {
		StringBuffer sb = new StringBuffer(num);
		for (int i = 0; i < num; i++) {
			sb.append(" ");
		}
		return (new String(sb));
	}
	/**
	 * debug
	 */
	private static void debug(String s) {
		 
		 System.out.println("[Converter]" + s);
		 
	}
//===========================================================================
// test code
//===========================================================================
	/**
	 *	テストメイン
	 */
	public static void main(String[] args)
	{
		System.out.println("------- << AddText Test Start >> -----------");
//		unitTest1();	//日付変換
		unitTest2();	//正規表現
		
		System.out.println("------- << AddText Test End >> -----------");
	}
	
	/**
	 *	unitTest1
	 *
	 * 日付変換テスト
	 *
	 */
	public static void unitTest1()
	{
		System.out.println("------- unitTest1 -----------");
		
		Date date = new Date();
		String s = null;
		
		//default
		s = Converter.getDateString(date);
		System.out.println("default:" + s);
		
		//DATE_TIME
		s = Converter.getDateString(date, Converter.DATE_TIME);
		System.out.println("DATE_TIME:" + s);
		
		//DATE
		s = Converter.getDateString(date, Converter.DATE);
		System.out.println("DATE:" + s);
		
		//SHORT_DATE
		s = Converter.getDateString(date, Converter.SHORT_DATE);
		System.out.println("SHORT_DATE:" + s);
		
		//MMDD
		s = Converter.getDateString(date, Converter.MMDD);
		System.out.println("MMDD:" + s);
		
		
		
	}
	/**
	 *	unitTest2
	 *
	 * 正規表現テスト
	 *
	 */
	public static void unitTest2()
	{
		System.out.println("------- unitTest2 -----------");
		
		//test1
		String s1 = " 故障番号:123は故障456と重複し、障害と思われるが、障害7または障害番号0145が故障である";
		String res = Converter.StringToHtml2(s1);
		System.out.println("s1  = " + s1);
		System.out.println("res = " + res);
		
		//test2
		String s2 = " 故障番号#111は\n故障番号\n0222と故障1";
		res = Converter.StringToHtml2(s2);
		System.out.println("s2  = " + s2);
		System.out.println("res = " + res);
		
		//test3
		String s3 = " #111と:123はbug12a3と重複";
		res = Converter.StringToHtml2(s3);
		System.out.println("s3  = " + s3);
		System.out.println("res = " + res);
	}
}
