/
VeraPdfCli.java
205 lines (188 loc) · 7.84 KB
/
VeraPdfCli.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* This file is part of VeraPDF Library GUI, a module of the veraPDF project.
* Copyright (c) 2015, veraPDF Consortium <info@verapdf.org> All rights
* reserved. VeraPDF Library GUI is free software: you can redistribute it
* and/or modify it under the terms of either: The GNU General public license
* GPLv3+. You should have received a copy of the GNU General Public License
* along with VeraPDF Library GUI as the LICENSE.GPL file in the root of the
* source tree. If not, see http://www.gnu.org/licenses/ or
* https://www.gnu.org/licenses/gpl-3.0.en.html. The Mozilla Public License
* MPLv2+. You should have received a copy of the Mozilla Public License along
* with VeraPDF Library GUI as the LICENSE.MPL file in the root of the source
* tree. If a copy of the MPL was not distributed with this file, you can obtain
* one at http://mozilla.org/MPL/2.0/.
*/
/**
*
*/
package org.verapdf.cli;
import java.io.*;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.verapdf.ReleaseDetails;
import org.verapdf.apps.Applications;
import org.verapdf.apps.ConfigManager;
import org.verapdf.apps.SoftwareUpdater;
import org.verapdf.cli.commands.VeraCliArgParser;
import org.verapdf.cli.multithread.MultiThreadProcessor;
import org.verapdf.core.VeraPDFException;
import org.verapdf.pdfa.flavours.PDFAFlavour;
import org.verapdf.pdfa.validation.profiles.ProfileDirectory;
import org.verapdf.pdfa.validation.profiles.Profiles;
import org.verapdf.pdfa.validation.profiles.ValidationProfile;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
import org.verapdf.processor.FeaturesPluginsLoader;
/**
* @author <a href="mailto:carl@openpreservation.org">Carl Wilson</a>
*/
public final class VeraPdfCli {
private static final Logger logger = Logger.getLogger(VeraCliArgParser.class.getCanonicalName());
private static final ConfigManager configManager = Applications.createAppConfigManager();
private static final int MEGABYTE = (1024 * 1024);
private static final String FLAVOURS_HEADING = CliConstants.APP_NAME + " supported PDF/A profiles:"; //$NON-NLS-1$
private static final ProfileDirectory PROFILES = Profiles.getVeraProfileDirectory();
public static final String EXIT = "q";
private VeraPdfCli() {
// disable default constructor
}
/**
* Main CLI entry point, process the command line arguments
*
* @param args Java.lang.String array of command line args, to be processed
* using Apache commons CLI.
*/
public static void main(final String[] args) throws VeraPDFException {
MemoryMXBean memoryMan = ManagementFactory.getMemoryMXBean();
FeaturesPluginsLoader.setBaseFolderPath(System.getProperty(Applications.APP_HOME_PROPERTY));
ReleaseDetails.addDetailsFromResource(
ReleaseDetails.APPLICATION_PROPERTIES_ROOT + "app." + ReleaseDetails.PROPERTIES_EXT); //$NON-NLS-1$
VeraCliArgParser cliArgParser = new VeraCliArgParser();
JCommander jCommander = new JCommander(cliArgParser);
jCommander.setProgramName(CliConstants.APP_NAME);
try {
jCommander.parse(args);
} catch (ParameterException e) {
System.err.println(e.getMessage());
displayHelpAndExit(cliArgParser, jCommander, 1);
}
if (cliArgParser.isHelp()) {
displayHelpAndExit(cliArgParser, jCommander, 0);
}
messagesFromParser(cliArgParser);
if (isProcess(cliArgParser)) {
if (args.length == 0) {
jCommander.usage();
}
try {
if (cliArgParser.isServerMode() || cliArgParser.getNumberOfProcesses() < 2) {
singleThreadProcess(cliArgParser);
} else {
MultiThreadProcessor.process(cliArgParser);
}
} catch (OutOfMemoryError oome) {
final String message = "The JVM appears to have run out of memory"; //$NON-NLS-1$
logger.log(Level.WARNING, message, oome);
MemoryUsage heapUsage = memoryMan.getHeapMemoryUsage();
long maxMemory = heapUsage.getMax() / MEGABYTE;
long usedMemory = heapUsage.getUsed() / MEGABYTE;
System.out.format(",%s\n", message); //$NON-NLS-1$
System.out.format("Memory Use: %sM/%sM\n", Long.valueOf(usedMemory), Long.valueOf(maxMemory)); //$NON-NLS-1$
System.out.format(
"To increase the memory available to the JVM please assign the JAVA_OPTS environment variable.\n"); //$NON-NLS-1$
System.out.format("The examples below increase the maximum heap available to the JVM to 2GB:\n"); //$NON-NLS-1$
System.out.format(" - Mac or Linux users:\n"); //$NON-NLS-1$
System.out.format(" export JAVA_OPTS=\"-Xmx2048m\"\n"); //$NON-NLS-1$
System.out.format(" - Windows users:\n"); //$NON-NLS-1$
System.out.format(" SET JAVA_OPTS=\"-Xmx2048m\"\n"); //$NON-NLS-1$
System.exit(1);
}
}
}
private static void singleThreadProcess(VeraCliArgParser cliArgParser) throws VeraPDFException {
try (VeraPdfCliProcessor processor = VeraPdfCliProcessor.createProcessorFromArgs(cliArgParser,
configManager)) {
// FIXME: trap policy IO Exception (deliberately left un-caught for development)
processor.processPaths(cliArgParser.getPdfPaths());
if (cliArgParser.isServerMode()) {
File tempFile = processor.getTempFile();
if (tempFile != null) {
System.out.println(tempFile.getAbsoluteFile());
}
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String path = scanner.nextLine();
if (path != null) {
if (path.equals(EXIT)) {
break;
} else {
List<String> pathes = new ArrayList<>();
pathes.add(path);
processor.processPaths(pathes);
System.out.println(processor.getTempFile().getAbsolutePath());
}
}
}
}
}
}
public static void displayHelpAndExit(VeraCliArgParser cliArgParser, JCommander jCommander, int i) {
showVersionInfo(cliArgParser.isVerbose());
jCommander.usage();
System.exit(i);
}
private static void messagesFromParser(final VeraCliArgParser parser) {
if (parser.listProfiles()) {
listProfiles();
}
if (parser.showVersion()) {
showVersionInfo(parser.isVerbose());
}
}
private static void listProfiles() {
System.out.println(FLAVOURS_HEADING);
EnumSet<PDFAFlavour> flavs = EnumSet.copyOf(PROFILES.getPDFAFlavours());
for (PDFAFlavour flav : flavs) {
ValidationProfile profile = PROFILES.getValidationProfileByFlavour(flav);
System.out.format(" %s - %s", profile.getPDFAFlavour().getId(), profile.getDetails().getName());//$NON-NLS-1$
System.out.println();
}
}
private static void showVersionInfo(final boolean isVerbose) {
ReleaseDetails appDetails = Applications.getAppDetails();
System.out.format("%s %s\n", CliConstants.APP_NAME, appDetails.getVersion()); //$NON-NLS-1$
System.out.format("Built: %s\n", appDetails.getBuildDate()); //$NON-NLS-1$
System.out.format("%s\n", ReleaseDetails.rightsStatement()); //$NON-NLS-1$
if (isVerbose)
showUpdateInfo(appDetails);
}
private static void showUpdateInfo(final ReleaseDetails details) {
SoftwareUpdater updater = Applications.softwareUpdater();
if (!updater.isOnline()) {
logger.log(Level.WARNING, Applications.UPDATE_SERVICE_NOT_AVAILABLE); //$NON-NLS-1$
return;
}
if (!updater.isUpdateAvailable(details)) {
System.out.format(Applications.UPDATE_LATEST_VERSION, ",", details.getVersion() + "\n"); //$NON-NLS-1$
return;
}
System.out.format(
Applications.UPDATE_OLD_VERSION, //$NON-NLS-1$
details.getVersion(), updater.getLatestVersion(details));
System.out.format("You can download the latest version from: %s.\n", //$NON-NLS-1$
Applications.UPDATE_URI); //$NON-NLS-1$
}
private static boolean isProcess(final VeraCliArgParser parser) {
if (parser.getPdfPaths().isEmpty() && (parser.isHelp() || parser.listProfiles() || parser.showVersion())) {
return false;
}
return true;
}
}