Przeglądaj źródła

Ensure the consistency of the created Fop object


git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_API_Finalization@383973 13f79535-47bb-0310-9956-ffa450edef68
Temp_API_Finalization
Simon Pepping 18 lat temu
rodzic
commit
b521d507d8

+ 1
- 10
examples/embedding/java/embedding/MultipleFO2PDF.java Wyświetl plik

@@ -43,7 +43,6 @@ import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.FormattingResults;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.apps.PageSequenceResults;
import org.xml.sax.helpers.DefaultHandler;

/**
* This class demonstrates the conversion of multiple FO files to PDF using FOP.
@@ -83,14 +82,6 @@ public class MultipleFO2PDF {
// Construct fop with desired output format and output stream
fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

// This will also check the consistency of your setup
DefaultHandler handler;
try {
handler = fop.getDefaultHandler();
} catch (IllegalStateException e) {
throw new FOPException(e);
}

// Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(); // identity transformer
@@ -99,7 +90,7 @@ public class MultipleFO2PDF {
Source src = new StreamSource(fo);

// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(handler);
Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.transform(src, res);

+ 23
- 6
src/java/org/apache/fop/apps/Fop.java Wyświetl plik

@@ -64,8 +64,9 @@ public class Fop {
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
* @param ua FOUserAgent object
* @param stream the output stream
* @throws FOPException if setting up the DefaultHandler fails
*/
public Fop(String outputFormat, FOUserAgent ua, OutputStream stream) {
public Fop(String outputFormat, FOUserAgent ua, OutputStream stream) throws FOPException {
this.outputFormat = outputFormat;

foUserAgent = ua;
@@ -74,6 +75,8 @@ public class Fop {
}
this.stream = stream;
createDefaultHandler();
}

/**
@@ -81,23 +84,28 @@ public class Fop {
* output format (ex. "application/pdf" for PDF).
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
* @param ua FOUserAgent object
* @throws FOPException if setting up the DefaultHandler fails
*/
public Fop(String outputFormat, FOUserAgent ua) {
public Fop(String outputFormat, FOUserAgent ua) throws FOPException {
this.outputFormat = outputFormat;

foUserAgent = ua;
if (foUserAgent == null) {
foUserAgent = new FOUserAgent();
}
createDefaultHandler();
}

/**
* Constructor for FOP with a default FOUserAgent. It uses MIME types to select the
* output format (ex. "application/pdf" for PDF).
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
* @deprecated Use a constructor with an FOUserAgent instead!
*/
public Fop(String outputFormat) {
this(outputFormat, null);
this.outputFormat = outputFormat;
foUserAgent = new FOUserAgent();
}

/**
@@ -118,19 +126,28 @@ public class Fop {
}

/**
* Returns a DefaultHandler object used to generate the document.
* Creates a DefaultHandler object used to generate the document.
* Note this object implements the ContentHandler interface.
* For processing with a Transformer object, this DefaultHandler object
* can be used in the SAXResult constructor.
* Alternatively, for processing with a SAXParser, this object can be
* used as the DefaultHandler argument to its parse() methods.
*
* @return a SAX DefaultHandler for handling the SAX events.
* @throws FOPException if setting up the DefaultHandler fails
*/
private void createDefaultHandler() throws FOPException {
this.foTreeBuilder = new FOTreeBuilder(outputFormat, foUserAgent, stream);
}

/**
* Returns the DefaultHandler object used to generate the document.
* Checking for null and the exception is only for the deprecated constructor.
* @return the SAX DefaultHandler for handling the SAX events.
* @throws FOPException if setting up the DefaultHandler fails
*/
public DefaultHandler getDefaultHandler() throws FOPException {
if (foTreeBuilder == null) {
this.foTreeBuilder = new FOTreeBuilder(outputFormat, foUserAgent, stream);
createDefaultHandler();
}
return this.foTreeBuilder;
}

+ 10
- 5
src/java/org/apache/fop/apps/FopFactory.java Wyświetl plik

@@ -133,8 +133,9 @@ public class FopFactory {
* use the constants defined in {@link MimeConstants}.
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
* @return the new Fop instance
* @throws FOPException when the constructor fails
*/
public Fop newFop(String outputFormat) {
public Fop newFop(String outputFormat) throws FOPException {
return new Fop(outputFormat, newFOUserAgent());
}

@@ -149,8 +150,9 @@ public class FopFactory {
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
* @param userAgent the user agent that will be used to control the rendering run
* @return the new Fop instance
* @throws FOPException when the constructor fails
*/
public Fop newFop(String outputFormat, FOUserAgent userAgent) {
public Fop newFop(String outputFormat, FOUserAgent userAgent) throws FOPException {
if (userAgent == null) {
throw new NullPointerException("The userAgent parameter must not be null!");
}
@@ -166,8 +168,9 @@ public class FopFactory {
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
* @param stream the output stream
* @return the new Fop instance
* @throws FOPException when the constructor fails
*/
public Fop newFop(String outputFormat, OutputStream stream) {
public Fop newFop(String outputFormat, OutputStream stream) throws FOPException {
return new Fop(outputFormat, newFOUserAgent(), stream);
}

@@ -184,8 +187,9 @@ public class FopFactory {
* @param userAgent the user agent that will be used to control the rendering run
* @param stream the output stream
* @return the new Fop instance
* @throws FOPException when the constructor fails
*/
public Fop newFop(String outputFormat, FOUserAgent userAgent, OutputStream stream) {
public Fop newFop(String outputFormat, FOUserAgent userAgent, OutputStream stream) throws FOPException {
if (userAgent == null) {
throw new NullPointerException("The userAgent parameter must not be null!");
}
@@ -199,8 +203,9 @@ public class FopFactory {
* instance instead of the default ones created internally by FOP.
* @param userAgent the user agent that will be used to control the rendering run
* @return the new Fop instance
* @throws FOPException when the constructor fails
*/
public Fop newFop(FOUserAgent userAgent) {
public Fop newFop(FOUserAgent userAgent) throws FOPException {
if (userAgent.getRendererOverride() == null
&& userAgent.getFOEventHandlerOverride() == null) {
throw new IllegalStateException("Either the overriding renderer or the overriding"

+ 5
- 3
src/java/org/apache/fop/cli/InputHandler.java Wyświetl plik

@@ -85,10 +85,12 @@ public class InputHandler implements ErrorListener, Renderable {
*/
public void renderTo(FOUserAgent userAgent, String outputFormat, OutputStream out)
throws FOPException {
Fop fop = new Fop(outputFormat, userAgent);
Fop fop;
if (out != null) {
fop.setOutputStream(out);
fop = new Fop(outputFormat, userAgent, out);
} else {
fop = new Fop(outputFormat, userAgent);
}

// if base URL was not explicitly set in FOUserAgent, obtain here

+ 2
- 2
src/java/org/apache/fop/render/RendererFactory.java Wyświetl plik

@@ -1,5 +1,5 @@
/*
* Copyright 2004-2005 The Apache Software Foundation.
* Copyright 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.
@@ -248,7 +248,7 @@ public class RendererFactory {
if (out == null
&& userAgent.getRendererOverride() == null
&& rendMaker.needsOutputStream()) {
throw new IllegalStateException(
throw new FOPException(
"OutputStream has not been set");
}
//Found a Renderer so we need to construct an AreaTreeHandler.

Ładowanie…
Anuluj
Zapisz