mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-16 06:23:10 +00:00
Replace handmade option parsing with Commons CLI to address LGTM array bounds complaints.
This commit is contained in:
@@ -29,6 +29,10 @@ import java.util.TimeZone;
|
|||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.apache.commons.cli.CommandLine;
|
||||||
|
import org.apache.commons.cli.DefaultParser;
|
||||||
|
import org.apache.commons.cli.Option;
|
||||||
|
import org.apache.commons.cli.Options;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.dspace.core.Context;
|
import org.dspace.core.Context;
|
||||||
import org.dspace.core.LogManager;
|
import org.dspace.core.LogManager;
|
||||||
@@ -337,44 +341,72 @@ public class LogAnalyser {
|
|||||||
Date myEndDate = null;
|
Date myEndDate = null;
|
||||||
boolean myLookUp = false;
|
boolean myLookUp = false;
|
||||||
|
|
||||||
// read in our command line options
|
// Define command line options.
|
||||||
for (int i = 0; i < argv.length; i++) {
|
Options options = new Options();
|
||||||
if (argv[i].equals("-log")) {
|
Option option;
|
||||||
myLogDir = argv[i + 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argv[i].equals("-file")) {
|
option = Option.builder().longOpt("log").hasArg().build();
|
||||||
myFileTemplate = argv[i + 1];
|
options.addOption(option);
|
||||||
}
|
|
||||||
|
|
||||||
if (argv[i].equals("-cfg")) {
|
option = Option.builder().longOpt("file").hasArg().build();
|
||||||
myConfigFile = argv[i + 1];
|
options.addOption(option);
|
||||||
}
|
|
||||||
|
|
||||||
if (argv[i].equals("-out")) {
|
option = Option.builder().longOpt("cfg").hasArg().build();
|
||||||
myOutFile = argv[i + 1];
|
options.addOption(option);
|
||||||
}
|
|
||||||
|
|
||||||
if (argv[i].equals("-help")) {
|
option = Option.builder().longOpt("out").hasArg().build();
|
||||||
LogAnalyser.usage();
|
options.addOption(option);
|
||||||
System.exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argv[i].equals("-start")) {
|
option = Option.builder().longOpt("help").build();
|
||||||
myStartDate = parseDate(argv[i + 1]);
|
options.addOption(option);
|
||||||
}
|
|
||||||
|
|
||||||
if (argv[i].equals("-end")) {
|
option = Option.builder().longOpt("start").hasArg().build();
|
||||||
myEndDate = parseDate(argv[i + 1]);
|
options.addOption(option);
|
||||||
}
|
|
||||||
|
|
||||||
if (argv[i].equals("-lookup")) {
|
option = Option.builder().longOpt("end").hasArg().build();
|
||||||
myLookUp = true;
|
options.addOption(option);
|
||||||
}
|
|
||||||
|
option = Option.builder().longOpt("lookup").build();
|
||||||
|
|
||||||
|
// Parse the command.
|
||||||
|
DefaultParser cmdParser = new DefaultParser();
|
||||||
|
CommandLine cmd = cmdParser.parse(options, argv);
|
||||||
|
|
||||||
|
// Analyze the command.
|
||||||
|
if (cmd.hasOption("help")) {
|
||||||
|
LogAnalyser.usage();
|
||||||
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (cmd.hasOption("log")) {
|
||||||
|
myLogDir = cmd.getOptionValue("log");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cmd.hasOption("file")) {
|
||||||
|
myFileTemplate = cmd.getOptionValue("file");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cmd.hasOption("cfg")) {
|
||||||
|
myConfigFile = cmd.getOptionValue("cfg");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cmd.hasOption("out")) {
|
||||||
|
myOutFile = cmd.getOptionValue("out");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cmd.hasOption("start")) {
|
||||||
|
myStartDate = parseDate(cmd.getOptionValue("start"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cmd.hasOption("end")) {
|
||||||
|
myEndDate = parseDate(cmd.getOptionValue("end"));
|
||||||
|
}
|
||||||
|
|
||||||
|
myLookUp = cmd.hasOption("lookup");
|
||||||
|
|
||||||
// now call the method which actually processes the logs
|
// now call the method which actually processes the logs
|
||||||
processLogs(context, myLogDir, myFileTemplate, myConfigFile, myOutFile, myStartDate, myEndDate, myLookUp);
|
processLogs(context, myLogDir, myFileTemplate, myConfigFile, myOutFile,
|
||||||
|
myStartDate, myEndDate, myLookUp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -27,6 +27,10 @@ import java.util.StringTokenizer;
|
|||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.apache.commons.cli.CommandLine;
|
||||||
|
import org.apache.commons.cli.DefaultParser;
|
||||||
|
import org.apache.commons.cli.Option;
|
||||||
|
import org.apache.commons.cli.Options;
|
||||||
import org.dspace.content.Item;
|
import org.dspace.content.Item;
|
||||||
import org.dspace.content.MetadataSchemaEnum;
|
import org.dspace.content.MetadataSchemaEnum;
|
||||||
import org.dspace.content.MetadataValue;
|
import org.dspace.content.MetadataValue;
|
||||||
@@ -162,7 +166,7 @@ public class ReportGenerator {
|
|||||||
/**
|
/**
|
||||||
* pattern that matches an unqualified aggregator property
|
* pattern that matches an unqualified aggregator property
|
||||||
*/
|
*/
|
||||||
private static final Pattern real = Pattern.compile("^(.+)=(.+)");
|
private static final Pattern REAL = Pattern.compile("^(.+)=(.+)");
|
||||||
|
|
||||||
//////////////////////////
|
//////////////////////////
|
||||||
// Miscellaneous variables
|
// Miscellaneous variables
|
||||||
@@ -221,28 +225,46 @@ public class ReportGenerator {
|
|||||||
String myOutput = null;
|
String myOutput = null;
|
||||||
String myMap = null;
|
String myMap = null;
|
||||||
|
|
||||||
// read in our command line options
|
Options options = new Options();
|
||||||
for (int i = 0; i < argv.length; i++) {
|
Option option;
|
||||||
if (argv[i].equals("-format")) {
|
|
||||||
myFormat = argv[i + 1].toLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argv[i].equals("-in")) {
|
option = Option.builder().longOpt("format").hasArg().build();
|
||||||
myInput = argv[i + 1];
|
options.addOption(option);
|
||||||
}
|
|
||||||
|
|
||||||
if (argv[i].equals("-out")) {
|
option = Option.builder().longOpt("in").hasArg().build();
|
||||||
myOutput = argv[i + 1];
|
options.addOption(option);
|
||||||
}
|
|
||||||
|
|
||||||
if (argv[i].equals("-map")) {
|
option = Option.builder().longOpt("out").hasArg().build();
|
||||||
myMap = argv[i + 1];
|
options.addOption(option);
|
||||||
}
|
|
||||||
|
|
||||||
if (argv[i].equals("-help")) {
|
option = Option.builder().longOpt("map").hasArg().build();
|
||||||
usage();
|
options.addOption(option);
|
||||||
System.exit(0);
|
|
||||||
}
|
option = Option.builder().longOpt("help").build();
|
||||||
|
options.addOption(option);
|
||||||
|
|
||||||
|
DefaultParser parser = new DefaultParser();
|
||||||
|
CommandLine cmd = parser.parse(options, argv);
|
||||||
|
|
||||||
|
if (cmd.hasOption("help")) {
|
||||||
|
usage();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cmd.hasOption("format")) {
|
||||||
|
myFormat = cmd.getOptionValue("format");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cmd.hasOption("in")) {
|
||||||
|
myInput = cmd.getOptionValue("in");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cmd.hasOption("out")) {
|
||||||
|
myOutput = cmd.getOptionValue("out");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cmd.hasOption("map")) {
|
||||||
|
myMap = cmd.getOptionValue("map");
|
||||||
}
|
}
|
||||||
|
|
||||||
processReport(context, myFormat, myInput, myOutput, myMap);
|
processReport(context, myFormat, myInput, myOutput, myMap);
|
||||||
@@ -576,7 +598,7 @@ public class ReportGenerator {
|
|||||||
|
|
||||||
// loop through the map file and read in the values
|
// loop through the map file and read in the values
|
||||||
while ((record = br.readLine()) != null) {
|
while ((record = br.readLine()) != null) {
|
||||||
Matcher matchReal = real.matcher(record);
|
Matcher matchReal = REAL.matcher(record);
|
||||||
|
|
||||||
// if the line is real then read it in
|
// if the line is real then read it in
|
||||||
if (matchReal.matches()) {
|
if (matchReal.matches()) {
|
||||||
@@ -650,7 +672,7 @@ public class ReportGenerator {
|
|||||||
// loop through the aggregator file and read in the values
|
// loop through the aggregator file and read in the values
|
||||||
while ((record = br.readLine()) != null) {
|
while ((record = br.readLine()) != null) {
|
||||||
// match real lines
|
// match real lines
|
||||||
Matcher matchReal = real.matcher(record);
|
Matcher matchReal = REAL.matcher(record);
|
||||||
|
|
||||||
// pre-prepare our input strings
|
// pre-prepare our input strings
|
||||||
String section = null;
|
String section = null;
|
||||||
|
Reference in New Issue
Block a user