From ed53ed2ac6ef7c26569f11c0df22ba4e841f43ce Mon Sep 17 00:00:00 2001 From: Glen Mazza Date: Wed, 7 Jul 2004 22:15:10 +0000 Subject: [PATCH] Switch from non-JAXP DOM processing to using JAXP identity transformation for ExampleDOM2PDF example. Patch from Jeremias Maerki. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@197764 13f79535-47bb-0310-9956-ffa450edef68 --- examples/embedding/build.xml | 4 +- .../java/embedding/ExampleDOM2PDF.java | 67 +++++++++++++------ .../java/embedding/ExampleXML2PDF.java | 2 +- 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/examples/embedding/build.xml b/examples/embedding/build.xml index 203d92a1d..c505475b1 100644 --- a/examples/embedding/build.xml +++ b/examples/embedding/build.xml @@ -121,7 +121,7 @@ - + @@ -130,7 +130,7 @@ - + diff --git a/examples/embedding/java/embedding/ExampleDOM2PDF.java b/examples/embedding/java/embedding/ExampleDOM2PDF.java index 5531cc252..674a11191 100644 --- a/examples/embedding/java/embedding/ExampleDOM2PDF.java +++ b/examples/embedding/java/embedding/ExampleDOM2PDF.java @@ -25,6 +25,14 @@ import java.io.OutputStream; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; +//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.dom.DOMSource; +import javax.xml.transform.sax.SAXResult; + // DOM import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -33,7 +41,7 @@ import org.w3c.dom.Text; // FOP import org.apache.fop.apps.Driver; -import org.apache.fop.apps.FOPException; + /** * This class demonstrates the conversion of a DOM Document to PDF @@ -51,27 +59,46 @@ public class ExampleDOM2PDF { * @throws IOException In case of an I/O problem * @throws FOPException In case of a FOP problem */ - public void convertDOM2PDF(Document xslfoDoc, File pdf) - throws IOException, FOPException { - // Construct driver - Driver driver = new Driver(); - - // Setup driver - driver.initialize(); - - // Setup Renderer (output format) - driver.setRenderer(Driver.RENDER_PDF); - - // Setup output - OutputStream out = new java.io.FileOutputStream(pdf); - out = new java.io.BufferedOutputStream(out); - + public void convertDOM2PDF(Document xslfoDoc, File pdf) { try { - driver.setOutputStream(out); - driver.render(xslfoDoc); - } finally { - out.close(); + // Construct driver + Driver driver = new Driver(); + + // Setup driver + driver.initialize(); + + // 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 { + driver.setOutputStream(out); + + // Setup Identity Transformer + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer = factory.newTransformer(); // identity transformer + + // Setup input for XSLT transformation + Source src = new DOMSource(xslfoDoc); + + // 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); + } finally { + out.close(); + } + + System.out.println("Success!"); + } catch (Exception e) { + e.printStackTrace(System.err); + System.exit(-1); } + } /** diff --git a/examples/embedding/java/embedding/ExampleXML2PDF.java b/examples/embedding/java/embedding/ExampleXML2PDF.java index bb16b6528..ec2e065f4 100644 --- a/examples/embedding/java/embedding/ExampleXML2PDF.java +++ b/examples/embedding/java/embedding/ExampleXML2PDF.java @@ -26,7 +26,6 @@ import java.io.OutputStream; //JAXP import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; -import javax.xml.transform.TransformerException; import javax.xml.transform.Source; import javax.xml.transform.Result; import javax.xml.transform.stream.StreamSource; @@ -79,6 +78,7 @@ public class ExampleXML2PDF { // Setup output OutputStream out = new java.io.FileOutputStream(pdffile); out = new java.io.BufferedOutputStream(out); + try { driver.setOutputStream(out); -- 2.39.5