import java.io.*; import java.util.*; import java.lang.String; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse ; import javax.xml.transform.*; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXSource; import org.xml.sax.InputSource; import org.apache.fop.apps.Driver; import org.apache.fop.apps.XSLTInputHandler; import org.apache.fop.messaging.MessageHandler; import org.apache.avalon.framework.ExceptionUtil; import org.apache.avalon.framework.logger.*; import org.apache.fop.apps.*; public class FopServlet extends HttpServlet { Logger log = null; public void init() throws ServletException { ServletContext ctx = getServletContext(); org.apache.fop.configuration.Configuration.put("baseDir",ctx.getRealPath("/")); try { File userConfigFile = new File(ctx.getRealPath("/conf/userconfig.xml")); Options option = new Options(userConfigFile); } catch (Exception ex) { throw new ServletException(ex); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Declaration String contenuDoc = null; String xmlData = null; byte[] content = null; ByteArrayOutputStream out; response.setContentType("application/pdf"); // Initialisation out = null; try { //Inputstream lesen //int txContentLength = request.getContentLength(); //InputStream inp = request.getInputStream(); //BufferedReader br = new BufferedReader(new InputStreamReader(inp)); //byte[] ba = new byte[txContentLength]; //int bytesRead = inp.read(ba); //while(bytesRead < txContentLength) { //int moreRead = inp.read(ba, bytesRead, (ba.length - bytesRead)); //if(moreRead == -1) break; //bytesRead += moreRead;} //xmlData = new String(new String(ba)); xmlData = new String(request.getParameter("XML").getBytes(), "UTF-8"); //PDF erzeugen convertXML(xmlData, response); } catch (Exception ex) { throw new ServletException(ex); } } public void convertXML(String xmlData, HttpServletResponse response) throws Exception { // Declaration Transformer transformer; Source src; Result res; ByteArrayOutputStream out; Driver driver; Logger logger; byte[] content; // Initialisation transformer = null; src = null; res = null; out = null; driver = null; logger = null; content = null; ServletContext ctx = getServletContext(); File xslParam = new File (ctx.getRealPath("transform.xsl")); try { out = new ByteArrayOutputStream(); driver = new Driver(); // Setup logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO); driver.setLogger(logger); MessageHandler.setScreenLogger(logger); driver.setRenderer(Driver.RENDER_PDF); driver.setOutputStream(out); System.out.println("PDF-TransformerFactory vorbereiten"); transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xslParam)); src = new StreamSource(new StringReader(xmlData)); System.out.println("Weiter an FOP"); res = new SAXResult(driver.getContentHandler()); transformer.transform(src, res); System.out.println("PDF-TransformerFactory abgeschlossen"); content = out.toByteArray(); System.out.println("PDF-Ausgabe vorbereiten"); response.setContentType("application/pdf"); response.setContentLength(content.length); response.getOutputStream().write(content); System.out.println("PDF-Ende"); response.getOutputStream().flush(); System.out.println("Ende");} catch (IOException e) {throw e;} catch (TransformerException e) {throw e;} } }
//write("<meta http-equiv='content-type' content='text/html; charset=utf-8'>\n</head>\n"); write("<body>\n"); write("<form name='vorschau' accept-charset='utf-8' enctype='text/xml' method='post' action='http://xx.xx.x.xx:8080/fop/'>\n"); write("<input type='hidden' size='100%' name='XML' value=\""+eingabe+"\">\n");