Skip to content

Commit

Permalink
Merge pull request #98 from spdx/issue97
Browse files Browse the repository at this point in the history
Add option to not copy license details
  • Loading branch information
goneall authored Feb 8, 2023
2 parents ef113a5 + 83a5abd commit 5e6e8d6
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ The following converter tools support spdx format:
Example to convert a SPDX file from tag to rdf format:

java -jar tools-java-1.1.0-jar-with-dependencies.jar Convert ../testResources/SPDXTagExample-v2.2.spdx TagToRDF.rdf

The file formats can optionally be provided as the 3rd and 4th parameter for the input and output formats respectively. An optional 5th option `excludeLicenseDetails` will not copy the listed license properties to the output file. The following example will copy a JSON format to an RDF Turtle format without including the listed license properties:

java -jar tools-java-1.1.0-jar-with-dependencies.jar Convert ../testResources/SPDXTagExample-v2.2.spdx TagToRDF.ttl TAG RDFTTL excludeLicenseDetails

## Compare utilities
The following tools can be used to compare one or more SPDX documents:
Expand Down
34 changes: 29 additions & 5 deletions src/main/java/org/spdx/tools/SpdxConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* arg[1] to file path
* arg[2] from file type [RDFXML|RDFTTL|JSON|XLS|XLSX|YAML|TAG] - if not present, file type of the from file will be used
* arg[3] to file type [RDFXML|RDFTTL|JSON|XLS|XLSX|YAML|TAG] - if not present, file type of the to file will be used
* arg[4] excludeLicenseDetails If present, listed license and listed exception properties will not be included in the output file
*
* the <code>covert(...)</code> methods can be called programmatically to convert files
* @author Gary O'Neall
Expand All @@ -49,7 +50,7 @@ public class SpdxConverter {
static final int ERROR_STATUS = 1;

static final int MIN_ARGS = 2;
static final int MAX_ARGS = 4;
static final int MAX_ARGS = 5;

/**
* @param args
Expand All @@ -68,6 +69,10 @@ public static void main(String[] args) {
if (args.length == 3) {
System.out.printf("Warning: only the input file type specified - it will be ignored");
}
boolean excludeLicenseDetails = false;
if (args.length == 5 && "excludelicensedetails".equals(args[4].toLowerCase())) {
excludeLicenseDetails = true;
}
if (args.length < 4) {
try {
convert(args[0], args[1]);
Expand Down Expand Up @@ -95,7 +100,7 @@ public static void main(String[] args) {
System.exit(ERROR_STATUS);
}
try {
convert(args[0], args[1], fromFileType, toFileType);
convert(args[0], args[1], fromFileType, toFileType, excludeLicenseDetails);
} catch (SpdxConverterException e) {
System.err.println("Error converting: "+e.getMessage());
System.exit(ERROR_STATUS);
Expand Down Expand Up @@ -125,15 +130,32 @@ public static void convert(String fromFilePath, String toFilePath) throws SpdxCo
convert(fromFilePath, toFilePath, fromFileType, toFileType);
}

/**
* Convert an SPDX file from the fromFilePath to a new file at the toFilePath including listed license property details
* @param fromFilePath Path of the file to convert from
* @param toFilePath Path of output file for the conversion
* @param fromFileType Serialization type of the file to convert from
* @param toFileType Serialization type of the file to convert to
* @param excludeLicenseDetails If true, don't copy over properties of the listed licenses
* @throws SpdxConverterException
*/
public static void convert(String fromFilePath, String toFilePath, SerFileType fromFileType,
SerFileType toFileType) throws SpdxConverterException {
convert(fromFilePath, toFilePath, fromFileType, toFileType, false);

}

/**
* Convert an SPDX file from the fromFilePath to a new file at the toFilePath
* @param fromFilePath Path of the file to convert from
* @param toFilePath Path of output file for the conversion
* @param fromFileType Serialization type of the file to convert from
* @param toFileType Serialization type of the file to convert to
* @param excludeLicenseDetails If true, don't copy over properties of the listed licenses
* @throws SpdxConverterException
*/
public static void convert(String fromFilePath, String toFilePath, SerFileType fromFileType, SerFileType toFileType) throws SpdxConverterException {
public static void convert(String fromFilePath, String toFilePath, SerFileType fromFileType,
SerFileType toFileType, boolean excludeLicenseDetails) throws SpdxConverterException {
File fromFile = new File(fromFilePath);
if (!fromFile.exists()) {
throw new SpdxConverterException("Input file "+fromFilePath+" does not exist.");
Expand Down Expand Up @@ -175,9 +197,10 @@ public static void convert(String fromFilePath, String toFilePath, SerFileType f
});
fromStore.getAllItems(documentUri, null).forEach(tv -> {
try {
if (!SpdxConstants.CLASS_EXTERNAL_DOC_REF.equals(tv.getType())) {
if (!SpdxConstants.CLASS_EXTERNAL_DOC_REF.equals(tv.getType()) &&
!(excludeLicenseDetails && SpdxConstants.CLASS_CROSS_REF.equals(tv.getType()))) {
copyManager.copy(toStore, documentUri, fromStore, documentUri,
tv.getId(), tv.getType());
tv.getId(), tv.getType(), excludeLicenseDetails);
}
} catch (InvalidSPDXAnalysisException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -223,6 +246,7 @@ private static void usage() {
System.out.println("\ttoFilePath - output file");
System.out.println("\t[fromFileType] - optional file type of the input file. One of JSON, XLS, XLSX, TAG, RDFXML, RDFTTL, YAML or XML. If not provided the file type will be determined by the file extension");
System.out.println("\t[toFileType] - optional file type of the output file. One of JSON, XLS, XLSX, TAG, RDFXML, RDFTTL, YAML or XML. If not provided the file type will be determined by the file extension");
System.out.println("\t[excludeLicenseDetails] - If present, listed license and listed exception properties will not be included in the output file");
}

}
49 changes: 49 additions & 0 deletions src/test/java/org/spdx/tools/SpdxConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Objects;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.model.SpdxDocument;
import org.spdx.tools.SpdxToolsHelper.SerFileType;
Expand All @@ -43,6 +48,7 @@ public class SpdxConverterTest extends TestCase {

static final String TEST_DIR = "testResources";
static final String TEST_JSON_FILE_PATH = TEST_DIR + File.separator + "SPDXJSONExample-v2.3.spdx.json";
static final String TEST_WITH_EXCEPTION_FILE_PATH = TEST_DIR + File.separator + "SPDXJSONExample-v2.3-with-exception.spdx.json";
static final String TEST_RDF_FILE_PATH = TEST_DIR + File.separator + "SPDXRdfExample-v2.3.spdx.rdf";
static final String TEST_SPREADSHEET_XLS_FILE_PATH = TEST_DIR + File.separator + "SPDXSpreadsheetExample-v2.3.xls";
static final String TEST_SPREADSHEET_XLSX_FILE_PATH = TEST_DIR + File.separator + "SPDXSpreadsheetExample-v2.3.xlsx";
Expand Down Expand Up @@ -355,5 +361,48 @@ public void testJsonToXml() throws SpdxConverterException, InvalidSPDXAnalysisEx
comparer.compare(sourceDoc, resultDoc);
assertFalse(comparer.isDifferenceFound());
}

public void testLicenseDetailsRdf() throws SpdxConverterException, InvalidSPDXAnalysisException, IOException, SpdxCompareException {
String detailsRdfFilePath = tempDirPath.resolve("details.rdf.xml").toString();
String noDetailsRdfFilePath = tempDirPath.resolve("nodetails.rdf.xml").toString();
SpdxConverter.convert(TEST_WITH_EXCEPTION_FILE_PATH, noDetailsRdfFilePath, SerFileType.JSON, SerFileType.RDFXML, true);
File noDetailsFile = new File(noDetailsRdfFilePath);
assertTrue(noDetailsFile.exists());
SpdxConverter.convert(TEST_WITH_EXCEPTION_FILE_PATH, detailsRdfFilePath, SerFileType.JSON, SerFileType.RDFXML, false);
File detailsFile = new File(detailsRdfFilePath);
assertTrue(detailsFile.exists());
File source = new File(TEST_WITH_EXCEPTION_FILE_PATH);
SpdxDocument sourceDoc = SpdxToolsHelper.deserializeDocument(source, SerFileType.JSON);
SpdxDocument detailsDoc = SpdxToolsHelper.deserializeDocument(detailsFile, SerFileType.RDFXML);
SpdxDocument noDetailsDoc = SpdxToolsHelper.deserializeDocument(noDetailsFile, SerFileType.RDFXML);

// Make sure they compare - the inclusion of the details should not impact of the 2 documents match
SpdxComparer comparer = new SpdxComparer();
comparer.compare(Arrays.asList(new SpdxDocument[] {sourceDoc, detailsDoc, noDetailsDoc}));
assertFalse(comparer.isDifferenceFound());

String mplLicenseUri = "http://spdx.org/licenses/MPL-1.0";
String licenseTextProperty = "http://spdx.org/rdf/terms#licenseText";
String exceptionUri = "http://spdx.org/licenses/Autoconf-exception-3.0";
String exceptionTextProperty = "http://spdx.org/rdf/terms#licenseExceptionText";

// Make sure the details has the details
Model detailModel = ModelFactory.createDefaultModel();
detailModel.read(detailsFile.toURI().toString());
Resource detailMplLicense = detailModel.createResource(mplLicenseUri);
Property detailLicenseTextProperty = detailModel.createProperty(licenseTextProperty);
assertTrue(detailModel.contains(detailMplLicense, detailLicenseTextProperty));
Resource detailException = detailModel.createResource(exceptionUri);
Property detailExceptionTextProperty = detailModel.createProperty(exceptionTextProperty);
assertTrue(detailModel.contains(detailException, detailExceptionTextProperty));

// Make sure the noDetails does not have the listed licenses
Model noDetailModel = ModelFactory.createDefaultModel();
noDetailModel.read(noDetailsFile.toURI().toString());
Resource noDetailMplLicense = noDetailModel.createResource(mplLicenseUri);
assertFalse(noDetailModel.contains(noDetailMplLicense, detailLicenseTextProperty));
Resource noDetailException = noDetailModel.createResource(exceptionUri);
assertFalse(noDetailModel.contains(noDetailException, detailExceptionTextProperty));
}

}
Loading

0 comments on commit 5e6e8d6

Please sign in to comment.