You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2020/12/07 18:21:28 UTC
svn commit: r1884187 - in
/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools: ImportFDF.java
ImportXFDF.java
Author: msahyoun
Date: Mon Dec 7 18:21:28 2020
New Revision: 1884187
URL: http://svn.apache.org/viewvc?rev=1884187&view=rev
Log:
PDFBOX-2602: use picocli for command line parsing
Modified:
pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportFDF.java
pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportXFDF.java
Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportFDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportFDF.java?rev=1884187&r1=1884186&r2=1884187&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportFDF.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportFDF.java Mon Dec 7 18:21:28 2020
@@ -18,6 +18,8 @@ package org.apache.pdfbox.tools;
import java.io.File;
import java.io.IOException;
+import java.io.PrintStream;
+import java.util.concurrent.Callable;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.Loader;
@@ -25,21 +27,31 @@ import org.apache.pdfbox.pdmodel.PDDocum
import org.apache.pdfbox.pdmodel.fdf.FDFDocument;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
+import picocli.CommandLine;
+import picocli.CommandLine.Command;
+import picocli.CommandLine.Parameters;
+
/**
* This example will take a PDF document and fill the fields with data from the
* FDF fields.
*
* @author Ben Litchfield
*/
-public class ImportFDF
+@Command(name = "ImportFDF", description = "Import AcroForm form data from FDF.")
+public class ImportFDF implements Callable<Integer>
{
- /**
- * Creates a new instance of ImportFDF.
- */
- public ImportFDF()
- {
- }
+ // Expected for CLI app to write to System.out/Sytem.err
+ @SuppressWarnings("squid:S106")
+ private static final PrintStream SYSERR = System.err;
+
+ @Parameters(paramLabel = "pdffile", index = "0", arity = "1", description = "the PDF file to import to.")
+ private File infile;
+ @Parameters(paramLabel = "fdffile", index = "1", arity = "1", description = "the FDF data file to import from.")
+ private File fdffile;
+
+ @Parameters(paramLabel = "outputfile", index = "2", arity = "0..1", description = "the PDF file to save to. If omitted the orginal PDF will be used.")
+ private File outfile;
/**
* This will takes the values from the fdf document and import them into the
* PDF document.
@@ -70,44 +82,37 @@ public class ImportFDF
* see usage() for commandline
*
* @param args command line arguments
- *
- * @throws IOException If there is an error importing the FDF document.
*/
- public static void main(String[] args) throws IOException
+ public static void main(String[] args)
{
// suppress the Dock icon on OS X
System.setProperty("apple.awt.UIElement", "true");
- ImportFDF importer = new ImportFDF();
- importer.importFDF( args );
+ int exitCode = new CommandLine(new ImportFDF()).execute(args);
+ System.exit(exitCode);
}
- private void importFDF( String[] args ) throws IOException
+ public Integer call()
{
- if( args.length != 3 )
- {
- usage();
- }
- else
+ ImportFDF importer = new ImportFDF();
+
+ try (PDDocument pdf = Loader.loadPDF(infile);
+ FDFDocument fdf = Loader.loadFDF(fdffile))
{
- ImportFDF importer = new ImportFDF();
+ importer.importFDF( pdf, fdf );
- try (PDDocument pdf = Loader.loadPDF(new File(args[0]));
- FDFDocument fdf = Loader.loadFDF(args[1]))
+ if (outfile == null)
{
- importer.importFDF( pdf, fdf );
-
- pdf.save( args[2] );
+ outfile = infile;
}
- }
- }
- /**
- * This will print out a message telling how to use this example.
- */
- private static void usage()
- {
- System.err.println( "usage: org.apache.pdfbox.tools.ImportFDF <pdf-file> <fdf-file> <output-file>" );
- System.exit(1);
+ pdf.save(outfile);
+ }
+ catch (IOException ioe)
+ {
+ SYSERR.println( "Error importing FDF data: " + ioe.getMessage());
+ return 4;
+ }
+ return 0;
}
}
Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportXFDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportXFDF.java?rev=1884187&r1=1884186&r2=1884187&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportXFDF.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportXFDF.java Mon Dec 7 18:21:28 2020
@@ -22,8 +22,13 @@ import org.apache.pdfbox.pdmodel.PDDocum
import org.apache.pdfbox.pdmodel.fdf.FDFDocument;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
+import picocli.CommandLine;
+import picocli.CommandLine.Command;
+import picocli.CommandLine.Parameters;
+
import java.io.File;
import java.io.IOException;
+import java.io.PrintStream;
/**
@@ -32,14 +37,21 @@ import java.io.IOException;
*
* @author Ben Litchfield
*/
+@Command(name = "ImportFDF", description = "Import AcroForm form data from XFDF.")
public class ImportXFDF
{
- /**
- * Creates a new instance of ImportFDF.
- */
- public ImportXFDF()
- {
- }
+ // Expected for CLI app to write to System.out/Sytem.err
+ @SuppressWarnings("squid:S106")
+ private static final PrintStream SYSERR = System.err;
+
+ @Parameters(paramLabel = "pdffile", index = "0", arity = "1", description = "the PDF file to import to.")
+ private File infile;
+
+ @Parameters(paramLabel = "xfdffile", index = "1", arity = "1", description = "the XFDF data file to import from.")
+ private File xfdffile;
+
+ @Parameters(paramLabel = "outputfile", index = "2", arity = "0..1", description = "the PDF file to save to. If omitted the orginal PDF will be used.")
+ private File outfile;
/**
* This will takes the values from the fdf document and import them into the
@@ -64,42 +76,36 @@ public class ImportXFDF
* see usage() for commandline
*
* @param args command line arguments
- *
- * @throws IOException If there is an error importing the FDF document.
*/
- public static void main(String[] args) throws IOException
+ public static void main(String[] args)
{
// suppress the Dock icon on OS X
System.setProperty("apple.awt.UIElement", "true");
- ImportXFDF importer = new ImportXFDF();
- importer.importXFDF( args );
+ int exitCode = new CommandLine(new ImportXFDF()).execute(args);
+ System.exit(exitCode);
}
- private void importXFDF( String[] args ) throws IOException
+ public Integer call()
{
- if( args.length != 3 )
- {
- usage();
- }
- else
+ ImportFDF importer = new ImportFDF();
+ try (PDDocument pdf = Loader.loadPDF(infile);
+ FDFDocument fdf = Loader.loadXFDF(xfdffile))
{
- ImportFDF importer = new ImportFDF();
- try (PDDocument pdf = Loader.loadPDF(new File(args[0]));
- FDFDocument fdf = Loader.loadXFDF(args[1]))
+ importer.importFDF( pdf, fdf );
+
+ if (outfile == null)
{
- importer.importFDF( pdf, fdf );
- pdf.save( args[2] );
+ outfile = infile;
}
- }
- }
- /**
- * This will print out a message telling how to use this example.
- */
- private static void usage()
- {
- System.err.println( "usage: org.apache.pdfbox.tools.ImportXFDF <pdf-file> <fdf-file> <output-file>" );
- System.exit(1);
+ pdf.save(outfile);
+ }
+ catch (IOException ioe)
+ {
+ SYSERR.println( "Error importing XFDF data: " + ioe.getMessage());
+ return 4;
+ }
+ return 0;
}
}