/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kr.co.kihyun.text.hml;

import java.io.*;
//import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
//import java.util.*; 

//import kr.co.kihyun.util.*;
import kr.co.kihyun.lang.MString;
import kr.co.kihyun.lang.Encoder;

public class HttpHWPSave extends HttpServlet {

    CyberHtm cyberHtm = new CyberHtm();
    Hwp97 hwp97 = new Hwp97();
    Hwp97Deci hwp97Deci = new Hwp97Deci();
    Hwp hwp = new Hwp();
    HwpDeciReport hdr = new HwpDeciReport();

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
        doPost(req, res);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {

        PrintWriter out = null;
        //PrintWriter out = new PrintWriter(new OutputStreamWriter(res.getOutputStream(),"KSC5601"),true);
        String mode = MString.checkNull(req.getParameter("mode"));
        String fileName = "";
        String preStr = "";
        String afterStr = "";
        //StringBuffer sb = new StringBuffer();
        StringBuilder sb = new StringBuilder();
        
        try {
            out = new PrintWriter(new OutputStreamWriter(res.getOutputStream(),"KSC5601"),true);
            
            if(mode.equals("deciReport")){
                fileName = hdr.getFileName();
                //preStr = HwpDeciReport.getPreHML();
                preStr = hdr.getPreHML();
                afterStr = HwpDeciReport.getAfterHML();
            }else{
                fileName = hwp.getFileName();
                preStr = Hwp.getPreHML();
                afterStr = Hwp.getAfterHML();
            }

            res.setContentType("application/unknown;charset=UTF-8");
            res.setHeader("Accept-Ranges", "bytes");
            //res.setHeader("Content-Disposition","attachment; filename="+Encoder.toDB(fileName)+".hml;");
            String tmpFileName = fileName.replaceAll(":","_");
            tmpFileName = tmpFileName.replaceAll("/","_");
            res.setHeader("Content-Disposition","attachment; filename="+Encoder.toDB(tmpFileName)+".hml;");


            if(mode.equals("deci")){//집계자에서 기안문 출력(한글 저장시)

                String str = hwp97Deci.getCODE( cyberHtm.getCyberHtm( hwp.getContent() ) ).toString();
                int st_body = str.indexOf("<BODY>");
                int ed_body = str.indexOf("</BODY>");
                str = str.substring(st_body+6, ed_body);

                sb.append(preStr);
                sb.append(str);
                sb.append(afterStr);
            }else if(mode.equals("deciReport")){//보고자 임시보고에서 기안문 출력(한글 저장시)

                String strDeciReport = hwp97.getCODE( cyberHtm.getCyberHtm( hdr.getContent() ) ).toString();
                sb.append(preStr);
                sb.append( "\n<P>"+hdr.getContentsCut(strDeciReport)+"</P>" );
                sb.append(afterStr);
            }else{
                sb.append( hwp97.getCODE( cyberHtm.getCyberHtm( (hwp.getContent()).replaceAll("<br>","</TEXT></P><P><TEXT>") ) ) );
            }
            //out.println(sb.toString());
            //System.out.println(sb.toString());
            String sbtmp = (sb.toString()).replaceAll("<[?]xml:namespace prefix = o ns = \"urn:schemas-microsoft-com:office:office\" />", "");
            out.println(sbtmp);
            out.flush();
            out.close();
        } catch(IOException ioex) {
            ioex.printStackTrace();
        } finally {
            //v2 21.부적절한 자원 해제 (IO)_CWE-404 : Add by YOUNGJUN,CHO
            if (out != null) {
                out.close();
            }
            //++++++++++++++++++++++++++++++++++++++++++++++++
        }
        
        
    }
}
