import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.xml.sax.InputSource;
import org.apache.fop.apps.Driver;
import javax.xml.transform.stream.*;
import javax.xml.transform.*;

public class SimpleServlet extends HttpServlet {

    private static final String FO_REQUEST_PARAM = "fo";
    private static final String XML_REQUEST_PARAM = "xml";
    private static final String XSL_REQUEST_PARAM = "xsl";

    // you might want to override init() and set up a log4j category for the servlet so you can easily add debugging
    // code

    public void doGet(HttpServletRequest request, HttpServletResponse
		      response) throws ServletException, IOException {
	    System.out.println("fop");
	try {
	    // you might want to set up a constant to represent thedirectory path or pull the directory path
	    // from a configuration file, otherwise you have toasupply the path in the get parameter
            String foParam = request.getParameter(FO_REQUEST_PARAM);
            String xmlParam = request.getParameter(XML_REQUEST_PARAM);
            String xslParam = request.getParameter(XSL_REQUEST_PARAM);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            InputSource input = null;

            // set content type as pdf so browser will know it's getting pdf content and display it properly
            response.setContentType("application/pdf");

            if ((request.getParameter(FO_REQUEST_PARAM)) != null) {
                FileInputStream fo = new FileInputStream(foParam);
                input = new InputSource(fo);
            } else if(((request.getParameter(XML_REQUEST_PARAM)) != null) &&
		      ((request.getParameter(XSL_REQUEST_PARAM)) != null)) {
            	File xml = new File(xmlParam);
            	File xsl = new File(xslParam);
            	Source xmlSource = new StreamSource(xml);
		Source xslSource = new StreamSource(xsl);
		StringWriter sw = new StringWriter();
		TransformerFactory tFactory =
		    TransformerFactory.newInstance();
		    //create Templates so the xsl document will function as a template and will be cached in
		    //order to optimize performance
		Templates cachedXSLT = tFactory.newTemplates(xslSource);
		Transformer transformer = cachedXSLT.newTransformer();
		//transform the xml we've been given using the cached xsl
		transformer.transform(xmlSource, new StreamResult(sw));
		Reader r = new StringReader(sw.toString());
		System.out.println("sw: " + sw.toString());
		input = new InputSource(r);
            } else {
				// handle situation:  get parameters are not correct
            }
	    System.out.println("YO, create file?");
	    if (input != null) {
		Driver driver = new Driver(input, out);
		driver.setRenderer(Driver.RENDER_PDF);
		driver.run();
	    }
            byte[] content = out.toByteArray();

	    // we convert this to base64 and attach in the pdf
            //send content to the browser now
	    File f = new File("c:/temp/foo.pdf");
	    FileOutputStream os = new FileOutputStream(f);
	    os.write(content);
	    os.flush();
	    os.close();

	    response.setContentLength(content.length);
	    response.getOutputStream().write(content);
	    response.getOutputStream().flush();
        } catch (Exception e) {
            // handle exceptions.
	    e.printStackTrace();
        }
    }

    // pass post data received to get for processing
    public void doPost(HttpServletRequest request,
		       HttpServletResponse
		       response)
	throws IOException, ServletException
    {
	doGet(request, response);
    }
}
