Skip to content

Commit

Permalink
Merge pull request #64 from spdx/rc1
Browse files Browse the repository at this point in the history
Update to SPDX Spec 3.0.1
  • Loading branch information
goneall authored Dec 15, 2024
2 parents 744549b + 622b5c2 commit 631b60a
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 346 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
<dependency>
<groupId>org.spdx</groupId>
<artifactId>java-spdx-library</artifactId>
<version>2.0.0-Alpha</version>
<version>2.0.0-RC1</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
Expand Down
87 changes: 41 additions & 46 deletions src/main/java/org/spdx/spdxRdfStore/CompatibilityUpgrader.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* Copyright (c) 2020 Source Auditor Inc.
*
* <p>
* SPDX-License-Identifier: Apache-2.0
*
* <p>
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* 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.
Expand All @@ -18,8 +18,6 @@
package org.spdx.spdxRdfStore;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -58,20 +56,17 @@ public class CompatibilityUpgrader {
static final Logger logger = LoggerFactory.getLogger(CompatibilityUpgrader.class);

static {
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(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);
TYPE_PROPERTY_MAP = Map.of(SpdxConstantsCompatV2.CLASS_SPDX_DOCUMENT, Map.of(
// TODO: In 3.0, uncomment those below to change the spec versions
SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_SPDX_VERSION.getName(),
SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_SPDX_SPEC_VERSION.getName())
);
}

/**
* Upgrade the properties in the model to the current version of the spec
* @param model
* @param documentNamespace
* @param model RDF model
* @param documentNamespace Namespace or URI for the SPDX document
*/
public static void upgrade(Model model, String documentNamespace) throws InvalidSPDXAnalysisException {
model.enterCriticalSection(false);
Expand All @@ -94,7 +89,8 @@ public static void upgrade(Model model, String documentNamespace) throws Invalid
RDFNode object = iter.next();
subject.addProperty(compatibleProperty, object);
}
subject.removeAll(incompatibleProperty);
// We'll leave the old property for compatibility
// subject.removeAll(incompatibleProperty);
}
}
}
Expand All @@ -103,18 +99,17 @@ public static void upgrade(Model model, String documentNamespace) throws Invalid
upgradeArtifactOf(model, documentNamespace);
upgradeReviewers(model, documentNamespace);
upgradeExternalDocumentRefs(model, documentNamespace);
upgradeHasFiles(model, documentNamespace);
upgradeHasFiles(model);
} finally {
model.leaveCriticalSection();
}
}

