/*********************************************************************
*	All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名		：eo顧客基幹システム
*	モジュール名	：JBSbatZMFileReaderUtil
*	ソースファイル名：JBSbatZMFileReaderUtil.java
*	作成者			：富士通
*	日付			：2011年08月30日
*＜機能概要＞
*	業務サービススーパークラス
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v2.00.00	2011/08/30	FJ)荒田		新規作成
*	v4.00.00	2012/09/12	FJ)早崎		【TAI-2012-0000095】Fortify対応
*	v4.01.00	2012/11/15	FJ)伊藤		【TAI-2012-0000140】品質強化対策
*
**********************************************************************/


package eo.business.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

import eo.common.constant.JPCBatchMessageConstant;
import eo.framework.application.JBSbatBusinessException;

/**
 * ファイル読込み処理を行うクラスです。
 * @author 909282
 */
public class JBSbatZMFileReaderUtil
{
	
	/**
	 * 読込むファイルパス
	 */
	private String fileName;
	
	/**
	 * ファイル読込みオブジェクト(バイトストリーム)
	 */
	private InputStreamReader isr;
	
	/**
	 * ファイル読込みオブジェクト
	 */
	private BufferedReader  br;
	
	/**
	 * char型のダブルクォーテーション
	 */
	private static final char DOUBLE_QUOT = '"';

	/**
	 * コンストラクタ<br>
	 * ファイル読込みします。エンコード(MS932)固定です。
	 * 
	 * @param commonItem バッチ共通パラメータ電文
	 * @param fullPath ファイル名(フルパスで)
	 * @throws Exception
	 */
	public JBSbatZMFileReaderUtil(String fullPath) throws Exception
	{
		try
		{
			
			// ファイルパスチェック
			if(null == fullPath || (JZMBatConst.BLANK).equals(fullPath))
			{
				
				// ファイルパスが指定されていない場合、
				return;
			}
			
			this.fileName = fullPath;
			isr = new InputStreamReader(new FileInputStream(new File(fullPath)), JZMBatConst.MS932);
			br = new BufferedReader(isr);
		}
		catch (FileNotFoundException e)
		{
			try
			{
				br.close();
			}
			catch (Exception e1)
			{
				
			}
			
			try
			{
				isr.close();
			}
			catch  (Exception e1)
			{
				
			}
			// EZMB0020CE:入力ファイルが存在しません。（ファイルパス:%1%）
			throw new JBSbatBusinessException(JPCBatchMessageConstant.EZMB0140CW, new String[]{fileName});
		}
	}
	
	/**
	 * コンストラクタ<br>
	 * ファイル読込みのエンコードが指定可能です。
	 * 
	 * @param commonItem バッチ共通パラメータ電文
	 * @param fullPath ファイル名(フルパスで)
	 * @param encode エンコード
	 * @throws Exception
	 */
	public JBSbatZMFileReaderUtil(String fullPath, String encode) throws Exception
	{
		try
		{
			
			// ファイルパスチェック
			if(null == fullPath || (JZMBatConst.BLANK).equals(fullPath))
			{
				
				// ファイルパスが指定されていない場合、
				return;
			}
			
			this.fileName = fullPath;
			isr = new InputStreamReader(new FileInputStream(new File(fullPath)), encode);
			br = new BufferedReader(isr);
		}
		catch (FileNotFoundException e)
		{
			try
			{
				br.close();
			}
			catch (Exception e1)
			{
				
			}
			
			try
			{
				isr.close();
			}
			catch  (Exception e1)
			{
				
			}
			// EZMB0020CE:入力ファイルが存在しません。（ファイルパス:%1%）
			throw new JBSbatBusinessException(JPCBatchMessageConstant.EZMB0140CW, new String[]{fileName});
		}
	}
	
	/**
	 * ファイルが読込み可能かを返却する処理
	 * @return ArrayList ファイル内データを格納したリスト
	 * @throws Exception 例外
	 */
	public boolean ready() throws Exception
	{
		return br.ready();
	}
	
	/**
	 * ファイルの1行分のデータを取得する処理
	 * @return ArrayList ファイル内データを格納したリスト
	 * @throws Exception 例外
	 */
	public String readLine() throws Exception
	{
		return br.readLine();
	}
	
	
	/**
	 * ファイルのフルパスを返却します。
	 * @return ファイルのフルパス
	 */
	public String getFilePath()
	{
		return this.fileName;
	}
	
	/**
	 * ファイルの一行分のデータを項目ごとに分割し、
	 * ダブルクォーテーションを除外した状態でList格納する。
	 * @return ArrayList 項目ごとにデータを格納したList
	 * @throws Exception 例外
	 */
	public ArrayList<String> convEscapeStringToList() throws Exception
	{
		// 戻り値格納用List
		ArrayList<String> resultList = new ArrayList<String>();

		String lineData = this.readLine();
		
		if (lineData == null || JZMBatConst.BLANK.equals(lineData))
		{
			// ファイルのデータが取得出来ない場合、nullを返却する。
			return null;
		}
		
		StringTokenizer st = new StringTokenizer(lineData, JZMBatConst.CONMA);
		StringBuffer bf = new StringBuffer();
		int len = 0;

		while(st.hasMoreElements())
		{
			String s = st.nextToken();
			bf = new StringBuffer();
			bf.append(s);
			len = bf.length();

			// 先頭文字がダブルクォーテーションの場合除去
			if(bf.charAt(0) == (DOUBLE_QUOT))
			{
				bf.delete(0, 1);
				len = len - 1;
			}

			// 終端文字がダブルクォーテーションの場合除去
			if (bf.charAt(len - 1) == (DOUBLE_QUOT))
			{
				bf.delete(len - 1, len);
			}

			// 戻り値となるリストに1項目分のデータ格納
			resultList.add(bf.toString());
		}
		
		// 1行分のデータを格納したListを返却
		return resultList;
	}
	
	/**
	 * ファイル書込みオブジェクトをクローズする。
	 * @throws Exception
	 */
	public void close() throws Exception
	{
		try
		{
			br.close();
		}
		catch (Exception e)
		{
			
		}
		
		try
		{
			isr.close();
		}
		catch  (Exception e)
		{
			
		}
	}
}
