diff options
author | Glen Mazza <gmazza@apache.org> | 2004-07-07 22:15:10 +0000 |
---|---|---|
committer | Glen Mazza <gmazza@apache.org> | 2004-07-07 22:15:10 +0000 |
commit | ed53ed2ac6ef7c26569f11c0df22ba4e841f43ce (patch) | |
tree | d04c3e03a4d25d804c002f2bd80c975aa7513da8 /examples/embedding | |
parent | 18a28ab107b07c5ba0fee9a57ede8fb00b2fbaa3 (diff) | |
download | xmlgraphics-fop-ed53ed2ac6ef7c26569f11c0df22ba4e841f43ce.tar.gz xmlgraphics-fop-ed53ed2ac6ef7c26569f11c0df22ba4e841f43ce.zip |
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
Diffstat (limited to 'examples/embedding')
-rw-r--r-- | examples/embedding/build.xml | 4 | ||||
-rw-r--r-- | examples/embedding/java/embedding/ExampleDOM2PDF.java | 67 | ||||
-rw-r--r-- | examples/embedding/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 @@ </java> </target> <target name="example6" depends="compile" description="Runs the DOM to PDF example"> - <echo message="Running the Object to PDF example"/> + <echo message="Running the DOM to PDF example"/> <java classname="${name}.ExampleDOM2PDF" fork="yes"> <classpath> <path refid="project.class.path"/> @@ -130,7 +130,7 @@ </java> </target> <target name="example7" depends="compile" description="Runs the SVG to PDF example"> - <echo message="Running the Object to PDF example"/> + <echo message="Running the SVG to PDF example"/> <java classname="${name}.ExampleSVG2PDF" fork="yes"> <classpath> <path refid="project.class.path"/> 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); |