Skip to content

Commit

Permalink
Merge pull request #59 from spdx/v3
Browse files Browse the repository at this point in the history
Updates to support SPDX Spec version 3
  • Loading branch information
goneall authored Sep 5, 2024
2 parents 817dc18 + c6c69f3 commit b769415
Show file tree
Hide file tree
Showing 39 changed files with 2,643 additions and 1,977 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Spdx-Java-Rdf-Store

This Java library implements an RDF store implementing the [SPDX Java Library Storage Interface](https://github.com/spdx/Spdx-Java-Library#storage-interface) using an underlying RDF store.
This Java library implements an RDF store supported SPDX spec version 2.3 and earlier implementing the [SPDX Java Library Storage Interface](https://github.com/spdx/Spdx-Java-Library#storage-interface) using an underlying RDF store.

# Code quality badges

Expand All @@ -10,7 +10,9 @@ This Java library implements an RDF store implementing the [SPDX Java Library St

This library is intended to be used in conjunction with the [SPDX Java Library](https://github.com/spdx/Spdx-Java-Library).

Simply create a new instance of `RdfStore()` and reference it as your storage.
Simply create a new instance of `RdfStore(documentNamespace)` and reference it as your storage. The documentNamespace is the namespace used for the store.

Note that this version of the RDF library only supports a single document namespace and DOES NOT support spec versions 3.0 and later.

# Serializing and Deserializing RDF Formats

Expand All @@ -24,5 +26,5 @@ A convenience method `public String loadModelFromFile(String fileNameOrUrl, bool

# Development Status

Mostly stable - although it has not been widely used.
Less stable - it has just been updated to support the redesigned storage interfaces.

4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.spdx</groupId>
<artifactId>spdx-rdf-store</artifactId>
<version>1.1.10-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spdx-rdf-store</name>
Expand Down Expand Up @@ -118,7 +118,7 @@
<dependency>
<groupId>org.spdx</groupId>
<artifactId>java-spdx-library</artifactId>
<version>1.1.11</version>
<version>2.0.0-Alpha</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
Expand Down
81 changes: 40 additions & 41 deletions src/main/java/org/spdx/spdxRdfStore/CompatibilityUpgrader.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
import org.apache.jena.rdf.model.StmtIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.SpdxConstants;
import org.spdx.library.SpdxVerificationHelper;
import org.spdx.library.model.enumerations.AnnotationType;
import org.spdx.library.model.enumerations.RelationshipType;
import org.spdx.core.InvalidSPDXAnalysisException;
import org.spdx.library.model.v2.SpdxConstantsCompatV2;
import org.spdx.library.model.v2.SpdxVerificationHelper;
import org.spdx.library.model.v2.enumerations.AnnotationType;
import org.spdx.library.model.v2.enumerations.RelationshipType;

/**
* Updates the RDF model for compatibility with the current version of the spec
Expand All @@ -61,9 +61,9 @@ public class CompatibilityUpgrader {
Map<String, Map<String, String>> mutableTypePropertyMap = new HashMap<>();
Map<String, String> documentMap = new HashMap<>();
//TODO: In 3.0, uncomment those below to change the spec versions
// documentMap.put(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_SPDX_VERSION,
// SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_SPDX_SPEC_VERSION);
mutableTypePropertyMap.put(SpdxConstants.CLASS_SPDX_DOCUMENT, Collections.unmodifiableMap(documentMap));
// documentMap.put(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_SPDX_VERSION.getName(),
// SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_SPDX_SPEC_VERSION.getName());
mutableTypePropertyMap.put(SpdxConstantsCompatV2.CLASS_SPDX_DOCUMENT, Collections.unmodifiableMap(documentMap));

TYPE_PROPERTY_MAP = Collections.unmodifiableMap(mutableTypePropertyMap);
}
Expand All @@ -79,8 +79,8 @@ public static void upgrade(Model model, String documentNamespace) throws Invalid
// update type property names
for (Entry<String, Map<String, String>> entry:TYPE_PROPERTY_MAP.entrySet()) {
String query = "SELECT ?s WHERE { ?s <" +
RdfSpdxDocumentModelManager.RDF_TYPE + "> <" +
SpdxConstants.SPDX_NAMESPACE + entry.getKey() + "> }";
RdfSpdxModelManager.RDF_TYPE + "> <" +
SpdxConstantsCompatV2.SPDX_NAMESPACE + entry.getKey() + "> }";
try (QueryExecution qe = QueryExecutionFactory.create(query, model)) {
ResultSet result = qe.execSelect();
while (result.hasNext()) {
Expand Down Expand Up @@ -117,7 +117,7 @@ public static void upgrade(Model model, String documentNamespace) throws Invalid
private static void upgradeHasFiles(Model model, String documentNamespace) {
List<Statement> statementsToRemove = new ArrayList<>();
Property hasFileProperty = model.createProperty("http://spdx.org/rdf/terms#hasFile");
Property relationshipProperty = model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_RELATIONSHIP);
Property relationshipProperty = model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_RELATIONSHIP.getName());
String query = "SELECT ?s ?o WHERE { ?s <http://spdx.org/rdf/terms#hasFile> ?o }";
try (QueryExecution qe = QueryExecutionFactory.create(query, model)) {
ResultSet result = qe.execSelect();
Expand All @@ -130,8 +130,8 @@ private static void upgradeHasFiles(Model model, String documentNamespace) {
List<Statement> foundContainsRelationships = new ArrayList<>();
pkg.listProperties(relationshipProperty).forEach((stmt) -> {
Resource existingRelationship = stmt.getObject().asResource();
Resource relatedElement = existingRelationship.getPropertyResourceValue(model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_RELATED_SPDX_ELEMENT));
Resource relationshipType = existingRelationship.getPropertyResourceValue(model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_RELATIONSHIP_TYPE));
Resource relatedElement = existingRelationship.getPropertyResourceValue(model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_RELATED_SPDX_ELEMENT.getName()));
Resource relationshipType = existingRelationship.getPropertyResourceValue(model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_RELATIONSHIP_TYPE.getName()));
if (relatedElement.isURIResource() && file.isURIResource() && relationshipType.isURIResource() &&
relatedElement.getURI().equals(file.getURI()) &&
relationshipType.getURI().equals(RelationshipType.CONTAINS.getIndividualURI())) {
Expand Down Expand Up @@ -201,17 +201,16 @@ private static void upgradeExternalDocumentRefs(Model model, String documentName
* @param documentNamespace
* @throws InvalidSPDXAnalysisException
*/
@SuppressWarnings("deprecation")
private static void upgradeReviewers(Model model, String documentNamespace) throws InvalidSPDXAnalysisException {
Resource document = model.createResource(documentNamespace + "#" + SpdxConstants.SPDX_DOCUMENT_ID);
Property annotationProperty = model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_ANNOTATION);
Resource document = model.createResource(documentNamespace + "#" + SpdxConstantsCompatV2.SPDX_DOCUMENT_ID);
Property annotationProperty = model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_ANNOTATION.getName());
Resource reviewerType = model.createResource(AnnotationType.REVIEW.getIndividualURI());
Property typeProperty = model.createProperty(RdfSpdxDocumentModelManager.RDF_TYPE);
Resource annotationClass = model.createResource(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.CLASS_ANNOTATION);
Property commentProperty = model.createProperty(SpdxConstants.RDFS_NAMESPACE + SpdxConstants.RDFS_PROP_COMMENT);
Property annotatorProperty = model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_ANNOTATOR);
Property annotationDateProperty = model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_ANNOTATION_DATE);
Property annotationTypeProperty = model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_ANNOTATION_TYPE);
Property typeProperty = model.createProperty(RdfSpdxModelManager.RDF_TYPE);
Resource annotationClass = model.createResource(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.CLASS_ANNOTATION);
Property commentProperty = model.createProperty(SpdxConstantsCompatV2.RDFS_NAMESPACE + SpdxConstantsCompatV2.RDFS_PROP_COMMENT.getName());
Property annotatorProperty = model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_ANNOTATOR.getName());
Property annotationDateProperty = model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_ANNOTATION_DATE.getName());
Property annotationTypeProperty = model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_ANNOTATION_TYPE.getName());
List<Statement> statementsToRemove = new ArrayList<>();
Set<Integer> addedAnnotations = new HashSet<>(); // to prevent duplicates
String query = "SELECT ?s ?o WHERE { ?s <http://spdx.org/rdf/terms#reviewer> ?o }";
Expand All @@ -226,7 +225,7 @@ private static void upgradeReviewers(Model model, String documentNamespace) thro
String reviewDate;
try {
reviewDate = review.getRequiredProperty(model.createProperty(
SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_REVIEW_DATE))
SpdxConstantsCompatV2.SPDX_NAMESPACE + "reviewDate"))
.getString();
if (Objects.isNull(reviewDate)) {
throw new InvalidSPDXAnalysisException("Missing or invalid review date for review");
Expand Down Expand Up @@ -258,7 +257,7 @@ private static void upgradeReviewers(Model model, String documentNamespace) thro
annotation.addProperty(annotationTypeProperty, reviewerType);
document.addProperty(annotationProperty, annotation);
}
StmtIterator iter = document.listProperties(model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_SPDX_REVIEWED_BY));
StmtIterator iter = document.listProperties(model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_SPDX_REVIEWED_BY.getName()));
while (iter.hasNext()) {
statementsToRemove.add(iter.next());
}
Expand All @@ -277,7 +276,7 @@ private static void upgradeArtifactOf(Model model, String documentNamespace) thr
Set<String> addedDoapProjects = new HashSet<String>(); // prevent duplicates
List<Statement> statementsToRemove = new ArrayList<>();
Property artifactOfProperty = model.createProperty("http://spdx.org/rdf/terms#artifactOf");
Property relationshipProperty = model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_RELATIONSHIP);
Property relationshipProperty = model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_RELATIONSHIP.getName());
String query = "SELECT ?s ?o WHERE { ?s <http://spdx.org/rdf/terms#artifactOf> ?o }";
try (QueryExecution qe = QueryExecutionFactory.create(query, model)) {
ResultSet result = qe.execSelect();
Expand Down Expand Up @@ -313,10 +312,10 @@ private static void upgradeArtifactOf(Model model, String documentNamespace) thr
*/
private static Resource createRelationship(Model model, Resource relatedElement, RelationshipType relationshipType) {
Resource retval = model.createResource();
retval.addProperty(model.createProperty(RdfSpdxDocumentModelManager.RDF_TYPE),
model.createResource(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.CLASS_RELATIONSHIP));
retval.addProperty(model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_RELATED_SPDX_ELEMENT), relatedElement);
retval.addProperty(model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_RELATIONSHIP_TYPE),
retval.addProperty(model.createProperty(RdfSpdxModelManager.RDF_TYPE),
model.createResource(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.CLASS_RELATIONSHIP));
retval.addProperty(model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_RELATED_SPDX_ELEMENT.getName()), relatedElement);
retval.addProperty(model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_RELATIONSHIP_TYPE.getName()),
model.createResource(relationshipType.getIndividualURI()));
return retval;
}
Expand Down Expand Up @@ -348,23 +347,23 @@ private static int getNexId(Model model, String docNamespace, String idPrefix, i
private static Resource convertDoapProjectToSpdxPackage(Model model, Resource doapProject, String packageUri) throws InvalidSPDXAnalysisException {
String packageName;
try {
packageName = doapProject.getProperty(model.createProperty(SpdxConstants.DOAP_NAMESPACE + SpdxConstants.PROP_NAME)).getString();
packageName = doapProject.getProperty(model.createProperty(SpdxConstantsCompatV2.DOAP_NAMESPACE + SpdxConstantsCompatV2.PROP_NAME.getName())).getString();
} catch (Exception ex) {
throw new InvalidSPDXAnalysisException("DOAP project name is not a valid string");
}
if (Objects.isNull(packageName)) {
throw new InvalidSPDXAnalysisException("Missing required DOAP project name");
}
String homePage = null;
Property homePageProperty = model.createProperty(SpdxConstants.DOAP_NAMESPACE + SpdxConstants.PROP_PROJECT_HOMEPAGE);
Property homePageProperty = model.createProperty(SpdxConstantsCompatV2.DOAP_NAMESPACE + SpdxConstantsCompatV2.PROP_PROJECT_HOMEPAGE.getName());
try {
homePage = doapProject.getProperty(homePageProperty).getString();
} catch (Exception ex) {
homePage = null;
}
String addedPackageComment = "This package was converted from a DOAP Project by the same name";
Property commentProperty = model.createProperty(SpdxConstants.RDFS_NAMESPACE + SpdxConstants.RDFS_PROP_COMMENT);
Property nameProperty = model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_NAME);
Property commentProperty = model.createProperty(SpdxConstantsCompatV2.RDFS_NAMESPACE + SpdxConstantsCompatV2.RDFS_PROP_COMMENT.getName());
Property nameProperty = model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_NAME.getName());
// See if there is already a package matching the name and home page
ResIterator iter = model.listResourcesWithProperty(nameProperty, packageName);
while (iter.hasNext()) {
Expand All @@ -385,21 +384,21 @@ private static Resource convertDoapProjectToSpdxPackage(Model model, Resource do
}
}
Resource retval = model.createResource(packageUri);
retval.addProperty(model.createProperty(RdfSpdxDocumentModelManager.RDF_TYPE),
model.createResource(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.CLASS_SPDX_PACKAGE));
retval.addProperty(model.createProperty(RdfSpdxModelManager.RDF_TYPE),
model.createResource(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.CLASS_SPDX_PACKAGE));
// download location
Resource noAssertion = model.createResource(SpdxConstants.URI_VALUE_NOASSERTION);
retval.addProperty(model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_PACKAGE_DOWNLOAD_URL), noAssertion);
Resource noAssertion = model.createResource(SpdxConstantsCompatV2.URI_VALUE_NOASSERTION);
retval.addProperty(model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_PACKAGE_DOWNLOAD_URL.getName()), noAssertion);
// concludedLicense
retval.addProperty(model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_PACKAGE_CONCLUDED_LICENSE), noAssertion);
retval.addProperty(model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_PACKAGE_CONCLUDED_LICENSE.getName()), noAssertion);
// declaredLicense
retval.addProperty(model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_PACKAGE_DECLARED_LICENSE), noAssertion);
retval.addProperty(model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_PACKAGE_DECLARED_LICENSE.getName()), noAssertion);
// copyright
retval.addProperty(model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_PACKAGE_DECLARED_COPYRIGHT), noAssertion);
retval.addProperty(model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_PACKAGE_DECLARED_COPYRIGHT.getName()), noAssertion);
// comment
retval.addProperty(commentProperty, addedPackageComment);
// filesAnalyzed
retval.addLiteral(model.createProperty(SpdxConstants.SPDX_NAMESPACE + SpdxConstants.PROP_PACKAGE_FILES_ANALYZED), false);
retval.addLiteral(model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_PACKAGE_FILES_ANALYZED.getName()), false);
// name
retval.addLiteral(nameProperty, packageName);
// homePage
Expand Down
Loading

0 comments on commit b769415

Please sign in to comment.