*/
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 */
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")) {
}
}
+ 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;
}
} 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());
case RENDER_RTF:
case RENDER_TIFF:
case RENDER_PNG:
+ case RENDER_NONE:
return outputmode;
case RENDER_XML:
foUserAgent.getRendererOptions().put("fineDetail", isCoarseAreaXml());
+ " -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"
/*
- * 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.
// Imported java.io classes
import java.io.File;
+import java.io.OutputStream;
import java.util.Vector;
// Imported TraX classes
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;
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);
/**
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();
// 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 ---
/**
throws TransformerException {
throw exc;
}
+
}
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();