From a60f0d8086dbb4f82c4bc056b49df00ef02c6406 Mon Sep 17 00:00:00 2001 From: Glen Mazza Date: Wed, 14 Jul 2004 22:42:29 +0000 Subject: [PATCH] Updated FO->PDF example to use JAXP. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@197788 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/embedding/ExampleFO2PDF.java | 49 ++++++++++++------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/examples/embedding/java/embedding/ExampleFO2PDF.java b/examples/embedding/java/embedding/ExampleFO2PDF.java index 9238c36ae..939803e0d 100644 --- a/examples/embedding/java/embedding/ExampleFO2PDF.java +++ b/examples/embedding/java/embedding/ExampleFO2PDF.java @@ -24,8 +24,13 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -// SAX -import org.xml.sax.InputSource; +//JAXP +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.Source; +import javax.xml.transform.Result; +import javax.xml.transform.stream.StreamSource; +import javax.xml.transform.sax.SAXResult; // FOP import org.apache.fop.apps.Driver; @@ -45,28 +50,34 @@ public class ExampleFO2PDF { */ public void convertFO2PDF(File fo, File pdf) throws IOException, FOPException { - // Construct driver - Driver driver = new Driver(); + OutputStream out = null; - // Setup Renderer (output format) - driver.setRenderer(Driver.RENDER_PDF); - - // Setup output - OutputStream out = new java.io.FileOutputStream(pdf); - out = new java.io.BufferedOutputStream(out); try { + // Construct driver and setup output format + Driver driver = new Driver(); + driver.setRenderer(Driver.RENDER_PDF); + + // Setup output + out = new java.io.FileOutputStream(pdf); + out = new java.io.BufferedOutputStream(out); driver.setOutputStream(out); - //Setup input - InputStream in = new java.io.FileInputStream(fo); - try { - driver.setInputSource(new InputSource(in)); + // Setup JAXP using identity transformer + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer = factory.newTransformer(); // identity transformer + + // Setup input for XSLT transformation + Source src = new StreamSource(fo); - //Process FO - driver.run(); - } finally { - in.close(); - } + // Resulting SAX events (the generated FO) must be piped through to FOP + Result res = new SAXResult(driver.getContentHandler()); + + // Start XSLT transformation and FOP processing + transformer.transform(src, res); + + } catch (Exception e) { + e.printStackTrace(System.err); + System.exit(-1); } finally { out.close(); } -- 2.39.5