/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：eo顧客基幹システム
*   モジュール名    ：JFUIncludeFile
*   ソースファイルパス：JFUIncludeFile.java
*   作成者          ：富士通
*   日付            ：2014年05月30日
*＜機能概要＞
*   指定されたパスのファイルを読み込んでHTMLデータ内に書き出す拡張タグ用クラスです。
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
*   v1.00.00    2014/05/30   FJ          新規作成
*
**********************************************************************/

package eo.web.webview.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.jsp.JspException;

import com.fujitsu.futurity.model.common.JCMAPLConstMgr;
import com.fujitsu.futurity.web.x33.X33VCustomTagBase;

/**
 * 指定されたキー情報を元にアプリケーションプロパティを参照し
 * ファイルパスと出力フラグ値を取得判定し
 * ファイル内容を読み込んでHTMLデータ内に書き出す。
 * 拡張タグ用クラス
 * @author 富士通
 *
 */
public class JFUIncludeFile extends X33VCustomTagBase
{

	/** taglibより渡されるキー */
	private String key = "";

	/**
	 * キーを設定します。
	 * <br>
	 * @param arg0 キー
	 */
	public void setKey(String arg0)
	{
		this.key = arg0;
	}

	/** taglibより渡される出力フラグキー */
	private String outPutFlgKey = "";

	/**
	 * 出力フラグキーを設定します。
	 * <br>
	 * @param arg0 キー
	 */
	public void setOutPutFlgKey(String arg0)
	{
		this.outPutFlgKey = arg0;
	}

	/**
	 * 指定されたパスのファイルを読み込んでHTMLデータ内に書き出す。
	 * <br>
	 * @return 処理結果
	 * @throws JspException JSPエラー。
	 */
	@Override
	public int doStartTag() throws JspException
	{

		if (this.key.length() == 0)
		{
			return SKIP_BODY;
		}

		// リアルパスの取得
		String path = JCMAPLConstMgr.getString(key);
		if (path == null || path.length() == 0)
		{
			return SKIP_BODY;
		}

		if (this.outPutFlgKey.length() != 0)
		{
			// 出力フラグ(0:出力しない、1:出力する)の取得
			String outPutFlg = JCMAPLConstMgr.getString(outPutFlgKey);
			if ("0".equals(outPutFlg))
			{
				return SKIP_BODY;
			}
		}

		StringWriter sw = null;
		FileReader fr = null;
		BufferedReader br = null;
		try
		{
			File file = new File(path);
			sw = new StringWriter(Long.valueOf(file.length()).intValue());
			fr = new FileReader(file);
			br = new BufferedReader(fr);
			char[] buf = new char[1024];

			int pos = 0;
			while (pos != -1)
			{
				pos = br.read(buf, 0, buf.length);
				if (pos != -1)
				{
					sw.write(buf, 0, pos);
				}
			}

			outputHtml(sw.getBuffer().toString());
		}
		catch (FileNotFoundException e)
		{
			throw new JspException(e);
		}
		catch (IOException e)
		{
			throw new JspException(e);
		}
		finally
		{
			try
			{
				if (br != null)
				{
					br.close();
				}
				if (fr != null)
				{
					fr.close();
				}
				if (sw != null)
				{
					sw.close();
				}
			}
			catch (IOException e)
			{
				throw new JspException(e);
			}
		}
		return SKIP_BODY;
	}
}
