1
- /**
2
- *
3
- */
4
- package org .verapdf .cli ;
5
-
6
- import org .apache .commons .cli .CommandLine ;
7
- import org .apache .commons .cli .CommandLineParser ;
8
- import org .apache .commons .cli .GnuParser ;
9
- import org .apache .commons .cli .HelpFormatter ;
10
- import org .apache .commons .cli .Option ;
11
- import org .apache .commons .cli .OptionBuilder ;
12
- import org .apache .commons .cli .Options ;
13
- import org .apache .commons .cli .ParseException ;
14
- import org .verapdf .VeraPdfTaskConfig ;
15
- import org .verapdf .pdfa .spec .PdfaFlavour ;
16
- import org .verapdf .pdfa .spec .PdfaSpecifications ;
17
-
18
- /**
19
- * @author cfw
20
- *
21
- */
22
- public class VeraPdfCli {
23
- /**
24
- * The application name, used to invoke the CLI application
25
- */
26
- public static final String APP_NAME = "veraPdf" ; //$NON-NLS-1$
27
-
28
- // String constants for CLI Options
29
- private static final String VALIDATE_OPT = "validate" ; //$NON-NLS-1$
30
- private static final String VALIDATE_OPT_DESC = "Request PDF/A validation" ;
31
- private static final String FILE_OPT = "file" ; //$NON-NLS-1$
32
- private static final String FILE_OPT_ARG = "filepath" ;
33
- private static final String FILE_OPT_DESC = "Absolute path of file to validate." ;
34
- private static final String FIXMETADATA_OPT = "fixmetadata" ; //$NON-NLS-1$
35
- private static final String FIXMETADATA_OPT_DESC = "Request automatic fix of metadata." ;
36
- private static final String URL_OPT = "url" ; //$NON-NLS-1$
37
- private static final String URL_OPT_ARG = "URL" ;
38
- private static final String URL_OPT_DESC = "URI encoded URL of file to validate." ;
39
- private static final String PDFA_OPT = "pdfa" ; //$NON-NLS-1$
40
- private static final String PDFA_OPT_ARG = "PDF/A flavour" ;
41
- private static final String PDFA_OPT_DESC = "PDF/A flavour to use for validation, can be (none|1a|1b|2a|2b|3a|3b|3u)." ;
42
- private static final String STOPERRORS_OPT = "stoperrors" ; //$NON-NLS-1$
43
- private static final String STOPERRORS_OPT_ARG = "number" ;
44
- private static final String STOPERRORS_OPT_DESC = "The number of errors after which validation is interupted, must be an integer greater than 0. Default is to not stop on any error." ;
45
- private static final String VERBOSITY_OPT = "verbosity" ; //$NON-NLS-1$
46
- private static final String VERBOSITY_OPT_ARG = "number" ;
47
- private static final String VERBOSITY_OPT_DESC = "Control reporting verbosity, a number between 0 and 9 (inclusive), defaults to 3." ;
48
- private static final String HELP_OPT = "help" ; //$NON-NLS-1$
49
- private static final String HELP_OPT_DESC = "Print this message." ;
50
-
51
- // Create the options object
52
- private static final Options OPTIONS = new Options ();
53
- static {
54
- Option help = new Option (HELP_OPT , HELP_OPT_DESC );
55
- Option validate = new Option (VALIDATE_OPT , VALIDATE_OPT_DESC );
56
- Option fixMetadata = new Option (FIXMETADATA_OPT , FIXMETADATA_OPT_DESC );
57
- @ SuppressWarnings ("static-access" )
58
- Option file = OptionBuilder .withArgName (FILE_OPT_ARG ).hasArg ()
59
- .withDescription (FILE_OPT_DESC ).create (FILE_OPT );
60
- @ SuppressWarnings ("static-access" )
61
- Option url = OptionBuilder .withArgName (URL_OPT_ARG ).hasArg ()
62
- .withDescription (URL_OPT_DESC ).create (URL_OPT );
63
- @ SuppressWarnings ("static-access" )
64
- Option pdfa = OptionBuilder .withArgName (PDFA_OPT_ARG ).hasArg ()
65
- .withDescription (PDFA_OPT_DESC ).create (PDFA_OPT );
66
- @ SuppressWarnings ("static-access" )
67
- Option stopErrors = OptionBuilder .withArgName (STOPERRORS_OPT_ARG )
68
- .hasArg ().withDescription (STOPERRORS_OPT_DESC )
69
- .create (STOPERRORS_OPT );
70
- @ SuppressWarnings ("static-access" )
71
- Option verbosity = OptionBuilder .withArgName (VERBOSITY_OPT_ARG )
72
- .hasArg ().withDescription (VERBOSITY_OPT_DESC )
73
- .create (VERBOSITY_OPT );
74
-
75
- OPTIONS .addOption (help );
76
- OPTIONS .addOption (validate );
77
- OPTIONS .addOption (fixMetadata );
78
- OPTIONS .addOption (file );
79
- OPTIONS .addOption (url );
80
- OPTIONS .addOption (pdfa );
81
- OPTIONS .addOption (verbosity );
82
- OPTIONS .addOption (stopErrors );
83
- }
84
-
85
- /**
86
- * Main CLI entry point, process the command line arguments
87
- *
88
- * @param args
89
- * Java.lang.String array of command line args, to be processed
90
- * using Apache commons CLI.
91
- */
92
- public static void main (String [] args ) {
93
- CommandLineParser gnuParser = new GnuParser ();
94
- VeraPdfTaskConfig taskConfig = VeraPdfTaskConfig .defaultInstance ();
95
- try {
96
- CommandLine cmdLine = gnuParser .parse (OPTIONS , args );
97
-
98
- // If help requested then output help message and terminate
99
- if (cmdLine .hasOption (HELP_OPT ) || args .length == 0 ) {
100
- outputHelpAndTerminate (0 );
101
- }
102
-
103
- taskConfig = createConfigFromCliOptions (cmdLine );
104
- } catch (ParseException excep ) {
105
- System .err .println ("Command line parsing failed, exception message: " + excep .getLocalizedMessage ());
106
- excep .printStackTrace ();
107
- outputHelpAndTerminate (1 );
108
- }
109
-
110
- }
111
-
112
- private final static void outputHelpAndTerminate (final int exitCode ) {
113
- HelpFormatter formatter = new HelpFormatter ();
114
- formatter .printHelp (APP_NAME , OPTIONS );
115
- System .exit (exitCode );
116
- }
117
-
118
- private final static VeraPdfTaskConfig createConfigFromCliOptions (final CommandLine cmdLine ) {
119
- boolean validate = cmdLine .hasOption (VALIDATE_OPT );
120
- boolean fixMetadata = cmdLine .hasOption (FIXMETADATA_OPT );
121
- PdfaFlavour flavour = PdfaFlavour .NONE ;
122
- if (cmdLine .hasOption (PDFA_OPT )) {
123
- String flavourString = cmdLine .getOptionValue (PDFA_OPT );
124
- flavour = PdfaSpecifications .byFlavourString (flavourString );
125
- }
126
- int verbosity = VeraPdfTaskConfig .VERBOSITY_DEFAULT ;
127
- if (cmdLine .hasOption (VERBOSITY_OPT )) {
128
- verbosity = Integer .valueOf (cmdLine .getOptionValue (VERBOSITY_OPT )).intValue ();
129
- }
130
- int stopErrors = VeraPdfTaskConfig .STOPERRORS_DEFAULT ;
131
- if (cmdLine .hasOption (STOPERRORS_OPT )) {
132
- stopErrors = Integer .valueOf (cmdLine .getOptionValue (STOPERRORS_OPT )).intValue ();
133
- }
134
- return VeraPdfTaskConfig .fromValues (flavour , validate , fixMetadata , verbosity , stopErrors );
135
- }
1
+ /**
2
+ *
3
+ */
4
+ package org .verapdf .cli ;
5
+
6
+ import com .beust .jcommander .JCommander ;
7
+ import org .verapdf .VeraPdfTaskConfig ;
8
+ import org .verapdf .cli .commands .CommandVeraPDF ;
9
+
10
+ /**
11
+ * @author cfw
12
+ *
13
+ */
14
+ public class VeraPdfCli {
15
+
16
+ /**
17
+ * The application name, used to invoke the CLI application
18
+ */
19
+ public static final String APP_NAME = "veraPdf" ; //$NON-NLS-1$
20
+
21
+ private static final CommandVeraPDF commandVeraPDF ;
22
+
23
+ static {
24
+ commandVeraPDF = new CommandVeraPDF ();
25
+ }
26
+
27
+ /**
28
+ * Main CLI entry point, process the command line arguments
29
+ *
30
+ * @param args
31
+ * Java.lang.String array of command line args, to be processed
32
+ * using Apache commons CLI.
33
+ */
34
+ public static void main (String [] args ) {
35
+ JCommander jCommander = new JCommander ();
36
+ jCommander .addCommand (commandVeraPDF );
37
+ VeraPdfTaskConfig taskConfig = VeraPdfTaskConfig .defaultInstance ();
38
+
39
+ jCommander .parse (args );
40
+ taskConfig = createConfigFromCliOptions (commandVeraPDF );
41
+ }
42
+
43
+ /**
44
+ * Creates instance of VeraPdfTaskConfig from parsed cli options
45
+ * @param commandVeraPDF options used by VeraPDF software
46
+ * @return an immutable VeraPdfTaskConfig object populated from the parsed options
47
+ */
48
+ private final static VeraPdfTaskConfig createConfigFromCliOptions (final CommandVeraPDF commandVeraPDF ) {
49
+ //TODO: if no logic will be placed there than I prefer not to use intermediate variables
50
+ boolean validate = commandVeraPDF .isValidate ();
51
+ String inputPath = commandVeraPDF .getInputPath ();
52
+ boolean inputPathURL = commandVeraPDF .isInputPathURL ();
53
+ String flavour = commandVeraPDF .getFlavour ();
54
+ String profile = commandVeraPDF .getProfile ();
55
+ boolean fixMetadata = commandVeraPDF .isFixMetadata ();
56
+ Integer verbosity = commandVeraPDF .getVerbosity ();
57
+ boolean progress = commandVeraPDF .isProgress ();
58
+ boolean progressToStdout = commandVeraPDF .isProgressToStdout ();
59
+ String progressPath = commandVeraPDF .getProgressPath ();
60
+ boolean progressPathURL = commandVeraPDF .isProgressPathURL ();
61
+ Integer stopErrors = commandVeraPDF .getStopErrors ();
62
+ String tempDir = commandVeraPDF .getTempDir ();
63
+ boolean output = commandVeraPDF .isOutput ();
64
+ boolean outputToInput = commandVeraPDF .isOutputToInput ();
65
+ String outputPath = commandVeraPDF .getOutputPath ();
66
+ boolean outputPathURL = commandVeraPDF .isOutputPathURL ();
67
+ String report = commandVeraPDF .getReport ();
68
+ return VeraPdfTaskConfig .fromValues (validate , inputPath , inputPathURL , flavour , profile ,
69
+ fixMetadata , verbosity , progress , progressToStdout , progressPath ,
70
+ progressPathURL , stopErrors , tempDir , output , outputToInput ,
71
+ outputPath , outputPathURL , report );
72
+ }
136
73
}
0 commit comments