Skip to content
Merged
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
44 changes: 34 additions & 10 deletions src/main/java/org/spdx/tools/MatchingStandardLicenses.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
* Tool to compare a license text to standard licenses. Lists all standard
* license ID's that are equivalent using the SPDX Legal team's license matching
* guidelines (http://spdx.org/spdx-license-list/matching-guidelines)
* <br/>
* Exit codes:
* <ul>
* <li>0 - the comparison completed (with or without a match)</li>
* <li>1 - the comparison failed - e.g. the input file could not be read or the standard licenses could not be compared</li>
* <li>2 - the command was invoked incorrectly (missing/invalid arguments)</li>
* </ul>
* @author Gary O'Neall
*/
public class MatchingStandardLicenses {
Expand All @@ -44,43 +51,60 @@ private MatchingStandardLicenses() {

static int MIN_ARGS = 1;
static int MAX_ARGS = 1;
static final int ERROR_STATUS = 1;

/**
* Main entry point for the MatchingStandardLicenses tool.
* Delegates to {@link #run(String[])} and terminates the JVM with its exit status.
* @param args
*/
public static void main(String[] args) {
System.exit(run(args));
}

/**
* Runs the MatchingStandardLicenses command logic and reports results to
* standard out.
* @param args
* @return process exit status, see {@link ExitCode}
*/
static int run(String[] args) {
if (args == null || args.length < MIN_ARGS || args.length > MAX_ARGS) {
System.out.println("Invalid arguments");
usage();
System.exit(ERROR_STATUS);
return ExitCode.USAGE_ERROR;
}
@SuppressWarnings("null")
File textFile = new File(args[0]);
String textFilePath = args[0];
if (textFilePath == null) {
System.out.println("Invalid arguments");
usage();
return ExitCode.USAGE_ERROR;
}
File textFile = new File(textFilePath);

if (!textFile.exists()) {
System.out.println("Text file "+textFile.getName()+" does not exist");
usage();
System.exit(ERROR_STATUS);
return ExitCode.ERROR;
}

SpdxToolsHelper.initialize();
String licenseText = null;
try {
licenseText = readAll(textFile);
} catch (IOException e) {
System.out.println("Error reading file: "+e.getMessage());
System.exit(ERROR_STATUS);
return ExitCode.ERROR;
}

String[] matchingLicenseIds = null;
try {
matchingLicenseIds = LicenseCompareHelper.matchingStandardLicenseIds(licenseText);
} catch (InvalidSPDXAnalysisException e) {
System.out.println("Error reading standard licenses: "+e.getMessage());
System.exit(ERROR_STATUS);
return ExitCode.ERROR;
} catch (SpdxCompareException e) {
System.out.println("Error comparing licenses: "+e.getMessage());
System.exit(ERROR_STATUS);
return ExitCode.ERROR;
}

if (matchingLicenseIds == null || matchingLicenseIds.length == 0) {
Expand All @@ -94,7 +118,7 @@ public static void main(String[] args) {
}
System.out.println(sb.toString());
}
System.exit(0);
return ExitCode.SUCCESS;
}

/**
Expand Down
Loading