11/**
22 * Copyright (c) 2020 Source Auditor Inc.
3- *
3+ * <p>
44 * SPDX-License-Identifier: Apache-2.0
5- *
5+ * <p>
66 * Licensed under the Apache License, Version 2.0 (the "License");
77 * you may not use this file except in compliance with the License.
88 * You may obtain a copy of the License at
9- *
9+ * <p>
1010 * http://www.apache.org/licenses/LICENSE-2.0
11- *
11+ * <p>
1212 * Unless required by applicable law or agreed to in writing, software
1313 * distributed under the License is distributed on an "AS IS" BASIS,
1414 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1818package org .spdx .spdxRdfStore ;
1919
2020import java .util .ArrayList ;
21- import java .util .Collections ;
22- import java .util .HashMap ;
2321import java .util .HashSet ;
2422import java .util .List ;
2523import java .util .Map ;
@@ -58,20 +56,17 @@ public class CompatibilityUpgrader {
5856 static final Logger logger = LoggerFactory .getLogger (CompatibilityUpgrader .class );
5957
6058 static {
61- Map <String , Map <String , String >> mutableTypePropertyMap = new HashMap <>();
62- Map <String , String > documentMap = new HashMap <>();
63- //TODO: In 3.0, uncomment those below to change the spec versions
64- // documentMap.put(SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_SPDX_VERSION.getName(),
65- // SpdxConstantsCompatV2.SPDX_NAMESPACE + SpdxConstantsCompatV2.PROP_SPDX_SPEC_VERSION.getName());
66- mutableTypePropertyMap .put (SpdxConstantsCompatV2 .CLASS_SPDX_DOCUMENT , Collections .unmodifiableMap (documentMap ));
67-
68- TYPE_PROPERTY_MAP = Collections .unmodifiableMap (mutableTypePropertyMap );
59+ TYPE_PROPERTY_MAP = Map .of (SpdxConstantsCompatV2 .CLASS_SPDX_DOCUMENT , Map .of (
60+ // TODO: In 3.0, uncomment those below to change the spec versions
61+ SpdxConstantsCompatV2 .SPDX_NAMESPACE + SpdxConstantsCompatV2 .PROP_SPDX_VERSION .getName (),
62+ SpdxConstantsCompatV2 .SPDX_NAMESPACE + SpdxConstantsCompatV2 .PROP_SPDX_SPEC_VERSION .getName ())
63+ );
6964 }
7065
7166 /**
7267 * Upgrade the properties in the model to the current version of the spec
73- * @param model
74- * @param documentNamespace
68+ * @param model RDF model
69+ * @param documentNamespace Namespace or URI for the SPDX document
7570 */
7671 public static void upgrade (Model model , String documentNamespace ) throws InvalidSPDXAnalysisException {
7772 model .enterCriticalSection (false );
@@ -94,7 +89,8 @@ public static void upgrade(Model model, String documentNamespace) throws Invalid
9489 RDFNode object = iter .next ();
9590 subject .addProperty (compatibleProperty , object );
9691 }
97- subject .removeAll (incompatibleProperty );
92+ // We'll leave the old property for compatibility
93+ // subject.removeAll(incompatibleProperty);
9894 }
9995 }
10096 }
@@ -103,18 +99,17 @@ public static void upgrade(Model model, String documentNamespace) throws Invalid
10399 upgradeArtifactOf (model , documentNamespace );
104100 upgradeReviewers (model , documentNamespace );
105101 upgradeExternalDocumentRefs (model , documentNamespace );
106- upgradeHasFiles (model , documentNamespace );
102+ upgradeHasFiles (model );
107103 } finally {
108104 model .leaveCriticalSection ();
109105 }
110106 }
111107
112108 /**
113109 * Changes all hasFile properties to CONTAINS relationships
114- * @param model
115- * @param documentNamespace
110+ * @param model RDF model
116111 */
117- private static void upgradeHasFiles (Model model , String documentNamespace ) {
112+ private static void upgradeHasFiles (Model model ) {
118113 List <Statement > statementsToRemove = new ArrayList <>();
119114 Property hasFileProperty = model .createProperty ("http://spdx.org/rdf/terms#hasFile" );
120115 Property relationshipProperty = model .createProperty (SpdxConstantsCompatV2 .SPDX_NAMESPACE + SpdxConstantsCompatV2 .PROP_RELATIONSHIP .getName ());
@@ -138,7 +133,7 @@ private static void upgradeHasFiles(Model model, String documentNamespace) {
138133 foundContainsRelationships .add (stmt );
139134 }
140135 });
141- if (foundContainsRelationships .size () == 0 ) {
136+ if (foundContainsRelationships .isEmpty () ) {
142137 Resource relationship = createRelationship (model , file , RelationshipType .CONTAINS );
143138 pkg .addProperty (relationshipProperty , relationship );
144139 }
@@ -149,9 +144,9 @@ private static void upgradeHasFiles(Model model, String documentNamespace) {
149144
150145 /**
151146 * Make sure all external document Ref's have a URI with proper ID rather than using the externalDocumentId property
152- * @param model
153- * @param documentNamespace
154- * @throws InvalidSPDXAnalysisException
147+ * @param model RDF model
148+ * @param documentNamespace Namespace or URI for the SPDX document
149+ * @throws InvalidSPDXAnalysisException on SPDX parsing errors
155150 */
156151 private static void upgradeExternalDocumentRefs (Model model , String documentNamespace ) throws InvalidSPDXAnalysisException {
157152 String query = "SELECT ?s ?o WHERE { ?s <http://spdx.org/rdf/terms#externalDocumentId> ?o }" ;
@@ -197,9 +192,9 @@ private static void upgradeExternalDocumentRefs(Model model, String documentName
197192
198193 /**
199194 * Upgrade the reviewers field to Annotations with a type reviewer
200- * @param model
201- * @param documentNamespace
202- * @throws InvalidSPDXAnalysisException
195+ * @param model RDF model
196+ * @param documentNamespace Namespace or URI for the SPDX document
197+ * @throws InvalidSPDXAnalysisException on SPDX parsing errors
203198 */
204199 private static void upgradeReviewers (Model model , String documentNamespace ) throws InvalidSPDXAnalysisException {
205200 Resource document = model .createResource (documentNamespace + "#" + SpdxConstantsCompatV2 .SPDX_DOCUMENT_ID );
@@ -267,13 +262,13 @@ private static void upgradeReviewers(Model model, String documentNamespace) thro
267262
268263 /**
269264 * Convert all artifactOf properties to relationships and remove the old properties and DOAP classes
270- * @param model
265+ * @param model RDF model
271266 * @param documentNamespace the document Namespace
272- * @throws InvalidSPDXAnalysisException
267+ * @throws InvalidSPDXAnalysisException on SPDX parsing errors
273268 */
274269 private static void upgradeArtifactOf (Model model , String documentNamespace ) throws InvalidSPDXAnalysisException {
275270 String docNamespace = documentNamespace + "#" ;
276- Set <String > addedDoapProjects = new HashSet <String >(); // prevent duplicates
271+ Set <String > addedDoapProjects = new HashSet <>(); // prevent duplicates
277272 List <Statement > statementsToRemove = new ArrayList <>();
278273 Property artifactOfProperty = model .createProperty ("http://spdx.org/rdf/terms#artifactOf" );
279274 Property relationshipProperty = model .createProperty (SpdxConstantsCompatV2 .SPDX_NAMESPACE + SpdxConstantsCompatV2 .PROP_RELATIONSHIP .getName ());
@@ -291,7 +286,7 @@ private static void upgradeArtifactOf(Model model, String documentNamespace) thr
291286 while (iter .hasNext ()) {
292287 statementsToRemove .add (iter .next ());
293288 }
294- Resource pkg = convertDoapProjectToSpdxPackage (model , doapProject , docNamespace + idPrefix + Integer . toString ( nextSpdxIdNum ) );
289+ Resource pkg = convertDoapProjectToSpdxPackage (model , doapProject , docNamespace + idPrefix + nextSpdxIdNum );
295290 if (pkg .isURIResource () && !addedDoapProjects .contains (pkg .getURI ())) {
296291 addedDoapProjects .add (pkg .getURI ());
297292 nextSpdxIdNum = getNexId (model , docNamespace , idPrefix , nextSpdxIdNum );
@@ -305,10 +300,10 @@ private static void upgradeArtifactOf(Model model, String documentNamespace) thr
305300
306301 /**
307302 * Creates an anonymous relationship resource
308- * @param model
309- * @param pkg
310- * @param relationshipType
311- * @return
303+ * @param model RDF model
304+ * @param relatedElement related element
305+ * @param relationshipType SPDX relationship type
306+ * @return resource for the relationship
312307 */
313308 private static Resource createRelationship (Model model , Resource relatedElement , RelationshipType relationshipType ) {
314309 Resource retval = model .createResource ();
@@ -321,28 +316,28 @@ private static Resource createRelationship(Model model, Resource relatedElement,
321316 }
322317
323318 /**
324- * @param model
325- * @param docNamespace
326- * @param idPrefix
319+ * @param model RDF model
320+ * @param docNamespace Namespace or URI for the SPDX document
321+ * @param idPrefix prefix for the ID to be used
327322 * @param startingNum Starting number to search for the next available ID
328323 * @return the next ID number available
329324 */
330325 private static int getNexId (Model model , String docNamespace , String idPrefix , int startingNum ) {
331326 int retval = startingNum ;
332- Resource idResource = model .getResource (docNamespace + idPrefix + Integer . toString ( retval ) );
327+ Resource idResource = model .getResource (docNamespace + idPrefix + retval );
333328 while (model .containsResource (idResource )) {
334329 retval ++;
335- idResource = model .getResource (docNamespace + idPrefix + Integer . toString ( retval ) );
330+ idResource = model .getResource (docNamespace + idPrefix + retval );
336331 }
337332 return retval ;
338333 }
339334
340335 /**
341336 * Convert a DOAP project resource into a package resource
342- * @param model
343- * @param doapProject
344- * @return
345- * @throws InvalidSPDXAnalysisException
337+ * @param model RDF model
338+ * @param doapProject DOAP project to convert
339+ * @return a related package representing the DOAP project
340+ * @throws InvalidSPDXAnalysisException on SPDX parsing errors
346341 */
347342 private static Resource convertDoapProjectToSpdxPackage (Model model , Resource doapProject , String packageUri ) throws InvalidSPDXAnalysisException {
348343 String packageName ;
@@ -354,7 +349,7 @@ private static Resource convertDoapProjectToSpdxPackage(Model model, Resource do
354349 if (Objects .isNull (packageName )) {
355350 throw new InvalidSPDXAnalysisException ("Missing required DOAP project name" );
356351 }
357- String homePage = null ;
352+ String homePage ;
358353 Property homePageProperty = model .createProperty (SpdxConstantsCompatV2 .DOAP_NAMESPACE + SpdxConstantsCompatV2 .PROP_PROJECT_HOMEPAGE .getName ());
359354 try {
360355 homePage = doapProject .getProperty (homePageProperty ).getString ();
0 commit comments