Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Piccolo Proof of Concept #625

Open
wants to merge 5 commits into
base: integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions jhove-apps/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.3.2</version>
</dependency>
</dependencies>

<profiles>
Expand Down
357 changes: 93 additions & 264 deletions jhove-apps/src/main/java/Jhove.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/**********************************************************************
* Jhove - JSTOR/Harvard Object Validation Environment
* Copyright 2004-2007 by the President and Fellows of Harvard College
Expand All @@ -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<String> 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());
}
}
}
Loading