You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by je...@apache.org on 2008/03/31 16:10:10 UTC
svn commit: r642997 - in
/xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events:
./ ExampleEvents.java missing-image.fo
Author: jeremias
Date: Mon Mar 31 07:10:08 2008
New Revision: 642997
URL: http://svn.apache.org/viewvc?rev=642997&view=rev
Log:
Added an example to demonstrate how to write your own event listener and how to deal with the exceptions thrown in the process.
Added:
xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events/
xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events/ExampleEvents.java (with props)
xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events/missing-image.fo (with props)
Added: xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events/ExampleEvents.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events/ExampleEvents.java?rev=642997&view=auto
==============================================================================
--- xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events/ExampleEvents.java (added)
+++ xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events/ExampleEvents.java Mon Mar 31 07:10:08 2008
@@ -0,0 +1,227 @@
+/*
+ * 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.events;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.URL;
+
+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.commons.io.IOUtils;
+
+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.events.Event;
+import org.apache.fop.events.EventFormatter;
+import org.apache.fop.events.EventListener;
+import org.apache.fop.events.model.EventSeverity;
+
+/**
+ * This class demonstrates how to register an event listener with FOP so you can customize
+ * FOP's error behaviour.
+ */
+public class ExampleEvents {
+
+ // 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 IOException In case of an I/O problem
+ * @throws FOPException In case of a FOP problem
+ * @throws TransformerException In case of a problem with XSLT
+ */
+ public void convertFO2PDF(URL fo, File pdf)
+ throws IOException, FOPException, TransformerException {
+
+ OutputStream out = null;
+
+ try {
+ //Create the user agent for this processing run
+ FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
+
+ //Adding a simple logging listener that writes to stdout and stderr
+ foUserAgent.getEventBroadcaster().addEventListener(new SysOutEventListener());
+
+ // Add your own event listener
+ foUserAgent.getEventBroadcaster().addEventListener(new MyEventListener());
+
+ // configure foUserAgent further 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
+ Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
+
+ // Setup JAXP using identity transformer
+ TransformerFactory factory = TransformerFactory.newInstance();
+ Transformer transformer = factory.newTransformer(); // identity transformer
+
+ // Setup input stream
+ Source src = new StreamSource(fo.toExternalForm());
+
+ // 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 {
+ IOUtils.closeQuietly(out);
+ }
+ }
+
+ private static class MyEventListener implements EventListener {
+
+ public void processEvent(Event event) {
+ if ("org.apache.fop.events.ResourceEventProducer.imageNotFound"
+ .equals(event.getEventID())) {
+
+ //Get the FileNotFoundException that's part of the event's parameters
+ FileNotFoundException fnfe = (FileNotFoundException)event.getParam("fnfe");
+
+ System.out.println("---=== imageNotFound Event for " + event.getParam("uri")
+ + "!!! ===---");
+ //Stop processing when an image could not be found. Otherwise, FOP would just
+ //continue without the image!
+
+ System.out.println("Throwing a RuntimeException...");
+ throw new RuntimeException(EventFormatter.format(event), fnfe);
+ } else {
+ //ignore all other events
+ }
+ }
+
+ }
+
+ /** A simple event listener that writes the events to stdout and sterr. */
+ private static class SysOutEventListener implements EventListener {
+
+ /** {@inheritDoc} */
+ public void processEvent(Event event) {
+ String msg = EventFormatter.format(event);
+ EventSeverity severity = event.getSeverity();
+ if (severity == EventSeverity.INFO) {
+ System.out.println("[INFO ] " + msg);
+ } else if (severity == EventSeverity.WARN) {
+ System.out.println("[WARN ] " + msg);
+ } else if (severity == EventSeverity.ERROR) {
+ System.err.println("[ERROR] " + msg);
+ } else if (severity == EventSeverity.FATAL) {
+ System.err.println("[FATAL] " + msg);
+ } else {
+ assert false;
+ }
+ }
+ }
+
+
+ /**
+ * This method extracts the original exception from some exception. The exception
+ * might be nested multiple levels deep.
+ * @param t the Throwable to inspect
+ * @return the original Throwable or the method parameter t if there are no nested Throwables.
+ */
+ private static Throwable getOriginalThrowable(Throwable t) {
+ if (t instanceof SAXException) {
+ SAXException saxe = (SAXException)t;
+ if (saxe.getException() != null) {
+ return getOriginalThrowable(saxe.getException());
+ } else {
+ return saxe;
+ }
+ } else {
+ if (t.getCause() != null) {
+ return getOriginalThrowable(t.getCause());
+ } else {
+ return t;
+ }
+ }
+ }
+
+ /**
+ * Main method.
+ * @param args command-line arguments
+ */
+ public static void main(String[] args) {
+ try {
+ System.out.println("FOP ExampleEvents\n");
+ System.out.println("Preparing...");
+
+ //Setup directories
+ File baseDir = new File(".");
+ File outDir = new File(baseDir, "out");
+ outDir.mkdirs();
+
+ //Setup input and output files
+ URL fo = ExampleEvents.class.getResource("missing-image.fo");
+ File pdffile = new File(outDir, "out.pdf");
+
+ System.out.println("Input: XSL-FO (" + fo.toExternalForm() + ")");
+ System.out.println("Output: PDF (" + pdffile + ")");
+ System.out.println();
+ System.out.println("Transforming...");
+
+ ExampleEvents app = new ExampleEvents();
+
+ try {
+ app.convertFO2PDF(fo, pdffile);
+ } catch (TransformerException te) {
+ //Note: We don't get the original exception here!
+ //FOP needs to embed the exception in a SAXException and the TraX transformer
+ //again wraps the SAXException in a TransformerException. Even our own
+ //RuntimeException just wraps the original FileNotFoundException.
+ //So we need to unpack to get the original exception (about three layers deep).
+ Throwable originalThrowable = getOriginalThrowable(te);
+ originalThrowable.printStackTrace(System.err);
+ System.out.println("Aborted!");
+ System.exit(-1);
+ }
+
+ System.out.println("Success!");
+ } catch (Exception e) {
+ //Some other error (shouldn't happen in this example)
+ e.printStackTrace(System.err);
+ System.exit(-1);
+ }
+ }
+
+}
Propchange: xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events/ExampleEvents.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events/ExampleEvents.java
------------------------------------------------------------------------------
svn:keywords = Id
Added: xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events/missing-image.fo
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events/missing-image.fo?rev=642997&view=auto
==============================================================================
--- xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events/missing-image.fo (added)
+++ xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events/missing-image.fo Mon Mar 31 07:10:08 2008
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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$ -->
+<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
+ <fo:layout-master-set>
+ <fo:simple-page-master master-name="A4" page-height="29.7cm" page-width="21cm" margin="2cm">
+ <fo:region-body/>
+ </fo:simple-page-master>
+ </fo:layout-master-set>
+ <fo:page-sequence master-reference="A4">
+ <fo:flow flow-name="xsl-region-body">
+ <fo:block>
+ The following image is not available:
+ <fo:external-graphic src="my-missing-image.png"/>
+ </fo:block>
+ </fo:flow>
+ </fo:page-sequence>
+</fo:root>
Propchange: xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events/missing-image.fo
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: xmlgraphics/fop/branches/Temp_ProcessingFeedback/examples/embedding/java/embedding/events/missing-image.fo
------------------------------------------------------------------------------
svn:keywords = Id
---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org