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/04 07:08:13 UTC
svn commit: r1884085 - in /pdfbox/trunk/tools: pom.xml
src/main/java/org/apache/pdfbox/tools/Decrypt.java
Author: msahyoun
Date: Fri Dec 4 07:08:13 2020
New Revision: 1884085
URL: http://svn.apache.org/viewvc?rev=1884085&view=rev
Log:
PDFBOX-2602: initial move to Picocli for command line parsing
Modified:
pdfbox/trunk/tools/pom.xml
pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/Decrypt.java
Modified: pdfbox/trunk/tools/pom.xml
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/pom.xml?rev=1884085&r1=1884084&r2=1884085&view=diff
==============================================================================
--- pdfbox/trunk/tools/pom.xml (original)
+++ pdfbox/trunk/tools/pom.xml Fri Dec 4 07:08:13 2020
@@ -49,9 +49,9 @@
<version>${project.version}</version>
</dependency>
<dependency>
- <groupId>commons-cli</groupId>
- <artifactId>commons-cli</artifactId>
- <version>1.4</version>
+ <groupId>info.picocli</groupId>
+ <artifactId>picocli</artifactId>
+ <version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/Decrypt.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/Decrypt.java?rev=1884085&r1=1884084&r2=1884085&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/Decrypt.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/Decrypt.java Fri Dec 4 07:08:13 2020
@@ -20,39 +20,45 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.util.List;
+import java.util.concurrent.Callable;
-import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.CommandLineParser;
-import org.apache.commons.cli.DefaultParser;
-import org.apache.commons.cli.HelpFormatter;
-import org.apache.commons.cli.Option;
-import org.apache.commons.cli.Options;
-import org.apache.commons.cli.ParseException;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
+import picocli.CommandLine;
+import picocli.CommandLine.Command;
+import picocli.CommandLine.Option;
+import picocli.CommandLine.Parameters;
+
/**
* This will read a document from the filesystem, decrypt it and and then write
* the result to the filesystem.
*
* @author Ben Litchfield
*/
-public final class Decrypt
+@Command(name = "Decrypt", description = "Decrypts a PDF file.")
+public final class Decrypt implements Callable<Integer>
{
- private static final String ALIAS = "alias";
- @SuppressWarnings({"squid:S2068"})
- private static final String PASSWORD = "password";
- private static final String KEYSTORE = "keyStore";
-
- private String password;
- private String infile;
- private String outfile;
+ @Option(names = "-alias", description = "the alias to the certificate in the keystore.")
private String alias;
+
+ @Option(names = {"-h", "--help"}, usageHelp = true, description = "display this help message")
+ boolean usageHelpRequested;
+
+ @Option(names = "-keyStore", description = "the path to the keystore that holds the certificate to decrypt the document. " +
+ "This is only required if the document is encrypted with a certificate, otherwise only the password is required.")
private String keyStore;
+ @Option(names = "-password", description = "the password for the PDF or certificate in keystore.")
+ private String password;
+
+ @Parameters(paramLabel = "inputfile", index = "0", arity = "1", description = "the PDF file to decrypt.")
+ private File infile;
+
+ @Parameters(paramLabel = "outputfile", index = "1", arity = "0..1", description = "the decrypted PDF file.")
+ private File outfile;
private Decrypt()
{
@@ -68,81 +74,13 @@ public final class Decrypt
{
// suppress the Dock icon on OS X
System.setProperty("apple.awt.UIElement", "true");
-
- Decrypt decrypt = new Decrypt();
- decrypt.parseCommandLine(args);
- decrypt.decrypt();
- }
-
- private void parseCommandLine(String[] args)
- {
- Options options = defineOptions();
- CommandLine commandLine = parseArguments(options, args);
-
- this.alias = commandLine.getOptionValue(ALIAS);
- this.password = commandLine.getOptionValue(PASSWORD, "");
- this.keyStore = commandLine.getOptionValue(KEYSTORE);
-
- // get the additional command line parameters
- // and handle these as the file names being passed
- List<String> fileNames = commandLine.getArgList();
- if (fileNames.isEmpty() || fileNames.size() > 2 )
- {
- usage(options);
- }
-
- this.infile = fileNames.get(0);
-
- if (fileNames.size() == 1)
- {
- this.outfile = fileNames.get(0);
- }
- else
- {
- this.outfile = fileNames.get(1);
- }
-
- }
-
- private static Options defineOptions()
- {
- Options options = new Options();
-
- options.addOption(Option.builder(ALIAS)
- .hasArg()
- .desc("The alias of the key in the certificate file (mandatory if several keys are available).")
- .build()
- );
- options.addOption(Option.builder(PASSWORD)
- .hasArg()
- .desc("The password to open the certificate and extract the private key from it.")
- .build()
- );
- options.addOption(Option.builder(KEYSTORE)
- .hasArg()
- .desc("The KeyStore that holds the certificate.")
- .build()
- );
- return options;
- }
-
- private static CommandLine parseArguments(Options options, String[] commandLineArguments)
- {
- CommandLineParser cmdLineParser = new DefaultParser();
- CommandLine commandLine = null;
- try
- {
- commandLine = cmdLineParser.parse(options, commandLineArguments);
- }
- catch (ParseException parseException)
- {
- System.out.println(parseException.getMessage());
- usage(options);
- }
- return commandLine;
+
+ int exitCode = new CommandLine(new Decrypt()).execute(args);
+ System.exit(exitCode);
}
- private void decrypt() throws IOException
+
+ public Integer call() throws IOException
{
PDDocument document = null;
InputStream keyStoreStream = null;
@@ -152,7 +90,12 @@ public final class Decrypt
{
keyStoreStream = new FileInputStream(keyStore);
}
- document = Loader.loadPDF(new File(infile), password, keyStoreStream, alias);
+ document = Loader.loadPDF(infile, password, keyStoreStream, alias);
+
+ // overwrite inputfile if no outputfile was specified
+ if (outfile == null) {
+ outfile = infile;
+ }
if (document.isEncrypted())
{
@@ -171,6 +114,7 @@ public final class Decrypt
else
{
System.err.println( "Error: Document is not encrypted." );
+ return 1;
}
}
finally
@@ -181,21 +125,6 @@ public final class Decrypt
}
IOUtils.closeQuietly(keyStoreStream);
}
+ return 0;
}
-
- /**
- * This will print a usage message.
- */
- private static void usage(Options options)
- {
- HelpFormatter formatter = new HelpFormatter();
- String syntax = "java -jar pdfbox-app-x.y.z.jar Decrypt [options] <inputfile> [outputfile]";
- String header = "\nOptions";
-
- formatter.setWidth(132);
- formatter.printHelp(syntax, header, options, "");
-
- System.exit(1);
- }
-
}