From 9124211fdd13b336c60f606a11983dff70cd8b10 Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Tue, 23 Aug 2005 14:33:38 +0000 Subject: [PATCH] New "-foout" parameter which only saves the intermediate XSL-FO file when -xml and -xsl are used. Saves people from having to call a separate XSL transformer. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@239414 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fop/cli/CommandLineOptions.java | 25 +++++++++++++++ src/java/org/apache/fop/cli/InputHandler.java | 31 +++++++++++++++---- src/java/org/apache/fop/cli/Main.java | 17 +++++++--- 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/src/java/org/apache/fop/cli/CommandLineOptions.java b/src/java/org/apache/fop/cli/CommandLineOptions.java index 3eb70b577..65391d79f 100644 --- a/src/java/org/apache/fop/cli/CommandLineOptions.java +++ b/src/java/org/apache/fop/cli/CommandLineOptions.java @@ -51,6 +51,9 @@ import org.apache.avalon.framework.configuration.ConfigurationException; */ public class CommandLineOptions implements Constants { + /** Used to indicate that only the result of the XSL transformation should be output */ + public static final int RENDER_NONE = -1; + /* show configuration information */ private Boolean showConfiguration = Boolean.FALSE; /* for area tree XML output, only down to block area level */ @@ -210,6 +213,8 @@ public class CommandLineOptions implements Constants { i = i + parseTextOutputOption(args, i); } else if (args[i].equals("-svg")) { i = i + parseSVGOutputOption(args, i); + } else if (args[i].equals("-foout")) { + i = i + parseFOOutputOption(args, i); } else if (args[i].charAt(0) != '-') { i = i + parseUnknownOption(args, i); } else if (args[i].equals("-at")) { @@ -412,6 +417,17 @@ public class CommandLineOptions implements Constants { } } + private int parseFOOutputOption(String[] args, int i) throws FOPException { + setOutputMode(RENDER_NONE); + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("you must specify the FO output file"); + } else { + outfile = new File(args[i + 1]); + return 1; + } + } + private int parseUnknownOption(String[] args, int i) throws FOPException { if (inputmode == NOT_SET) { inputmode = FO_INPUT; @@ -502,6 +518,10 @@ public class CommandLineOptions implements Constants { } } else if (inputmode == FO_INPUT) { + if (outputmode == RENDER_NONE) { + throw new FOPException( + "FO output mode is only available if you use -xml and -xsl"); + } if (xmlfile != null || xsltfile != null) { log.warn("fo input mode, but xmlfile or xslt file are set:"); log.error("xml file: " + xmlfile.toString()); @@ -556,6 +576,7 @@ public class CommandLineOptions implements Constants { case RENDER_RTF: case RENDER_TIFF: case RENDER_PNG: + case RENDER_NONE: return outputmode; case RENDER_XML: foUserAgent.getRendererOptions().put("fineDetail", isCoarseAreaXml()); @@ -701,9 +722,13 @@ public class CommandLineOptions implements Constants { + " -at outfile representation of area tree as XML (outfile req'd) \n" + " -print input file will be rendered and sent to the printer \n" + " see options with \"-print help\" \n\n" + + " -foout outfile input will only be XSL transformed. The intermediate \n" + + " XSL-FO file is saved and no rendering is performed. \n" + + " (Only available if you use -xml and -xsl parameters)\n\n" + " [Examples]\n" + " Fop foo.fo foo.pdf \n" + " Fop -fo foo.fo -pdf foo.pdf (does the same as the previous line)\n" + " Fop -xml foo.xml -xsl foo.xsl -pdf foo.pdf\n" + + " Fop -xml foo.xml -xsl foo.xsl -foout foo.fo\n" + " Fop foo.fo -mif foo.mif\n" + " Fop foo.fo -rtf foo.rtf\n" + " Fop foo.fo -print or Fop -print foo.fo \n" diff --git a/src/java/org/apache/fop/cli/InputHandler.java b/src/java/org/apache/fop/cli/InputHandler.java index 0de3cc9fe..53e4649f1 100644 --- a/src/java/org/apache/fop/cli/InputHandler.java +++ b/src/java/org/apache/fop/cli/InputHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2004 The Apache Software Foundation. + * Copyright 1999-2005 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,6 +20,7 @@ package org.apache.fop.cli; // Imported java.io classes import java.io.File; +import java.io.OutputStream; import java.util.Vector; // Imported TraX classes @@ -30,6 +31,7 @@ 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.apache.commons.logging.Log; @@ -49,6 +51,7 @@ public class InputHandler implements ErrorListener, Renderable { private File stylesheet = null; // for XML/XSLT usage private Vector xsltParams = null; // for XML/XSLT usage + /** the logger */ protected Log log = LogFactory.getLog(InputHandler.class); /** @@ -92,6 +95,24 @@ public class InputHandler implements ErrorListener, Renderable { fop.getUserAgent().setBaseURL(baseURL); } + // Resulting SAX events (the generated FO) must be piped through to FOP + Result res = new SAXResult(fop.getDefaultHandler()); + + transformTo(res); + } + + /** + * In contrast to render(Fop) this method only performs the XSLT stage and saves the + * intermediate XSL-FO file to the output file. + * @param out OutputStream to write the transformation result to. + * @throws FOPException in case of an error during processing + */ + public void transformTo(OutputStream out) throws FOPException { + Result res = new StreamResult(out); + transformTo(res); + } + + private void transformTo(Result result) throws FOPException { try { // Setup XSLT TransformerFactory factory = TransformerFactory.newInstance(); @@ -116,17 +137,14 @@ public class InputHandler implements ErrorListener, Renderable { // Create a SAXSource from the input Source file Source src = new StreamSource(sourcefile); - // 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); + transformer.transform(src, result); } catch (Exception e) { throw new FOPException(e); } } - + // --- Implementation of the ErrorListener interface --- /** @@ -150,4 +168,5 @@ public class InputHandler implements ErrorListener, Renderable { throws TransformerException { throw exc; } + } diff --git a/src/java/org/apache/fop/cli/Main.java b/src/java/org/apache/fop/cli/Main.java index 4f05dcab1..93befd5b1 100644 --- a/src/java/org/apache/fop/cli/Main.java +++ b/src/java/org/apache/fop/cli/Main.java @@ -147,16 +147,25 @@ public class Main { options.parse(args); foUserAgent = options.getFOUserAgent(); - Fop fop = new Fop(options.getRenderer(), foUserAgent); + Fop fop = null; + if (options.getOutputMode() != CommandLineOptions.RENDER_NONE) { + fop = new Fop(options.getRenderer(), foUserAgent); + } try { if (options.getOutputFile() != null) { bos = new BufferedOutputStream(new FileOutputStream( options.getOutputFile())); - fop.setOutputStream(bos); - foUserAgent.setOutputFile(options.getOutputFile()); + if (fop != null) { + fop.setOutputStream(bos); + foUserAgent.setOutputFile(options.getOutputFile()); + } + } + if (fop != null) { + options.getInputHandler().render(fop); + } else { + options.getInputHandler().transformTo(bos); } - options.getInputHandler().render(fop); } finally { if (bos != null) { bos.close(); -- 2.39.5