Skip to content

Commit

Permalink
Call onlineFunction from main function only, removes the effort of ma…
Browse files Browse the repository at this point in the history
…intaining copies of the same function

Signed-off-by: rtgdk <rohit.lodhartg@gmail.com>
  • Loading branch information
rtgdk committed Aug 7, 2017
1 parent 65a8eef commit 303450d
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 643 deletions.
75 changes: 18 additions & 57 deletions src/org/spdx/tools/CompareMultpleSpdxDocs.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,57 +57,11 @@ public static void main(String[] args) {
usage();
System.exit(ERROR_STATUS);
}
File outputFile = new File(args[0]);
if (outputFile.exists()) {
System.out.println("Output file "+args[0]+" already exists.");
usage();
System.exit(ERROR_STATUS);
}
SpdxDocument[] compareDocs = new SpdxDocument[args.length-1];
String[] docNames = new String[args.length-1];
@SuppressWarnings("unchecked")
List<String>[] verificationErrors = new List[args.length-1];
for (int i = 1; i < args.length; i++) {
try {
List<String> warnings = new ArrayList<String>();
compareDocs[i-1] = CompareSpdxDocs.openRdfOrTagDoc(args[i], warnings);
if (!warnings.isEmpty()) {
System.out.println("Verification errors were found in "+args[i].trim()+". See verification errors sheet for details.");
}
docNames[i-1] = CompareSpdxDocs.convertDocName(args[i]);
verificationErrors[i-1] = compareDocs[i-1].verify();
if (verificationErrors[i-1] != null && verificationErrors[i-1].size() > 0) {
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);
}
}
MultiDocumentSpreadsheet outSheet = null;
try {
outSheet = new MultiDocumentSpreadsheet(outputFile, true, false);
outSheet.importVerificationErrors(verificationErrors, docNames);
SpdxComparer comparer = new SpdxComparer();
comparer.compare(compareDocs);
outSheet.importCompareResults(comparer, docNames);
} catch (SpreadsheetException e) {
System.out.println("Unable to create output spreadsheet: "+e.getMessage());
System.exit(ERROR_STATUS);
} catch (InvalidSPDXAnalysisException e) {
System.out.println("Invalid SPDX analysis: "+e.getMessage());
System.exit(ERROR_STATUS);
} catch (SpdxCompareException e) {
System.out.println("Error comparing SPDX documents: "+e.getMessage());
onlineFunction(args);
} catch (OnlineToolException e){
System.out.println(e.getMessage());
System.exit(ERROR_STATUS);
} finally {
if (outSheet != null) {
try {
outSheet.close();
} catch (SpreadsheetException e) {
System.out.println("Warning - error closing spreadsheet: "+e.getMessage());
}
}
}
}

