From fbfdbea38c593b79b29656276873309d1b33865f Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Fri, 17 Feb 2006 13:48:26 +0000 Subject: [PATCH] =?utf8?q?Added=20support=20for=20userconfig=20attribute?= =?utf8?q?=20in=20FOP=20Ant=20task.=20Submitted=20by:=20Jir=C3=AD=20Mare?= =?utf8?q?=C5=A1=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit In addition to that: Now deleting the output file when an exception is encountered. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@378508 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/fop/tools/anttasks/Fop.java | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/java/org/apache/fop/tools/anttasks/Fop.java b/src/java/org/apache/fop/tools/anttasks/Fop.java index 10badfefa..1096feb5a 100644 --- a/src/java/org/apache/fop/tools/anttasks/Fop.java +++ b/src/java/org/apache/fop/tools/anttasks/Fop.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. @@ -27,6 +27,7 @@ import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.util.GlobPatternMapper; // Java +import java.io.BufferedOutputStream; import java.io.File; import java.io.IOException; import java.io.OutputStream; @@ -39,8 +40,12 @@ import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.MimeConstants; import org.apache.fop.cli.InputHandler; +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; import org.apache.commons.logging.impl.SimpleLog; import org.apache.commons.logging.Log; +import org.xml.sax.SAXException; /** * Wrapper for FOP which allows it to be accessed from within an Ant task. @@ -403,8 +408,20 @@ class FOPTaskStarter { */ public void run() throws FOPException { //Setup configuration + Configuration userConfig = null; if (task.getUserconfig() != null) { - /**@todo implement me */ + if (task.getUserconfig() != null) { + DefaultConfigurationBuilder configBuilder = new DefaultConfigurationBuilder(); + try { + userConfig = configBuilder.buildFromFile(task.getUserconfig()); + } catch (SAXException e) { + throw new FOPException(e); + } catch (ConfigurationException e) { + throw new FOPException(e); + } catch (IOException e) { + throw new FOPException(e); + } + } } //Set base directory @@ -450,7 +467,7 @@ class FOPTaskStarter { // output file is older than input file if (task.getForce() || !outf.exists() || (task.getFofile().lastModified() > outf.lastModified() )) { - render(task.getFofile(), outf, outputFormat); + render(task.getFofile(), outf, outputFormat, userConfig); actioncount++; } else if (outf.exists() && (task.getFofile().lastModified() <= outf.lastModified() )) { @@ -502,7 +519,7 @@ class FOPTaskStarter { // output file is older than input file if (task.getForce() || !outf.exists() || (f.lastModified() > outf.lastModified() )) { - render(f, outf, outputFormat); + render(f, outf, outputFormat, userConfig); actioncount++; } else if (outf.exists() && (f.lastModified() <= outf.lastModified() )) { skippedcount++; @@ -521,12 +538,13 @@ class FOPTaskStarter { } private void render(File foFile, File outFile, - String outputFormat) throws FOPException { + String outputFormat, Configuration userConfig) throws FOPException { InputHandler inputHandler = new InputHandler(foFile); OutputStream out = null; try { out = new java.io.FileOutputStream(outFile); + out = new BufferedOutputStream(out); } catch (Exception ex) { throw new BuildException("Failed to open " + outFile, ex); } @@ -535,10 +553,15 @@ class FOPTaskStarter { task.log(foFile + " -> " + outFile, Project.MSG_INFO); } + boolean success = false; try { FOUserAgent userAgent = new FOUserAgent(); userAgent.setBaseURL(this.baseURL); + if (userConfig != null) { + userAgent.setUserConfig(userConfig); + } inputHandler.renderTo(userAgent, outputFormat, out); + success = true; } catch (Exception ex) { throw new BuildException(ex); } finally { @@ -547,6 +570,9 @@ class FOPTaskStarter { } catch (IOException ioe) { logger.error("Error closing output file", ioe); } + if (!success) { + outFile.delete(); + } } } -- 2.39.5