import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import org.apache.fop.apps.Driver;
import org.apache.fop.apps.Version;
import org.apache.fop.apps.XSLTInputHandler;

import org.apache.log.*;

/**
 * Insert the type's description here.
 * Creation date: (2/26/2002 4:01:54 PM)
 * @author: Administrator
 */
 

public class  ServletCreatePdf extends HttpServlet {
	public static final String FO_REQUEST_PARAM = null;
	public static final String XML_REQUEST_PARAM = "C:\\ESub\\FOP\\Fop203\\fop0203\\test-fod.xml";
	public static final String XSL_REQUEST_PARAM = "C:\\ESub\\FOP\\Fop203\\fop0203\\test-fod.xsl";
	Logger log = null;

/**
 * Insert the method's description here.
 * Creation date: (4/30/2002 11:43:49 AM)
 */
/**
	 * creates a SAX parser, using the value of org.xml.sax.parser
	 * defaulting to org.apache.xerces.parsers.SAXParser
	 *
	 * @return the created SAX parser
	 */
	static XMLReader createParser() throws ServletException {
		String parserClassName = System.getProperty("org.xml.sax.parser");
		if (parserClassName == null) {
		 parserClassName = "org.apache.xerces.parsers.SAXParser";
		}

		try {
			return (XMLReader) Class.forName(
					 parserClassName).newInstance();
		} catch (Exception e) {
			throw new ServletException(e);
		}
	}
	public void renderFO(InputSource foFile,
						 HttpServletResponse response) throws ServletException {
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();

			response.setContentType("application/pdf");

			Driver driver = new Driver(foFile, out);
			driver.setLogger(log);
			driver.setRenderer(Driver.RENDER_PDF);
			driver.run();

			byte[] content = out.toByteArray();
			response.setContentLength(content.length);
			response.getOutputStream().write(content);
			response.getOutputStream().flush();
		} catch (Exception ex) {
			throw new ServletException(ex);
		}
	}
	public void renderXML(XSLTInputHandler input,
						 HttpServletResponse response) throws ServletException {
		try {
			
			ByteArrayOutputStream out = new ByteArrayOutputStream();

			response.setContentType("application/pdf");
			
	response.setHeader("Content-disposition",
	  "inline; filename=\"ServletCreatePdf.pdf\"");

			Driver driver = new Driver();
			driver.setLogger(log);
			driver.setRenderer(Driver.RENDER_PDF);
			driver.setOutputStream(out);
		//	driver.render(input.getParser(), input.getInputSource());
			driver.render(createParser(), input.getInputSource());

			byte[] content = out.toByteArray();
			response.setContentLength(content.length);
			response.getOutputStream().write(content);
			response.getOutputStream().flush(); 
		} catch (Exception ex) {
			throw new ServletException(ex);
		} 
	}
/**
 * service method comment.
 */
public void service(HttpServletRequest request, HttpServletResponse response) 
throws javax.servlet.ServletException, java.io.IOException{
	
	if(log == null) {

			Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
			log = hierarchy.getLoggerFor("fop");
			log.setPriority(Priority.WARN);
		}
				
		try {
			ByteArrayOutputStream ot = new ByteArrayOutputStream();

			response.setContentType("application/pdf");
			
	//response.setHeader("Content-disposition",
	//  "inline; filename=\"ServletCreatePdf.pdf\"");
			//String foParam = request.getParameter(FO_REQUEST_PARAM);
			//String xmlParam = request.getParameter(XML_REQUEST_PARAM);
			//String xslParam = request.getParameter(XSL_REQUEST_PARAM);
			String xmlParam = "C:\\Root_Developpement\\esdp\\ihm\\Test-fod.xml";
			String xslParam = "C:\\Root_Developpement\\esdp\\ihm\\test-fod.xsl";
			String foParam = null; //"C:\\Root_Developpement\\esdp\\ihm\\test.fo";

			if (foParam != null) {
				FileInputStream file = new FileInputStream(foParam);
				renderFO(new InputSource(file), response);
			} else 
		if((xmlParam != null) && (xslParam != null)) {

				XSLTInputHandler input = new XSLTInputHandler(new File(xmlParam), new File(xslParam));
				renderXML(input, response);				
			} else {
				PrintWriter out = response.getWriter();
				out.println("<html><head><title>Error</title></head>\n"+
							"<body><h1>FopServlet Error</h1><h3>No 'fo' "+
							"request param given.</body></html>");
			} 

		}
		/*catch (ServletException ex) {
			throw ex;
		}*/
		catch (Exception ex) {
			throw new ServletException(ex);
			
		} 

	}
}
