aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/embedding/java/embedding/ExampleAWTViewer.java20
-rw-r--r--examples/embedding/java/embedding/ExampleDOM2PDF.java94
-rw-r--r--examples/embedding/java/embedding/ExampleFO2OldStylePrint.java15
-rw-r--r--examples/embedding/java/embedding/ExampleFO2PDF.java15
-rw-r--r--examples/embedding/java/embedding/ExampleFO2PDFUsingSAXParser.java27
-rw-r--r--examples/embedding/java/embedding/ExampleFO2RTF.java19
-rw-r--r--examples/embedding/java/embedding/ExampleObj2PDF.java14
-rw-r--r--examples/embedding/java/embedding/ExampleXML2PDF.java16
-rw-r--r--examples/embedding/java/embedding/MultipleFO2PDF.java173
-rw-r--r--examples/embedding/java/embedding/intermediate/ExampleConcat.java14
-rw-r--r--examples/embedding/java/embedding/intermediate/ExampleStamp.java6
11 files changed, 326 insertions, 87 deletions
diff --git a/examples/embedding/java/embedding/ExampleAWTViewer.java b/examples/embedding/java/embedding/ExampleAWTViewer.java
index 2e25f6985..7749b7fa2 100644
--- a/examples/embedding/java/embedding/ExampleAWTViewer.java
+++ b/examples/embedding/java/embedding/ExampleAWTViewer.java
@@ -1,5 +1,5 @@
/*
- * Copyright 1999-2003,2005 The Apache Software Foundation.
+ * Copyright 1999-2003,2005-2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,19 +37,29 @@ import org.apache.avalon.framework.ExceptionUtil;
//FOP
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.Fop;
+import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
-import org.apache.fop.fo.Constants;
/**
* This class demonstrates the use of the AWT Viewer.
*/
public class ExampleAWTViewer {
+ // configure fopFactory as desired
+ private FopFactory fopFactory = FopFactory.newInstance();
+
+ /**
+ * Display an FO file in the AWT Preview.
+ * @param fo the FO file
+ * @throws IOException In case of an I/O problem
+ * @throws FOPException In case of a problem during layout
+ * @throws TransformerException In case of a problem during XML processing
+ */
public void viewFO(File fo)
throws IOException, FOPException, TransformerException {
//Setup FOP
- Fop fop = new Fop(MimeConstants.MIME_FOP_AWT_PREVIEW);
+ Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AWT_PREVIEW);
try {
@@ -68,6 +78,10 @@ public class ExampleAWTViewer {
}
}
+ /**
+ * Main method.
+ * @param args the command-line arguments
+ */
public static void main(String[] args) {
try {
System.out.println("FOP ExampleAWTViewer\n");
diff --git a/examples/embedding/java/embedding/ExampleDOM2PDF.java b/examples/embedding/java/embedding/ExampleDOM2PDF.java
index b46912a61..84c56c142 100644
--- a/examples/embedding/java/embedding/ExampleDOM2PDF.java
+++ b/examples/embedding/java/embedding/ExampleDOM2PDF.java
@@ -1,5 +1,5 @@
/*
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-2004, 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,10 +20,10 @@ package embedding;
// Java
import java.io.File;
-import java.io.IOException;
import java.io.OutputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.ParserConfigurationException;
//JAXP
import javax.xml.transform.Transformer;
@@ -40,7 +40,9 @@ import org.w3c.dom.Node;
import org.w3c.dom.Text;
// FOP
+import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
+import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
@@ -50,6 +52,9 @@ import org.apache.fop.apps.MimeConstants;
*/
public class ExampleDOM2PDF {
+ // configure fopFactory as desired
+ private FopFactory fopFactory = FopFactory.newInstance();
+
/** xsl-fo namespace URI */
protected static String foNS = "http://www.w3.org/1999/XSL/Format";
@@ -57,20 +62,19 @@ public class ExampleDOM2PDF {
* Converts a DOM Document to a PDF file using FOP.
* @param xslfoDoc the DOM Document
* @param pdf the target PDF file
- * @throws IOException In case of an I/O problem
- * @throws FOPException In case of a FOP problem
*/
public void convertDOM2PDF(Document xslfoDoc, File pdf) {
try {
- // Construct fop with desired output format
- Fop fop = new Fop(MimeConstants.MIME_PDF);
-
+ FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
+ // configure foUserAgent as desired
+
// Setup output
OutputStream out = new java.io.FileOutputStream(pdf);
out = new java.io.BufferedOutputStream(out);
try {
- fop.setOutputStream(out);
+ // Construct fop with desired output format and output stream
+ Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup Identity Transformer
TransformerFactory factory = TransformerFactory.newInstance();
@@ -113,38 +117,7 @@ public class ExampleDOM2PDF {
System.out.println("PDF Output File: " + pdffile);
System.out.println();
- // Create a sample XSL-FO DOM document
- Document foDoc = null;
- Element root = null, ele1 = null, ele2 = null, ele3 = null;
-
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware(true);
- DocumentBuilder db = dbf.newDocumentBuilder();
- foDoc = db.newDocument();
-
- root = foDoc.createElementNS(foNS, "fo:root");
- foDoc.appendChild(root);
-
- ele1 = foDoc.createElementNS(foNS, "fo:layout-master-set");
- root.appendChild(ele1);
- ele2 = foDoc.createElementNS(foNS, "fo:simple-page-master");
- ele1.appendChild(ele2);
- ele2.setAttributeNS(null, "master-name", "letter");
- ele2.setAttributeNS(null, "page-height", "11in");
- ele2.setAttributeNS(null, "page-width", "8.5in");
- ele2.setAttributeNS(null, "margin-top", "1in");
- ele2.setAttributeNS(null, "margin-bottom", "1in");
- ele2.setAttributeNS(null, "margin-left", "1in");
- ele2.setAttributeNS(null, "margin-right", "1in");
- ele3 = foDoc.createElementNS(foNS, "fo:region-body");
- ele2.appendChild(ele3);
- ele1 = foDoc.createElementNS(foNS, "fo:page-sequence");
- root.appendChild(ele1);
- ele1.setAttributeNS(null, "master-reference", "letter");
- ele2 = foDoc.createElementNS(foNS, "fo:flow");
- ele1.appendChild(ele2);
- ele2.setAttributeNS(null, "flow-name", "xsl-region-body");
- addElement(ele2, "fo:block", "Hello World!");
+ Document foDoc = buildDOMDocument();
ExampleDOM2PDF app = new ExampleDOM2PDF();
app.convertDOM2PDF(foDoc, pdffile);
@@ -158,6 +131,47 @@ public class ExampleDOM2PDF {
}
/**
+ * Builds the example FO document as a DOM in memory.
+ * @return the FO document
+ * @throws ParserConfigurationException In case there is a problem creating a DOM document
+ */
+ private static Document buildDOMDocument() throws ParserConfigurationException {
+ // Create a sample XSL-FO DOM document
+ Document foDoc = null;
+ Element root = null, ele1 = null, ele2 = null, ele3 = null;
+
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ dbf.setNamespaceAware(true);
+ DocumentBuilder db = dbf.newDocumentBuilder();
+ foDoc = db.newDocument();
+
+ root = foDoc.createElementNS(foNS, "fo:root");
+ foDoc.appendChild(root);
+
+ ele1 = foDoc.createElementNS(foNS, "fo:layout-master-set");
+ root.appendChild(ele1);
+ ele2 = foDoc.createElementNS(foNS, "fo:simple-page-master");
+ ele1.appendChild(ele2);
+ ele2.setAttributeNS(null, "master-name", "letter");
+ ele2.setAttributeNS(null, "page-height", "11in");
+ ele2.setAttributeNS(null, "page-width", "8.5in");
+ ele2.setAttributeNS(null, "margin-top", "1in");
+ ele2.setAttributeNS(null, "margin-bottom", "1in");
+ ele2.setAttributeNS(null, "margin-left", "1in");
+ ele2.setAttributeNS(null, "margin-right", "1in");
+ ele3 = foDoc.createElementNS(foNS, "fo:region-body");
+ ele2.appendChild(ele3);
+ ele1 = foDoc.createElementNS(foNS, "fo:page-sequence");
+ root.appendChild(ele1);
+ ele1.setAttributeNS(null, "master-reference", "letter");
+ ele2 = foDoc.createElementNS(foNS, "fo:flow");
+ ele1.appendChild(ele2);
+ ele2.setAttributeNS(null, "flow-name", "xsl-region-body");
+ addElement(ele2, "fo:block", "Hello World!");
+ return foDoc;
+ }
+
+ /**
* Adds an element to the DOM.
* @param parent parent node to attach the new element to
* @param newNodeName name of the new node
diff --git a/examples/embedding/java/embedding/ExampleFO2OldStylePrint.java b/examples/embedding/java/embedding/ExampleFO2OldStylePrint.java
index cc8e2bd91..137367640 100644
--- a/examples/embedding/java/embedding/ExampleFO2OldStylePrint.java
+++ b/examples/embedding/java/embedding/ExampleFO2OldStylePrint.java
@@ -1,5 +1,5 @@
/*
- * Copyright 1999-2005 The Apache Software Foundation.
+ * Copyright 1999-2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@ import javax.xml.transform.sax.SAXResult;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FOPException;
-import org.apache.fop.apps.MimeConstants;
+import org.apache.fop.apps.FopFactory;
import org.apache.fop.render.print.PrintRenderer;
/**
@@ -44,6 +44,9 @@ import org.apache.fop.render.print.PrintRenderer;
*/
public class ExampleFO2OldStylePrint {
+ // configure fopFactory as desired
+ private FopFactory fopFactory = FopFactory.newInstance();
+
/**
* Prints an FO file using an old-style PrinterJob.
* @param fo the FO file
@@ -60,13 +63,11 @@ public class ExampleFO2OldStylePrint {
try {
//Set up a custom user agent so we can supply our own renderer instance
- FOUserAgent userAgent = new FOUserAgent();
+ FOUserAgent userAgent = fopFactory.newFOUserAgent();
userAgent.setRendererOverride(renderer);
- // Construct fop with desired output format
- Fop fop = new Fop(MimeConstants.MIME_FOP_PRINT, userAgent);
- //Note: the first parameter here has no effect if we use
- //FOUserAgent.setRendererOverride()
+ // Construct fop with desired output format (here, it is set through the user agent)
+ Fop fop = fopFactory.newFop(userAgent);
// Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
diff --git a/examples/embedding/java/embedding/ExampleFO2PDF.java b/examples/embedding/java/embedding/ExampleFO2PDF.java
index 32d4de3aa..916d13f36 100644
--- a/examples/embedding/java/embedding/ExampleFO2PDF.java
+++ b/examples/embedding/java/embedding/ExampleFO2PDF.java
@@ -1,5 +1,5 @@
/*
- * Copyright 1999-2005 The Apache Software Foundation.
+ * Copyright 1999-2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,8 +35,10 @@ import javax.xml.transform.sax.SAXResult;
// FOP
+import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FOPException;
+import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.FormattingResults;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.apps.PageSequenceResults;
@@ -46,6 +48,9 @@ import org.apache.fop.apps.PageSequenceResults;
*/
public class ExampleFO2PDF {
+ // configure fopFactory as desired
+ private FopFactory fopFactory = FopFactory.newInstance();
+
/**
* Converts an FO file to a PDF file using FOP
* @param fo the FO file
@@ -58,14 +63,16 @@ public class ExampleFO2PDF {
OutputStream out = null;
try {
- // Construct fop with desired output format
- Fop fop = new Fop(MimeConstants.MIME_PDF);
+ FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
+ // configure foUserAgent as desired
// Setup output stream. Note: Using BufferedOutputStream
// for performance reasons (helpful with FileOutputStreams).
out = new FileOutputStream(pdf);
out = new BufferedOutputStream(out);
- fop.setOutputStream(out);
+
+ // Construct fop with desired output format
+ Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
diff --git a/examples/embedding/java/embedding/ExampleFO2PDFUsingSAXParser.java b/examples/embedding/java/embedding/ExampleFO2PDFUsingSAXParser.java
index 38d1ff6c4..2a1f72a5d 100644
--- a/examples/embedding/java/embedding/ExampleFO2PDFUsingSAXParser.java
+++ b/examples/embedding/java/embedding/ExampleFO2PDFUsingSAXParser.java
@@ -1,5 +1,5 @@
/*
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-2004,2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -36,8 +36,9 @@ import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
// FOP
+import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
-import org.apache.fop.apps.FOPException;
+import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
/**
@@ -46,32 +47,36 @@ import org.apache.fop.apps.MimeConstants;
*/
public class ExampleFO2PDFUsingSAXParser {
+ // configure fopFactory as desired
+ private FopFactory fopFactory = FopFactory.newInstance();
+
/**
* Converts an FO file to a PDF file using FOP
* @param fo the FO file
* @param pdf the target PDF file
- * @throws FactoryConfigurationError
- * @throws ParserConfigurationException
- * @throws SAXException
+ * @throws FactoryConfigurationError In case of a problem with the JAXP factory configuration
+ * @throws ParserConfigurationException In case of a problem with the parser configuration
+ * @throws SAXException In case of a problem during XML processing
* @throws IOException In case of an I/O problem
- * @throws FOPException In case of a FOP problem
*/
public void convertFO2PDF(File fo, File pdf)
throws FactoryConfigurationError,
ParserConfigurationException,
- FOPException, SAXException, IOException {
+ SAXException, IOException {
+
+ FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
+ // configure foUserAgent as desired
OutputStream out = null;
try {
- // Construct fop and setup output format
- Fop fop = new Fop(MimeConstants.MIME_PDF);
-
// Setup output stream. Note: Using BufferedOutputStream
// for performance reasons (helpful with FileOutputStreams).
out = new FileOutputStream(pdf);
out = new BufferedOutputStream(out);
- fop.setOutputStream(out);
+
+ // Construct fop and setup output format
+ Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup SAX parser
// throws FactoryConfigurationError
diff --git a/examples/embedding/java/embedding/ExampleFO2RTF.java b/examples/embedding/java/embedding/ExampleFO2RTF.java
index 8c85f8a4f..9bff99399 100644
--- a/examples/embedding/java/embedding/ExampleFO2RTF.java
+++ b/examples/embedding/java/embedding/ExampleFO2RTF.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2005 The Apache Software Foundation.
+ * Copyright 2005-2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,18 +34,23 @@ import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;
// FOP
+import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FOPException;
+import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
/**
* This class demonstrates the conversion of an FO file to RTF using FOP.
* <p>
* Please note that this is practically the same as the ExampleFO2PDF example. Only
- * one parameter to the Fop contructor is different!
+ * the MIME parameter to the newFop() method is different!
*/
public class ExampleFO2RTF {
+ // configure fopFactory as desired
+ private FopFactory fopFactory = FopFactory.newInstance();
+
/**
* Converts an FO file to a RTF file using FOP
* @param fo the FO file
@@ -55,18 +60,20 @@ public class ExampleFO2RTF {
*/
public void convertFO2RTF(File fo, File rtf) throws IOException, FOPException {
+ FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
+ // configure foUserAgent as desired
+
OutputStream out = null;
try {
- // Construct fop with desired output format
- Fop fop = new Fop(MimeConstants.MIME_RTF);
-
// Setup output stream. Note: Using BufferedOutputStream
// for performance reasons (helpful with FileOutputStreams).
out = new FileOutputStream(rtf);
out = new BufferedOutputStream(out);
- fop.setOutputStream(out);
+ // Construct fop with desired output format
+ Fop fop = fopFactory.newFop(MimeConstants.MIME_RTF, foUserAgent, out);
+
// Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(); // identity transformer
diff --git a/examples/embedding/java/embedding/ExampleObj2PDF.java b/examples/embedding/java/embedding/ExampleObj2PDF.java
index 99039642c..cd573317f 100644
--- a/examples/embedding/java/embedding/ExampleObj2PDF.java
+++ b/examples/embedding/java/embedding/ExampleObj2PDF.java
@@ -1,5 +1,5 @@
/*
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-2004,2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,8 +33,10 @@ import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;
// FOP
+import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FOPException;
+import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import embedding.model.ProjectTeam;
@@ -45,6 +47,9 @@ import embedding.model.ProjectTeam;
*/
public class ExampleObj2PDF {
+ // configure fopFactory as desired
+ private FopFactory fopFactory = FopFactory.newInstance();
+
/**
* Converts a ProjectTeam object to a PDF file.
* @param team the ProjectTeam object
@@ -57,14 +62,15 @@ public class ExampleObj2PDF {
public void convertProjectTeam2PDF(ProjectTeam team, File xslt, File pdf)
throws IOException, FOPException, TransformerException {
- // Construct fop with desired output format
- Fop fop = new Fop(MimeConstants.MIME_PDF);
+ FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
+ // configure foUserAgent as desired
// Setup output
OutputStream out = new java.io.FileOutputStream(pdf);
out = new java.io.BufferedOutputStream(out);
try {
- fop.setOutputStream(out);
+ // Construct fop with desired output format
+ Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
diff --git a/examples/embedding/java/embedding/ExampleXML2PDF.java b/examples/embedding/java/embedding/ExampleXML2PDF.java
index c554dacee..01868b2c1 100644
--- a/examples/embedding/java/embedding/ExampleXML2PDF.java
+++ b/examples/embedding/java/embedding/ExampleXML2PDF.java
@@ -1,5 +1,5 @@
/*
- * Copyright 1999-2005 The Apache Software Foundation.
+ * Copyright 1999-2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,7 +31,9 @@ import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;
//FOP
+import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
+import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
/**
@@ -65,15 +67,19 @@ public class ExampleXML2PDF {
System.out.println();
System.out.println("Transforming...");
- // Construct fop with desired output format
- Fop fop = new Fop(MimeConstants.MIME_PDF);
-
+ // configure fopFactory as desired
+ FopFactory fopFactory = FopFactory.newInstance();
+
+ FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
+ // configure foUserAgent as desired
+
// Setup output
OutputStream out = new java.io.FileOutputStream(pdffile);
out = new java.io.BufferedOutputStream(out);
try {
- fop.setOutputStream(out);
+ // Construct fop with desired output format
+ Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
diff --git a/examples/embedding/java/embedding/MultipleFO2PDF.java b/examples/embedding/java/embedding/MultipleFO2PDF.java
new file mode 100644
index 000000000..eccdba46b
--- /dev/null
+++ b/examples/embedding/java/embedding/MultipleFO2PDF.java
@@ -0,0 +1,173 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package embedding;
+
+// Java
+import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+//JAXP
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+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.FOUserAgent;
+import org.apache.fop.apps.Fop;
+import org.apache.fop.apps.FOPException;
+import org.apache.fop.apps.FopFactory;
+import org.apache.fop.apps.FormattingResults;
+import org.apache.fop.apps.MimeConstants;
+import org.apache.fop.apps.PageSequenceResults;
+
+/**
+ * This class demonstrates the conversion of multiple FO files to PDF using FOP.
+ * The FopFactory is reused. Its configuration is applied to each rendering run.
+ * The FOUserAgent and Fop are newly created by the FopFactory for each run.
+ * The FOUserAgent can be configured differently for each run.
+ */
+public class MultipleFO2PDF {
+
+ // configure fopFactory as desired
+ private FopFactory fopFactory = FopFactory.newInstance();
+
+ // JAXP TransformerFactory can be reused, too
+ private TransformerFactory factory = TransformerFactory.newInstance();
+
+ /**
+ * Converts an FO file to a PDF file using FOP
+ * @param fo the FO file
+ * @param pdf the target PDF file
+ * @throws TransformerException in case of a transformation problem
+ * @throws IOException in case of an I/O problem
+ * @throws FOPException in case of a FOP problem
+ * @return the formatting results of the run
+ */
+ public FormattingResults convertFO2PDF(File fo, File pdf)
+ throws TransformerException, IOException, FOPException {
+
+ OutputStream out = null;
+ Fop fop;
+
+ try {
+ FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
+ // configure foUserAgent as desired
+
+ // Setup output stream. Note: Using BufferedOutputStream
+ // for performance reasons (helpful with FileOutputStreams).
+ out = new FileOutputStream(pdf);
+ out = new BufferedOutputStream(out);
+
+ // Construct fop with desired output format and output stream
+ fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
+
+ // Setup JAXP using identity transformer
+ Transformer transformer = factory.newTransformer(); // identity transformer
+
+ // Setup input stream
+ Source src = new StreamSource(fo);
+
+ // Resulting SAX events (the generated FO) must be piped through to FOP
+ Result res = new SAXResult(fop.getDefaultHandler());
+
+ // Start XSLT transformation and FOP processing
+ transformer.transform(src, res);
+ } finally {
+ out.close();
+ }
+
+ return fop.getResults();
+ }
+
+ /**
+ * Listens on standard in for names of fo files to be transformed to pdf.
+ * 'quit' or the null string (for piped input) cause the listener to stop listening.
+ */
+ public void listen() {
+
+ //Setup directories
+ File baseDir = new File(".");
+ File outDir = new File(baseDir, "out");
+ outDir.mkdirs();
+ BufferedReader in = new BufferedReader(new java.io.InputStreamReader(System.in));
+
+ while (true) {
+ try {
+ // Listen for the input file name
+ System.out.print("Input XSL-FO file ('quit' to stop): ");
+ String foname = in.readLine();
+ if (foname == null) {
+ System.out.println("Null input, quitting");
+ return;
+ }
+ foname.trim();
+ if (foname.equals("quit")) {
+ System.out.println("Quitting");
+ return;
+ }
+ File fofile = new File(baseDir, foname);
+ String pdfname = foname;
+ pdfname.replaceFirst("\\.fo", ".pdf");
+ File pdffile = new File(outDir, pdfname);
+
+ // transform and render
+ System.out.print("Transforming " + fofile + " to PDF file " + pdffile + "...");
+ FormattingResults foResults = convertFO2PDF(fofile, pdffile);
+ System.out.println("done!");
+
+ // Result processing
+ java.util.List pageSequences = foResults.getPageSequences();
+ for (java.util.Iterator it = pageSequences.iterator(); it.hasNext();) {
+ PageSequenceResults pageSequenceResults = (PageSequenceResults)it.next();
+ System.out.println("PageSequence "
+ + (String.valueOf(pageSequenceResults.getID()).length() > 0
+ ? pageSequenceResults.getID() : "<no id>")
+ + " generated " + pageSequenceResults.getPageCount() + " pages.");
+ }
+ System.out.println("Generated " + foResults.getPageCount() + " pages in total.");
+
+ } catch (Exception e) {
+ System.out.println("failure!");
+ e.printStackTrace(System.out);
+ } finally {
+ System.out.println("");
+ }
+ }
+ }
+
+ /**
+ * Main method. Set up the listener.
+ * @param args command-line arguments
+ */
+ public static void main(String[] args) {
+ System.out.println("FOP MultipleFO2PDF\n");
+ System.out.println("Preparing...");
+ MultipleFO2PDF m = new MultipleFO2PDF();
+ m.listen();
+ }
+
+}
diff --git a/examples/embedding/java/embedding/intermediate/ExampleConcat.java b/examples/embedding/java/embedding/intermediate/ExampleConcat.java
index 845d3bfbf..61af4cf65 100644
--- a/examples/embedding/java/embedding/intermediate/ExampleConcat.java
+++ b/examples/embedding/java/embedding/intermediate/ExampleConcat.java
@@ -33,6 +33,7 @@ import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
+import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.area.AreaTreeModel;
import org.apache.fop.area.AreaTreeParser;
@@ -53,6 +54,9 @@ import embedding.model.ProjectTeam;
*/
public class ExampleConcat {
+ // configure fopFactory as desired
+ private FopFactory fopFactory = FopFactory.newInstance();
+
/**
* Creates a sample ProjectTeam instance for this demo.
* @return ProjectTeam the newly created ProjectTeam instance
@@ -80,7 +84,7 @@ public class ExampleConcat {
throws IOException, FOPException, TransformerException {
//Create a user agent
- FOUserAgent userAgent = new FOUserAgent();
+ FOUserAgent userAgent = fopFactory.newFOUserAgent();
//Create an instance of the target renderer so the XMLRenderer can use its font setup
Renderer targetRenderer = userAgent.getRendererFactory().createRenderer(
@@ -96,14 +100,12 @@ public class ExampleConcat {
//Make sure the prepared XMLRenderer is used
userAgent.setRendererOverride(xmlRenderer);
- // Construct fop (the MIME type here is unimportant due to the override on the user agent)
- Fop fop = new Fop(MimeConstants.MIME_FOP_AREA_TREE, userAgent);
-
// Setup output
OutputStream out = new java.io.FileOutputStream(intermediate);
out = new java.io.BufferedOutputStream(out);
try {
- fop.setOutputStream(out);
+ // Construct fop (the MIME type here is unimportant due to the override on the user agent)
+ Fop fop = new Fop(MimeConstants.MIME_FOP_AREA_TREE, userAgent, out);
// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
@@ -140,7 +142,7 @@ public class ExampleConcat {
try {
//Setup fonts and user agent
FontInfo fontInfo = new FontInfo();
- FOUserAgent userAgent = new FOUserAgent();
+ FOUserAgent userAgent = fopFactory.newFOUserAgent();
//Construct the AreaTreeModel that will received the individual pages
AreaTreeModel treeModel = new RenderPagesModel(userAgent,
diff --git a/examples/embedding/java/embedding/intermediate/ExampleStamp.java b/examples/embedding/java/embedding/intermediate/ExampleStamp.java
index 719ae1fe2..05f386bf8 100644
--- a/examples/embedding/java/embedding/intermediate/ExampleStamp.java
+++ b/examples/embedding/java/embedding/intermediate/ExampleStamp.java
@@ -30,6 +30,7 @@ import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.FOUserAgent;
+import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.area.AreaTreeModel;
import org.apache.fop.area.AreaTreeParser;
@@ -46,6 +47,9 @@ import embedding.model.ProjectTeam;
*/
public class ExampleStamp {
+ // configure fopFactory as desired
+ private FopFactory fopFactory = FopFactory.newInstance();
+
/**
* Stamps an intermediate file and renders it to a PDF file.
* @param atfile the intermediate file (area tree XML)
@@ -63,7 +67,7 @@ public class ExampleStamp {
try {
//Setup fonts and user agent
FontInfo fontInfo = new FontInfo();
- FOUserAgent userAgent = new FOUserAgent();
+ FOUserAgent userAgent = fopFactory.newFOUserAgent();
//Construct the AreaTreeModel that will received the individual pages
AreaTreeModel treeModel = new RenderPagesModel(userAgent,