Expand All @@ -119,18 +73,29 @@ public static void main(String[] args) {
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.");
// Output File name will be checked in the Python code for no clash, but if still found
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];
@SuppressWarnings("unchecked")
List<String>[] verificationErrors = new List[args.length-1];
for (int i = 1; i < args.length; i++) {
// CompareSpdxDocs.openRdfOrTagDoc and SpdxDocument.verify function already called in Python code- Verify.verify
docNames[i-1] = CompareSpdxDocs.convertDocName(args[i]);
try {
List<String> warnings = new ArrayList<String>();
compareDocs[i-1] = CompareSpdxDocs.openRdfOrTagDoc(args[i], warnings);
if (!warnings.isEmpty()) {
System.out.println("Verification errors were found in "+args[i].trim()+". See verification errors sheet for details.");
}
docNames[i-1] = CompareSpdxDocs.convertDocName(args[i]);
verificationErrors[i-1] = compareDocs[i-1].verify();
if (verificationErrors[i-1] != null && verificationErrors[i-1].size() > 0) {
System.out.println("Warning: "+docNames[i-1]+" contains verification errors.");
}
} catch (SpdxCompareException e) {
throw new OnlineToolException("Error opening SPDX document "+args[i]+": "+e.getMessage());
}
}
MultiDocumentSpreadsheet outSheet = null;
try {
Expand All @@ -140,20 +105,16 @@ public static void onlineFunction(String[] args) throws OnlineToolException{
comparer.compare(compareDocs);
outSheet.importCompareResults(comparer, docNames);
} catch (SpreadsheetException e) {
System.out.println("Unable to create output spreadsheet: "+e.getMessage());
throw new OnlineToolException("Unable to create output spreadsheet: "+e.getMessage());
} catch (InvalidSPDXAnalysisException e) {
System.out.println("Invalid SPDX analysis: "+e.getMessage());
throw new OnlineToolException("Invalid SPDX analysis: "+e.getMessage());
} catch (SpdxCompareException e) {
System.out.println("Error comparing SPDX documents: "+e.getMessage());
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
108 changes: 4 additions & 104 deletions src/org/spdx/tools/RdfToHtml.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,100 +104,12 @@ public static void main(String[] args) {
if (args.length > MAX_ARGS) {
System.out.println("Warning: Extra arguments will be ignored");
usage();
}
File spdxFile = new File(args[0]);
if (!spdxFile.exists()) {
System.out.println("SPDX File "+args[0]+" does not exist.");
usage();
System.exit(ERROR);
}
if (!spdxFile.canRead()) {
System.out.println("Can not read SPDX File "+args[0]+". Check permissions on the file.");
usage();
System.exit(ERROR);
}
File outputDirectory = new File(args[1]);
if (!outputDirectory.exists()) {
if (!outputDirectory.mkdirs()) {
System.out.println("Unable to create output directory");
System.exit(ERROR);
}
}
SpdxDocument doc = null;
try {
doc = SPDXDocumentFactory.createSpdxDocument(args[0]);
} catch (IOException e2) {
System.out.println("IO Error creating the SPDX document");
System.exit(ERROR);
} catch (InvalidSPDXAnalysisException e2) {
System.out.println("Invalid SPDX Document: "+e2.getMessage());
System.exit(ERROR);
}
String documentName = doc.getName();
List<File> filesToCreate = Lists.newArrayList();
String docHtmlFilePath = outputDirectory.getPath() + File.separator + documentName + DOC_HTML_FILE_POSTFIX;
File docHtmlFile = new File(docHtmlFilePath);
filesToCreate.add(docHtmlFile);
String snippetHtmlFilePath = outputDirectory.getPath() + File.separator + SNIPPET_FILE_NAME;
File snippetHtmlFile = new File(snippetHtmlFilePath);
filesToCreate.add(snippetHtmlFile);
String licenseHtmlFilePath = outputDirectory.getPath() + File.separator + documentName + LICENSE_HTML_FILE_POSTFIX;
File licenseHtmlFile = new File(licenseHtmlFilePath);
filesToCreate.add(licenseHtmlFile);
String docFilesHtmlFilePath = outputDirectory.getPath() + File.separator + documentName + DOCUMENT_FILE_HTML_FILE_POSTFIX;
File docFilesHtmlFile = new File(docFilesHtmlFilePath);
filesToCreate.add(docFilesHtmlFile);
List<SpdxPackage> pkgs = null;
try {
pkgs = doc.getDocumentContainer().findAllPackages();
} catch (InvalidSPDXAnalysisException e1) {
System.out.println("Error getting packages from the SPDX document: "+e1.getMessage());
System.exit(ERROR);
}
Iterator<SpdxPackage> iter = pkgs.iterator();
while (iter.hasNext()) {
String packageName = iter.next().getName();
String packageHtmlFilePath = outputDirectory.getPath() + File.separator + packageName +
PACKAGE_HTML_FILE_POSTFIX;
File packageHtmlFile = new File(packageHtmlFilePath);
filesToCreate.add(packageHtmlFile);
String packageFilesHtmlFilePath = outputDirectory.getPath() + File.separator + packageName +
PACKAGE_FILE_HTML_FILE_POSTFIX;
File packageFilesHtmlFile = new File(packageFilesHtmlFilePath);
filesToCreate.add(packageFilesHtmlFile);
}
Iterator<File> fileIter = filesToCreate.iterator();
while (fileIter.hasNext()) {
File file = fileIter.next();
if (file.exists()) {
System.out.println("File "+file.getName()+" already exists.");
System.exit(ERROR);
}
}
Writer writer = null;
}
try {
rdfToHtml(doc, docHtmlFile, licenseHtmlFile, snippetHtmlFile, docFilesHtmlFile);
} catch (IOException e) {
System.out.println("IO Error opening SPDX Document");
usage();
onlineFunction(args);
} catch (OnlineToolException e){
System.out.println(e.getMessage());
System.exit(ERROR);
} catch (InvalidSPDXAnalysisException e) {
System.out.println("Invalid SPDX Document: "+e.getMessage());
usage();
System.exit(ERROR);
} catch (MustacheException e) {
System.out.println("Unexpected error reading the HTML template: "+e.getMessage());
usage();
System.exit(ERROR);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
System.out.println("Warning: error closing HTML file: "+e.getMessage());
}
writer = null;
}
}
}

Expand All @@ -212,31 +124,25 @@ public static List<String> onlineFunction(String[] args) throws OnlineToolExcept
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.");
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.");
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");
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");
throw new OnlineToolException("IO Error creating the SPDX document");
} catch (InvalidSPDXAnalysisException e2) {
System.out.println("Invalid SPDX Document: "+e2.getMessage());
throw new OnlineToolException("Invalid SPDX Document: "+e2.getMessage());
} catch (Exception e) {
System.out.println("Error creating SPDX Document: "+e.getMessage());
throw new OnlineToolException("Error creating SPDX Document: "+e.getMessage(),e);
}
List<String> verify = new ArrayList<String>();
Expand Down Expand Up @@ -265,7 +171,6 @@ public static List<String> onlineFunction(String[] args) throws OnlineToolExcept
try {
pkgs = doc.getDocumentContainer().findAllPackages();
} catch (InvalidSPDXAnalysisException e1) {
System.out.println("Error getting packages from the SPDX document: "+e1.getMessage());
throw new OnlineToolException("Error getting packages from the SPDX document: "+e1.getMessage());
}
Iterator<SpdxPackage> iter = pkgs.iterator();
Expand All @@ -284,28 +189,23 @@ public static List<String> onlineFunction(String[] args) throws OnlineToolExcept
while (fileIter.hasNext()) {
File file = fileIter.next();
if (file.exists()) {
System.out.println("File "+file.getName()+" already exists.");
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");
throw new OnlineToolException("IO Error opening SPDX Document");
} catch (InvalidSPDXAnalysisException e) {
System.out.println("Invalid SPDX Document: "+e.getMessage());
throw new OnlineToolException("Invalid SPDX Document: "+e.getMessage());
} catch (MustacheException e) {
System.out.println("Unexpected error reading the HTML template: "+e.getMessage());
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;
Expand Down
Loading

0 comments on commit 303450d

Please sign in to comment.