KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > publishing > renderers > impl > FOPRenderer


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.publishing.renderers.impl;
20
21 import java.io.*;
22
23 import javax.xml.transform.*;
24 import javax.xml.transform.dom.*;
25 import javax.xml.transform.stream.*;
26
27 import org.apache.avalon.framework.logger.*;
28 import org.apache.fop.apps.*;
29 import org.openharmonise.commons.xml.*;
30 import org.openharmonise.rm.config.*;
31 import org.openharmonise.rm.publishing.renderers.*;
32 import org.xml.sax.*;
33
34
35
36 /**
37  *
38  * Implementation of <code>PageRenderer</code> to handle the rendering
39  * of <code>WebPage</code>s by a FOP processor, usually to PDF format.
40  *
41  * @author Michael Bell
42  * @version $Revision: 1.2 $
43  *
44  */

45 public class FOPRenderer implements PageRenderer {
46
47     private static String JavaDoc RESOURCES_ROOT_PNAME = "RESOURCES_ROOT";
48
49     static {
50         //Configure base DIR for FOP
51
String JavaDoc sResourcesRoot;
52         try {
53             sResourcesRoot =
54                 ConfigSettings.getProperty(RESOURCES_ROOT_PNAME, "/");
55         } catch (ConfigException e) {
56             throw new ConfigRuntimeException("Configuration error getting RESOURCES_ROOT_PNAME value",e);
57         }
58
59         org.apache.fop.configuration.Configuration.put(
60             "baseDir",
61             sResourcesRoot);
62     }
63
64     /**
65      *
66      */

67     public FOPRenderer() {
68         super();
69         
70     }
71
72     /* (non-Javadoc)
73      * @see org.openharmonise.rm.publishing.renderers.PageRenderer#render(org.openharmonise.commons.xml.XMLDocument, javax.xml.transform.Templates, java.io.OutputStream)
74      */

75     public void render(
76         XMLDocument xdoc,
77         Templates templates,
78         OutputStream out) throws RenderException {
79             try {
80                 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
81             
82                 Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
83             
84                 Transformer trans = templates.newTransformer();
85                 DOMSource ds = new DOMSource(xdoc.getDocumentElement());
86                 StreamResult res = new StreamResult(outputStream);
87                 trans.transform(ds, res);
88             
89                 ByteArrayInputStream inputStream = new ByteArrayInputStream(
90                                                            outputStream.toByteArray());
91             
92                 InputStreamReader isr = new InputStreamReader(inputStream, "UTF-8");
93             
94                 InputSource input = new InputSource(isr);
95             
96                 Driver driver = new Driver(input, out);
97             
98                 driver.setLogger(log);
99                 driver.setRenderer(Driver.RENDER_PDF);
100             
101                 //TODO this should be handled some other way
102
File userConfigFile = new File("userconfig.xml");
103                 Options options = new Options(userConfigFile);
104             
105                 org.apache.fop.render.Renderer renderer = driver.getRenderer();
106                 driver.run();
107             } catch (TransformerConfigurationException e) {
108                 throw new RenderException("Transformer configuration exception",e);
109             } catch (UnsupportedEncodingException e) {
110                 throw new RenderException("Unsupported Encoding Exception",e);
111             } catch (TransformerException e) {
112                 throw new RenderException("Transformer Exception",e);
113             } catch (FOPException e) {
114                 throw new RenderException("FOP Exception",e);
115             } catch (IOException e) {
116                 throw new RenderException("IO Exception",e);
117             }
118             
119             
120     
121     }
122     
123
124 }
125
Popular Tags