diff --git a/jhove-apps/pom.xml b/jhove-apps/pom.xml index 416d9ed1a..bf7efd7d1 100644 --- a/jhove-apps/pom.xml +++ b/jhove-apps/pom.xml @@ -65,6 +65,12 @@ junit junit + + + info.picocli + picocli + 4.3.2 + diff --git a/jhove-apps/src/main/java/Jhove.java b/jhove-apps/src/main/java/Jhove.java index 72b289d9e..9f05b298d 100644 --- a/jhove-apps/src/main/java/Jhove.java +++ b/jhove-apps/src/main/java/Jhove.java @@ -1,3 +1,4 @@ + /********************************************************************** * Jhove - JSTOR/Harvard Object Validation Environment * Copyright 2004-2007 by the President and Fellows of Harvard College @@ -24,272 +25,100 @@ import edu.harvard.hul.ois.jhove.JhoveBase; import edu.harvard.hul.ois.jhove.Module; import edu.harvard.hul.ois.jhove.OutputHandler; +import picocli.CommandLine; -import java.util.ArrayList; -import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; -public class Jhove -{ - /** Application name. */ - private static final String NAME = "Jhove"; - /** Logger for this class. */ - private static final Logger LOGGER = Logger.getLogger(Jhove.class.getCanonicalName()); - - private Jhove() - { - throw new AssertionError("Should never enter private constructor"); - } - - private static final String C_CONFIG_OPTION = "-c"; - private static final String X_CONFIG_OPTION = "-x"; - private static final String NOT_FOUND = "' not found"; - private static final String HANDLER = "Handler '"; - - - /** - * MAIN ENTRY POINT. - */ - public static void main(String [] args) - { - // Make sure we have a satisfactory version of Java. - String version = System.getProperty("java.vm.version"); - if (version.compareTo("1.8.0") < 0) { - LOGGER.log(Level.SEVERE, CoreMessageConstants.EXC_JAVA_VER_INCMPT); - System.exit(ExitCode.INCOMPATIBLE_VM.getReturnCode()); - } - - try { - - // Initialize the application state object. - App app = App.newAppWithName(NAME); - - // Retrieve configuration. - String configFile = JhoveBase.getConfigFileFromProperties(); - String saxClass = JhoveBase.getSaxClassFromProperties(); - - /* Pre-parse the command line for -c and -x config options. - * With Windows, we have to deal with quote marks on our own. - * With Unix, the shell takes care of quotes for us. */ - boolean quoted = false; - for (int i = 0; i < args.length; i++) { - if (quoted) { - int len = args[i].length(); - if (args[i].charAt(len - 1) == '"') { - quoted = false; - } - } - else { - if (C_CONFIG_OPTION.equals(args[i])) { - if (i < args.length - 1) { - configFile = args[++i]; - } - } - else if (X_CONFIG_OPTION.equals(args[i])) { - if (i < args.length - 1) { - saxClass = args[++i]; - } - } - else if (args[i].charAt(0) == '"') { - quoted = true; - } - } - } - - // Initialize the JHOVE engine. - String encoding = null; - String tempDir = null; - int bufferSize = -1; - String moduleName = null; - String handlerName = null; - String aboutHandler = null; - String logLevel = null; - String outputFile = null; - boolean checksum = false; - boolean showRaw = false; - boolean signature = false; - List list = new ArrayList<>(); - - /* - * Parse command line arguments: - * -m module Module name - * -h handler Output handler - * -e encoding Output encoding - * -H handler About handler - * -o output Output file pathname - * -t tempdir Directory for temp files - * -b bufsize Buffer size for buffered I/O - * -k Calculate checksums - * -r Display raw numeric flags - * -s Check internal signatures only - * dirFileOrUri Directories, file pathnames, or URIs - * - * The following arguments were defined in previous - * versions, but are now obsolete - * -p param OBSOLETE - * -P param OBSOLETE - */ - - quoted = false; - StringBuilder filename = null; - - for (int i = 0; i < args.length; i++) { - if (quoted) { - filename.append(" "); - int len = args[i].length(); - if (args[i].charAt(len - 1) == '"') { - filename.append(args[i].substring(0, len - 1)); - list.add(filename.toString()); - quoted = false; - } - else { - filename.append(args[i]); - } - } - else { - if (C_CONFIG_OPTION.equals(args[i])) { - i++; - } - else if ("-m".equals(args[i])) { - if (i < args.length - 1) { - moduleName = args[++i]; - } - } - else if ("-p".equals(args[i])) { - // Obsolete -- but eat the next arg for compatibility - if (i < args.length - 1) { - @SuppressWarnings("unused") - String moduleParam = args[++i]; - } - } - else if ("-h".equals(args[i])) { - if (i < args.length - 1) { - handlerName = args[++i]; - } - } - else if ("-P".equals(args[i])) { - // Obsolete -- but eat the next arg for compatibility - if (i < args.length - 1) { - @SuppressWarnings("unused") - String handlerParam = args[++i]; - } - } - else if ("-e".equals(args[i])) { - if (i < args.length - 1) { - encoding = args[++i]; - } - } - else if ("-H".equals(args[i])) { - if (i < args.length - 1) { - aboutHandler = args[++i]; - } - } - else if ("-l".equals(args[i])) { - if (i < args.length - 1) { - logLevel = args[++i]; - } - } - else if ("-o".equals(args[i])) { - if (i < args.length - 1) { - outputFile = args[++i]; - } - } - else if (X_CONFIG_OPTION.equals(args[i])) { - i++; - } - else if ("-t".equals(args[i])) { - if (i < args.length - 1) { - tempDir = args[++i]; - } - } - else if ("-b".equals(args[i])) { - if (i < args.length - 1) { - try { - bufferSize = Integer.parseInt(args[++i]); - } - catch (NumberFormatException nfe) { - LOGGER.log(Level.WARNING, "Invalid buffer size, using default."); - } - } - } - else if ("-k".equals(args[i])) { - checksum = true; - } - else if ("-r".equals(args[i])) { - showRaw = true; - } - else if ("-s".equals(args[i])) { - signature = true; - } - else if (args[i].charAt(0) != '-') { - if (args[i].charAt(0) == '"') { - filename = new StringBuilder(); - filename.append(args[i].substring(1)); - quoted = true; - } - else { - list.add(args[i]); - } - } - } - } - if (quoted) { - list.add(filename.toString()); - } - - JhoveBase je = new JhoveBase(); - // Only set the log level if a param value was assigned - if (logLevel != null) { - je.setLogLevel(logLevel); - } - je.init (configFile, saxClass); - if (encoding == null) { - encoding = je.getEncoding(); - } - if (tempDir == null) { - tempDir = je.getTempDirectory(); - } - if (bufferSize < 0) { - bufferSize = je.getBufferSize(); - } - Module module = je.getModule(moduleName); - if (module == null && moduleName != null) { - LOGGER.log(Level.SEVERE, "Module '" + moduleName + NOT_FOUND); - System.exit(ExitCode.ERROR.getReturnCode()); - } - OutputHandler about = je.getHandler(aboutHandler); - if (about == null && aboutHandler != null) { - LOGGER.log(Level.SEVERE, HANDLER + aboutHandler + NOT_FOUND); - System.exit(ExitCode.ERROR.getReturnCode()); - } - OutputHandler handler = je.getHandler(handlerName); - if (handler == null && handlerName != null) { - LOGGER.log(Level.SEVERE, HANDLER + handlerName + NOT_FOUND); - System.exit(ExitCode.ERROR.getReturnCode()); - } - String[] dirFileOrUri = null; - int len = list.size(); - if (len > 0) { - dirFileOrUri = new String[len]; - for (int i = 0; i < len; i++) { - dirFileOrUri[i] = list.get(i); - } - } - - // Invoke the JHOVE engine. - je.setEncoding(encoding); - je.setTempDirectory(tempDir); - je.setBufferSize(bufferSize); - je.setChecksumFlag(checksum); - je.setShowRawFlag(showRaw); - je.setSignatureFlag(signature); - je.dispatch(app, module, about, handler, outputFile, dirFileOrUri); - } - catch (Exception e) { - LOGGER.log(Level.SEVERE, e.getMessage()); - e.printStackTrace(System.err); - System.exit(ExitCode.ERROR.getReturnCode()); - } - } +//TODO why is that never used? +//import picocli.CommandLine; +//import picocli.CommandLine.Option; +//import picocli.CommandLine.Parameters; + +public class Jhove { + /** Application name. */ + private static final String NAME = "Jhove"; + /** Logger for this class. */ + private static final Logger LOGGER = Logger.getLogger(Jhove.class.getCanonicalName()); + + private Jhove() { + throw new AssertionError("Should never enter private constructor"); + } + + private static final String NOT_FOUND = "' not found"; + private static final String HANDLER = "Handler '"; + + /** + * MAIN ENTRY POINT. + */ + public static void main(String[] args) { + // Make sure we have a satisfactory version of Java. + String version = System.getProperty("java.vm.version"); + if (version.compareTo("1.8.0") < 0) { + LOGGER.log(Level.SEVERE, CoreMessageConstants.EXC_JAVA_VER_INCMPT); + System.exit(ExitCode.INCOMPATIBLE_VM.getReturnCode()); + } + + // Set up config + JhoveConfig config = new JhoveConfig(); + // Parse the args passed + new CommandLine(config).parseArgs(args); + //TODO Carl can delete it if he doesn't want it + // Test the use of -k for checksumming. + /* + * System.out.println("-c opt is: <" + config.configFile + ">"); + * System.out.println("-x opt is: <" + config.saxClass + ">"); + * System.out.println("Checksum opt is: <" + config.isCheckum + ">"); + * System.exit(0); + */ + + try { + // Initialize the application state object. + App app = App.newAppWithName(NAME); + + JhoveBase je = new JhoveBase(); + // Only set the log level if a param value was assigned + if (config.logLevel != null) { + je.setLogLevel(config.logLevel); + } + je.init(config.configFile, config.saxClass); + if (config.encoding == null) { + config.encoding = je.getEncoding(); + } + if (config.tempDir == null) { + config.tempDir = je.getTempDirectory(); + } + if (config.bufferSize < 0) { + config.bufferSize = je.getBufferSize(); + } + Module module = je.getModule(config.moduleName); + if (module == null && config.moduleName != null) { + LOGGER.log(Level.SEVERE, "Module '" + config.moduleName + NOT_FOUND); + System.exit(ExitCode.ERROR.getReturnCode()); + } + OutputHandler about = je.getHandler(config.aboutHandler); + if (about == null && config.aboutHandler != null) { + LOGGER.log(Level.SEVERE, HANDLER + config.aboutHandler + NOT_FOUND); + System.exit(ExitCode.ERROR.getReturnCode()); + } + OutputHandler handler = je.getHandler(config.handlerName); + if (handler == null && config.handlerName != null) { + LOGGER.log(Level.SEVERE, HANDLER + config.handlerName + NOT_FOUND); + System.exit(ExitCode.ERROR.getReturnCode()); + } + + // Invoke the JHOVE engine. + je.setEncoding(config.encoding); + je.setTempDirectory(config.tempDir); + je.setBufferSize(config.bufferSize); + je.setChecksumFlag(config.checksum); + je.setShowRawFlag(config.showRaw); + je.setSignatureFlag(config.signature); + je.dispatch(app, module, about, handler, config.outputFile, config.dirFileOrUri); + } catch (Exception e) { + LOGGER.log(Level.SEVERE, e.getMessage()); + e.printStackTrace(System.err); + System.exit(ExitCode.ERROR.getReturnCode()); + } + } } diff --git a/jhove-apps/src/main/java/JhoveConfig.java b/jhove-apps/src/main/java/JhoveConfig.java new file mode 100644 index 000000000..a2e07636e --- /dev/null +++ b/jhove-apps/src/main/java/JhoveConfig.java @@ -0,0 +1,49 @@ + +/** + * + */ + +import java.io.File; + +import edu.harvard.hul.ois.jhove.JhoveBase; +/** + * @author cfw + * @author Alfred Wutschka + * + */ +import picocli.CommandLine.Option; +import picocli.CommandLine.Parameters; + +public final class JhoveConfig { + //TODO a lot of tbd in here + @Option(names = { "-c", "--config" }, description = "Path to an alternative jhove config file.") + String configFile = JhoveBase.getConfigFileFromProperties(); + @Option(names = { "-x", "--x-tbd" }, description = "TBD") + String saxClass = JhoveBase.getSaxClassFromProperties(); + @Option(names = { "-e", "--encoding" }, description = "TBD") + String encoding = null; + @Option(names = { "-t", "--t-tbd" }, description = "TBD") + String tempDir = null; + @Option(names = { "-b", "--buffer" }, description = "Buffer size for buffered I/O.") + int bufferSize = -1; + @Option(names = { "-m", "--module" }, description = "Name of the JHOVE module to invoke.") + String moduleName = null; + @Option(names = { "-h", "--output-handler" }, description = "TBD") + String handlerName = null; + @Option(names = { "-H", "--about-handler" }, description = "TBD") + String aboutHandler = null; + @Option(names = { "-l", "--l-tbd" }, description = "TBD") + String logLevel = null; + @Option(names = { "-o", "--output" }, description = "Outputfile Pathname.") + String outputFile = null; + @Option(names = { "-k", "--checksum" }, description = "Calculate checksums.") + boolean checksum = false; + @Option(names = { "-r", "--raw" }, description = "Display raw numeric flags.") + boolean showRaw = false; + @Option(names = { "-s", "--signature" }, description = "Check internal signatures only.") + boolean signature = false; + + // I'd like to see us go multi file if we can + @Parameters(arity = "0..*", paramLabel = "FILE", description = "Directories, files or URIs to process.") + String[] dirFileOrUri = null; +} diff --git a/jhove-ext-modules/src/main/java/edu/harvard/hul/ois/jhove/module/gzip/package-info.java b/jhove-ext-modules/src/main/java/edu/harvard/hul/ois/jhove/module/gzip/package-info.java index 722738d5a..be0150ea1 100644 --- a/jhove-ext-modules/src/main/java/edu/harvard/hul/ois/jhove/module/gzip/package-info.java +++ b/jhove-ext-modules/src/main/java/edu/harvard/hul/ois/jhove/module/gzip/package-info.java @@ -10,4 +10,4 @@ *
  • JHOVE home page * */ -package hul.ois.jhove.module.gzip; +package edu.harvard.hul.ois.jhove.module.gzip; diff --git a/jhove-ext-modules/src/main/java/edu/harvard/hul/ois/jhove/module/warc/package-info.java b/jhove-ext-modules/src/main/java/edu/harvard/hul/ois/jhove/module/warc/package-info.java index c918dcb08..3bf6175b8 100644 --- a/jhove-ext-modules/src/main/java/edu/harvard/hul/ois/jhove/module/warc/package-info.java +++ b/jhove-ext-modules/src/main/java/edu/harvard/hul/ois/jhove/module/warc/package-info.java @@ -10,4 +10,4 @@ *
  • JHOVE home page * */ -package hul.ois.jhove.module.warc; +package edu.harvard.hul.ois.jhove.module.warc; diff --git a/test-root/corpora/examples/modules/AIFF-hul/8-Bit-Noise-1.aif b/test-root/corpora/examples/modules/AIFF-hul/8-Bit-Noise-1.aif deleted file mode 100644 index 108a0d874..000000000 Binary files a/test-root/corpora/examples/modules/AIFF-hul/8-Bit-Noise-1.aif and /dev/null differ