Hi Jeremias,

why don't you use the new processing feedback mechanism for the
notifications we can find in the code below?

Vincent


> Author: jeremias
> Date: Thu Apr 17 08:12:23 2008
> New Revision: 649146
>
> URL: http://svn.apache.org/viewvc?rev=649146&view=rev
> Log:
> 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".
>
> Added:
>     xmlgraphics/fop/trunk/src/java/org/apache/fop/render/print/PagesMode.java 
>   (with props)
> Modified:
>     xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java
>     xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/Main.java
>     
> xmlgraphics/fop/trunk/src/java/org/apache/fop/render/java2d/Java2DRenderer.java
>     
> xmlgraphics/fop/trunk/src/java/org/apache/fop/render/print/PrintRenderer.java
>     xmlgraphics/fop/trunk/status.xml
>
> Modified: 
> xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java
> URL: 
> http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java?rev=649146&r1=649145&r2=649146&view=diff
> ==============================================================================
> --- xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java 
> (original)
> +++ xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java 
> Thu Apr 17 08:12:23 2008
> @@ -46,6 +46,8 @@
>  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 @@
>       * @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 @@
>                  }
>                  addXSLTParameter("fop-output-format", getOutputFormat());
>                  addXSLTParameter("fop-version", Version.getVersion());
> +            } else {
> +                return false;
>              }
>          } catch (FOPException e) {
>              printUsage();
> @@ -193,6 +198,7 @@
>              //Make sure the prepared XMLRenderer is used
>              foUserAgent.setRendererOverride(xmlRenderer);
>          }
> +        return true;
>      }
>
>      /**
> @@ -268,7 +274,6 @@
>              } 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 @@
>                          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 @@
>                        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 @@
>
>      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 @@
>              + "  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");
>      }
>
>      /**
>
> Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/Main.java
> URL: 
> http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/Main.java?rev=649146&r1=649145&r2=649146&view=diff
> ==============================================================================
> --- xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/Main.java (original)
> +++ xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/Main.java Thu Apr 17 
> 08:12:23 2008
> @@ -28,6 +28,7 @@
>  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 @@
>
>          try {
>              options = new CommandLineOptions();
> -            options.parse(args);
> +            if (!options.parse(args)) {
> +                System.exit(1);
> +            }
>
>              foUserAgent = options.getFOUserAgent();
>              String outputFormat = options.getOutputFormat();
>
> Modified: 
> xmlgraphics/fop/trunk/src/java/org/apache/fop/render/java2d/Java2DRenderer.java
> URL: 
> http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/render/java2d/Java2DRenderer.java?rev=649146&r1=649145&r2=649146&view=diff
> ==============================================================================
> --- 
> xmlgraphics/fop/trunk/src/java/org/apache/fop/render/java2d/Java2DRenderer.java
>  (original)
> +++ 
> xmlgraphics/fop/trunk/src/java/org/apache/fop/render/java2d/Java2DRenderer.java
>  Thu Apr 17 08:12:23 2008
> @@ -121,9 +121,6 @@
>      /** 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 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 @@
>       * @return The 0-based total number of rendered pages
>       */
>      public int getNumberOfPages() {
> -            return numberOfPages;
> +        return pageViewportList.size();
>      }
>
>      /**
>
> Added: 
> xmlgraphics/fop/trunk/src/java/org/apache/fop/render/print/PagesMode.java
> URL: 
> http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/render/print/PagesMode.java?rev=649146&view=auto
> ==============================================================================
> --- xmlgraphics/fop/trunk/src/java/org/apache/fop/render/print/PagesMode.java 
> (added)
> +++ xmlgraphics/fop/trunk/src/java/org/apache/fop/render/print/PagesMode.java 
> Thu Apr 17 08:12:23 2008
> @@ -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);
> +        }
> +    }
> +
> +    /** [EMAIL PROTECTED] */
> +    public String toString() {
> +        return "PagesMode:" + name;
> +    }
> +
> +}
>
> Propchange: 
> xmlgraphics/fop/trunk/src/java/org/apache/fop/render/print/PagesMode.java
> ------------------------------------------------------------------------------
>     svn:eol-style = native
>
> Propchange: 
> xmlgraphics/fop/trunk/src/java/org/apache/fop/render/print/PagesMode.java
> ------------------------------------------------------------------------------
>     svn:keywords = Id
>
> Modified: 
> xmlgraphics/fop/trunk/src/java/org/apache/fop/render/print/PrintRenderer.java
> URL: 
> http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/render/print/PrintRenderer.java?rev=649146&r1=649145&r2=649146&view=diff
> ==============================================================================
> --- 
> xmlgraphics/fop/trunk/src/java/org/apache/fop/render/print/PrintRenderer.java 
> (original)
> +++ 
> xmlgraphics/fop/trunk/src/java/org/apache/fop/render/print/PrintRenderer.java 
> Thu Apr 17 08:12:23 2008
> @@ -48,18 +48,35 @@
>       */
>      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 @@
>          //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 @@
>              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 @@
>
>          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 @@
>          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;
>
> Modified: xmlgraphics/fop/trunk/status.xml
> URL: 
> http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/status.xml?rev=649146&r1=649145&r2=649146&view=diff
> ==============================================================================
> --- xmlgraphics/fop/trunk/status.xml (original)
> +++ xmlgraphics/fop/trunk/status.xml Thu Apr 17 08:12:23 2008
> @@ -58,6 +58,10 @@
>          Added SVG support for AFP (GOCA).
>          </action>
>        -->
> +      <action context="Renderers" dev="JM" type="add" fixes-bug="41687">
> +        Restored ability to specify from/to and odd/even pages as well as 
> the number of copies
> +        for printing from the command-line.
> +      </action>
>        <action context="Renderers" dev="JM" type="add" fixes-bug="44678" 
> due-to="Antti Karanta">
>          Added ability to pass a preconfigured PrinterJob instance to the 
> PrintRenderer via
>          the rendering options map.

-- 
Vincent Hennebert                            Anyware Technologies
http://people.apache.org/~vhennebert         http://www.anyware-tech.com
Apache FOP Committer                         FOP Development/Consulting

Reply via email to