package com.abnamro.infinity.servlet;

import java.io.*;
import java.net.*;
import java.util.*;

import com.ibm.xml.parser.*;
import org.w3c.dom.*;
import com.lotus.xsl.*;
import com.lotus.xsl.xml4j.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class xml2html extends HttpServlet
{
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
        // Right now there is nothing to do herex
    }

    public void destroy()
    {
        super.destroy();
        // And, nothing to do here yet, either
    }

    protected void doGet(HttpServletRequest req,
                         HttpServletResponse resp) throws ServletException, IOException
    {
        doRequest_(req, resp);
    }

    protected void doPost(HttpServletRequest req,
                          HttpServletResponse resp) throws ServletException, IOException
    {
        doRequest_(req, resp);
    }

    // Purposely do not implement doPut - no ftp-like interface yet
    // Purposely do not implement doDelete - no delete interface yet

    private void doRequest_(HttpServletRequest req,
                            HttpServletResponse resp) throws ServletException, IOException
    {
        String xmlFileName = req.getParameter(XML_);
        String xslFileName = req.getParameter(XSL_);
        PrintWriter printer = new PrintWriter(resp.getWriter());
        if (xmlFileName != null &&
            xslFileName != null)
        {
            resp.setContentType("text/html");
            process(xmlFileName, xslFileName, printer);
        }
        else
        {
            resp.setContentType("text/html");
            resp.getWriter().write("<HTML><HEAD><TITLE>Mistake in Request</TITLE></HEAD>");
            resp.getWriter().write("<BODY><H1>Mistake in using xml2html</H1>");
            resp.getWriter().write("XML and XSL are both required parameters to the servlet</BODY></HTML>");
        }
    }

    public void process(String inputFileName, String xslFileName, PrintWriter pr)
    {
        boolean anErrorOccurred = false;
        ProcessXSL xmlProcessorLiason = null;
        XSLProcessor p = null;
        try
        {
            xmlProcessorLiason = new ProcessXSL();
            xmlProcessorLiason.m_use_validation = false;
            p = new XSLProcessor(xmlProcessorLiason);
            try
            {
                String resultns = new String("DEBUG");                          // DEBUG marks error messages
                p.process(inputFileName, xslFileName, resultns, pr);            // Process to our print writer here
            }
            catch (Exception e)
            {
                e.printStackTrace(pr);                                          // Post our exception back to the web page
            }
        }
        catch (Exception e)
        {
            e.printStackTrace(pr);                                              // Post our exception back to the web page
        }
    }

    public static void main(String [] argv)
    {
        xml2html x = new xml2html();
        PrintWriter printer = new PrintWriter(System.out);
        x.process(argv[0], argv[1], printer);
    }

    private static final String XML_ = "XML";
    private static final String XSL_ = "XSL";
}
