From a7f0c56ea055740c873845d160abff89191a15ec Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Mon, 20 Jul 2026 03:30:46 +0100 Subject: [PATCH 1/6] Extract run() Signed-off-by: Arthit Suriyawongkul --- src/main/java/org/spdx/tools/SpdxViewer.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/spdx/tools/SpdxViewer.java b/src/main/java/org/spdx/tools/SpdxViewer.java index cd2c37a..61ae591 100644 --- a/src/main/java/org/spdx/tools/SpdxViewer.java +++ b/src/main/java/org/spdx/tools/SpdxViewer.java @@ -50,13 +50,23 @@ public class SpdxViewer { * @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, without terminating the JVM - allows the logic to be unit tested. + * @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 + */ + 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 0; } if (args.length > MAX_ARGS) { System.out.printf("Warning: Extra arguments will be ignored"); @@ -79,7 +89,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 ERROR_STATUS; } } else { fileType = SpdxToolsHelper.fileToFileType(file); @@ -95,7 +105,7 @@ public static void main(String[] args) { } catch (Exception ex) { System.out .print("Error creating SPDX Document: " + ex.getMessage()); - return; + return 0; } writer = new PrintWriter(System.out); List verify = doc.verify(); @@ -113,7 +123,7 @@ public static void main(String[] args) { } catch (InvalidSPDXAnalysisException e) { System.out.print("Error pretty printing SPDX Document: " + e.getMessage()); - return; + return 0; } catch (Exception e) { System.out.print("Unexpected error displaying SPDX Document: " + e.getMessage()); @@ -129,5 +139,6 @@ public static void main(String[] args) { } } } + return 0; } } From 656e5677f3ef8f8ea7ea1ed11bb30e624ef18fec Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Mon, 20 Jul 2026 03:32:15 +0100 Subject: [PATCH 2/6] Return errors at unsuccessful Signed-off-by: Arthit Suriyawongkul --- src/main/java/org/spdx/tools/SpdxViewer.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/spdx/tools/SpdxViewer.java b/src/main/java/org/spdx/tools/SpdxViewer.java index 61ae591..b0cc878 100644 --- a/src/main/java/org/spdx/tools/SpdxViewer.java +++ b/src/main/java/org/spdx/tools/SpdxViewer.java @@ -66,7 +66,7 @@ static int run(String[] args) { + "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 0; + return ERROR_STATUS; } if (args.length > MAX_ARGS) { System.out.printf("Warning: Extra arguments will be ignored"); @@ -105,7 +105,7 @@ static int run(String[] args) { } catch (Exception ex) { System.out .print("Error creating SPDX Document: " + ex.getMessage()); - return 0; + return ERROR_STATUS; } writer = new PrintWriter(System.out); List verify = doc.verify(); @@ -123,10 +123,11 @@ static int run(String[] args) { } catch (InvalidSPDXAnalysisException e) { System.out.print("Error pretty printing SPDX Document: " + e.getMessage()); - return 0; + return ERROR_STATUS; } catch (Exception e) { System.out.print("Unexpected error displaying SPDX Document: " + e.getMessage()); + return ERROR_STATUS; } finally { if (Objects.nonNull(writer)) { writer.close(); From bfb061d8b5f74834fe1e45dac5f832720c28845c Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Mon, 20 Jul 2026 03:34:21 +0100 Subject: [PATCH 3/6] Use new exit code Signed-off-by: Arthit Suriyawongkul --- src/main/java/org/spdx/tools/SpdxViewer.java | 26 ++++++++++++-------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/spdx/tools/SpdxViewer.java b/src/main/java/org/spdx/tools/SpdxViewer.java index b0cc878..40231c1 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,11 +49,10 @@ 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) { @@ -57,7 +63,7 @@ public static void main(String[] args) { * Runs the SpdxViewer command logic and reports results to standard * out/error, without terminating the JVM - allows the logic to be unit tested. * @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 + * @return process exit status, see {@link ExitCode} */ static int run(String[] args) { if (args.length < MIN_ARGS) { @@ -66,7 +72,7 @@ static int run(String[] args) { + "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 ERROR_STATUS; + return ExitCode.USAGE_ERROR; } if (args.length > MAX_ARGS) { System.out.printf("Warning: Extra arguments will be ignored"); @@ -89,7 +95,7 @@ static int run(String[] args) { fileType = SpdxToolsHelper.strToFileType(args[1]); } catch (Exception ex) { System.err.println("Invalid file type: "+args[1]); - return ERROR_STATUS; + return ExitCode.USAGE_ERROR; } } else { fileType = SpdxToolsHelper.fileToFileType(file); @@ -105,7 +111,7 @@ static int run(String[] args) { } catch (Exception ex) { System.out .print("Error creating SPDX Document: " + ex.getMessage()); - return ERROR_STATUS; + return ExitCode.ERROR; } writer = new PrintWriter(System.out); List verify = doc.verify(); @@ -123,11 +129,11 @@ static int run(String[] args) { } catch (InvalidSPDXAnalysisException e) { System.out.print("Error pretty printing SPDX Document: " + e.getMessage()); - return ERROR_STATUS; + return ExitCode.ERROR; } catch (Exception e) { System.out.print("Unexpected error displaying SPDX Document: " + e.getMessage()); - return ERROR_STATUS; + return ExitCode.ERROR; } finally { if (Objects.nonNull(writer)) { writer.close(); @@ -140,6 +146,6 @@ static int run(String[] args) { } } } - return 0; + return ExitCode.SUCCESS; } } From f1bb97394d68dad669eadfe021ae659a3123ebd6 Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Mon, 20 Jul 2026 03:41:51 +0100 Subject: [PATCH 4/6] Shorten comment Signed-off-by: Arthit Suriyawongkul --- src/main/java/org/spdx/tools/SpdxConverter.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 From 2dcaff902d4e2d6c0807ff069dbfdf4224d73efa Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Mon, 20 Jul 2026 18:53:44 +0100 Subject: [PATCH 5/6] Shorten comments Signed-off-by: Arthit Suriyawongkul --- src/main/java/org/spdx/tools/SpdxViewer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/spdx/tools/SpdxViewer.java b/src/main/java/org/spdx/tools/SpdxViewer.java index 40231c1..2b61c1b 100644 --- a/src/main/java/org/spdx/tools/SpdxViewer.java +++ b/src/main/java/org/spdx/tools/SpdxViewer.java @@ -61,7 +61,7 @@ public static void main(String[] args) { /** * Runs the SpdxViewer command logic and reports results to standard - * out/error, without terminating the JVM - allows the logic to be unit tested. + * 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} */ @@ -105,7 +105,7 @@ static int run(String[] args) { } catch (InvalidSPDXAnalysisException e) { throw new SpdxVerificationException("Error converting fileType to store",e); } - + try { doc = SpdxToolsHelper.readDocumentFromFileCompatV2(store, file); } catch (Exception ex) { From b16e2f294df3ab7eb68356ac122ee894d531b446 Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Mon, 20 Jul 2026 18:56:07 +0100 Subject: [PATCH 6/6] Restore original comment Signed-off-by: Arthit Suriyawongkul --- src/main/java/org/spdx/tools/SpdxViewer.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/spdx/tools/SpdxViewer.java b/src/main/java/org/spdx/tools/SpdxViewer.java index 2b61c1b..5316c4e 100644 --- a/src/main/java/org/spdx/tools/SpdxViewer.java +++ b/src/main/java/org/spdx/tools/SpdxViewer.java @@ -51,6 +51,8 @@ public class SpdxViewer { static final int MAX_ARGS = 2; /** + * 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