diff --git a/src/org/spdx/tools/CompareMultpleSpdxDocs.java b/src/org/spdx/tools/CompareMultpleSpdxDocs.java index 9c5a97a4..f5649c39 100644 --- a/src/org/spdx/tools/CompareMultpleSpdxDocs.java +++ b/src/org/spdx/tools/CompareMultpleSpdxDocs.java @@ -57,11 +57,25 @@ public static void main(String[] args) { usage(); System.exit(ERROR_STATUS); } + try { + onlineFunction(args); + } catch (OnlineToolException e){ + System.out.println(e.getMessage()); + System.exit(ERROR_STATUS); + } + } + + /** + * + * @param args args[0] is the output Excel file name, all other args are SPDX document file names + * @throws OnlineToolException Exception caught by JPype and displayed to the user + */ + public static void onlineFunction(String[] args) throws OnlineToolException{ + // Arguments length( 14>=args length>=3 ) will checked in the Python Code File outputFile = new File(args[0]); + // Output File name will be checked in the Python code for no clash, but if still found if (outputFile.exists()) { - System.out.println("Output file "+args[0]+" already exists."); - usage(); - System.exit(ERROR_STATUS); + throw new OnlineToolException("Output file "+args[0]+" already exists. Change the name of the result file."); } SpdxDocument[] compareDocs = new SpdxDocument[args.length-1]; String[] docNames = new String[args.length-1]; @@ -80,8 +94,7 @@ public static void main(String[] args) { System.out.println("Warning: "+docNames[i-1]+" contains verification errors."); } } catch (SpdxCompareException e) { - System.out.println("Error opening SPDX document "+args[i]+": "+e.getMessage()); - System.exit(ERROR_STATUS); + throw new OnlineToolException("Error opening SPDX document "+args[i]+": "+e.getMessage()); } } MultiDocumentSpreadsheet outSheet = null; @@ -92,26 +105,22 @@ public static void main(String[] args) { comparer.compare(compareDocs); outSheet.importCompareResults(comparer, docNames); } catch (SpreadsheetException e) { - System.out.println("Unable to create output spreadsheet: "+e.getMessage()); - System.exit(ERROR_STATUS); + throw new OnlineToolException("Unable to create output spreadsheet: "+e.getMessage()); } catch (InvalidSPDXAnalysisException e) { - System.out.println("Invalid SPDX analysis: "+e.getMessage()); - System.exit(ERROR_STATUS); + throw new OnlineToolException("Invalid SPDX analysis: "+e.getMessage()); } catch (SpdxCompareException e) { - System.out.println("Error comparing SPDX documents: "+e.getMessage()); - System.exit(ERROR_STATUS); + throw new OnlineToolException("Error comparing SPDX documents: "+e.getMessage()); } finally { if (outSheet != null) { try { outSheet.close(); } catch (SpreadsheetException e) { - System.out.println("Warning - error closing spreadsheet: "+e.getMessage()); + throw new OnlineToolException("Warning - error closing spreadsheet: "+e.getMessage()); } } } } - - + /** * */ diff --git a/src/org/spdx/tools/OnlineToolException.java b/src/org/spdx/tools/OnlineToolException.java new file mode 100644 index 00000000..4fa2efec --- /dev/null +++ b/src/org/spdx/tools/OnlineToolException.java @@ -0,0 +1,70 @@ +/** + * Copyright (c) 2017 Source Auditor Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ +package org.spdx.tools; + +/** + * Default Exception thrown to the Online Tool + * + * @author Rohit Lodha + * + */ + +public class OnlineToolException extends Exception { + + /** + * + */ + public OnlineToolException() { + } + + /** + * + * @param arg0 + */ + public OnlineToolException(String arg0) { + super(arg0); + } + + /** + * + * @param arg0 + */ + public OnlineToolException(Throwable arg0) { + super(arg0); + } + + /** + * + * @param arg0 + * @param arg1 + */ + public OnlineToolException(String arg0, Throwable arg1) { + super(arg0, arg1); + } + + /** + * + * @param arg0 + * @param arg1 + * @param arg2 + * @param arg3 + */ + public OnlineToolException(String arg0, Throwable arg1, boolean arg2, boolean arg3) { + super(arg0, arg1, arg2, arg3); + } + +} diff --git a/src/org/spdx/tools/RdfToHtml.java b/src/org/spdx/tools/RdfToHtml.java index 19ef5570..8450692a 100644 --- a/src/org/spdx/tools/RdfToHtml.java +++ b/src/org/spdx/tools/RdfToHtml.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -103,35 +104,55 @@ public static void main(String[] args) { if (args.length > MAX_ARGS) { System.out.println("Warning: Extra arguments will be ignored"); usage(); - } + } + try { + onlineFunction(args); + } catch (OnlineToolException e){ + System.out.println(e.getMessage()); + System.exit(ERROR); + } + } + + /** + * + * @param args args[0] is the RDF file to be converted, args[1] is the result HTML file name + * @throws OnlineToolException Exception caught by JPype and displayed to the user + * + */ + public static List onlineFunction(String[] args) throws OnlineToolException{ + // Arguments length(args length== 2 ) will checked in the Python Code File spdxFile = new File(args[0]); + // Output File name will be checked in the Python code for no clash, but if still found if (!spdxFile.exists()) { - System.out.println("SPDX File "+args[0]+" does not exist."); - usage(); - System.exit(ERROR); + throw new OnlineToolException("SPDX file " + args[0] +" does not exists."); } if (!spdxFile.canRead()) { - System.out.println("Can not read SPDX File "+args[0]+". Check permissions on the file."); - usage(); - System.exit(ERROR); + throw new OnlineToolException("Can not read SPDX File "+args[0]+". Check permissions on the file."); } File outputDirectory = new File(args[1]); if (!outputDirectory.exists()) { if (!outputDirectory.mkdirs()) { - System.out.println("Unable to create output directory"); - System.exit(ERROR); + throw new OnlineToolException("Unable to create output directory"); } } SpdxDocument doc = null; try { doc = SPDXDocumentFactory.createSpdxDocument(args[0]); } catch (IOException e2) { - System.out.println("IO Error creating the SPDX document"); - System.exit(ERROR); + throw new OnlineToolException("IO Error creating the SPDX document"); } catch (InvalidSPDXAnalysisException e2) { - System.out.println("Invalid SPDX Document: "+e2.getMessage()); - System.exit(ERROR); + throw new OnlineToolException("Invalid SPDX Document: "+e2.getMessage()); + } catch (Exception e) { + throw new OnlineToolException("Error creating SPDX Document: "+e.getMessage(),e); } + List verify = new ArrayList(); + verify = doc.verify(); + if (verify != null && verify.size() > 0) { + System.out.println("Warning: The following verifications failed for the resultant SPDX RDF file:"); + for (int i = 0; i < verify.size(); i++) { + System.out.println("\t" + verify.get(i)); + } + } String documentName = doc.getName(); List filesToCreate = Lists.newArrayList(); String docHtmlFilePath = outputDirectory.getPath() + File.separator + documentName + DOC_HTML_FILE_POSTFIX; @@ -150,8 +171,7 @@ public static void main(String[] args) { try { pkgs = doc.getDocumentContainer().findAllPackages(); } catch (InvalidSPDXAnalysisException e1) { - System.out.println("Error getting packages from the SPDX document: "+e1.getMessage()); - System.exit(ERROR); + throw new OnlineToolException("Error getting packages from the SPDX document: "+e1.getMessage()); } Iterator iter = pkgs.iterator(); while (iter.hasNext()) { @@ -169,35 +189,29 @@ public static void main(String[] args) { while (fileIter.hasNext()) { File file = fileIter.next(); if (file.exists()) { - System.out.println("File "+file.getName()+" already exists."); - System.exit(ERROR); + throw new OnlineToolException("File "+file.getName()+" already exists."); } } Writer writer = null; try { rdfToHtml(doc, docHtmlFile, licenseHtmlFile, snippetHtmlFile, docFilesHtmlFile); } catch (IOException e) { - System.out.println("IO Error opening SPDX Document"); - usage(); - System.exit(ERROR); + throw new OnlineToolException("IO Error opening SPDX Document"); } catch (InvalidSPDXAnalysisException e) { - System.out.println("Invalid SPDX Document: "+e.getMessage()); - usage(); - System.exit(ERROR); + throw new OnlineToolException("Invalid SPDX Document: "+e.getMessage()); } catch (MustacheException e) { - System.out.println("Unexpected error reading the HTML template: "+e.getMessage()); - usage(); - System.exit(ERROR); + throw new OnlineToolException("Unexpected error reading the HTML template: "+e.getMessage()); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { - System.out.println("Warning: error closing HTML file: "+e.getMessage()); + throw new OnlineToolException("Warning: error closing HTML file: "+e.getMessage()); } writer = null; } } + return verify; } /** diff --git a/src/org/spdx/tools/RdfToSpreadsheet.java b/src/org/spdx/tools/RdfToSpreadsheet.java index fe28e2a0..223adce8 100644 --- a/src/org/spdx/tools/RdfToSpreadsheet.java +++ b/src/org/spdx/tools/RdfToSpreadsheet.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; @@ -85,31 +86,49 @@ public static void main(String[] args) { usage(); return; } + try { + onlineFunction(args); + } catch (OnlineToolException e){ + System.out.println(e.getMessage()); + usage(); + return; + } + } + + /** + * + * @param args args[0] is the RDF file to be converted, args[1] is the result HTML file name + * @throws OnlineToolException Exception caught by JPype and displayed to the user + * @return Warnings of the conversion, displayed to the user + */ + public static List onlineFunction(String[] args) throws OnlineToolException{ + // Arguments length(args length== 2 ) will checked in the Python Code File spdxRdfFile = new File(args[0]); + // Output File name will be checked in the Python code for no clash, but if still found if (!spdxRdfFile.exists()) { - System.out.printf("Error: File %1$s does not exist.%n", args[0]); - return; + throw new OnlineToolException("Error: File " + args[0] + " does not exist."); } File spdxSpreadsheetFile = new File(args[1]); if (spdxSpreadsheetFile.exists()) { - System.out.println("Spreadsheet file already exists - please specify a new file."); - return; + throw new OnlineToolException("Spreadsheet file already exists - please specify a new file."); } SpdxDocument doc = null; try { doc = SPDXDocumentFactory.createSpdxDocument(args[0]); } catch (InvalidSPDXAnalysisException ex) { - System.out.print("Error creating SPDX Document: "+ex.getMessage()); - return; + throw new OnlineToolException("Error creating SPDX Document: "+ex.getMessage()); } catch (IOException e) { - System.out.print("Unable to open file :"+args[0]+", "+e.getMessage()); + throw new OnlineToolException("Error creating SPDX Document:"+args[0]+", "+e.getMessage()); + } catch (Exception e) { + throw new OnlineToolException("Error creating SPDX Document: "+e.getMessage(),e); } + List verify = new ArrayList(); if (doc != null) { SPDXSpreadsheet ss = null; try { ss = new SPDXSpreadsheet(spdxSpreadsheetFile, true, false); copyRdfXmlToSpreadsheet(doc, ss); - List verify = doc.verify(); + verify = doc.verify(); if (verify != null && verify.size() > 0) { System.out.println("Warning: The following verifications failed for the resultant SPDX RDF file:"); for (int i = 0; i < verify.size(); i++) { @@ -117,25 +136,26 @@ public static void main(String[] args) { } } } catch (SpreadsheetException e) { - System.out.println("Error opening or writing to spreadsheet: " + e.getMessage()); + throw new OnlineToolException("Error opening or writing to spreadsheet: " + e.getMessage()); } catch (InvalidSPDXAnalysisException e) { - System.out.println("Error translating the RDF file: " + e.getMessage()); + throw new OnlineToolException("Error translating the RDF file: " + e.getMessage()); } catch (Exception ex) { - System.out.println("Unexpected error translating the RDF to spreadsheet: " + ex.getMessage()); + throw new OnlineToolException("Unexpected error translating the RDF to spreadsheet: " + ex.getMessage()); } finally { if (ss != null) { try { ss.close(); } catch (SpreadsheetException e) { - System.out.println("Error closing spreadsheet: " + e.getMessage()); + throw new OnlineToolException("Error closing spreadsheet: " + e.getMessage()); } } } }else{ - System.out.println("Error creating SPDX document reference, null reference returned"); + throw new OnlineToolException("Error creating SPDX document reference, null reference returned"); } - } - + return verify; + } + @SuppressWarnings("deprecation") public static void copyRdfXmlToSpreadsheet(SpdxDocument doc, SPDXSpreadsheet ss) throws InvalidSPDXAnalysisException, SpreadsheetException { diff --git a/src/org/spdx/tools/RdfToTag.java b/src/org/spdx/tools/RdfToTag.java index eb6b0ece..3cca7aa8 100644 --- a/src/org/spdx/tools/RdfToTag.java +++ b/src/org/spdx/tools/RdfToTag.java @@ -69,53 +69,63 @@ public static void main(String[] args) { System.out.printf("Warning: Extra arguments will be ignored%n"); usage(); } + try { + onlineFunction(args); + } catch (OnlineToolException e){ + System.out.println(e.getMessage()); + usage(); + return; + } + } + + /** + * + * @param args args[0] is the RDF file to be converted, args[1] is the result Tag file name + * @throws OnlineToolException Exception caught by JPype and displayed to the user + * @return Warnings of the conversion, displayed to the user + */ + public static List onlineFunction(String[] args) throws OnlineToolException{ + // Arguments length(args length== 2 ) will checked in the Python Code File spdxRdfFile = new File(args[0]); + // Output File name will be checked in the Python code for no clash, but if still found if (!spdxRdfFile.exists()) { - System.out.printf("RDF file %1$s does not exists.%n", args[0]); - return; + throw new OnlineToolException("RDF file " + args[0] +" does not exists."); } File spdxTagFile = new File(args[1]); if (spdxTagFile.exists()) { - System.out - .printf("Error: File %1$s already exists - please specify a new file.%n", - args[1]); - return; + throw new OnlineToolException("Error: File " +args[1] +" already exists - please specify a new file."); } try { if (!spdxTagFile.createNewFile()) { - System.out.println("Could not create the new SPDX Tag file " + throw new OnlineToolException("Could not create the new SPDX Tag file " + args[1]); - usage(); - return; } } catch (IOException e1) { - System.out.println("Could not create the new SPDX Tag file " - + args[1]); - System.out.println("due to error " + e1.getMessage()); - usage(); - return; + throw new OnlineToolException("Could not create the new SPDX Tag file " + args[1] + "due to error " + e1.getMessage()); } PrintWriter out = null; + List verify = new LinkedList(); try { try { out = new PrintWriter(spdxTagFile, "UTF-8"); } catch (IOException e1) { - System.out.println("Could not write to the new SPDX Tag file " - + args[1]); - System.out.println("due to error " + e1.getMessage()); - usage(); - return; + throw new OnlineToolException("Could not write to the new SPDX Tag file " + + args[1] + "due to error " + e1.getMessage()); + } SpdxDocument doc = null; try { doc = SPDXDocumentFactory.createSpdxDocument(args[0]); - } catch (Exception ex) { - System.out.print("Error creating SPDX Document: " - + ex.getMessage()); - return; + } catch (InvalidSPDXAnalysisException ex) { + throw new OnlineToolException("Error creating SPDX Document: "+ex.getMessage()); + } catch (IOException e) { + throw new OnlineToolException("Unable to open file :"+args[0]+", "+e.getMessage()); + } catch (Exception e) { + throw new OnlineToolException("Error creating SPDX Document: "+e.getMessage(),e); } + try { - List verify = new LinkedList(); // doc.verify(); + verify = doc.verify(); if (verify.size() > 0) { System.out .println("This SPDX Document is not valid due to:"); @@ -129,12 +139,10 @@ public static void main(String[] args) { // print document to a file using tag-value format CommonCode.printDoc(doc, out, constants); } catch (InvalidSPDXAnalysisException e) { - System.out - .print("Error transalting SPDX Document to tag-value format: " - + e.getMessage()); - return; + throw new OnlineToolException("Error transalting SPDX Document to tag-value format: " + + e.getMessage()); } catch (Exception e) { - System.out.print("Unexpected error displaying SPDX Document: " + throw new OnlineToolException("Unexpected error displaying SPDX Document: " + e.getMessage()); } } finally { @@ -143,8 +151,9 @@ public static void main(String[] args) { out.close(); } } + return verify; } - + private static void usage() { System.out .println("Usage: RdfToTag rdfxmlfile.rdf spdxfile.spdx\n" diff --git a/src/org/spdx/tools/SpreadsheetToRDF.java b/src/org/spdx/tools/SpreadsheetToRDF.java index 93217f75..c3cd99b1 100644 --- a/src/org/spdx/tools/SpreadsheetToRDF.java +++ b/src/org/spdx/tools/SpreadsheetToRDF.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; @@ -92,44 +93,53 @@ public static void main(String[] args) { usage(); return; } + try { + onlineFunction(args); + } catch (OnlineToolException e){ + System.out.println(e.getMessage()); + usage(); + return; + } + } + + /** + * + * @param args args[0] is the Spreadsheet file to be converted, args[1] is the result RDF file name + * @throws OnlineToolException Exception caught by JPype and displayed to the user + * @return Warnings of the conversion, displayed to the user + */ + public static List onlineFunction(String[] args) throws OnlineToolException{ + // Arguments length(args length== 2 ) will checked in the Python Code File spdxSpreadsheetFile = new File(args[0]); if (!spdxSpreadsheetFile.exists()) { - System.out.printf("Spreadsheet file %1$s does not exists.%n", args[0]); - return; + throw new OnlineToolException("Spreadsheet file " + "does not exists."); } File spdxRdfFile = new File(args[1]); + // Output File name will be checked in the Python code for no clash, but if still found if (spdxRdfFile.exists()) { - System.out.printf("Error: File %1$s already exists - please specify a new file.%n", args[1]); - return; + throw new OnlineToolException("Error: File " + args[1] + " already exists - please specify a new file."); } try { if (!spdxRdfFile.createNewFile()) { - System.out.println("Could not create the new SPDX RDF file "+args[1]); - usage(); - return; + throw new OnlineToolException("Could not create the new SPDX RDF file "+args[1]); } } catch (IOException e1) { - System.out.println("Could not create the new SPDX RDF file "+args[1]); - System.out.println("due to error "+e1.getMessage()); - usage(); - return; + throw new OnlineToolException("Could not create the new SPDX RDF file "+args[1] + "due to error "+e1.getMessage() ); } FileOutputStream out; try { out = new FileOutputStream(spdxRdfFile); } catch (FileNotFoundException e1) { - System.out.println("Could not write to the new SPDX RDF file "+args[1]); - System.out.println("due to error "+e1.getMessage()); - usage(); - return; + throw new OnlineToolException("Could not write to the new SPDX RDF file "+args[1] +"due to error "+e1.getMessage() ); } SPDXSpreadsheet ss = null; + List verify = new ArrayList(); try { ss = new SPDXSpreadsheet(spdxSpreadsheetFile, false, true); SpdxDocument analysis = copySpreadsheetToSPDXAnalysis(ss); - List verify = analysis.verify(); + verify = analysis.verify(); if (verify.size() > 0) { System.out.println("Warning: The following verification errors were found in the resultant SPDX Document:"); for (int i = 0; i < verify.size(); i++) { @@ -138,27 +148,27 @@ public static void main(String[] args) { } analysis.getDocumentContainer().getModel().write(out, "RDF/XML-ABBREV"); } catch (SpreadsheetException e) { - System.out.println("Error creating or writing to spreadsheet: "+e.getMessage()); + throw new OnlineToolException("Error creating or writing to spreadsheet: "+e.getMessage()); } catch (InvalidSPDXAnalysisException e) { - System.out.println("Error translating the RDF file: "+e.getMessage()); + throw new OnlineToolException("Error translating the RDF file: "+e.getMessage()); } finally { if (ss != null) { try { ss.close(); } catch (SpreadsheetException e) { - System.out.println("Error closing spreadsheet: "+e.getMessage()); + throw new OnlineToolException("Error closing spreadsheet: "+e.getMessage()); } } if (out != null) { try { out.close(); } catch (IOException e) { - System.out.println("Error closing RDF file: "+e.getMessage()); + throw new OnlineToolException("Error closing RDF file: "+e.getMessage()); } } } + return verify; } - public static SpdxDocument copySpreadsheetToSPDXAnalysis(SPDXSpreadsheet ss) throws SpreadsheetException, InvalidSPDXAnalysisException { String pkgUrl = ss.getOriginsSheet().getNamespace() + "#" + SpdxRdfConstants.SPDX_DOCUMENT_ID; if (!SpdxVerificationHelper.isValidUri(pkgUrl)) { diff --git a/src/org/spdx/tools/SpreadsheetToTag.java b/src/org/spdx/tools/SpreadsheetToTag.java index 79ded502..0000ae17 100644 --- a/src/org/spdx/tools/SpreadsheetToTag.java +++ b/src/org/spdx/tools/SpreadsheetToTag.java @@ -21,6 +21,7 @@ import java.io.PrintWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -59,51 +60,57 @@ public static void main(String[] args) { usage(); return; } + try { + onlineFunction(args); + } catch (OnlineToolException e){ + System.out.println(e.getMessage()); + usage(); + return; + } + } + + /** + * + * @param args args[0] is the Spreadsheet file to be converted, args[1] is the result Tag Value file name + * @throws OnlineToolException Exception caught by JPype and displayed to the user + * @return Warnings of the conversion, displayed to the user + */ + public static List onlineFunction(String[] args) throws OnlineToolException{ + // Arguments length(args length== 2 ) will checked in the Python Code File spdxSpreadsheetFile = new File(args[0]); if (!spdxSpreadsheetFile.exists()) { - System.out.printf("Spreadsheet file %1$s does not exists.%n", - args[0]); - return; + throw new OnlineToolException("Spreadsheet file " + args[0] + " does not exists."); } File spdxTagFile = new File(args[1]); + // Output File name will be checked in the Python code for no clash, but if still found if (spdxTagFile.exists()) { - System.out - .printf("Error: File %1$s already exists - please specify a new file.%n", - args[1]); - return; + throw new OnlineToolException("Error: File " + args[1] +" already exists - please specify a new file."); } try { if (!spdxTagFile.createNewFile()) { - System.out.println("Could not create the new SPDX Tag file " + throw new OnlineToolException("Could not create the new SPDX Tag file " + args[1]); - usage(); - return; } } catch (IOException e1) { - System.out.println("Could not create the new SPDX Tag file " - + args[1]); - System.out.println("due to error " + e1.getMessage()); - usage(); - return; + throw new OnlineToolException("Could not create the new SPDX Tag file " + + args[1] + "due to error " + e1.getMessage()); } PrintWriter out = null; + List verify = new ArrayList(); try { try { out = new PrintWriter(spdxTagFile, "UTF-8"); } catch (IOException e1) { - System.out.println("Could not write to the new SPDX Tag file " - + args[1]); - System.out.println("due to error " + e1.getMessage()); - usage(); - return; + throw new OnlineToolException("Could not write to the new SPDX Tag file " + + args[1] + "due to error " + e1.getMessage()); } SPDXSpreadsheet ss = null; try { ss = new SPDXSpreadsheet(spdxSpreadsheetFile, false, true); SpdxDocument analysis = SpreadsheetToRDF.copySpreadsheetToSPDXAnalysis(ss); - List verify = analysis.verify(); + verify = analysis.verify(); if (verify.size() > 0) { System.out .println("Warning: The following verification errors were found in the resultant SPDX Document:"); @@ -116,20 +123,20 @@ public static void main(String[] args) { .getTextFromProperties("org/spdx/tag/SpdxTagValueConstants.properties"); CommonCode.printDoc(analysis, out, constants); } catch (SpreadsheetException e) { - System.out.println("Error creating or writing to spreadsheet: " + throw new OnlineToolException("Error creating or writing to spreadsheet: " + e.getMessage()); } catch (InvalidSPDXAnalysisException e) { - System.out.println("Error translating the Tag file: " + throw new OnlineToolException("Error translating the Tag file: " + e.getMessage()); } catch (Exception e) { - System.out.print("Unexpected error converting SPDX Document: " + throw new OnlineToolException("Unexpected error converting SPDX Document: " + e.getMessage()); } finally { if (ss != null) { try { ss.close(); } catch (SpreadsheetException e) { - System.out.println("Error closing spreadsheet: " + throw new OnlineToolException("Error closing spreadsheet: " + e.getMessage()); } } @@ -140,8 +147,8 @@ public static void main(String[] args) { out.close(); } } + return verify; } - private static void usage() { System.out .println("Usage: SpreadsheetToTag spreadsheetfile.xls spdxfile.spdx \n" diff --git a/src/org/spdx/tools/TagToRDF.java b/src/org/spdx/tools/TagToRDF.java index 58bd1caf..f579f67b 100644 --- a/src/org/spdx/tools/TagToRDF.java +++ b/src/org/spdx/tools/TagToRDF.java @@ -86,71 +86,72 @@ public static void main(String[] args) { return; } } + try { + onlineFunction(args); + } catch (OnlineToolException e){ + System.out.println(e.getMessage()); + usage(); + return; + } + } + + /** + * + * @param args args[0] is the Tag Value file to be converted, args[1] is the result RDF file name + * @throws OnlineToolException Exception caught by JPype and displayed to the user + * @return Warnings of the conversion, displayed to the user + */ + public static List onlineFunction(String[] args) throws OnlineToolException{ + // Arguments length(args length== 2 ) will checked in the Python Code + String outputFormat = DEFAULT_OUTPUT_FORMAT; FileInputStream spdxTagStream; try { spdxTagStream = new FileInputStream(args[0]); } catch (FileNotFoundException ex) { - System.out - .printf("Tag-Value file %1$s does not exists.%n", args[0]); - return; + throw new OnlineToolException("Tag-Value file "+ args[0] + " does not exists."); } - File spdxRDFFile = new File(args[1]); + // Output File name will be checked in the Python code for no clash, but if still found if (spdxRDFFile.exists()) { - System.out - .printf("Error: File %1$s already exists - please specify a new file.%n", - args[1]); try { spdxTagStream.close(); } catch (IOException e) { - System.out.println("Warning: Unable to close input file on error."); + throw new OnlineToolException("Warning: Unable to close input file on error."); } - return; + throw new OnlineToolException("Error: File " + args[1] +" already exists - please specify a new file."); } try { if (!spdxRDFFile.createNewFile()) { - System.out.println("Could not create the new SPDX RDF file " - + args[1]); - usage(); try { spdxTagStream.close(); } catch (IOException e) { - System.out.println("Warning: Unable to close input file on error."); + throw new OnlineToolException("Warning: Unable to close input file on error."); } - return; + throw new OnlineToolException("Could not create the new SPDX RDF file "+ args[1]); } } catch (IOException e1) { - System.out.println("Could not create the new SPDX Tag-Value file " - + args[1]); - System.out.println("due to error " + e1.getMessage()); - usage(); try { spdxTagStream.close(); } catch (IOException e) { - System.out.println("Warning: Unable to close input file on error."); + throw new OnlineToolException("Warning: Unable to close input file on error."); } - return; + throw new OnlineToolException("Could not create the new SPDX Tag-Value file "+ args[1] + "due to error " + e1.getMessage()); } FileOutputStream out; try { out = new FileOutputStream(spdxRDFFile); } catch (FileNotFoundException e1) { - System.out.println("Could not write to the new SPDX RDF file " - + args[1]); - System.out.println("due to error " + e1.getMessage()); - usage(); try { spdxTagStream.close(); } catch (IOException e) { - System.out.println("Warning: Unable to close input file on error."); + throw new OnlineToolException("Warning: Unable to close input file on error."); } - return; + throw new OnlineToolException("Could not write to the new SPDX RDF file "+ args[1]+ "due to error " + e1.getMessage()); } - + List warnings = new ArrayList(); try { - List warnings = new ArrayList(); convertTagFileToRdf(spdxTagStream, out, outputFormat, warnings); if (!warnings.isEmpty()) { System.out.println("The following warnings and or verification errors were found:"); @@ -159,25 +160,25 @@ public static void main(String[] args) { } } } catch (Exception e) { - System.err.println("Error creating SPDX Analysis: " + e); + throw new OnlineToolException("Error creating SPDX Analysis: " + e.getMessage()); } finally { if (out != null) { try { out.close(); } catch (IOException e) { - System.out.println("Error closing RDF file: " + e.getMessage()); + throw new OnlineToolException("Error closing RDF file: " + e.getMessage()); } } if (spdxTagStream != null) { try { spdxTagStream.close(); } catch (IOException e) { - System.out.println("Error closing Tag/Value file: " + e.getMessage()); + throw new OnlineToolException("Error closing Tag/Value file: " + e.getMessage()); } } } + return warnings; } - /** * Convert a Tag File to an RDF output stream * @param spdxTagFile File containing a tag/value formatted SPDX file diff --git a/src/org/spdx/tools/TagToSpreadsheet.java b/src/org/spdx/tools/TagToSpreadsheet.java index 77c0bfdd..1f2725f5 100644 --- a/src/org/spdx/tools/TagToSpreadsheet.java +++ b/src/org/spdx/tools/TagToSpreadsheet.java @@ -32,7 +32,11 @@ import org.spdx.tag.BuildDocument; import org.spdx.tag.CommonCode; import org.spdx.tag.HandBuiltParser; +import org.spdx.tag.InvalidFileFormatException; +import org.spdx.tag.InvalidSpdxTagFileException; import org.spdx.tag.NoCommentInputStream; + +import antlr.RecognitionException; /** * Translates an tag-value file to a SPDX Spreadsheet format Usage: * TagToSpreadsheet spdxfile.spdx spreadsheetfile.xls where spdxfile.spdx is a @@ -58,69 +62,90 @@ public static void main(String[] args) { usage(); return; } + try { + onlineFunction(args); + } catch (OnlineToolException e){ + System.out.println(e.getMessage()); + usage(); + return; + } + } + + /** + * + * @param args args[0] is the Tag Value file to be converted, args[1] is the result spreadsheet file name + * @throws OnlineToolException Exception caught by JPype and displayed to the user + * @return Warnings of the conversion, display to the user + */ + public static List onlineFunction(String[] args) throws OnlineToolException{ + // Arguments length(args length== 2 ) will checked in the Python Code FileInputStream spdxTagFile; try { spdxTagFile = new FileInputStream(args[0]); } catch (FileNotFoundException ex) { - System.out - .printf("Tag-Value file %1$s does not exists.%n", args[0]); - return; + throw new OnlineToolException("Tag-Value file "+ args[0]+" does not exists."); } File spdxSpreadsheetFile = new File(args[1]); + // Output File name will be checked in the Python code for no clash, but if still found if (spdxSpreadsheetFile.exists()) { - System.out - .println("Spreadsheet file already exists - please specify a new file."); try { spdxTagFile.close(); } catch (IOException e) { - System.out.println("Warning: Unable to close output SPDX tag file on error: "+e.getMessage()); + throw new OnlineToolException("Warning: Unable to close output SPDX tag file on error: "+e.getMessage()); } - return; + throw new OnlineToolException("Spreadsheet file already exists - please specify a new file name."); } SpdxDocumentContainer[] result = new SpdxDocumentContainer[1]; + List warnings = new ArrayList(); try { // read the tag-value constants from a file Properties constants = CommonCode.getTextFromProperties("org/spdx/tag/SpdxTagValueConstants.properties"); NoCommentInputStream nci = new NoCommentInputStream(spdxTagFile); HandBuiltParser parser = new HandBuiltParser(nci); - List warnings = new ArrayList(); parser.setBehavior(new BuildDocument(result, constants, warnings)); parser.data(); + if (result[0] == null) { + throw(new OnlineToolException("Unexpected error parsing SPDX tag document - the result is null.")); + } if (!warnings.isEmpty()) { System.out.println("The following warnings and or verification errors were found:"); for (String warning:warnings) { System.out.println("\t"+warning); } } + } catch (RecognitionException e) { + // error in tag value file + throw(new OnlineToolException(e.getMessage())); + } catch (InvalidFileFormatException e) { + // invalid spdx file format + throw(new OnlineToolException(e.getMessage())); } catch (Exception e) { - System.err.println("Error creating SPDX Analysis: " + e); + // If any other exception - assume this is an RDF/XML file. + throw new OnlineToolException("Error creating SPDX Analysis: " + e); } SPDXSpreadsheet ss = null; try { ss = new SPDXSpreadsheet(spdxSpreadsheetFile, true, false); RdfToSpreadsheet.copyRdfXmlToSpreadsheet(result[0].getSpdxDocument(), ss); } catch (SpreadsheetException e) { - System.out.println("Error opening or writing to spreadsheet: " - + e.getMessage()); + throw new OnlineToolException("Error opening or writing to spreadsheet: "+ e.getMessage()); } catch (InvalidSPDXAnalysisException e) { - System.out.println("Error translating the Tag file: " - + e.getMessage()); + throw new OnlineToolException("Error translating the Tag file: "+ e.getMessage()); } catch (Exception ex) { - System.out - .println("Unexpected error translating the tag-value to spreadsheet: " - + ex.getMessage()); + throw new OnlineToolException("Unexpected error translating the tag-value to spreadsheet: "+ ex.getMessage()); } finally { if (ss != null) { try { ss.close(); } catch (SpreadsheetException e) { - System.out.println("Error closing spreadsheet: " - + e.getMessage()); + throw new OnlineToolException("Error closing spreadsheet: "+ e.getMessage()); } } } + return warnings; } - + + private static void usage() { System.out .println("Usage: TagToSpreadsheet spdxfile.spdx spreadsheetfile.xls \n" diff --git a/src/org/spdx/tools/Verify.java b/src/org/spdx/tools/Verify.java index fdda17f7..577926ff 100644 --- a/src/org/spdx/tools/Verify.java +++ b/src/org/spdx/tools/Verify.java @@ -173,13 +173,7 @@ public static List verifyRDFFile(String filePath) throws SpdxVerificatio } catch (Exception e) { throw new SpdxVerificationException("Unable to parse the file: "+e.getMessage(),e); } - List verify = doc.verify(); - List retval = new ArrayList(); - if (!verify.isEmpty()) { - for (String verifyMsg:verify) { - retval.add(verifyMsg); - } - } + List retval = doc.verify(); return retval; } }