KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > screen > ScreenFopPdfViewHandler


1 /*
2  * $Id: ScreenFopPdfViewHandler.java 6548 2006-01-23 07:36:14Z jaz $
3  *
4  * Copyright (c) 2003-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.widget.screen;
26
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.StringWriter JavaDoc;
30 import java.io.Writer JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.apache.avalon.framework.logger.Log4JLogger;
36 import org.apache.avalon.framework.logger.Logger;
37 import org.apache.fop.apps.Driver;
38 import org.apache.fop.image.FopImageFactory;
39 import org.apache.fop.messaging.MessageHandler;
40 import org.apache.fop.tools.DocumentInputSource;
41 import org.ofbiz.base.util.Debug;
42 import org.ofbiz.base.util.UtilXml;
43 import org.ofbiz.webapp.view.ViewHandlerException;
44 import org.w3c.dom.Document JavaDoc;
45 import org.xml.sax.InputSource JavaDoc;
46
47 /**
48  * Uses XSL-FO formatted templates to generate PDF views
49  * This handler will use JPublish to generate the XSL-FO
50  *
51  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
52  * @version $Rev: 6548 $
53  * @since 3.0
54  */

55 public class ScreenFopPdfViewHandler extends ScreenWidgetViewHandler {
56     
57     public static final String JavaDoc module = ScreenFopPdfViewHandler.class.getName();
58     
59     /**
60      * @see org.ofbiz.content.webapp.view.ViewHandler#render(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
61      */

62     public void render(String JavaDoc name, String JavaDoc page, String JavaDoc info, String JavaDoc contentType, String JavaDoc encoding, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ViewHandlerException {
63         // render and obtain the XSL-FO
64
Writer JavaDoc writer = new StringWriter JavaDoc();
65         try {
66             ScreenRenderer screens = new ScreenRenderer(writer, null, htmlScreenRenderer);
67             screens.populateContextForRequest(request, response, servletContext);
68             screens.render(page);
69         } catch (Throwable JavaDoc t) {
70             throw new ViewHandlerException("Problems with the response writer/output stream", t);
71         }
72         if (Debug.verboseOn()) {
73            // Debug.logVerbose("XSL-FO : " + writer.toString(), module);
74
}
75         
76         // configure logging for the FOP
77
Logger logger = new Log4JLogger(Debug.getLogger(module));
78         MessageHandler.setScreenLogger(logger);
79                           
80         // load the FOP driver
81
Driver driver = new Driver();
82         driver.setRenderer(Driver.RENDER_PDF);
83         driver.setLogger(logger);
84                                         
85         /*
86         try {
87             String buf = writer.toString();
88             java.io.FileWriter fw = new java.io.FileWriter(new java.io.File("/tmp/xslfo.out"));
89             fw.write(buf.toString());
90             fw.close();
91         } catch (IOException e) {
92             throw new ViewHandlerException("Unable write to browser OutputStream", e);
93         }
94         */

95         // read the XSL-FO XML Document
96
Document JavaDoc xslfo = null;
97         try {
98             xslfo = UtilXml.readXmlDocument(writer.toString());
99         } catch (Throwable JavaDoc t) {
100             throw new ViewHandlerException("Problems reading the parsed content to XML Document", t);
101         }
102         
103         // create the output stream for the PDF
104
ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
105         driver.setOutputStream(out);
106                 
107         // set the input source (XSL-FO) and generate the PDF
108
InputSource is = new DocumentInputSource(xslfo);
109         driver.setInputSource(is);
110         try {
111             driver.run();
112             FopImageFactory.resetCache();
113         } catch (Throwable JavaDoc t) {
114             throw new ViewHandlerException("Unable to generate PDF from XSL-FO", t);
115         }
116                   
117         // set the content type and length
118
response.setContentType("application/pdf");
119         response.setContentLength(out.size());
120         
121         // write to the browser
122
try {
123             out.writeTo(response.getOutputStream());
124             response.getOutputStream().flush();
125         } catch (IOException JavaDoc e) {
126             throw new ViewHandlerException("Unable write to browser OutputStream", e);
127         }
128     }
129 }
130
Popular Tags