diff --git a/src/main/java/org/spdx/tools/SpdxConverter.java b/src/main/java/org/spdx/tools/SpdxConverter.java
index 723628b..b1876c9 100644
--- a/src/main/java/org/spdx/tools/SpdxConverter.java
+++ b/src/main/java/org/spdx/tools/SpdxConverter.java
@@ -73,14 +73,13 @@ public static void main(String[] args) {
/**
* Runs the SpdxConverter command logic and reports results to standard
- * out/error, without terminating the JVM - allows the logic to be unit tested.
+ * out/error.
* @param args
* @return process exit status, see {@link ExitCode}
*/
static int run(String[] args) {
SpdxToolsHelper.initialize();
if (args.length < MIN_ARGS) {
-
System.err
.println("Invalid number of arguments");
usage();
@@ -131,7 +130,7 @@ static int run(String[] args) {
}
return ExitCode.SUCCESS;
}
-
+
/**
* Convert an SPDX file from the fromFilePath to a new file at the toFilePath using the file extensions to determine the serialization type
* @param fromFilePath Path of the file to convert from
diff --git a/src/main/java/org/spdx/tools/SpdxViewer.java b/src/main/java/org/spdx/tools/SpdxViewer.java
index cd2c37a..5316c4e 100644
--- a/src/main/java/org/spdx/tools/SpdxViewer.java
+++ b/src/main/java/org/spdx/tools/SpdxViewer.java
@@ -34,6 +34,13 @@
* Simple pretty printer for SPDX RDF XML files. Writes output to System.out.
* Usage: PrettyPrinter SPDXRdfXMLFile > textFile where SPDXRdfXMLFile is a
* valid SPDX RDF XML file
+ *
+ * Exit codes:
+ *
+ * - 0 - the document was pretty-printed successfully
+ * - 1 - the document could not be read, parsed, or printed
+ * - 2 - the command was invoked incorrectly (missing/invalid arguments)
+ *
*
* @author Gary O'Neall
* @version 0.1
@@ -42,21 +49,32 @@ public class SpdxViewer {
static final int MIN_ARGS = 1;
static final int MAX_ARGS = 2;
- static final int ERROR_STATUS = 1;
/**
* Pretty Printer for an SPDX Document
*
+ * Main entry point for the SpdxViewer tool.
+ * Delegates to {@link #run(String[])} and terminates the JVM with its exit status.
* @param args args[0] SPDX file path; args[1] [RDFXML|JSON|XLS|XLSX|YAML|TAG] an optional file type - if not present, file type of the to file will be used
*/
public static void main(String[] args) {
+ System.exit(run(args));
+ }
+
+ /**
+ * Runs the SpdxViewer command logic and reports results to standard
+ * out/error.
+ * @param args args[0] SPDX file path; args[1] [RDFXML|JSON|XLS|XLSX|YAML|TAG] an optional file type - if not present, file type of the to file will be used
+ * @return process exit status, see {@link ExitCode}
+ */
+ static int run(String[] args) {
if (args.length < MIN_ARGS) {
System.err
.println("Usage:\n SPDXViewer file [RDFXML|JSON|XLS|XLSX|YAML|TAG] \n"
+ "where file is the file path to a valid SPDX file\n"
+ "and [RDFXML|JSON|XLS|XLSX|YAML|TAG|JSONLD] is an optional file type\n"
+ "if not present, file type of the to file will be used");
- return;
+ return ExitCode.USAGE_ERROR;
}
if (args.length > MAX_ARGS) {
System.out.printf("Warning: Extra arguments will be ignored");
@@ -79,7 +97,7 @@ public static void main(String[] args) {
fileType = SpdxToolsHelper.strToFileType(args[1]);
} catch (Exception ex) {
System.err.println("Invalid file type: "+args[1]);
- System.exit(ERROR_STATUS);
+ return ExitCode.USAGE_ERROR;
}
} else {
fileType = SpdxToolsHelper.fileToFileType(file);
@@ -89,13 +107,13 @@ public static void main(String[] args) {
} catch (InvalidSPDXAnalysisException e) {
throw new SpdxVerificationException("Error converting fileType to store",e);
}
-
+
try {
doc = SpdxToolsHelper.readDocumentFromFileCompatV2(store, file);
} catch (Exception ex) {
System.out
.print("Error creating SPDX Document: " + ex.getMessage());
- return;
+ return ExitCode.ERROR;
}
writer = new PrintWriter(System.out);
List verify = doc.verify();
@@ -113,10 +131,11 @@ public static void main(String[] args) {
} catch (InvalidSPDXAnalysisException e) {
System.out.print("Error pretty printing SPDX Document: "
+ e.getMessage());
- return;
+ return ExitCode.ERROR;
} catch (Exception e) {
System.out.print("Unexpected error displaying SPDX Document: "
+ e.getMessage());
+ return ExitCode.ERROR;
} finally {
if (Objects.nonNull(writer)) {
writer.close();
@@ -129,5 +148,6 @@ public static void main(String[] args) {
}
}
}
+ return ExitCode.SUCCESS;
}
}