From d23dad3ac526243651ecfc64e5ba42f2b76c959c Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Tue, 23 Aug 2005 14:06:25 +0000 Subject: Moved CLI to its own package. New main class is org.apache.fop.cli.Main. InputHandler is not passed through the FOUserAgent anymore. IMO it doesn't belong there. Instead I defined an interface "Renderable" in the AWT preview that InputHandler implements. This is used exclusively for reloading the document inside the preview dialog. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@239409 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/fop/apps/CommandLineOptions.java | 809 -------------------- src/java/org/apache/fop/apps/FOUserAgent.java | 18 - src/java/org/apache/fop/apps/Fop.java | 166 +---- src/java/org/apache/fop/apps/InputHandler.java | 148 ---- src/java/org/apache/fop/apps/package.html | 3 +- .../org/apache/fop/cli/CommandLineOptions.java | 829 +++++++++++++++++++++ src/java/org/apache/fop/cli/InputHandler.java | 153 ++++ src/java/org/apache/fop/cli/Main.java | 192 +++++ src/java/org/apache/fop/cli/package.html | 6 + .../org/apache/fop/render/awt/AWTRenderer.java | 18 +- .../fop/render/awt/viewer/PreviewDialog.java | 22 +- .../apache/fop/render/awt/viewer/PreviewPanel.java | 19 +- .../apache/fop/render/awt/viewer/Renderable.java | 36 + src/java/org/apache/fop/tools/TestConverter.java | 4 +- src/java/org/apache/fop/tools/anttasks/Fop.java | 4 +- 15 files changed, 1273 insertions(+), 1154 deletions(-) delete mode 100644 src/java/org/apache/fop/apps/CommandLineOptions.java delete mode 100644 src/java/org/apache/fop/apps/InputHandler.java create mode 100644 src/java/org/apache/fop/cli/CommandLineOptions.java create mode 100644 src/java/org/apache/fop/cli/InputHandler.java create mode 100644 src/java/org/apache/fop/cli/Main.java create mode 100644 src/java/org/apache/fop/cli/package.html create mode 100644 src/java/org/apache/fop/render/awt/viewer/Renderable.java (limited to 'src/java/org/apache') diff --git a/src/java/org/apache/fop/apps/CommandLineOptions.java b/src/java/org/apache/fop/apps/CommandLineOptions.java deleted file mode 100644 index 434fb8be1..000000000 --- a/src/java/org/apache/fop/apps/CommandLineOptions.java +++ /dev/null @@ -1,809 +0,0 @@ -/* - * 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. - * 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 org.apache.fop.apps; - -// java -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.Locale; -import java.util.Vector; - -import org.apache.fop.fo.Constants; -import org.apache.fop.util.CommandLineLogger; - -// commons logging -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -// SAX -import org.xml.sax.XMLReader; -import org.xml.sax.SAXException; -import javax.xml.parsers.SAXParserFactory; - -// avalon configuration -import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; -import org.apache.avalon.framework.configuration.Configuration; -import org.apache.avalon.framework.configuration.ConfigurationException; - -/** - * Options parses the commandline arguments - */ -public class CommandLineOptions implements Constants { - - /* show configuration information */ - private Boolean showConfiguration = Boolean.FALSE; - /* for area tree XML output, only down to block area level */ - private Boolean suppressLowLevelAreas = Boolean.FALSE; - /* user configuration file */ - private File userConfigFile = null; - /* input fo file */ - private File fofile = null; - /* xsltfile (xslt transformation as input) */ - private File xsltfile = null; - /* xml file (xslt transformation as input) */ - private File xmlfile = null; - /* output file */ - private File outfile = null; - /* input mode */ - private int inputmode = NOT_SET; - /* output mode */ - private int outputmode = NOT_SET; - - private FOUserAgent foUserAgent; - - private Log log; - - private Vector xsltParams = null; - - /** - * Construct a command line option object. - */ - public CommandLineOptions() { - LogFactory logFactory = LogFactory.getFactory(); - - // Enable the simple command line logging when no other logger is - // defined. - if (System.getProperty("org.apache.commons.logging.Log") == null) { - logFactory.setAttribute("org.apache.commons.logging.Log", - CommandLineLogger.class.getName()); - setLogLevel("info"); - } - - log = LogFactory.getLog("FOP"); - } - - /** - * Parse the command line arguments. - * @param args the command line arguments. - * @throws FOPException for general errors - * @throws FileNotFoundException if an input file wasn't found - * @throws IOException if the the configuration file could not be loaded - */ - public void parse(String[] args) - throws FOPException, IOException { - boolean optionsParsed = true; - - foUserAgent = new FOUserAgent(); - - try { - optionsParsed = parseOptions(args); - if (optionsParsed) { - if (showConfiguration == Boolean.TRUE) { - dumpConfiguration(); - } - checkSettings(); - createUserConfig(); - } - } catch (FOPException e) { - printUsage(); - throw e; - } catch (java.io.FileNotFoundException e) { - printUsage(); - throw e; - } - - foUserAgent.setInputHandler(createInputHandler()); - } - - /** - * Get the logger. - * @return the logger - */ - public Log getLogger() { - return log; - } - - /** - * parses the commandline arguments - * @return true if parse was successful and processing can continue, false - * if processing should stop - * @exception FOPException if there was an error in the format of the options - */ - private boolean parseOptions(String[] args) throws FOPException { - for (int i = 0; i < args.length; i++) { - if (args[i].equals("-x") - || args[i].equals("--dump-config")) { - showConfiguration = Boolean.TRUE; - } else if (args[i].equals("-c")) { - i = i + parseConfigurationOption(args, i); - } else if (args[i].equals("-l")) { - i = i + parseLanguageOption(args, i); - } else if (args[i].equals("-s")) { - suppressLowLevelAreas = Boolean.TRUE; - } else if (args[i].equals("-d")) { - setLogLevel("debug"); - } else if (args[i].equals("-r")) { - foUserAgent.setStrictValidation(false); - } else if (args[i].equals("-dpi")) { - i = i + parseResolution(args, i); - } else if (args[i].equals("-q") || args[i].equals("--quiet")) { - setLogLevel("error"); - } else if (args[i].equals("-fo")) { - i = i + parseFOInputOption(args, i); - } else if (args[i].equals("-xsl")) { - i = i + parseXSLInputOption(args, i); - } else if (args[i].equals("-xml")) { - i = i + parseXMLInputOption(args, i); - } else if (args[i].equals("-awt")) { - i = i + parseAWTOutputOption(args, i); - } else if (args[i].equals("-pdf")) { - i = i + parsePDFOutputOption(args, i); - } else if (args[i].equals("-mif")) { - i = i + parseMIFOutputOption(args, i); - } else if (args[i].equals("-rtf")) { - i = i + parseRTFOutputOption(args, i); - } else if (args[i].equals("-tiff")) { - i = i + parseTIFFOutputOption(args, i); - } else if (args[i].equals("-png")) { - i = i + parsePNGOutputOption(args, i); - } else if (args[i].equals("-print")) { - i = i + parsePrintOutputOption(args, i); - // show print help - if (i + 1 < args.length) { - if (args[i + 1].equals("help")) { - printUsagePrintOutput(); - return false; - } - } - } else if (args[i].equals("-pcl")) { - i = i + parsePCLOutputOption(args, i); - } else if (args[i].equals("-ps")) { - i = i + parsePostscriptOutputOption(args, i); - } else if (args[i].equals("-txt")) { - i = i + parseTextOutputOption(args, i); - } else if (args[i].equals("-svg")) { - i = i + parseSVGOutputOption(args, i); - } else if (args[i].charAt(0) != '-') { - i = i + parseUnknownOption(args, i); - } else if (args[i].equals("-at")) { - i = i + parseAreaTreeOption(args, i); - } else if (args[i].equals("-v")) { - System.out.println("FOP Version " + Fop.getVersion()); - } else if (args[i].equals("-param")) { - if (i + 2 < args.length) { - if (xsltParams == null) { - xsltParams = new Vector(); - } - String name = args[++i]; - xsltParams.addElement(name); - String expression = args[++i]; - xsltParams.addElement(expression); - } else { - throw new FOPException("invalid param usage: use -param "); - } - } else { - printUsage(); - return false; - } - } - return true; - } // end parseOptions - - private int parseConfigurationOption(String[] args, int i) throws FOPException { - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException("if you use '-c', you must specify " - + "the name of the configuration file"); - } else { - userConfigFile = new File(args[i + 1]); - return 1; - } - } - - private int parseLanguageOption(String[] args, int i) throws FOPException { - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException("if you use '-l', you must specify a language"); - } else { - Locale.setDefault(new Locale(args[i + 1], "")); - return 1; - } - } - - private int parseResolution(String[] args, int i) throws FOPException { - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException( - "if you use '-dpi', you must specify a resolution (dots per inch)"); - } else { - foUserAgent.setResolution(Integer.parseInt(args[i + 1])); - return 1; - } - } - - private int parseFOInputOption(String[] args, int i) throws FOPException { - inputmode = FO_INPUT; - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException("you must specify the fo file for the '-fo' option"); - } else { - fofile = new File(args[i + 1]); - return 1; - } - } - - private int parseXSLInputOption(String[] args, int i) throws FOPException { - inputmode = XSLT_INPUT; - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException("you must specify the stylesheet " - + "file for the '-xsl' option"); - } else { - xsltfile = new File(args[i + 1]); - return 1; - } - } - - private int parseXMLInputOption(String[] args, int i) throws FOPException { - inputmode = XSLT_INPUT; - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException("you must specify the input file " - + "for the '-xml' option"); - } else { - xmlfile = new File(args[i + 1]); - return 1; - } - } - - private int parseAWTOutputOption(String[] args, int i) throws FOPException { - setOutputMode(RENDER_AWT); - return 0; - } - - private int parsePDFOutputOption(String[] args, int i) throws FOPException { - setOutputMode(RENDER_PDF); - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException("you must specify the pdf output file"); - } else { - outfile = new File(args[i + 1]); - return 1; - } - } - - private int parseMIFOutputOption(String[] args, int i) throws FOPException { - setOutputMode(RENDER_MIF); - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException("you must specify the mif output file"); - } else { - outfile = new File(args[i + 1]); - return 1; - } - } - - private int parseRTFOutputOption(String[] args, int i) throws FOPException { - setOutputMode(RENDER_RTF); - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException("you must specify the rtf output file"); - } else { - outfile = new File(args[i + 1]); - return 1; - } - } - - private int parseTIFFOutputOption(String[] args, int i) throws FOPException { - setOutputMode(RENDER_TIFF); - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException("you must specify the tiff output file"); - } else { - outfile = new File(args[i + 1]); - return 1; - } - } - - private int parsePNGOutputOption(String[] args, int i) throws FOPException { - setOutputMode(RENDER_PNG); - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException("you must specify the png output file"); - } else { - outfile = new File(args[i + 1]); - return 1; - } - } - - private int parsePrintOutputOption(String[] args, int i) throws FOPException { - setOutputMode(RENDER_PRINT); - return 0; - } - - private int parsePCLOutputOption(String[] args, int i) throws FOPException { - setOutputMode(RENDER_PCL); - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException("you must specify the pdf output file"); - } else { - outfile = new File(args[i + 1]); - return 1; - } - } - - private int parsePostscriptOutputOption(String[] args, int i) throws FOPException { - setOutputMode(RENDER_PS); - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException("you must specify the PostScript output file"); - } else { - outfile = new File(args[i + 1]); - return 1; - } - } - - private int parseTextOutputOption(String[] args, int i) throws FOPException { - setOutputMode(RENDER_TXT); - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException("you must specify the text output file"); - } else { - outfile = new File(args[i + 1]); - return 1; - } - } - - private int parseSVGOutputOption(String[] args, int i) throws FOPException { - setOutputMode(RENDER_SVG); - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException("you must specify the svg 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; - fofile = new File(args[i]); - } else if (outputmode == NOT_SET) { - outputmode = RENDER_PDF; - outfile = new File(args[i]); - } else { - throw new FOPException("Don't know what to do with " - + args[i]); - } - return 0; - } - - private int parseAreaTreeOption(String[] args, int i) throws FOPException { - setOutputMode(RENDER_XML); - if ((i + 1 == args.length) - || (args[i + 1].charAt(0) == '-')) { - throw new FOPException("you must specify the area-tree output file"); - } else { - outfile = new File(args[i + 1]); - return 1; - } - } - - private void setOutputMode(int mode) throws FOPException { - if (outputmode == NOT_SET) { - outputmode = mode; - } else { - throw new FOPException("you can only set one output method"); - } - } - - private void setLogLevel(String level) { - // Set the evel for future loggers. - LogFactory.getFactory().setAttribute("level", level); - if (log instanceof CommandLineLogger) { - // Set the level for the logger creates already. - ((CommandLineLogger) log).setLogLevel(level); - } - } - - /** - * checks whether all necessary information has been given in a consistent way - */ - private void checkSettings() throws FOPException, FileNotFoundException { - if (inputmode == NOT_SET) { - throw new FOPException("No input file specified"); - } - - if (outputmode == NOT_SET) { - throw new FOPException("No output file specified"); - } - - if ((outputmode == RENDER_AWT || outputmode == RENDER_PRINT) && outfile != null) { - throw new FOPException("Output file may not be specified " - + "for AWT or PRINT output"); - } - - if (inputmode == XSLT_INPUT) { - // check whether xml *and* xslt file have been set - if (xmlfile == null) { - throw new FOPException("XML file must be specified for the transform mode"); - } - if (xsltfile == null) { - throw new FOPException("XSLT file must be specified for the transform mode"); - } - - // warning if fofile has been set in xslt mode - if (fofile != null) { - log.warn("Can't use fo file with transform mode! Ignoring.\n" - + "Your input is " + "\n xmlfile: " - + xmlfile.getAbsolutePath() - + "\nxsltfile: " - + xsltfile.getAbsolutePath() - + "\n fofile: " - + fofile.getAbsolutePath()); - } - if (!xmlfile.exists()) { - throw new FileNotFoundException("Error: xml file " - + xmlfile.getAbsolutePath() - + " not found "); - } - if (!xsltfile.exists()) { - throw new FileNotFoundException("Error: xsl file " - + xsltfile.getAbsolutePath() - + " not found "); - } - - } else if (inputmode == FO_INPUT) { - if (xmlfile != null || xsltfile != null) { - log.warn("fo input mode, but xmlfile or xslt file are set:"); - log.error("xml file: " + xmlfile.toString()); - log.error("xslt file: " + xsltfile.toString()); - } - if (!fofile.exists()) { - throw new FileNotFoundException("Error: fo file " - + fofile.getAbsolutePath() - + " not found "); - } - - } - } // end checkSettings - - /** - * Create the user configuration. - * @throws FOPException if creating the user configuration fails - * @throws IOException - */ - private void createUserConfig() throws FOPException, IOException { - if (userConfigFile == null) { - return; - } - XMLReader parser = createParser(); - DefaultConfigurationBuilder configBuilder - = new DefaultConfigurationBuilder(parser); - Configuration userConfig = null; - try { - userConfig = configBuilder.buildFromFile(userConfigFile); - } catch (SAXException e) { - throw new FOPException(e); - } catch (ConfigurationException e) { - throw new FOPException(e); - } - foUserAgent.setUserConfig(userConfig); - } - - /** - * @return the type chosen renderer - * @throws FOPException for invalid output modes - */ - protected int getRenderer() throws FOPException { - switch (outputmode) { - case RENDER_PDF: - case RENDER_AWT: - case RENDER_MIF: - case RENDER_PRINT: - case RENDER_PCL: - case RENDER_PS: - case RENDER_TXT: - case RENDER_SVG: - case RENDER_RTF: - case RENDER_TIFF: - case RENDER_PNG: - return outputmode; - case RENDER_XML: - foUserAgent.getRendererOptions().put("fineDetail", isCoarseAreaXml()); - return RENDER_XML; - case NOT_SET: - throw new FOPException("Renderer has not been set!"); - default: - throw new FOPException("Invalid Renderer setting!"); - } - } - - /** - * Create an InputHandler object based on command-line parameters - * @return a new InputHandler instance - * @throws IllegalArgumentException if invalid/missing parameters - */ - private InputHandler createInputHandler() throws IllegalArgumentException { - switch (inputmode) { - case FO_INPUT: - return new InputHandler(fofile); - case XSLT_INPUT: - return new InputHandler(xmlfile, xsltfile, xsltParams); - default: - throw new IllegalArgumentException("Error creating InputHandler object."); - } - } - - /** - * Get the FOUserAgent for this Command-Line run - * @return FOUserAgent instance - */ - protected FOUserAgent getFOUserAgent() { - return foUserAgent; - } - - /** - * Returns the output mode (output format, ex. NOT_SET or RENDER_PDF) - * @return the output mode - */ - public int getOutputMode() { - return outputmode; - } - - /** - * Returns the XSL-FO file if set. - * @return the XSL-FO file, null if not set - */ - public File getFOFile() { - return fofile; - } - - /** - * Returns the input XML file if set. - * @return the input XML file, null if not set - */ - public File getXMLFile() { - return xmlfile; - } - - /** - * Returns the stylesheet to be used for transformation to XSL-FO. - * @return stylesheet - */ - public File getXSLFile() { - return xsltfile; - } - - /** - * Returns the output file - * @return the output file - */ - public File getOutputFile() { - return outfile; - } - - /** - * Returns the user configuration file to be used. - * @return the userconfig.xml file - */ - public File getUserConfigFile() { - return userConfigFile; - } - - /** - * Indicates whether the XML renderer should generate coarse area XML - * @return true if coarse area XML is desired - */ - public Boolean isCoarseAreaXml() { - return suppressLowLevelAreas; - } - - /** - * Returns the input file. - * @return either the fofile or the xmlfile - */ - public File getInputFile() { - switch (inputmode) { - case FO_INPUT: - return fofile; - case XSLT_INPUT: - return xmlfile; - default: - return fofile; - } - } - - /** - * shows the commandline syntax including a summary of all available options and some examples - */ - public static void printUsage() { - System.err.println( - "\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] " - + "[-awt|-pdf|-mif|-rtf|-tiff|-png|-pcl|-ps|-txt|-at|-print] \n" - + " [OPTIONS] \n" - + " -d debug mode \n" - + " -x dump configuration settings \n" - + " -q quiet mode \n" - + " -c cfg.xml use additional configuration file cfg.xml\n" - + " -l lang the language to use for user information \n" - + " -r relaxed/less strict validation (where available)\n" - + " -dpi xxx resolution in dots per inch (dpi) where xxx is a number\n" - + " -s for area tree XML, down to block areas only\n" - + " -v to show FOP version being used\n\n" - + " [INPUT] \n" - + " infile xsl:fo input file (the same as the next) \n" - + " -fo infile xsl:fo input file \n" - + " -xml infile xml input file, must be used together with -xsl \n" - + " -xsl stylesheet xslt stylesheet \n \n" - + " -param name value to use for parameter in xslt stylesheet\n" - + " (repeat '-param name value' for each parameter)\n \n" - + " [OUTPUT] \n" - + " outfile input will be rendered as pdf file into outfile \n" - + " -pdf outfile input will be rendered as pdf file (outfile req'd) \n" - + " -awt input will be displayed on screen \n" - + " -mif outfile input will be rendered as mif file (outfile req'd)\n" - + " -rtf outfile input will be rendered as rtf file (outfile req'd)\n" - + " -tiff outfile input will be rendered as tiff file (outfile req'd)\n" - + " -png outfile input will be rendered as png file (outfile req'd)\n" - + " -pcl outfile input will be rendered as pcl file (outfile req'd) \n" - + " -ps outfile input will be rendered as PostScript file (outfile req'd) \n" - + " -txt outfile input will be rendered as text file (outfile req'd) \n" - + " -svg outfile input will be rendered as an svg slides file (outfile req'd) \n" - + " -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" - + " [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 foo.fo -mif foo.mif\n" - + " Fop foo.fo -rtf foo.rtf\n" - + " Fop foo.fo -print or Fop -print foo.fo \n" - + " Fop foo.fo -awt \n"); - } - - /** - * shows the options for print output - */ - private void printUsagePrintOutput() { - System.err.println("USAGE: -print [-Dstart=i] [-Dend=i] [-Dcopies=i] [-Deven=true|false] " - + " org.apache.fop.apps.Fop (..) -print \n" - + "Example:\n" - + "java -Dstart=1 -Dend=2 org.apache.Fop.apps.Fop infile.fo -print "); - } - - /** - * Outputs all commandline settings - */ - private void dumpConfiguration() { - log.info("Input mode: "); - switch (inputmode) { - case NOT_SET: - log.info("not set"); - break; - case FO_INPUT: - log.info("FO "); - log.info("fo input file: " + fofile.toString()); - break; - case XSLT_INPUT: - log.info("xslt transformation"); - log.info("xml input file: " + xmlfile.toString()); - log.info("xslt stylesheet: " + xsltfile.toString()); - break; - default: - log.info("unknown input type"); - } - log.info("Output mode: "); - switch (outputmode) { - case NOT_SET: - log.info("not set"); - break; - case RENDER_PDF: - log.info("pdf"); - log.info("output file: " + outfile.toString()); - break; - case RENDER_AWT: - log.info("awt on screen"); - if (outfile != null) { - log.error("awt mode, but outfile is set:"); - log.info("out file: " + outfile.toString()); - } - break; - case RENDER_MIF: - log.info("mif"); - log.info("output file: " + outfile.toString()); - break; - case RENDER_RTF: - log.info("rtf"); - log.info("output file: " + outfile.toString()); - break; - case RENDER_TIFF: - log.info("tiff"); - log.info("output file: " + outfile.toString()); - break; - case RENDER_PNG: - log.info("png"); - log.info("output file: " + outfile.toString()); - break; - case RENDER_PRINT: - log.info("print directly"); - if (outfile != null) { - log.error("print mode, but outfile is set:"); - log.error("out file: " + outfile.toString()); - } - break; - case RENDER_PCL: - log.info("pcl"); - log.info("output file: " + outfile.toString()); - break; - case RENDER_PS: - log.info("PostScript"); - log.info("output file: " + outfile.toString()); - break; - case RENDER_TXT: - log.info("txt"); - log.info("output file: " + outfile.toString()); - break; - case RENDER_SVG: - log.info("svg"); - log.info("output file: " + outfile.toString()); - break; - default: - log.info("unknown input type"); - } - - log.info("OPTIONS"); - - if (userConfigFile != null) { - log.info("user configuration file: " - + userConfigFile.toString()); - } else { - log.info("no user configuration file is used [default]"); - } - } - - /** - * Creates XMLReader object using default - * SAXParserFactory - * @return the created XMLReader - * @throws FOPException if the parser couldn't be created or configured for proper operation. - */ - private XMLReader createParser() throws FOPException { - try { - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setNamespaceAware(true); - return factory.newSAXParser().getXMLReader(); - } catch (Exception e) { - throw new FOPException("Couldn't create XMLReader", e); - } - } -} - diff --git a/src/java/org/apache/fop/apps/FOUserAgent.java b/src/java/org/apache/fop/apps/FOUserAgent.java index c371a42b6..328087f3b 100644 --- a/src/java/org/apache/fop/apps/FOUserAgent.java +++ b/src/java/org/apache/fop/apps/FOUserAgent.java @@ -81,7 +81,6 @@ public class FOUserAgent { private PDFEncryptionParams pdfEncryptionParams; private float px2mm = DEFAULT_PX2MM; private Map rendererOptions = new java.util.HashMap(); - private InputHandler inputHandler = null; private File outputFile = null; private Renderer rendererOverride = null; private FOEventHandler foEventHandlerOverride = null; @@ -122,23 +121,6 @@ public class FOUserAgent { /** Set of keywords applicable to this document. */ protected String keywords = null; - /** - * Sets the InputHandler object for this process - * @param inputHandler holding input file name information - */ - public void setInputHandler(InputHandler inputHandler) { - this.inputHandler = inputHandler; - } - - /** - * Returns the apps.InputHandler object created during command-line - * processing - * @return InputHandler object - */ - public InputHandler getInputHandler() { - return inputHandler; - } - /** * Add the element mapping with the given class name. * @param elementMapping the class name representing the element mapping. diff --git a/src/java/org/apache/fop/apps/Fop.java b/src/java/org/apache/fop/apps/Fop.java index b359e23b7..bef2b4a3e 100644 --- a/src/java/org/apache/fop/apps/Fop.java +++ b/src/java/org/apache/fop/apps/Fop.java @@ -19,15 +19,7 @@ package org.apache.fop.apps; // Java -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileFilter; -import java.io.FileOutputStream; import java.io.OutputStream; -import java.lang.reflect.Method; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.List; // XML import org.xml.sax.helpers.DefaultHandler; @@ -37,8 +29,7 @@ import org.apache.fop.fo.Constants; import org.apache.fop.fo.FOTreeBuilder; /** - * Primary class that activates the FOP process for both command line - * and embedded usage. + * Primary class that activates the FOP process for embedded usage. *

* JAXP is the standard method of embedding FOP in Java programs. * Please check our embedding page (http://xml.apache.org/fop/embedding.html) @@ -160,161 +151,6 @@ public class Fop implements Constants { } } - /** - * @return the list of URLs to all libraries. - * @throws MalformedURLException In case there is a problem converting java.io.File - * instances to URLs. - */ - public static URL[] getJARList() throws MalformedURLException { - File baseDir = new File(".").getAbsoluteFile().getParentFile(); - File buildDir; - if ("build".equals(baseDir.getName())) { - buildDir = baseDir; - baseDir = baseDir.getParentFile(); - } else { - buildDir = new File(baseDir, "build"); - } - File fopJar = new File(buildDir, "fop.jar"); - if (!fopJar.exists()) { - fopJar = new File(baseDir, "fop.jar"); - } - if (!fopJar.exists()) { - throw new RuntimeException("fop.jar not found in directory: " - + baseDir.getAbsolutePath() + " (or below)"); - } - List jars = new java.util.ArrayList(); - jars.add(fopJar.toURL()); - File[] files; - FileFilter filter = new FileFilter() { - public boolean accept(File pathname) { - return pathname.getName().endsWith(".jar"); - } - }; - File libDir = new File(baseDir, "lib"); - if (!libDir.exists()) { - libDir = baseDir; - } - files = libDir.listFiles(filter); - if (files != null) { - for (int i = 0, size = files.length; i < size; i++) { - jars.add(files[i].toURL()); - } - } - String optionalLib = System.getProperty("fop.optional.lib"); - if (optionalLib != null) { - files = new File(optionalLib).listFiles(filter); - if (files != null) { - for (int i = 0, size = files.length; i < size; i++) { - jars.add(files[i].toURL()); - } - } - } - URL[] urls = (URL[])jars.toArray(new URL[jars.size()]); - /* - for (int i = 0, c = urls.length; i < c; i++) { - System.out.println(urls[i]); - }*/ - return urls; - } - - /** - * @return true if FOP's dependecies are available in the current ClassLoader setup. - */ - public static boolean checkDependencies() { - try { - //System.out.println(Thread.currentThread().getContextClassLoader()); - Class clazz = Class.forName("org.apache.batik.Version"); - if (clazz != null) { - clazz = Class.forName("org.apache.avalon.framework.configuration.Configuration"); - } - return (clazz != null); - } catch (Exception e) { - return false; - } - } - - /** - * Dynamically builds a ClassLoader and executes FOP. - * @param args command-line arguments - */ - public static void startFOPWithDynamicClasspath(String[] args) { - try { - URL[] urls = getJARList(); - //System.out.println("CCL: " - // + Thread.currentThread().getContextClassLoader().toString()); - ClassLoader loader = new java.net.URLClassLoader(urls, null); - Thread.currentThread().setContextClassLoader(loader); - Class clazz = Class.forName("org.apache.fop.apps.Fop", true, loader); - //System.out.println("CL: " + clazz.getClassLoader().toString()); - Method mainMethod = clazz.getMethod("startFOP", new Class[] {String[].class}); - mainMethod.invoke(null, new Object[] {args}); - } catch (Exception e) { - System.err.println("Unable to start FOP:"); - e.printStackTrace(); - System.exit(-1); - } - } - - /** - * Executes FOP with the given ClassLoader setup. - * @param args command-line arguments - */ - public static void startFOP(String[] args) { - //System.out.println("static CCL: " - // + Thread.currentThread().getContextClassLoader().toString()); - //System.out.println("static CL: " + Fop.class.getClassLoader().toString()); - CommandLineOptions options = null; - FOUserAgent foUserAgent = null; - BufferedOutputStream bos = null; - - try { - options = new CommandLineOptions(); - options.parse(args); - foUserAgent = options.getFOUserAgent(); - - Fop 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()); - } - foUserAgent.getInputHandler().render(fop); - } finally { - if (bos != null) { - bos.close(); - } - } - - // System.exit(0) called to close AWT/SVG-created threads, if any. - // AWTRenderer closes with window shutdown, so exit() should not - // be called here - if (options.getOutputMode() != CommandLineOptions.RENDER_AWT) { - System.exit(0); - } - } catch (Exception e) { - if (options != null) { - options.getLogger().error("Exception", e); - } - System.exit(1); - } - } - - /** - * The main routine for the command line interface - * @param args the command line parameters - */ - public static void main(String[] args) { - if (checkDependencies()) { - startFOP(args); - } else { - startFOPWithDynamicClasspath(args); - } - } - - /** * Get the version of FOP * @return the version string diff --git a/src/java/org/apache/fop/apps/InputHandler.java b/src/java/org/apache/fop/apps/InputHandler.java deleted file mode 100644 index ec4daa5e7..000000000 --- a/src/java/org/apache/fop/apps/InputHandler.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Copyright 1999-2004 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. - * 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 org.apache.fop.apps; - -// Imported java.io classes -import java.io.File; -import java.util.Vector; - -// Imported TraX classes -import javax.xml.transform.ErrorListener; -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.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * Class for handling files input from command line - * either with XML and XSLT files (and optionally xsl - * parameters) or FO File input alone - */ -public class InputHandler implements ErrorListener { - private File sourcefile = null; // either FO or XML/XSLT usage - private File stylesheet = null; // for XML/XSLT usage - private Vector xsltParams = null; // for XML/XSLT usage - - protected Log log = LogFactory.getLog(InputHandler.class); - - /** - * Constructor for XML->XSLT->FO input - * @param xmlfile XML file - * @param xsltfile XSLT file - * @param params Vector of command-line parameters (name, value, - * name, value, ...) for XSL stylesheet, null if none - */ - public InputHandler(File xmlfile, File xsltfile, Vector params) { - sourcefile = xmlfile; - stylesheet = xsltfile; - xsltParams = params; - } - - /** - * Constructor for FO input - * @param fofile the file to read the FO document. - */ - public InputHandler(File fofile) { - sourcefile = fofile; - } - - /** - * Generate a document, given an initialized Fop object - * @param fop -- Fop object - * @throws FOPException in case of an error during processing - */ - public void render(Fop fop) throws FOPException { - - // if base URL was not explicitly set in FOUserAgent, obtain here - if (fop.getUserAgent().getBaseURL() == null) { - String baseURL = null; - - try { - baseURL = - new File(sourcefile.getAbsolutePath()). - getParentFile().toURL().toExternalForm(); - } catch (Exception e) { - baseURL = ""; - } - fop.getUserAgent().setBaseURL(baseURL); - } - - try { - // Setup XSLT - TransformerFactory factory = TransformerFactory.newInstance(); - Transformer transformer; - - if (stylesheet == null) { // FO Input - transformer = factory.newTransformer(); - } else { // XML/XSLT input - transformer = factory.newTransformer(new StreamSource( - stylesheet)); - - // Set the value of parameters, if any, defined for stylesheet - if (xsltParams != null) { - for (int i = 0; i < xsltParams.size(); i += 2) { - transformer.setParameter((String) xsltParams.elementAt(i), - (String) xsltParams.elementAt(i + 1)); - } - } - } - transformer.setErrorListener(this); - - // 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); - - } catch (Exception e) { - throw new FOPException(e); - } - } - - /** - * Implementation of the ErrorListener interface. - */ - public void warning(TransformerException exc) { - log.warn(exc.toString()); - } - - /** - * Implementation of the ErrorListener interface. - */ - public void error(TransformerException exc) { - log.error(exc.toString()); - } - - /** - * Implementation of the ErrorListener interface. - */ - public void fatalError(TransformerException exc) - throws TransformerException { - throw exc; - } -} diff --git a/src/java/org/apache/fop/apps/package.html b/src/java/org/apache/fop/apps/package.html index c8a9798d5..2bf75de3f 100644 --- a/src/java/org/apache/fop/apps/package.html +++ b/src/java/org/apache/fop/apps/package.html @@ -1,7 +1,6 @@ org.apache.fop.apps Package -

Application classes used for running FOP both on the command line and -embedded in other applications.

+

Application classes used for running FOP embedded in other applications.

\ No newline at end of file diff --git a/src/java/org/apache/fop/cli/CommandLineOptions.java b/src/java/org/apache/fop/cli/CommandLineOptions.java new file mode 100644 index 000000000..3eb70b577 --- /dev/null +++ b/src/java/org/apache/fop/cli/CommandLineOptions.java @@ -0,0 +1,829 @@ +/* + * 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. + * 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 org.apache.fop.cli; + +// java +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Locale; +import java.util.Vector; + +import org.apache.fop.apps.FOPException; +import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.Fop; +import org.apache.fop.fo.Constants; +import org.apache.fop.render.awt.AWTRenderer; +import org.apache.fop.util.CommandLineLogger; + +// commons logging +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +// SAX +import org.xml.sax.XMLReader; +import org.xml.sax.SAXException; +import javax.xml.parsers.SAXParserFactory; + +// avalon configuration +import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; + +/** + * Options parses the commandline arguments + */ +public class CommandLineOptions implements Constants { + + /* show configuration information */ + private Boolean showConfiguration = Boolean.FALSE; + /* for area tree XML output, only down to block area level */ + private Boolean suppressLowLevelAreas = Boolean.FALSE; + /* user configuration file */ + private File userConfigFile = null; + /* input fo file */ + private File fofile = null; + /* xsltfile (xslt transformation as input) */ + private File xsltfile = null; + /* xml file (xslt transformation as input) */ + private File xmlfile = null; + /* output file */ + private File outfile = null; + /* input mode */ + private int inputmode = NOT_SET; + /* output mode */ + private int outputmode = NOT_SET; + + private FOUserAgent foUserAgent; + + private InputHandler inputHandler; + + private Log log; + + private Vector xsltParams = null; + + /** + * Construct a command line option object. + */ + public CommandLineOptions() { + LogFactory logFactory = LogFactory.getFactory(); + + // Enable the simple command line logging when no other logger is + // defined. + if (System.getProperty("org.apache.commons.logging.Log") == null) { + logFactory.setAttribute("org.apache.commons.logging.Log", + CommandLineLogger.class.getName()); + setLogLevel("info"); + } + + log = LogFactory.getLog("FOP"); + } + + /** + * Parse the command line arguments. + * @param args the command line arguments. + * @throws FOPException for general errors + * @throws FileNotFoundException if an input file wasn't found + * @throws IOException if the the configuration file could not be loaded + */ + public void parse(String[] args) + throws FOPException, IOException { + boolean optionsParsed = true; + + foUserAgent = new FOUserAgent(); + + try { + optionsParsed = parseOptions(args); + if (optionsParsed) { + if (showConfiguration == Boolean.TRUE) { + dumpConfiguration(); + } + checkSettings(); + createUserConfig(); + } + } catch (FOPException e) { + printUsage(); + throw e; + } catch (java.io.FileNotFoundException e) { + printUsage(); + throw e; + } + + inputHandler = createInputHandler(); + + if (outputmode == RENDER_AWT) { + AWTRenderer renderer = new AWTRenderer(); + renderer.setRenderable(inputHandler); //set before user agent! + renderer.setUserAgent(foUserAgent); + foUserAgent.setRendererOverride(renderer); + } + } + + /** + * @return the InputHandler instance defined by the command-line options. + */ + public InputHandler getInputHandler() { + return inputHandler; + } + + /** + * Get the logger. + * @return the logger + */ + public Log getLogger() { + return log; + } + + /** + * parses the commandline arguments + * @return true if parse was successful and processing can continue, false + * if processing should stop + * @exception FOPException if there was an error in the format of the options + */ + private boolean parseOptions(String[] args) throws FOPException { + for (int i = 0; i < args.length; i++) { + if (args[i].equals("-x") + || args[i].equals("--dump-config")) { + showConfiguration = Boolean.TRUE; + } else if (args[i].equals("-c")) { + i = i + parseConfigurationOption(args, i); + } else if (args[i].equals("-l")) { + i = i + parseLanguageOption(args, i); + } else if (args[i].equals("-s")) { + suppressLowLevelAreas = Boolean.TRUE; + } else if (args[i].equals("-d")) { + setLogLevel("debug"); + } else if (args[i].equals("-r")) { + foUserAgent.setStrictValidation(false); + } else if (args[i].equals("-dpi")) { + i = i + parseResolution(args, i); + } else if (args[i].equals("-q") || args[i].equals("--quiet")) { + setLogLevel("error"); + } else if (args[i].equals("-fo")) { + i = i + parseFOInputOption(args, i); + } else if (args[i].equals("-xsl")) { + i = i + parseXSLInputOption(args, i); + } else if (args[i].equals("-xml")) { + i = i + parseXMLInputOption(args, i); + } else if (args[i].equals("-awt")) { + i = i + parseAWTOutputOption(args, i); + } else if (args[i].equals("-pdf")) { + i = i + parsePDFOutputOption(args, i); + } else if (args[i].equals("-mif")) { + i = i + parseMIFOutputOption(args, i); + } else if (args[i].equals("-rtf")) { + i = i + parseRTFOutputOption(args, i); + } else if (args[i].equals("-tiff")) { + i = i + parseTIFFOutputOption(args, i); + } else if (args[i].equals("-png")) { + i = i + parsePNGOutputOption(args, i); + } else if (args[i].equals("-print")) { + i = i + parsePrintOutputOption(args, i); + // show print help + if (i + 1 < args.length) { + if (args[i + 1].equals("help")) { + printUsagePrintOutput(); + return false; + } + } + } else if (args[i].equals("-pcl")) { + i = i + parsePCLOutputOption(args, i); + } else if (args[i].equals("-ps")) { + i = i + parsePostscriptOutputOption(args, i); + } else if (args[i].equals("-txt")) { + i = i + parseTextOutputOption(args, i); + } else if (args[i].equals("-svg")) { + i = i + parseSVGOutputOption(args, i); + } else if (args[i].charAt(0) != '-') { + i = i + parseUnknownOption(args, i); + } else if (args[i].equals("-at")) { + i = i + parseAreaTreeOption(args, i); + } else if (args[i].equals("-v")) { + System.out.println("FOP Version " + Fop.getVersion()); + } else if (args[i].equals("-param")) { + if (i + 2 < args.length) { + if (xsltParams == null) { + xsltParams = new Vector(); + } + String name = args[++i]; + xsltParams.addElement(name); + String expression = args[++i]; + xsltParams.addElement(expression); + } else { + throw new FOPException("invalid param usage: use -param "); + } + } else { + printUsage(); + return false; + } + } + return true; + } // end parseOptions + + private int parseConfigurationOption(String[] args, int i) throws FOPException { + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("if you use '-c', you must specify " + + "the name of the configuration file"); + } else { + userConfigFile = new File(args[i + 1]); + return 1; + } + } + + private int parseLanguageOption(String[] args, int i) throws FOPException { + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("if you use '-l', you must specify a language"); + } else { + Locale.setDefault(new Locale(args[i + 1], "")); + return 1; + } + } + + private int parseResolution(String[] args, int i) throws FOPException { + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException( + "if you use '-dpi', you must specify a resolution (dots per inch)"); + } else { + foUserAgent.setResolution(Integer.parseInt(args[i + 1])); + return 1; + } + } + + private int parseFOInputOption(String[] args, int i) throws FOPException { + inputmode = FO_INPUT; + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("you must specify the fo file for the '-fo' option"); + } else { + fofile = new File(args[i + 1]); + return 1; + } + } + + private int parseXSLInputOption(String[] args, int i) throws FOPException { + inputmode = XSLT_INPUT; + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("you must specify the stylesheet " + + "file for the '-xsl' option"); + } else { + xsltfile = new File(args[i + 1]); + return 1; + } + } + + private int parseXMLInputOption(String[] args, int i) throws FOPException { + inputmode = XSLT_INPUT; + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("you must specify the input file " + + "for the '-xml' option"); + } else { + xmlfile = new File(args[i + 1]); + return 1; + } + } + + private int parseAWTOutputOption(String[] args, int i) throws FOPException { + setOutputMode(RENDER_AWT); + return 0; + } + + private int parsePDFOutputOption(String[] args, int i) throws FOPException { + setOutputMode(RENDER_PDF); + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("you must specify the pdf output file"); + } else { + outfile = new File(args[i + 1]); + return 1; + } + } + + private int parseMIFOutputOption(String[] args, int i) throws FOPException { + setOutputMode(RENDER_MIF); + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("you must specify the mif output file"); + } else { + outfile = new File(args[i + 1]); + return 1; + } + } + + private int parseRTFOutputOption(String[] args, int i) throws FOPException { + setOutputMode(RENDER_RTF); + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("you must specify the rtf output file"); + } else { + outfile = new File(args[i + 1]); + return 1; + } + } + + private int parseTIFFOutputOption(String[] args, int i) throws FOPException { + setOutputMode(RENDER_TIFF); + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("you must specify the tiff output file"); + } else { + outfile = new File(args[i + 1]); + return 1; + } + } + + private int parsePNGOutputOption(String[] args, int i) throws FOPException { + setOutputMode(RENDER_PNG); + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("you must specify the png output file"); + } else { + outfile = new File(args[i + 1]); + return 1; + } + } + + private int parsePrintOutputOption(String[] args, int i) throws FOPException { + setOutputMode(RENDER_PRINT); + return 0; + } + + private int parsePCLOutputOption(String[] args, int i) throws FOPException { + setOutputMode(RENDER_PCL); + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("you must specify the pdf output file"); + } else { + outfile = new File(args[i + 1]); + return 1; + } + } + + private int parsePostscriptOutputOption(String[] args, int i) throws FOPException { + setOutputMode(RENDER_PS); + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("you must specify the PostScript output file"); + } else { + outfile = new File(args[i + 1]); + return 1; + } + } + + private int parseTextOutputOption(String[] args, int i) throws FOPException { + setOutputMode(RENDER_TXT); + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("you must specify the text output file"); + } else { + outfile = new File(args[i + 1]); + return 1; + } + } + + private int parseSVGOutputOption(String[] args, int i) throws FOPException { + setOutputMode(RENDER_SVG); + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("you must specify the svg 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; + fofile = new File(args[i]); + } else if (outputmode == NOT_SET) { + outputmode = RENDER_PDF; + outfile = new File(args[i]); + } else { + throw new FOPException("Don't know what to do with " + + args[i]); + } + return 0; + } + + private int parseAreaTreeOption(String[] args, int i) throws FOPException { + setOutputMode(RENDER_XML); + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("you must specify the area-tree output file"); + } else { + outfile = new File(args[i + 1]); + return 1; + } + } + + private void setOutputMode(int mode) throws FOPException { + if (outputmode == NOT_SET) { + outputmode = mode; + } else { + throw new FOPException("you can only set one output method"); + } + } + + private void setLogLevel(String level) { + // Set the evel for future loggers. + LogFactory.getFactory().setAttribute("level", level); + if (log instanceof CommandLineLogger) { + // Set the level for the logger creates already. + ((CommandLineLogger) log).setLogLevel(level); + } + } + + /** + * checks whether all necessary information has been given in a consistent way + */ + private void checkSettings() throws FOPException, FileNotFoundException { + if (inputmode == NOT_SET) { + throw new FOPException("No input file specified"); + } + + if (outputmode == NOT_SET) { + throw new FOPException("No output file specified"); + } + + if ((outputmode == RENDER_AWT || outputmode == RENDER_PRINT) && outfile != null) { + throw new FOPException("Output file may not be specified " + + "for AWT or PRINT output"); + } + + if (inputmode == XSLT_INPUT) { + // check whether xml *and* xslt file have been set + if (xmlfile == null) { + throw new FOPException("XML file must be specified for the transform mode"); + } + if (xsltfile == null) { + throw new FOPException("XSLT file must be specified for the transform mode"); + } + + // warning if fofile has been set in xslt mode + if (fofile != null) { + log.warn("Can't use fo file with transform mode! Ignoring.\n" + + "Your input is " + "\n xmlfile: " + + xmlfile.getAbsolutePath() + + "\nxsltfile: " + + xsltfile.getAbsolutePath() + + "\n fofile: " + + fofile.getAbsolutePath()); + } + if (!xmlfile.exists()) { + throw new FileNotFoundException("Error: xml file " + + xmlfile.getAbsolutePath() + + " not found "); + } + if (!xsltfile.exists()) { + throw new FileNotFoundException("Error: xsl file " + + xsltfile.getAbsolutePath() + + " not found "); + } + + } else if (inputmode == FO_INPUT) { + if (xmlfile != null || xsltfile != null) { + log.warn("fo input mode, but xmlfile or xslt file are set:"); + log.error("xml file: " + xmlfile.toString()); + log.error("xslt file: " + xsltfile.toString()); + } + if (!fofile.exists()) { + throw new FileNotFoundException("Error: fo file " + + fofile.getAbsolutePath() + + " not found "); + } + + } + } // end checkSettings + + /** + * Create the user configuration. + * @throws FOPException if creating the user configuration fails + * @throws IOException + */ + private void createUserConfig() throws FOPException, IOException { + if (userConfigFile == null) { + return; + } + XMLReader parser = createParser(); + DefaultConfigurationBuilder configBuilder + = new DefaultConfigurationBuilder(parser); + Configuration userConfig = null; + try { + userConfig = configBuilder.buildFromFile(userConfigFile); + } catch (SAXException e) { + throw new FOPException(e); + } catch (ConfigurationException e) { + throw new FOPException(e); + } + foUserAgent.setUserConfig(userConfig); + } + + /** + * @return the type chosen renderer + * @throws FOPException for invalid output modes + */ + protected int getRenderer() throws FOPException { + switch (outputmode) { + case RENDER_PDF: + case RENDER_AWT: + case RENDER_MIF: + case RENDER_PRINT: + case RENDER_PCL: + case RENDER_PS: + case RENDER_TXT: + case RENDER_SVG: + case RENDER_RTF: + case RENDER_TIFF: + case RENDER_PNG: + return outputmode; + case RENDER_XML: + foUserAgent.getRendererOptions().put("fineDetail", isCoarseAreaXml()); + return RENDER_XML; + case NOT_SET: + throw new FOPException("Renderer has not been set!"); + default: + throw new FOPException("Invalid Renderer setting!"); + } + } + + /** + * Create an InputHandler object based on command-line parameters + * @return a new InputHandler instance + * @throws IllegalArgumentException if invalid/missing parameters + */ + private InputHandler createInputHandler() throws IllegalArgumentException { + switch (inputmode) { + case FO_INPUT: + return new InputHandler(fofile); + case XSLT_INPUT: + return new InputHandler(xmlfile, xsltfile, xsltParams); + default: + throw new IllegalArgumentException("Error creating InputHandler object."); + } + } + + /** + * Get the FOUserAgent for this Command-Line run + * @return FOUserAgent instance + */ + protected FOUserAgent getFOUserAgent() { + return foUserAgent; + } + + /** + * Returns the output mode (output format, ex. NOT_SET or RENDER_PDF) + * @return the output mode + */ + public int getOutputMode() { + return outputmode; + } + + /** + * Returns the XSL-FO file if set. + * @return the XSL-FO file, null if not set + */ + public File getFOFile() { + return fofile; + } + + /** + * Returns the input XML file if set. + * @return the input XML file, null if not set + */ + public File getXMLFile() { + return xmlfile; + } + + /** + * Returns the stylesheet to be used for transformation to XSL-FO. + * @return stylesheet + */ + public File getXSLFile() { + return xsltfile; + } + + /** + * Returns the output file + * @return the output file + */ + public File getOutputFile() { + return outfile; + } + + /** + * Returns the user configuration file to be used. + * @return the userconfig.xml file + */ + public File getUserConfigFile() { + return userConfigFile; + } + + /** + * Indicates whether the XML renderer should generate coarse area XML + * @return true if coarse area XML is desired + */ + public Boolean isCoarseAreaXml() { + return suppressLowLevelAreas; + } + + /** + * Returns the input file. + * @return either the fofile or the xmlfile + */ + public File getInputFile() { + switch (inputmode) { + case FO_INPUT: + return fofile; + case XSLT_INPUT: + return xmlfile; + default: + return fofile; + } + } + + /** + * shows the commandline syntax including a summary of all available options and some examples + */ + public static void printUsage() { + System.err.println( + "\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] " + + "[-awt|-pdf|-mif|-rtf|-tiff|-png|-pcl|-ps|-txt|-at|-print] \n" + + " [OPTIONS] \n" + + " -d debug mode \n" + + " -x dump configuration settings \n" + + " -q quiet mode \n" + + " -c cfg.xml use additional configuration file cfg.xml\n" + + " -l lang the language to use for user information \n" + + " -r relaxed/less strict validation (where available)\n" + + " -dpi xxx resolution in dots per inch (dpi) where xxx is a number\n" + + " -s for area tree XML, down to block areas only\n" + + " -v to show FOP version being used\n\n" + + " [INPUT] \n" + + " infile xsl:fo input file (the same as the next) \n" + + " -fo infile xsl:fo input file \n" + + " -xml infile xml input file, must be used together with -xsl \n" + + " -xsl stylesheet xslt stylesheet \n \n" + + " -param name value to use for parameter in xslt stylesheet\n" + + " (repeat '-param name value' for each parameter)\n \n" + + " [OUTPUT] \n" + + " outfile input will be rendered as pdf file into outfile \n" + + " -pdf outfile input will be rendered as pdf file (outfile req'd) \n" + + " -awt input will be displayed on screen \n" + + " -mif outfile input will be rendered as mif file (outfile req'd)\n" + + " -rtf outfile input will be rendered as rtf file (outfile req'd)\n" + + " -tiff outfile input will be rendered as tiff file (outfile req'd)\n" + + " -png outfile input will be rendered as png file (outfile req'd)\n" + + " -pcl outfile input will be rendered as pcl file (outfile req'd) \n" + + " -ps outfile input will be rendered as PostScript file (outfile req'd) \n" + + " -txt outfile input will be rendered as text file (outfile req'd) \n" + + " -svg outfile input will be rendered as an svg slides file (outfile req'd) \n" + + " -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" + + " [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 foo.fo -mif foo.mif\n" + + " Fop foo.fo -rtf foo.rtf\n" + + " Fop foo.fo -print or Fop -print foo.fo \n" + + " Fop foo.fo -awt \n"); + } + + /** + * shows the options for print output + */ + private void printUsagePrintOutput() { + System.err.println("USAGE: -print [-Dstart=i] [-Dend=i] [-Dcopies=i] [-Deven=true|false] " + + " org.apache.fop.apps.Fop (..) -print \n" + + "Example:\n" + + "java -Dstart=1 -Dend=2 org.apache.Fop.apps.Fop infile.fo -print "); + } + + /** + * Outputs all commandline settings + */ + private void dumpConfiguration() { + log.info("Input mode: "); + switch (inputmode) { + case NOT_SET: + log.info("not set"); + break; + case FO_INPUT: + log.info("FO "); + log.info("fo input file: " + fofile.toString()); + break; + case XSLT_INPUT: + log.info("xslt transformation"); + log.info("xml input file: " + xmlfile.toString()); + log.info("xslt stylesheet: " + xsltfile.toString()); + break; + default: + log.info("unknown input type"); + } + log.info("Output mode: "); + switch (outputmode) { + case NOT_SET: + log.info("not set"); + break; + case RENDER_PDF: + log.info("pdf"); + log.info("output file: " + outfile.toString()); + break; + case RENDER_AWT: + log.info("awt on screen"); + if (outfile != null) { + log.error("awt mode, but outfile is set:"); + log.info("out file: " + outfile.toString()); + } + break; + case RENDER_MIF: + log.info("mif"); + log.info("output file: " + outfile.toString()); + break; + case RENDER_RTF: + log.info("rtf"); + log.info("output file: " + outfile.toString()); + break; + case RENDER_TIFF: + log.info("tiff"); + log.info("output file: " + outfile.toString()); + break; + case RENDER_PNG: + log.info("png"); + log.info("output file: " + outfile.toString()); + break; + case RENDER_PRINT: + log.info("print directly"); + if (outfile != null) { + log.error("print mode, but outfile is set:"); + log.error("out file: " + outfile.toString()); + } + break; + case RENDER_PCL: + log.info("pcl"); + log.info("output file: " + outfile.toString()); + break; + case RENDER_PS: + log.info("PostScript"); + log.info("output file: " + outfile.toString()); + break; + case RENDER_TXT: + log.info("txt"); + log.info("output file: " + outfile.toString()); + break; + case RENDER_SVG: + log.info("svg"); + log.info("output file: " + outfile.toString()); + break; + default: + log.info("unknown input type"); + } + + log.info("OPTIONS"); + + if (userConfigFile != null) { + log.info("user configuration file: " + + userConfigFile.toString()); + } else { + log.info("no user configuration file is used [default]"); + } + } + + /** + * Creates XMLReader object using default + * SAXParserFactory + * @return the created XMLReader + * @throws FOPException if the parser couldn't be created or configured for proper operation. + */ + private XMLReader createParser() throws FOPException { + try { + SAXParserFactory factory = SAXParserFactory.newInstance(); + factory.setNamespaceAware(true); + return factory.newSAXParser().getXMLReader(); + } catch (Exception e) { + throw new FOPException("Couldn't create XMLReader", e); + } + } +} + diff --git a/src/java/org/apache/fop/cli/InputHandler.java b/src/java/org/apache/fop/cli/InputHandler.java new file mode 100644 index 000000000..0de3cc9fe --- /dev/null +++ b/src/java/org/apache/fop/cli/InputHandler.java @@ -0,0 +1,153 @@ +/* + * Copyright 1999-2004 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. + * 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 org.apache.fop.cli; + +// Imported java.io classes +import java.io.File; +import java.util.Vector; + +// Imported TraX classes +import javax.xml.transform.ErrorListener; +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.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.fop.apps.FOPException; +import org.apache.fop.apps.Fop; +import org.apache.fop.render.awt.viewer.Renderable; + +/** + * Class for handling files input from command line + * either with XML and XSLT files (and optionally xsl + * parameters) or FO File input alone + */ +public class InputHandler implements ErrorListener, Renderable { + + private File sourcefile = null; // either FO or XML/XSLT usage + private File stylesheet = null; // for XML/XSLT usage + private Vector xsltParams = null; // for XML/XSLT usage + + protected Log log = LogFactory.getLog(InputHandler.class); + + /** + * Constructor for XML->XSLT->FO input + * @param xmlfile XML file + * @param xsltfile XSLT file + * @param params Vector of command-line parameters (name, value, + * name, value, ...) for XSL stylesheet, null if none + */ + public InputHandler(File xmlfile, File xsltfile, Vector params) { + sourcefile = xmlfile; + stylesheet = xsltfile; + xsltParams = params; + } + + /** + * Constructor for FO input + * @param fofile the file to read the FO document. + */ + public InputHandler(File fofile) { + sourcefile = fofile; + } + + /** + * Generate a document, given an initialized Fop object + * @param fop -- Fop object + * @throws FOPException in case of an error during processing + */ + public void render(Fop fop) throws FOPException { + + // if base URL was not explicitly set in FOUserAgent, obtain here + if (fop.getUserAgent().getBaseURL() == null) { + String baseURL = null; + + try { + baseURL = new File(sourcefile.getAbsolutePath()). + getParentFile().toURL().toExternalForm(); + } catch (Exception e) { + baseURL = ""; + } + fop.getUserAgent().setBaseURL(baseURL); + } + + try { + // Setup XSLT + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer; + + if (stylesheet == null) { // FO Input + transformer = factory.newTransformer(); + } else { // XML/XSLT input + transformer = factory.newTransformer(new StreamSource( + stylesheet)); + + // Set the value of parameters, if any, defined for stylesheet + if (xsltParams != null) { + for (int i = 0; i < xsltParams.size(); i += 2) { + transformer.setParameter((String) xsltParams.elementAt(i), + (String) xsltParams.elementAt(i + 1)); + } + } + } + transformer.setErrorListener(this); + + // 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); + + } catch (Exception e) { + throw new FOPException(e); + } + } + + // --- Implementation of the ErrorListener interface --- + + /** + * @see javax.xml.transform.ErrorListener#warning(javax.xml.transform.TransformerException) + */ + public void warning(TransformerException exc) { + log.warn(exc.toString()); + } + + /** + * @see javax.xml.transform.ErrorListener#error(javax.xml.transform.TransformerException) + */ + public void error(TransformerException exc) { + log.error(exc.toString()); + } + + /** + * @see javax.xml.transform.ErrorListener#fatalError(javax.xml.transform.TransformerException) + */ + public void fatalError(TransformerException exc) + throws TransformerException { + throw exc; + } +} diff --git a/src/java/org/apache/fop/cli/Main.java b/src/java/org/apache/fop/cli/Main.java new file mode 100644 index 000000000..4f05dcab1 --- /dev/null +++ b/src/java/org/apache/fop/cli/Main.java @@ -0,0 +1,192 @@ +/* + * Copyright 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. + * 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 org.apache.fop.cli; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileFilter; +import java.io.FileOutputStream; +import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; + +import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.Fop; + +/** + * Main command-line class for Apache FOP. + */ +public class Main { + + /** + * @return the list of URLs to all libraries. + * @throws MalformedURLException In case there is a problem converting java.io.File + * instances to URLs. + */ + public static URL[] getJARList() throws MalformedURLException { + File baseDir = new File(".").getAbsoluteFile().getParentFile(); + File buildDir; + if ("build".equals(baseDir.getName())) { + buildDir = baseDir; + baseDir = baseDir.getParentFile(); + } else { + buildDir = new File(baseDir, "build"); + } + File fopJar = new File(buildDir, "fop.jar"); + if (!fopJar.exists()) { + fopJar = new File(baseDir, "fop.jar"); + } + if (!fopJar.exists()) { + throw new RuntimeException("fop.jar not found in directory: " + + baseDir.getAbsolutePath() + " (or below)"); + } + List jars = new java.util.ArrayList(); + jars.add(fopJar.toURL()); + File[] files; + FileFilter filter = new FileFilter() { + public boolean accept(File pathname) { + return pathname.getName().endsWith(".jar"); + } + }; + File libDir = new File(baseDir, "lib"); + if (!libDir.exists()) { + libDir = baseDir; + } + files = libDir.listFiles(filter); + if (files != null) { + for (int i = 0, size = files.length; i < size; i++) { + jars.add(files[i].toURL()); + } + } + String optionalLib = System.getProperty("fop.optional.lib"); + if (optionalLib != null) { + files = new File(optionalLib).listFiles(filter); + if (files != null) { + for (int i = 0, size = files.length; i < size; i++) { + jars.add(files[i].toURL()); + } + } + } + URL[] urls = (URL[])jars.toArray(new URL[jars.size()]); + /* + for (int i = 0, c = urls.length; i < c; i++) { + System.out.println(urls[i]); + }*/ + return urls; + } + + /** + * @return true if FOP's dependecies are available in the current ClassLoader setup. + */ + public static boolean checkDependencies() { + try { + //System.out.println(Thread.currentThread().getContextClassLoader()); + Class clazz = Class.forName("org.apache.batik.Version"); + if (clazz != null) { + clazz = Class.forName("org.apache.avalon.framework.configuration.Configuration"); + } + return (clazz != null); + } catch (Exception e) { + return false; + } + } + + /** + * Dynamically builds a ClassLoader and executes FOP. + * @param args command-line arguments + */ + public static void startFOPWithDynamicClasspath(String[] args) { + try { + URL[] urls = getJARList(); + //System.out.println("CCL: " + // + Thread.currentThread().getContextClassLoader().toString()); + ClassLoader loader = new java.net.URLClassLoader(urls, null); + Thread.currentThread().setContextClassLoader(loader); + Class clazz = Class.forName("org.apache.fop.cli.Main", true, loader); + //System.out.println("CL: " + clazz.getClassLoader().toString()); + Method mainMethod = clazz.getMethod("startFOP", new Class[] {String[].class}); + mainMethod.invoke(null, new Object[] {args}); + } catch (Exception e) { + System.err.println("Unable to start FOP:"); + e.printStackTrace(); + System.exit(-1); + } + } + + /** + * Executes FOP with the given ClassLoader setup. + * @param args command-line arguments + */ + public static void startFOP(String[] args) { + //System.out.println("static CCL: " + // + Thread.currentThread().getContextClassLoader().toString()); + //System.out.println("static CL: " + Fop.class.getClassLoader().toString()); + CommandLineOptions options = null; + FOUserAgent foUserAgent = null; + BufferedOutputStream bos = null; + + try { + options = new CommandLineOptions(); + options.parse(args); + foUserAgent = options.getFOUserAgent(); + + Fop 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()); + } + options.getInputHandler().render(fop); + } finally { + if (bos != null) { + bos.close(); + } + } + + // System.exit(0) called to close AWT/SVG-created threads, if any. + // AWTRenderer closes with window shutdown, so exit() should not + // be called here + if (options.getOutputMode() != CommandLineOptions.RENDER_AWT) { + System.exit(0); + } + } catch (Exception e) { + if (options != null) { + options.getLogger().error("Exception", e); + } + System.exit(1); + } + } + + /** + * The main routine for the command line interface + * @param args the command line parameters + */ + public static void main(String[] args) { + if (checkDependencies()) { + startFOP(args); + } else { + startFOPWithDynamicClasspath(args); + } + } + +} diff --git a/src/java/org/apache/fop/cli/package.html b/src/java/org/apache/fop/cli/package.html new file mode 100644 index 000000000..8363121fd --- /dev/null +++ b/src/java/org/apache/fop/cli/package.html @@ -0,0 +1,6 @@ + +org.apache.fop.cli Package + +

This package contains the command-line client for Apache FOP.

+ + \ No newline at end of file diff --git a/src/java/org/apache/fop/render/awt/AWTRenderer.java b/src/java/org/apache/fop/render/awt/AWTRenderer.java index b790d43a1..5287bd15d 100644 --- a/src/java/org/apache/fop/render/awt/AWTRenderer.java +++ b/src/java/org/apache/fop/render/awt/AWTRenderer.java @@ -44,6 +44,7 @@ import org.apache.fop.area.PageViewport; import org.apache.fop.datatypes.ColorType; import org.apache.fop.fo.properties.ColorTypeProperty; import org.apache.fop.render.awt.viewer.PreviewDialog; +import org.apache.fop.render.awt.viewer.Renderable; import org.apache.fop.render.awt.viewer.Translator; import org.apache.fop.render.java2d.Java2DRenderer; @@ -72,6 +73,12 @@ public class AWTRenderer extends Java2DRenderer implements Pageable { */ protected PreviewDialog frame; + /** + * Renderable instance that can be used to reload and re-render a document after + * modifications. + */ + protected Renderable renderable; + /** * Creates a new AWTRenderer instance. */ @@ -87,6 +94,15 @@ public class AWTRenderer extends Java2DRenderer implements Pageable { } } + /** + * A Renderable instance can be set so the Preview Dialog can enable the "Reload" button + * which causes the current document to be reprocessed and redisplayed. + * @param renderable the Renderable instance. + */ + public void setRenderable(Renderable renderable) { + this.renderable = renderable; + } + /** * Sets whether the preview dialog should be created and displayed when * the rendering is finished. @@ -135,7 +151,7 @@ public class AWTRenderer extends Java2DRenderer implements Pageable { /** Creates and initialize the AWT Viewer main window */ private PreviewDialog createPreviewDialog() { - frame = new PreviewDialog(userAgent); + frame = new PreviewDialog(userAgent, this.renderable); frame.addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent we) { System.exit(0); diff --git a/src/java/org/apache/fop/render/awt/viewer/PreviewDialog.java b/src/java/org/apache/fop/render/awt/viewer/PreviewDialog.java index 6992689bf..5c0b29faf 100644 --- a/src/java/org/apache/fop/render/awt/viewer/PreviewDialog.java +++ b/src/java/org/apache/fop/render/awt/viewer/PreviewDialog.java @@ -68,6 +68,11 @@ public class PreviewDialog extends JFrame { protected AWTRenderer renderer; /** The FOUserAgent associated with this window */ protected FOUserAgent foUserAgent; + /** + * Renderable instance that can be used to reload and re-render a document after + * modifications. + */ + protected Renderable renderable; /** The JCombobox to rescale the rendered page view */ private JComboBox scale; @@ -87,10 +92,13 @@ public class PreviewDialog extends JFrame { /** * Creates a new PreviewDialog that uses the given renderer. * @param foUserAgent the user agent + * @param renderable the Renderable instance that is used to reload/re-render a document + * after modifications. */ - public PreviewDialog(FOUserAgent foUserAgent) { + public PreviewDialog(FOUserAgent foUserAgent, Renderable renderable) { renderer = (AWTRenderer) foUserAgent.getRendererOverride(); this.foUserAgent = foUserAgent; + this.renderable = renderable; translator = renderer.getTranslator(); //Commands aka Actions @@ -156,7 +164,7 @@ public class PreviewDialog extends JFrame { setSize(screen.width * 61 / 100, screen.height * 9 / 10); //Page view stuff - previewPanel = new PreviewPanel(foUserAgent, renderer); + previewPanel = new PreviewPanel(foUserAgent, renderable, renderer); getContentPane().add(previewPanel, BorderLayout.CENTER); //Scaling combobox @@ -231,6 +239,14 @@ public class PreviewDialog extends JFrame { getContentPane().add(statusBar, BorderLayout.SOUTH); } + /** + * Creates a new PreviewDialog that uses the given renderer. + * @param foUserAgent the user agent + */ + public PreviewDialog(FOUserAgent foUserAgent) { + this(foUserAgent, null); + } + /** * Creates a new menubar to be shown in this window. * @return the newly created menubar @@ -246,7 +262,7 @@ public class PreviewDialog extends JFrame { } }); // inputHandler must be set to allow reloading - if (foUserAgent.getInputHandler() != null) { + if (renderable != null) { menu.add(new Command(translator.getString("Menu.Reload"), KeyEvent.VK_R) { public void doit() { reload(); diff --git a/src/java/org/apache/fop/render/awt/viewer/PreviewPanel.java b/src/java/org/apache/fop/render/awt/viewer/PreviewPanel.java index b568ddeb2..18436ac53 100644 --- a/src/java/org/apache/fop/render/awt/viewer/PreviewPanel.java +++ b/src/java/org/apache/fop/render/awt/viewer/PreviewPanel.java @@ -13,6 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +/* $Id$ */ + package org.apache.fop.render.awt.viewer; import java.awt.Color; @@ -100,6 +103,11 @@ public class PreviewPanel extends JPanel { /** The FOUserAgent associated with this panel - often shared with PreviewDialog */ protected FOUserAgent foUserAgent; + /** + * Renderable instance that can be used to reload and re-render a document after + * modifications. + */ + protected Renderable renderable; /** The number of the page which is currently selected */ private int currentPage = 0; @@ -136,10 +144,13 @@ public class PreviewPanel extends JPanel { /** * Creates a new PreviewPanel instance. * @param foUserAgent the user agent + * @param renderable the Renderable instance that is used to reload/re-render a document + * after modifications. * @param renderer the AWT Renderer instance to paint with */ - public PreviewPanel(FOUserAgent foUserAgent, AWTRenderer renderer) { + public PreviewPanel(FOUserAgent foUserAgent, Renderable renderable, AWTRenderer renderer) { super(new GridLayout(1, 1)); + this.renderable = renderable; this.renderer = renderer; this.foUserAgent = foUserAgent; @@ -284,7 +295,7 @@ public class PreviewPanel extends JPanel { // do not allow the reloading while FOP is still rendering JOptionPane.showMessageDialog(previewArea, "Cannot perform the requested operation until " - + "all page are rendererd. Please wait", + + "all page are rendered. Please wait", "Please wait ", 1 /* INFORMATION_MESSAGE */); return; } @@ -325,9 +336,9 @@ public class PreviewPanel extends JPanel { } try { - if (foUserAgent.getInputHandler() != null) { + if (renderable != null) { renderer.clearViewportList(); - foUserAgent.getInputHandler().render(fop); + renderable.render(fop); } } catch (FOPException e) { e.printStackTrace(); diff --git a/src/java/org/apache/fop/render/awt/viewer/Renderable.java b/src/java/org/apache/fop/render/awt/viewer/Renderable.java new file mode 100644 index 000000000..dc25243ee --- /dev/null +++ b/src/java/org/apache/fop/render/awt/viewer/Renderable.java @@ -0,0 +1,36 @@ +/* + * Copyright 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. + * 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 org.apache.fop.render.awt.viewer; + +import org.apache.fop.apps.FOPException; +import org.apache.fop.apps.Fop; + +/** + * The interface is used by the AWT preview dialog to reload a document. + */ +public interface Renderable { + + /** + * Renders the pre-setup document. + * @param fop the Fop instance to do the FO processing with + * @exception FOPException if the FO processing fails + */ + void render(Fop fop) throws FOPException; + +} diff --git a/src/java/org/apache/fop/tools/TestConverter.java b/src/java/org/apache/fop/tools/TestConverter.java index 601c14944..04bcac275 100644 --- a/src/java/org/apache/fop/tools/TestConverter.java +++ b/src/java/org/apache/fop/tools/TestConverter.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. @@ -27,7 +27,7 @@ import javax.xml.parsers.DocumentBuilderFactory; import org.apache.fop.apps.Fop; import org.apache.fop.apps.FOUserAgent; -import org.apache.fop.apps.InputHandler; +import org.apache.fop.cli.InputHandler; import org.apache.fop.tools.anttasks.FileCompare; import org.w3c.dom.Document; import org.w3c.dom.Node; diff --git a/src/java/org/apache/fop/tools/anttasks/Fop.java b/src/java/org/apache/fop/tools/anttasks/Fop.java index a26febb40..49d64c961 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-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. @@ -34,10 +34,10 @@ import java.net.MalformedURLException; import java.util.List; // FOP -import org.apache.fop.apps.InputHandler; import org.apache.fop.fo.Constants; import org.apache.fop.apps.FOPException; import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.cli.InputHandler; import org.apache.commons.logging.impl.SimpleLog; import org.apache.commons.logging.Log; -- cgit v1.2.3