aboutsummaryrefslogtreecommitdiffstats
path: root/examples/embedding
diff options
context:
space:
mode:
authorJeremias Maerki <jeremias@apache.org>2009-02-06 14:39:07 +0000
committerJeremias Maerki <jeremias@apache.org>2009-02-06 14:39:07 +0000
commit5301aae80aeae45bef73bd80a2a4f08ab2bcda2e (patch)
tree292cb78e11940f915981660eef96090081e7f250 /examples/embedding
parent90d362f21b82852afe3db15ab8af6435ee579e16 (diff)
downloadxmlgraphics-fop-5301aae80aeae45bef73bd80a2a4f08ab2bcda2e.tar.gz
xmlgraphics-fop-5301aae80aeae45bef73bd80a2a4f08ab2bcda2e.zip
Moved area tree example to package embedding/atxml.
Created a new concatenation example for the new IF in package embedding/intermediate. Exchanged IFRendererMaker with IFSerializerMaker. Removed unused method getConfigurator() from document handler makers. Extracting original nested IFException in IFParser's parse() method. Removed obsolete IFSerializerConfiguration. Now covered by PrintRendererConfigurator. Delegating FontInfo setter calls to mimic handlers in IFSerializer. Fixed a bug in RendererFactory for the case when FOUserAgent.setDocumentHandlerOverride() is used. Added a generic IFDocumentHandler proxy class (IFDocumentHandlerProxy). Added a simple IFConcatenator helper class for simple IF concatenations (experimental). git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign@741576 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'examples/embedding')
-rw-r--r--examples/embedding/java/embedding/atxml/ExampleConcat.java219
-rw-r--r--examples/embedding/java/embedding/atxml/ExampleStamp.java143
-rw-r--r--examples/embedding/java/embedding/intermediate/ExampleConcat.java79
3 files changed, 406 insertions, 35 deletions
diff --git a/examples/embedding/java/embedding/atxml/ExampleConcat.java b/examples/embedding/java/embedding/atxml/ExampleConcat.java
new file mode 100644
index 000000000..adec1b08c
--- /dev/null
+++ b/examples/embedding/java/embedding/atxml/ExampleConcat.java
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.atxml;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.xml.sax.SAXException;
+
+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;
+import org.apache.fop.area.RenderPagesModel;
+import org.apache.fop.fonts.FontInfo;
+import org.apache.fop.render.Renderer;
+import org.apache.fop.render.xml.XMLRenderer;
+
+import embedding.ExampleObj2XML;
+import embedding.model.ProjectMember;
+import embedding.model.ProjectTeam;
+
+/**
+ * Example for the area tree XML format that demonstrates the concatenation of two documents
+ * rendered to the area tree XML format. A single PDF file is generated from the two area tree
+ * files.
+ */
+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
+ */
+ public static ProjectTeam createAnotherProjectTeam() {
+ ProjectTeam team = new ProjectTeam();
+ team.setProjectName("The Dynamic Duo");
+ team.addMember(new ProjectMember(
+ "Batman", "lead", "batman@heroes.org"));
+ team.addMember(new ProjectMember(
+ "Robin", "aid", "robin@heroes.org"));
+ return team;
+ }
+
+ /**
+ * Converts an XSL-FO document to an area tree XML file.
+ * @param src the source file
+ * @param xslt the stylesheet file
+ * @param areaTreeFile the target area tree XML file
+ * @throws IOException In case of an I/O problem
+ * @throws FOPException In case of a FOP problem
+ * @throws TransformerException In case of a XSL transformation problem
+ */
+ public void convertToAreaTreeXML(Source src, Source xslt, File areaTreeFile)
+ throws IOException, FOPException, TransformerException {
+
+ //Create a user agent
+ FOUserAgent userAgent = fopFactory.newFOUserAgent();
+
+ //Create an instance of the target renderer so the XMLRenderer can use its font setup
+ Renderer targetRenderer = userAgent.getRendererFactory().createRenderer(
+ userAgent, MimeConstants.MIME_PDF);
+
+ //Create the XMLRenderer to create the area tree XML
+ XMLRenderer xmlRenderer = new XMLRenderer();
+ xmlRenderer.setUserAgent(userAgent);
+
+ //Tell the XMLRenderer to mimic the target renderer
+ xmlRenderer.mimicRenderer(targetRenderer);
+
+ //Make sure the prepared XMLRenderer is used
+ userAgent.setRendererOverride(xmlRenderer);
+
+ // Setup output
+ OutputStream out = new java.io.FileOutputStream(areaTreeFile);
+ out = new java.io.BufferedOutputStream(out);
+ try {
+ // Construct fop (the MIME type here is unimportant due to the override
+ // on the user agent)
+ Fop fop = fopFactory.newFop(null, userAgent, out);
+
+ // Setup XSLT
+ TransformerFactory factory = TransformerFactory.newInstance();
+ Transformer transformer;
+ if (xslt != null) {
+ transformer = factory.newTransformer(xslt);
+ } else {
+ transformer = factory.newTransformer();
+ }
+
+ // 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();
+ }
+ }
+
+ /**
+ * Concatenates an array of area tree XML files to a single PDF file.
+ * @param files the array of area tree XML files
+ * @param pdffile the target PDF file
+ * @throws IOException In case of an I/O problem
+ * @throws TransformerException In case of a XSL transformation problem
+ * @throws SAXException In case of an XML-related problem
+ */
+ public void concatToPDF(File[] files, File pdffile)
+ throws IOException, TransformerException, SAXException {
+ // Setup output
+ OutputStream out = new java.io.FileOutputStream(pdffile);
+ out = new java.io.BufferedOutputStream(out);
+ try {
+ //Setup fonts and user agent
+ FontInfo fontInfo = new FontInfo();
+ FOUserAgent userAgent = fopFactory.newFOUserAgent();
+
+ //Construct the AreaTreeModel that will received the individual pages
+ AreaTreeModel treeModel = new RenderPagesModel(userAgent,
+ MimeConstants.MIME_PDF, fontInfo, out);
+
+ //Iterate over all area tree files
+ AreaTreeParser parser = new AreaTreeParser();
+ for (int i = 0; i < files.length; i++) {
+ Source src = new StreamSource(files[i]);
+ parser.parse(src, treeModel, userAgent);
+ }
+
+ //Signal the end of the processing. The renderer can finalize the target document.
+ treeModel.endDocument();
+ } finally {
+ out.close();
+ }
+ }
+
+ /**
+ * Main method.
+ * @param args command-line arguments
+ */
+ public static void main(String[] args) {
+ try {
+ System.out.println("FOP ExampleConcat\n");
+
+ //Setup directories
+ File baseDir = new File(".");
+ File outDir = new File(baseDir, "out");
+ outDir.mkdirs();
+
+ //Setup output file
+ File xsltfile = new File(baseDir, "xml/xslt/projectteam2fo.xsl");
+ File[] files = new File[] {
+ new File(outDir, "team1.at.xml"),
+ new File(outDir, "team2.at.xml")};
+ File pdffile = new File(outDir, "ResultConcat.pdf");
+ for (int i = 0; i < files.length; i++) {
+ System.out.println("Area Tree XML file " + (i + 1) + ": "
+ + files[i].getCanonicalPath());
+ }
+ System.out.println("PDF Output File: " + pdffile.getCanonicalPath());
+ System.out.println();
+
+
+ ProjectTeam team1 = ExampleObj2XML.createSampleProjectTeam();
+ ProjectTeam team2 = createAnotherProjectTeam();
+
+ ExampleConcat app = new ExampleConcat();
+
+ //Create area tree XML files
+ app.convertToAreaTreeXML(
+ team1.getSourceForProjectTeam(),
+ new StreamSource(xsltfile), files[0]);
+ app.convertToAreaTreeXML(
+ team2.getSourceForProjectTeam(),
+ new StreamSource(xsltfile), files[1]);
+
+ //Concatenate the individual area tree files to one document
+ app.concatToPDF(files, pdffile);
+
+ System.out.println("Success!");
+
+ } catch (Exception e) {
+ e.printStackTrace(System.err);
+ System.exit(-1);
+ }
+ }
+
+}
diff --git a/examples/embedding/java/embedding/atxml/ExampleStamp.java b/examples/embedding/java/embedding/atxml/ExampleStamp.java
new file mode 100644
index 000000000..53a12d6ed
--- /dev/null
+++ b/examples/embedding/java/embedding/atxml/ExampleStamp.java
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.atxml;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.xml.sax.SAXException;
+
+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;
+import org.apache.fop.area.RenderPagesModel;
+import org.apache.fop.fonts.FontInfo;
+
+import embedding.ExampleObj2XML;
+import embedding.model.ProjectTeam;
+
+/**
+ * Example for the area tree XML format that demonstrates the stamping of a document with some
+ * kind of watermark. The resulting document is then rendered to a PDF file.
+ */
+public class ExampleStamp {
+
+ // configure fopFactory as desired
+ private FopFactory fopFactory = FopFactory.newInstance();
+
+ /**
+ * Stamps an area tree XML file and renders it to a PDF file.
+ * @param atfile the area tree XML file
+ * @param stampSheet the stylesheet that does the stamping
+ * @param pdffile the target PDF file
+ * @throws IOException In case of an I/O problem
+ * @throws TransformerException In case of a XSL transformation problem
+ * @throws SAXException In case of an XML-related problem
+ */
+ public void stampToPDF(File atfile, File stampSheet, File pdffile)
+ throws IOException, TransformerException, SAXException {
+ // Setup output
+ OutputStream out = new java.io.FileOutputStream(pdffile);
+ out = new java.io.BufferedOutputStream(out);
+ try {
+ //Setup fonts and user agent
+ FontInfo fontInfo = new FontInfo();
+ FOUserAgent userAgent = fopFactory.newFOUserAgent();
+
+ //Construct the AreaTreeModel that will received the individual pages
+ AreaTreeModel treeModel = new RenderPagesModel(userAgent,
+ MimeConstants.MIME_PDF, fontInfo, out);
+
+ //Iterate over all area tree files
+ AreaTreeParser parser = new AreaTreeParser();
+ Source src = new StreamSource(atfile);
+ Source xslt = new StreamSource(stampSheet);
+
+ //Setup Transformer for XSLT processing
+ TransformerFactory tFactory = TransformerFactory.newInstance();
+ Transformer transformer = tFactory.newTransformer(xslt);
+
+ //Send XSLT result to AreaTreeParser
+ SAXResult res = new SAXResult(parser.getContentHandler(treeModel, userAgent));
+
+ //Start XSLT transformation and area tree parsing
+ transformer.transform(src, res);
+
+ //Signal the end of the processing. The renderer can finalize the target document.
+ treeModel.endDocument();
+ } finally {
+ out.close();
+ }
+ }
+
+ /**
+ * Main method.
+ * @param args command-line arguments
+ */
+ public static void main(String[] args) {
+ try {
+ System.out.println("FOP ExampleConcat\n");
+
+ //Setup directories
+ File baseDir = new File(".");
+ File outDir = new File(baseDir, "out");
+ outDir.mkdirs();
+
+ //Setup output file
+ File xsltfile = new File(baseDir, "xml/xslt/projectteam2fo.xsl");
+ File atfile = new File(outDir, "team.at.xml");
+ File stampxsltfile = new File(baseDir, "xml/xslt/atstamp.xsl");
+ File pdffile = new File(outDir, "ResultStamped.pdf");
+ System.out.println("Area Tree XML file : " + atfile.getCanonicalPath());
+ System.out.println("Stamp XSLT: " + stampxsltfile.getCanonicalPath());
+ System.out.println("PDF Output File: " + pdffile.getCanonicalPath());
+ System.out.println();
+
+ ProjectTeam team1 = ExampleObj2XML.createSampleProjectTeam();
+
+ //Create area tree XML file
+ ExampleConcat concatapp = new ExampleConcat();
+ concatapp.convertToAreaTreeXML(
+ team1.getSourceForProjectTeam(),
+ new StreamSource(xsltfile), atfile);
+
+ //Stamp document and produce a PDF from the area tree XML format
+ ExampleStamp app = new ExampleStamp();
+ app.stampToPDF(atfile, stampxsltfile, pdffile);
+
+ System.out.println("Success!");
+
+ } catch (Exception e) {
+ e.printStackTrace(System.err);
+ System.exit(-1);
+ }
+ }
+
+}
diff --git a/examples/embedding/java/embedding/intermediate/ExampleConcat.java b/examples/embedding/java/embedding/intermediate/ExampleConcat.java
index 6967ebc8a..a5765f49b 100644
--- a/examples/embedding/java/embedding/intermediate/ExampleConcat.java
+++ b/examples/embedding/java/embedding/intermediate/ExampleConcat.java
@@ -29,20 +29,22 @@ import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
+import org.xml.sax.SAXException;
+
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;
-import org.apache.fop.area.RenderPagesModel;
-import org.apache.fop.fonts.FontInfo;
-import org.apache.fop.render.Renderer;
-import org.apache.fop.render.xml.XMLRenderer;
-import org.xml.sax.SAXException;
+import org.apache.fop.render.intermediate.IFContext;
+import org.apache.fop.render.intermediate.IFDocumentHandler;
+import org.apache.fop.render.intermediate.IFException;
+import org.apache.fop.render.intermediate.IFSerializer;
+import org.apache.fop.render.intermediate.IFUtil;
+import org.apache.fop.render.intermediate.util.IFConcatenator;
import embedding.ExampleObj2XML;
import embedding.model.ProjectMember;
@@ -50,7 +52,7 @@ import embedding.model.ProjectTeam;
/**
* Example for the intermediate format that demonstrates the concatenation of two documents
- * renderered to the intermediate format. A single PDF file is generated from the two intermediate
+ * rendered to the intermediate format. A single PDF file is generated from the two intermediate
* files.
*/
public class ExampleConcat {
@@ -76,7 +78,7 @@ public class ExampleConcat {
* Converts an XSL-FO document to an intermediate file.
* @param src the source file
* @param xslt the stylesheet file
- * @param intermediate the target intermediate file (area tree XML)
+ * @param intermediate the target intermediate file
* @throws IOException In case of an I/O problem
* @throws FOPException In case of a FOP problem
* @throws TransformerException In case of a XSL transformation problem
@@ -87,25 +89,26 @@ public class ExampleConcat {
//Create a user agent
FOUserAgent userAgent = fopFactory.newFOUserAgent();
- //Create an instance of the target renderer so the XMLRenderer can use its font setup
- Renderer targetRenderer = userAgent.getRendererFactory().createRenderer(
- userAgent, MimeConstants.MIME_PDF);
+ //Create an instance of the target document handler so the IFSerializer
+ //can use its font setup
+ IFDocumentHandler targetHandler = userAgent.getRendererFactory().createDocumentHandler(
+ userAgent, MimeConstants.MIME_PDF + ";mode=painter");
- //Create the XMLRenderer to create the intermediate format (area tree XML)
- XMLRenderer xmlRenderer = new XMLRenderer();
- xmlRenderer.setUserAgent(userAgent);
+ //Create the IFSerializer to write the intermediate format
+ IFSerializer ifSerializer = new IFSerializer();
+ ifSerializer.setContext(new IFContext(userAgent));
- //Tell the XMLRenderer to mimic the target renderer
- xmlRenderer.mimicRenderer(targetRenderer);
+ //Tell the IFSerializer to mimic the target format
+ ifSerializer.mimicDocumentHandler(targetHandler);
- //Make sure the prepared XMLRenderer is used
- userAgent.setRendererOverride(xmlRenderer);
+ //Make sure the prepared document handler is used
+ userAgent.setDocumentHandlerOverride(ifSerializer);
// Setup output
OutputStream out = new java.io.FileOutputStream(intermediate);
out = new java.io.BufferedOutputStream(out);
try {
- // Construct fop (the MIME type here is unimportant due to the override
+ // Construct FOP (the MIME type here is unimportant due to the override
// on the user agent)
Fop fop = fopFactory.newFop(null, userAgent, out);
@@ -130,35 +133,41 @@ public class ExampleConcat {
/**
* Concatenates an array of intermediate files to a single PDF file.
- * @param files the array of intermediate files (area tree XML)
+ * @param files the array of intermediate files
* @param pdffile the target PDF file
* @throws IOException In case of an I/O problem
* @throws TransformerException In case of a XSL transformation problem
* @throws SAXException In case of an XML-related problem
+ * @throws IFException if there was an IF-related error while creating the output file
*/
public void concatToPDF(File[] files, File pdffile)
- throws IOException, TransformerException, SAXException {
+ throws IOException, TransformerException, SAXException, IFException {
// Setup output
OutputStream out = new java.io.FileOutputStream(pdffile);
out = new java.io.BufferedOutputStream(out);
try {
- //Setup fonts and user agent
- FontInfo fontInfo = new FontInfo();
+ //Setup user agent
FOUserAgent userAgent = fopFactory.newFOUserAgent();
- //Construct the AreaTreeModel that will received the individual pages
- AreaTreeModel treeModel = new RenderPagesModel(userAgent,
- MimeConstants.MIME_PDF, fontInfo, out);
+ //Setup target handler
+ String mime = MimeConstants.MIME_PDF + ";mode=painter";
+ IFDocumentHandler targetHandler = fopFactory.getRendererFactory().createDocumentHandler(
+ userAgent, mime);
+
+ //Setup fonts
+ IFUtil.setupFonts(targetHandler);
+ targetHandler.setResult(new StreamResult(pdffile));
+
+ IFConcatenator concatenator = new IFConcatenator(targetHandler, null);
//Iterate over all intermediate files
- AreaTreeParser parser = new AreaTreeParser();
for (int i = 0; i < files.length; i++) {
Source src = new StreamSource(files[i]);
- parser.parse(src, treeModel, userAgent);
+ concatenator.appendDocument(src);
}
- //Signal the end of the processing. The renderer can finalize the target document.
- treeModel.endDocument();
+ //Signal the end of the processing so the target file can be finalized properly.
+ concatenator.finish();
} finally {
out.close();
}
@@ -170,7 +179,7 @@ public class ExampleConcat {
*/
public static void main(String[] args) {
try {
- System.out.println("FOP ExampleConcat\n");
+ System.out.println("FOP ExampleConcat (for the Intermediate Format)\n");
//Setup directories
File baseDir = new File(".");
@@ -180,9 +189,9 @@ public class ExampleConcat {
//Setup output file
File xsltfile = new File(baseDir, "xml/xslt/projectteam2fo.xsl");
File[] files = new File[] {
- new File(outDir, "team1.at.xml"),
- new File(outDir, "team2.at.xml")};
- File pdffile = new File(outDir, "ResultConcat.pdf");
+ new File(outDir, "team1.if.xml"),
+ new File(outDir, "team2.if.xml")};
+ File pdffile = new File(outDir, "ResultIFConcat.pdf");
for (int i = 0; i < files.length; i++) {
System.out.println("Intermediate file " + (i + 1) + ": "
+ files[i].getCanonicalPath());