/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：eo顧客基幹システム
*   モジュール名    ：JCCIncludeMessage
*   ソースファイル名：JCCIncludeMessage.java
*   作成者          ：富士通
*   日付            ：2011年08月01日
*＜機能概要＞
*   メッセージ用jsファイルを読み込んでHTMLデータ内に書き出す拡張タグ用クラスです。
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
*   v1.00.00    2011/08/01   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.web.x33.X33VCustomTagBase;

/**
 * JavaScriptで使用するメッセージ用jsファイルを読み込んでHTMLデータ内に書き出す
 * 拡張タグ用クラス
 * @author 富士通
 *
 */
public class JCCIncludeMessage extends X33VCustomTagBase
{
	
	/** 書出し対象のファイル名 */
	private String fileName = "";
	
	/**
	 * ファイル名のgetterメソッド
	 * @return ファイル名
	 */
	public String getFileName()
	{
		return fileName;
	}

	/**
	 * ファイル名のsetterメソッド
	 * @param name 書出し対象のファイル名
	 */
	public void setFileName(String name)
	{
		this.fileName = name;
	}

	/**
	 * メッセージ用jsファイルを<script></script>タグ内に書き出す。
	 * <br>
	 * @return SKIP_BODY バックヤード共通ヘッダの拡張タグは空タグの為、ボディー部をスキップする。
	 * @throws JspException JSPエラー。
	 */
	@Override
	public int doStartTag() throws JspException
	{
		// script開始タグ
		outputHtml("<script type=\"text/javascript\">\n");

		// リアルパスの取得
		String realPath = pageContext.getServletContext().getRealPath("/js/" + fileName);
		
// 2012/08/17 FST)arata Fortify対応 start
		StringWriter sw = null;
		FileReader fr = null;
		BufferedReader br = null;
		try
		{
			File file = new File(realPath);
			sw = new StringWriter(new Long(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(sw != null)
				{
					sw.close();
				}
				if(fr != null)
				{
					fr.close();
				}
				if(br != null)
				{
					br.close();
				}
			}
			catch(IOException e)
			{
				throw new JspException(e);
			}
		}
// 2012/08/17 FST)arata Fortify対応 end

		// script終了タグ
		outputHtml("</script>\n");
		
		return SKIP_BODY;
	}

}
