]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Switch from non-JAXP DOM processing to using JAXP identity transformation
authorGlen Mazza <gmazza@apache.org>
Wed, 7 Jul 2004 22:15:10 +0000 (22:15 +0000)
committerGlen Mazza <gmazza@apache.org>
Wed, 7 Jul 2004 22:15:10 +0000 (22:15 +0000)
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
examples/embedding/java/embedding/ExampleDOM2PDF.java
examples/embedding/java/embedding/ExampleXML2PDF.java

index 203d92a1dfee6cfb920613a7f654eb6da8c7b5c2..c505475b117312b2be92dc0b92a91c8d45008850 100644 (file)
                </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"/>
                </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"/>
index 5531cc2520119d699d5626b69f6212e014ffb85a..674a11191f10bd91841cd9345cb8bb97034754ee 100644 (file)
@@ -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);
         }
+
     }
 
     /**
index bb16b6528bdd86da6355a7413454b3d9f4dd2b83..ec2e065f4c5f8892ea8d34beb3bc3917d62b7dbf 100644 (file)
@@ -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);