Skip to content

Commit

Permalink
Merge pull request #106 from rtgdk/patch6
Browse files Browse the repository at this point in the history
Update java tools with methods to be called from online tool.
  • Loading branch information
goneall authored Aug 8, 2017
2 parents 0f2b2d2 + 303450d commit 5b82195
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 191 deletions.
37 changes: 23 additions & 14 deletions src/org/spdx/tools/CompareMultpleSpdxDocs.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
Expand All @@ -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());
}
}
}
}



/**
*
*/
Expand Down
70 changes: 70 additions & 0 deletions src/org/spdx/tools/OnlineToolException.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
68 changes: 41 additions & 27 deletions src/org/spdx/tools/RdfToHtml.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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<String> verify = new ArrayList<String>();
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<File> filesToCreate = Lists.newArrayList();
String docHtmlFilePath = outputDirectory.getPath() + File.separator + documentName + DOC_HTML_FILE_POSTFIX;
Expand All @@ -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<SpdxPackage> iter = pkgs.iterator();
while (iter.hasNext()) {
Expand All @@ -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;
}

/**
Expand Down
50 changes: 35 additions & 15 deletions src/org/spdx/tools/RdfToSpreadsheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,57 +86,76 @@ 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<String> 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<String> verify = new ArrayList<String>();
if (doc != null) {
SPDXSpreadsheet ss = null;
try {
ss = new SPDXSpreadsheet(spdxSpreadsheetFile, true, false);
copyRdfXmlToSpreadsheet(doc, ss);
List<String> 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++) {
System.out.println("\t" + verify.get(i));
}
}
} 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 {
Expand Down
Loading

0 comments on commit 5b82195

Please sign in to comment.