diff options
author | Jeremias Maerki <jeremias@apache.org> | 2008-04-17 15:12:23 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2008-04-17 15:12:23 +0000 |
commit | 5772532f675b18349b6c9262b1e5d8d645729fe6 (patch) | |
tree | 786bba4fc9a79bb868a808c56eb9417e43b17128 /src/java/org/apache/fop | |
parent | 2110cefe609d1ec729fb271e970809d2a84e5af4 (diff) | |
download | xmlgraphics-fop-5772532f675b18349b6c9262b1e5d8d645729fe6.tar.gz xmlgraphics-fop-5772532f675b18349b6c9262b1e5d8d645729fe6.zip |
Bugzilla #41687:
Restored ability to specify from/to and odd/even pages as well as the number of copies for printing from the command-line. Note that this is no longer done via system properties but through the renderer options. Syntax is slightly different. See "fop -print help".
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@649146 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop')
5 files changed, 203 insertions, 31 deletions
diff --git a/src/java/org/apache/fop/cli/CommandLineOptions.java b/src/java/org/apache/fop/cli/CommandLineOptions.java index 3bb0aae7f..4e5c8ae44 100644 --- a/src/java/org/apache/fop/cli/CommandLineOptions.java +++ b/src/java/org/apache/fop/cli/CommandLineOptions.java @@ -46,6 +46,8 @@ import org.apache.fop.pdf.PDFXMode; import org.apache.fop.render.Renderer; import org.apache.fop.render.awt.AWTRenderer; import org.apache.fop.render.pdf.PDFRenderer; +import org.apache.fop.render.print.PagesMode; +import org.apache.fop.render.print.PrintRenderer; import org.apache.fop.render.xml.XMLRenderer; import org.apache.fop.util.CommandLineLogger; @@ -133,8 +135,9 @@ public class CommandLineOptions { * @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 + * @return true if the processing can continue, false to abort */ - public void parse(String[] args) + public boolean parse(String[] args) throws FOPException, IOException { boolean optionsParsed = true; @@ -155,6 +158,8 @@ public class CommandLineOptions { } addXSLTParameter("fop-output-format", getOutputFormat()); addXSLTParameter("fop-version", Version.getVersion()); + } else { + return false; } } catch (FOPException e) { printUsage(); @@ -193,6 +198,7 @@ public class CommandLineOptions { //Make sure the prepared XMLRenderer is used foUserAgent.setRendererOverride(xmlRenderer); } + return true; } /** @@ -268,7 +274,6 @@ public class CommandLineOptions { } 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")) { @@ -276,6 +281,9 @@ public class CommandLineOptions { return false; } } + i = i + parsePrintOutputOption(args, i); + } else if (args[i].equals("-copies")) { + i = i + parseCopiesOption(args, i); } else if (args[i].equals("-pcl")) { i = i + parsePCLOutputOption(args, i); } else if (args[i].equals("-ps")) { @@ -302,7 +310,7 @@ public class CommandLineOptions { String expression = args[++i]; addXSLTParameter(name, expression); } else { - throw new FOPException("invalid param usage: use -param <name> <value>"); + throw new FOPException("invalid param usage: use -param <name> <value>"); } } else if (args[i].equals("-o")) { i = i + parsePDFOwnerPassword(args, i); @@ -461,7 +469,37 @@ public class CommandLineOptions { private int parsePrintOutputOption(String[] args, int i) throws FOPException { setOutputMode(MimeConstants.MIME_FOP_PRINT); - return 0; + if ((i + 1 <= args.length) + && (args[i + 1].charAt(0) != '-')) { + String arg = args[i + 1]; + String[] parts = arg.split(","); + for (int j = 0; j < parts.length; j++) { + String s = parts[j]; + if (s.matches("\\d+")) { + renderingOptions.put(PrintRenderer.START_PAGE, new Integer(s)); + } else if (s.matches("\\d+-\\d+")) { + String[] startend = s.split("-"); + renderingOptions.put(PrintRenderer.START_PAGE, new Integer(startend[0])); + renderingOptions.put(PrintRenderer.END_PAGE, new Integer(startend[1])); + } else { + PagesMode mode = PagesMode.byName(s); + renderingOptions.put(PrintRenderer.PAGES_MODE, mode); + } + } + return 1; + } else { + return 0; + } + } + + private int parseCopiesOption(String[] args, int i) throws FOPException { + if ((i + 1 == args.length) + || (args[i + 1].charAt(0) == '-')) { + throw new FOPException("you must specify the number of copies"); + } else { + renderingOptions.put(PrintRenderer.COPIES, new Integer(args[i + 1])); + return 1; + } } private int parsePCLOutputOption(String[] args, int i) throws FOPException { @@ -991,18 +1029,21 @@ public class CommandLineOptions { + " 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" - + " Fop foo.fo -awt \n"); + + " Fop foo.fo -print\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 "); + System.err.println("USAGE: -print [from[-to][,even|odd]] [-copies numCopies]\n\n" + + "Example:\n" + + "all pages: Fop infile.fo -print\n" + + "all pages with two copies: Fop infile.fo -print -copies 2\n" + + "all pages starting with page 7: Fop infile.fo -print 7\n" + + "pages 2 to 3: Fop infile.fo -print 2-3\n" + + "only even page between 10 and 20: Fop infile.fo -print 10-20,even\n"); } /** diff --git a/src/java/org/apache/fop/cli/Main.java b/src/java/org/apache/fop/cli/Main.java index e2297380d..43da8d966 100644 --- a/src/java/org/apache/fop/cli/Main.java +++ b/src/java/org/apache/fop/cli/Main.java @@ -28,6 +28,7 @@ import java.net.URL; import java.util.List; import org.apache.commons.io.IOUtils; + import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.MimeConstants; @@ -151,7 +152,9 @@ public class Main { try { options = new CommandLineOptions(); - options.parse(args); + if (!options.parse(args)) { + System.exit(1); + } foUserAgent = options.getFOUserAgent(); String outputFormat = options.getOutputFormat(); diff --git a/src/java/org/apache/fop/render/java2d/Java2DRenderer.java b/src/java/org/apache/fop/render/java2d/Java2DRenderer.java index 0ffe3307a..1f3194949 100644 --- a/src/java/org/apache/fop/render/java2d/Java2DRenderer.java +++ b/src/java/org/apache/fop/render/java2d/Java2DRenderer.java @@ -121,9 +121,6 @@ public abstract class Java2DRenderer extends AbstractPathOrientedRenderer implem /** The 0-based current page number */ private int currentPageNumber = 0; - /** The 0-based total number of rendered pages */ - private int numberOfPages; - /** true if antialiasing is set */ protected boolean antialiasing = true; @@ -208,7 +205,7 @@ public abstract class Java2DRenderer extends AbstractPathOrientedRenderer implem public void stopRenderer() throws IOException { log.debug("Java2DRenderer stopped"); renderingDone = true; - numberOfPages = currentPageNumber; + int numberOfPages = currentPageNumber; // TODO set all vars to null for gc if (numberOfPages == 0) { new FOPException("No page could be rendered"); @@ -238,7 +235,7 @@ public abstract class Java2DRenderer extends AbstractPathOrientedRenderer implem * @return The 0-based total number of rendered pages */ public int getNumberOfPages() { - return numberOfPages; + return pageViewportList.size(); } /** diff --git a/src/java/org/apache/fop/render/print/PagesMode.java b/src/java/org/apache/fop/render/print/PagesMode.java new file mode 100644 index 000000000..6879aa68d --- /dev/null +++ b/src/java/org/apache/fop/render/print/PagesMode.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.print; + +/** Enum class for pages mode (all, even, odd pages). */ +public final class PagesMode { + + /** the all pages mode */ + public static final PagesMode ALL = new PagesMode("all"); + /** the even pages mode */ + public static final PagesMode EVEN = new PagesMode("even"); + /** the odd pages mode */ + public static final PagesMode ODD = new PagesMode("odd"); + + private String name; + + /** + * Constructor to add a new named item. + * @param name Name of the item. + */ + private PagesMode(String name) { + this.name = name; + } + + /** @return the name of the enum */ + public String getName() { + return this.name; + } + + /** + * Returns a PagesMode instance by name. + * @param name the name of the pages mode + * @return the pages mode + */ + public static PagesMode byName(String name) { + if (PagesMode.ALL.getName().equalsIgnoreCase(name)) { + return PagesMode.ALL; + } else if (PagesMode.EVEN.getName().equalsIgnoreCase(name)) { + return PagesMode.EVEN; + } else if (PagesMode.ODD.getName().equalsIgnoreCase(name)) { + return PagesMode.ODD; + } else { + throw new IllegalArgumentException("Invalid value for PagesMode: " + name); + } + } + + /** {@inheritDoc} */ + public String toString() { + return "PagesMode:" + name; + } + +} diff --git a/src/java/org/apache/fop/render/print/PrintRenderer.java b/src/java/org/apache/fop/render/print/PrintRenderer.java index b70bb21aa..2774b5373 100644 --- a/src/java/org/apache/fop/render/print/PrintRenderer.java +++ b/src/java/org/apache/fop/render/print/PrintRenderer.java @@ -48,18 +48,35 @@ public class PrintRenderer extends Java2DRenderer implements Pageable { */ public static final String PRINTER_JOB = "printerjob"; - - private static final int EVEN_AND_ALL = 0; - - private static final int EVEN = 1; + /** + * Printing parameter: the pages to be printed (all, even or odd), + * datatype: the strings "all", "even" or "odd" or one of PagesMode.* + */ + public static final String PAGES_MODE = "even-odd"; - private static final int ODD = 2; + /** + * Printing parameter: the page number (1-based) of the first page to be printed, + * datatype: a positive Integer + */ + public static final String START_PAGE = "start-page"; + /** + * Printing parameter: the page number (1-based) of the last page to be printed, + * datatype: a positive Integer + */ + public static final String END_PAGE = "end-page"; + + /** + * Printing parameter: the number of copies of the document to be printed, + * datatype: a positive Integer + */ + public static final String COPIES = "copies"; + + private int startNumber = 0; - private int endNumber = -1; - private int mode = EVEN_AND_ALL; + private PagesMode mode = PagesMode.ALL; private int copies = 1; @@ -104,11 +121,11 @@ public class PrintRenderer extends Java2DRenderer implements Pageable { //TODO Remove me! This is not a beautiful way to do this. // read from command-line options copies = getIntProperty("copies", 1); - startNumber = getIntProperty("start", 1) - 1; + startNumber = getIntProperty("start", 1); endNumber = getIntProperty("end", -1); String str = System.getProperty("even"); if (str != null) { - mode = Boolean.valueOf(str).booleanValue() ? EVEN : ODD; + mode = Boolean.valueOf(str).booleanValue() ? PagesMode.EVEN : PagesMode.ODD; } } @@ -129,9 +146,53 @@ public class PrintRenderer extends Java2DRenderer implements Pageable { printerJob = (PrinterJob)printerJobO; printerJob.setPageable(this); } + Object o = rendererOptions.get(PrintRenderer.PAGES_MODE); + if (o != null) { + if (o instanceof PagesMode) { + this.mode = (PagesMode)o; + } else if (o instanceof String) { + this.mode = PagesMode.byName((String)o); + } else { + throw new IllegalArgumentException( + "Renderer option " + PrintRenderer.PAGES_MODE + + " must be an 'all', 'even', 'odd' or a PagesMode instance."); + } + } + + o = rendererOptions.get(PrintRenderer.START_PAGE); + if (o != null) { + this.startNumber = getPositiveInteger(o); + } + o = rendererOptions.get(PrintRenderer.END_PAGE); + if (o != null) { + this.endNumber = getPositiveInteger(o); + } + if (this.endNumber >= 0 && this.endNumber < this.endNumber) { + this.endNumber = this.startNumber; + } + o = rendererOptions.get(PrintRenderer.COPIES); + if (o != null) { + this.copies = getPositiveInteger(o); + } initializePrinterJob(); } + private int getPositiveInteger(Object o) { + if (o instanceof Integer) { + Integer i = (Integer)o; + if (i.intValue() < 1) { + throw new IllegalArgumentException( + "Value must be a positive Integer"); + } + return i.intValue(); + } else if (o instanceof String) { + return Integer.parseInt((String)o); + } else { + throw new IllegalArgumentException( + "Value must be a positive integer"); + } + } + /** @return the PrinterJob instance that this renderer prints to */ public PrinterJob getPrinterJob() { return this.printerJob; @@ -174,7 +235,8 @@ public class PrintRenderer extends Java2DRenderer implements Pageable { Vector numbers = getInvalidPageNumbers(); for (int i = numbers.size() - 1; i > -1; i--) { - // removePage(Integer.parseInt((String)numbers.elementAt(i))); + int page = ((Integer)numbers.elementAt(i)).intValue(); + pageViewportList.remove(page - 1); } try { @@ -204,20 +266,20 @@ public class PrintRenderer extends Java2DRenderer implements Pageable { Vector vec = new Vector(); int max = getNumberOfPages(); boolean isValid; - for (int i = 0; i < max; i++) { + for (int i = 1; i <= max; i++) { isValid = true; if (i < startNumber || i > endNumber) { isValid = false; - } else if (mode != EVEN_AND_ALL) { - if (mode == EVEN && ((i + 1) % 2 != 0)) { + } else if (mode != PagesMode.ALL) { + if (mode == PagesMode.EVEN && (i % 2 != 0)) { isValid = false; - } else if (mode == ODD && ((i + 1) % 2 != 1)) { + } else if (mode == PagesMode.ODD && (i % 2 == 0)) { isValid = false; } } if (!isValid) { - vec.add(Integer.toString(i)); + vec.add(new Integer(i)); } } return vec; |