KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > view > FopRenderer


1 /*
2  * $Id: FopRenderer.java 6535 2006-01-22 03:53:59Z jaz $
3  *
4  * Copyright (c) 2001-2006 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.webapp.view;
26
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.Writer JavaDoc;
29
30 import org.apache.avalon.framework.logger.Logger;
31 import org.apache.avalon.framework.logger.Log4JLogger;
32 import org.apache.fop.messaging.MessageHandler;
33 import org.apache.fop.apps.Driver;
34 import org.apache.fop.tools.DocumentInputSource;
35 import org.apache.fop.image.FopImageFactory;
36 import org.w3c.dom.Document JavaDoc;
37 import org.xml.sax.InputSource JavaDoc;
38
39 import org.ofbiz.base.util.Debug;
40 import org.ofbiz.base.util.UtilXml;
41 import org.ofbiz.base.util.GeneralException;
42
43 /**
44  * FopRenderer
45  *
46  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
47  * @version $Rev: 6535 $
48  * @since 3.5
49  */

50 public class FopRenderer {
51
52     public static final String JavaDoc module = FopRenderer.class.getName();
53
54     public static ByteArrayOutputStream JavaDoc render(Writer JavaDoc writer) throws GeneralException {
55         // configure logging for the FOP
56
Logger logger = new Log4JLogger(Debug.getLogger(module));
57         MessageHandler.setScreenLogger(logger);
58
59         // load the FOP driver
60
Driver driver = new Driver();
61         driver.setRenderer(Driver.RENDER_PDF);
62         driver.setLogger(logger);
63
64         /*
65         try {
66             String buf = writer.toString();
67             java.io.FileWriter fw = new java.io.FileWriter(new java.io.File("/tmp/xslfo.out"));
68             fw.write(buf.toString());
69             fw.close();
70         } catch (IOException e) {
71             throw new GeneralException("Unable write to browser OutputStream", e);
72         }
73         */

74
75         // read the XSL-FO XML Document
76
Document JavaDoc xslfo = null;
77         try {
78             xslfo = UtilXml.readXmlDocument(writer.toString());
79         } catch (Throwable JavaDoc t) {
80             throw new GeneralException("Problems reading the parsed content to XML Document", t);
81         }
82
83         // create the output stream for the PDF
84
ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
85         driver.setOutputStream(out);
86
87         // set the input source (XSL-FO) and generate the PDF
88
InputSource is = new DocumentInputSource(xslfo);
89         driver.setInputSource(is);
90         try {
91             driver.run();
92             FopImageFactory.resetCache();
93         } catch (Throwable JavaDoc t) {
94             throw new GeneralException("Unable to generate PDF from XSL-FO", t);
95         }
96
97         return out;
98     }
99
100 }
101
Popular Tags