/**
* Changes all hasFile properties to CONTAINS relationships
* @param model
* @param documentNamespace
* @param model RDF model
*/
private static void upgradeHasFiles(Model model, String documentNamespace) {
private static void upgradeHasFiles(Model model) {
List<Statement> statementsToRemove = new ArrayList<>();
Property hasFileProperty = model.createProperty("http://spdx.org/rdf/terms#hasFile");
Property relationshipProperty = model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_RELATIONSHIP.getName());
Expand All @@ -138,7 +133,7 @@ private static void upgradeHasFiles(Model model, String documentNamespace) {
foundContainsRelationships.add(stmt);
}
});
if (foundContainsRelationships.size() == 0) {
if (foundContainsRelationships.isEmpty()) {
Resource relationship = createRelationship(model, file, RelationshipType.CONTAINS);
pkg.addProperty(relationshipProperty, relationship);
}
Expand All @@ -149,9 +144,9 @@ private static void upgradeHasFiles(Model model, String documentNamespace) {

/**
* Make sure all external document Ref's have a URI with proper ID rather than using the externalDocumentId property
* @param model
* @param documentNamespace
* @throws InvalidSPDXAnalysisException
* @param model RDF model
* @param documentNamespace Namespace or URI for the SPDX document
* @throws InvalidSPDXAnalysisException on SPDX parsing errors
*/
private static void upgradeExternalDocumentRefs(Model model, String documentNamespace) throws InvalidSPDXAnalysisException {
String query = "SELECT ?s ?o WHERE { ?s <http://spdx.org/rdf/terms#externalDocumentId> ?o }";
Expand Down Expand Up @@ -197,9 +192,9 @@ private static void upgradeExternalDocumentRefs(Model model, String documentName

/**
* Upgrade the reviewers field to Annotations with a type reviewer
* @param model
* @param documentNamespace
* @throws InvalidSPDXAnalysisException
* @param model RDF model
* @param documentNamespace Namespace or URI for the SPDX document
* @throws InvalidSPDXAnalysisException on SPDX parsing errors
*/
private static void upgradeReviewers(Model model, String documentNamespace) throws InvalidSPDXAnalysisException {
Resource document = model.createResource(documentNamespace + "#" + SpdxConstantsCompatV2.SPDX_DOCUMENT_ID);
Expand Down Expand Up @@ -267,13 +262,13 @@ private static void upgradeReviewers(Model model, String documentNamespace) thro

/**
* Convert all artifactOf properties to relationships and remove the old properties and DOAP classes
* @param model
* @param model RDF model
* @param documentNamespace the document Namespace
* @throws InvalidSPDXAnalysisException
* @throws InvalidSPDXAnalysisException on SPDX parsing errors
*/
private static void upgradeArtifactOf(Model model, String documentNamespace) throws InvalidSPDXAnalysisException {
String docNamespace = documentNamespace + "#";
Set<String> addedDoapProjects = new HashSet<String>(); // prevent duplicates
Set<String> addedDoapProjects = new HashSet<>(); // prevent duplicates
List<Statement> statementsToRemove = new ArrayList<>();
Property artifactOfProperty = model.createProperty("http://spdx.org/rdf/terms#artifactOf");
Property relationshipProperty = model.createProperty(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_RELATIONSHIP.getName());
Expand All @@ -291,7 +286,7 @@ private static void upgradeArtifactOf(Model model, String documentNamespace) thr
while (iter.hasNext()) {
statementsToRemove.add(iter.next());
}
Resource pkg = convertDoapProjectToSpdxPackage(model, doapProject, docNamespace + idPrefix + Integer.toString(nextSpdxIdNum));
Resource pkg = convertDoapProjectToSpdxPackage(model, doapProject, docNamespace + idPrefix + nextSpdxIdNum);
if (pkg.isURIResource() && !addedDoapProjects.contains(pkg.getURI())) {
addedDoapProjects.add(pkg.getURI());
nextSpdxIdNum = getNexId(model, docNamespace, idPrefix, nextSpdxIdNum);
Expand All @@ -305,10 +300,10 @@ private static void upgradeArtifactOf(Model model, String documentNamespace) thr

/**
* Creates an anonymous relationship resource
* @param model
* @param pkg
* @param relationshipType
* @return
* @param model RDF model
* @param relatedElement related element
* @param relationshipType SPDX relationship type
* @return resource for the relationship
*/
private static Resource createRelationship(Model model, Resource relatedElement, RelationshipType relationshipType) {
Resource retval = model.createResource();
Expand All @@ -321,28 +316,28 @@ private static Resource createRelationship(Model model, Resource relatedElement,
}

/**
* @param model
* @param docNamespace
* @param idPrefix
* @param model RDF model
* @param docNamespace Namespace or URI for the SPDX document
* @param idPrefix prefix for the ID to be used
* @param startingNum Starting number to search for the next available ID
* @return the next ID number available
*/
private static int getNexId(Model model, String docNamespace, String idPrefix, int startingNum) {
int retval = startingNum;
Resource idResource = model.getResource(docNamespace + idPrefix + Integer.toString(retval));
Resource idResource = model.getResource(docNamespace + idPrefix + retval);
while (model.containsResource(idResource)) {
retval++;
idResource = model.getResource(docNamespace + idPrefix + Integer.toString(retval));
idResource = model.getResource(docNamespace + idPrefix + retval);
}
return retval;
}

/**
* Convert a DOAP project resource into a package resource
* @param model
* @param doapProject
* @return
* @throws InvalidSPDXAnalysisException
* @param model RDF model
* @param doapProject DOAP project to convert
* @return a related package representing the DOAP project
* @throws InvalidSPDXAnalysisException on SPDX parsing errors
*/
private static Resource convertDoapProjectToSpdxPackage(Model model, Resource doapProject, String packageUri) throws InvalidSPDXAnalysisException {
String packageName;
Expand All @@ -354,7 +349,7 @@ private static Resource convertDoapProjectToSpdxPackage(Model model, Resource do
if (Objects.isNull(packageName)) {
throw new InvalidSPDXAnalysisException("Missing required DOAP project name");
}
String homePage = null;
String homePage;
Property homePageProperty = model.createProperty(SpdxConstantsCompatV2.DOAP_NAMESPACE + SpdxConstantsCompatV2.PROP_PROJECT_HOMEPAGE.getName());
try {
homePage = doapProject.getProperty(homePageProperty).getString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/**
* Copyright (c) 2020 Source Auditor Inc.
*
* <p>
* SPDX-License-Identifier: Apache-2.0
*
* <p>
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* 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.
Expand All @@ -31,15 +32,15 @@ public class MissingDataTypeAndClassRestriction extends SpdxRdfException {
private static final long serialVersionUID = 1L;

/**
* @param msg
* @param msg error message
*/
public MissingDataTypeAndClassRestriction(String msg) {
super(msg);
}

/**
* @param msg
* @param inner
* @param msg error message
* @param inner exception
*/
public MissingDataTypeAndClassRestriction(String msg, Throwable inner) {
super(msg, inner);
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/spdx/spdxRdfStore/OutputFormat.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* Copyright (c) 2020 Source Auditor Inc.
*
* <p>
* SPDX-License-Identifier: Apache-2.0
*
* <p>
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* 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.
Expand All @@ -31,10 +31,10 @@ public enum OutputFormat {
TURTLE("TURTLE"),
JSON_LD("JSON-LD");

private String type;
private OutputFormat(String type) {
private final String type;
OutputFormat(String type) {
this.type = type;
}
String getType() { return type; };
String getType() { return type; }
}

Loading

0 comments on commit 631b60a

Please sign in to comment.