001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.publishing.renderers.impl;
020:
021: import java.io.*;
022:
023: import javax.xml.transform.*;
024: import javax.xml.transform.dom.*;
025: import javax.xml.transform.stream.*;
026:
027: import org.apache.avalon.framework.logger.*;
028: import org.apache.fop.apps.*;
029: import org.openharmonise.commons.xml.*;
030: import org.openharmonise.rm.config.*;
031: import org.openharmonise.rm.publishing.renderers.*;
032: import org.xml.sax.*;
033:
034: /**
035: *
036: * Implementation of <code>PageRenderer</code> to handle the rendering
037: * of <code>WebPage</code>s by a FOP processor, usually to PDF format.
038: *
039: * @author Michael Bell
040: * @version $Revision: 1.2 $
041: *
042: */
043: public class FOPRenderer implements PageRenderer {
044:
045: private static String RESOURCES_ROOT_PNAME = "RESOURCES_ROOT";
046:
047: static {
048: //Configure base DIR for FOP
049: String sResourcesRoot;
050: try {
051: sResourcesRoot = ConfigSettings.getProperty(
052: RESOURCES_ROOT_PNAME, "/");
053: } catch (ConfigException e) {
054: throw new ConfigRuntimeException(
055: "Configuration error getting RESOURCES_ROOT_PNAME value",
056: e);
057: }
058:
059: org.apache.fop.configuration.Configuration.put("baseDir",
060: sResourcesRoot);
061: }
062:
063: /**
064: *
065: */
066: public FOPRenderer() {
067: super ();
068:
069: }
070:
071: /* (non-Javadoc)
072: * @see org.openharmonise.rm.publishing.renderers.PageRenderer#render(org.openharmonise.commons.xml.XMLDocument, javax.xml.transform.Templates, java.io.OutputStream)
073: */
074: public void render(XMLDocument xdoc, Templates templates,
075: OutputStream out) throws RenderException {
076: try {
077: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
078:
079: Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
080:
081: Transformer trans = templates.newTransformer();
082: DOMSource ds = new DOMSource(xdoc.getDocumentElement());
083: StreamResult res = new StreamResult(outputStream);
084: trans.transform(ds, res);
085:
086: ByteArrayInputStream inputStream = new ByteArrayInputStream(
087: outputStream.toByteArray());
088:
089: InputStreamReader isr = new InputStreamReader(inputStream,
090: "UTF-8");
091:
092: InputSource input = new InputSource(isr);
093:
094: Driver driver = new Driver(input, out);
095:
096: driver.setLogger(log);
097: driver.setRenderer(Driver.RENDER_PDF);
098:
099: //TODO this should be handled some other way
100: File userConfigFile = new File("userconfig.xml");
101: Options options = new Options(userConfigFile);
102:
103: org.apache.fop.render.Renderer renderer = driver
104: .getRenderer();
105: driver.run();
106: } catch (TransformerConfigurationException e) {
107: throw new RenderException(
108: "Transformer configuration exception", e);
109: } catch (UnsupportedEncodingException e) {
110: throw new RenderException("Unsupported Encoding Exception",
111: e);
112: } catch (TransformerException e) {
113: throw new RenderException("Transformer Exception", e);
114: } catch (FOPException e) {
115: throw new RenderException("FOP Exception", e);
116: } catch (IOException e) {
117: throw new RenderException("IO Exception", e);
118: }
119:
120: }
121:
122: }
|