|
31 | 31 | import java.util.regex.Pattern;
|
32 | 32 | import java.util.stream.Collectors;
|
33 | 33 |
|
| 34 | +import javax.annotation.Nullable; |
| 35 | + |
34 | 36 | import org.slf4j.Logger;
|
35 | 37 | import org.slf4j.LoggerFactory;
|
36 | 38 | import org.spdx.core.IModelCopyManager;
|
@@ -106,23 +108,23 @@ public class Spdx2to3Converter implements ISpdxConverter {
|
106 | 108 |
|
107 | 109 | static final Pattern SPDX_2_CREATOR_PATTERN = Pattern.compile("(Person|Organization):\\s*([^(]+)\\s*(\\(([^)]*)\\))?");
|
108 | 110 |
|
109 |
| - private static final Map<org.spdx.library.model.v2.enumerations.RelationshipType, RelationshipType> RELATIONSHIP_TYPE_MAP; |
| 111 | + public static final Map<org.spdx.library.model.v2.enumerations.RelationshipType, RelationshipType> RELATIONSHIP_TYPE_MAP; |
110 | 112 |
|
111 |
| - private static final Map<org.spdx.library.model.v2.enumerations.RelationshipType, LifecycleScopeType> LIFECYCLE_SCOPE_MAP; |
| 113 | + public static final Map<org.spdx.library.model.v2.enumerations.RelationshipType, LifecycleScopeType> LIFECYCLE_SCOPE_MAP; |
112 | 114 |
|
113 |
| - private static final Set<org.spdx.library.model.v2.enumerations.RelationshipType> SWAP_TO_FROM_REL_TYPES; |
| 115 | + public static final Set<org.spdx.library.model.v2.enumerations.RelationshipType> SWAP_TO_FROM_REL_TYPES; |
114 | 116 |
|
115 |
| - private static final Map<org.spdx.library.model.v2.enumerations.AnnotationType, AnnotationType> ANNOTATION_TYPE_MAP; |
| 117 | + public static final Map<org.spdx.library.model.v2.enumerations.AnnotationType, AnnotationType> ANNOTATION_TYPE_MAP; |
116 | 118 |
|
117 |
| - private static final Map<org.spdx.library.model.v2.enumerations.ChecksumAlgorithm, HashAlgorithm> HASH_ALGORITH_MAP; |
| 119 | + public static final Map<org.spdx.library.model.v2.enumerations.ChecksumAlgorithm, HashAlgorithm> HASH_ALGORITH_MAP; |
118 | 120 |
|
119 |
| - private static final Map<String, ContentIdentifierType> CONTENT_IDENTIFIER_TYPE_MAP; |
| 121 | + public static final Map<String, ContentIdentifierType> CONTENT_IDENTIFIER_TYPE_MAP; |
120 | 122 |
|
121 |
| - private static final Map<String, ExternalIdentifierType> EXTERNAL_IDENTIFIER_TYPE_MAP; |
| 123 | + public static final Map<String, ExternalIdentifierType> EXTERNAL_IDENTIFIER_TYPE_MAP; |
122 | 124 |
|
123 |
| - private static final Map<String, ExternalRefType> EXTERNAL_REF_TYPE_MAP; |
| 125 | + public static final Map<String, ExternalRefType> EXTERNAL_REF_TYPE_MAP; |
124 | 126 |
|
125 |
| - private static final Map<org.spdx.library.model.v2.enumerations.Purpose, SoftwarePurpose> PURPOSE_MAP; |
| 127 | + public static final Map<org.spdx.library.model.v2.enumerations.Purpose, SoftwarePurpose> PURPOSE_MAP; |
126 | 128 |
|
127 | 129 | static {
|
128 | 130 | Map<org.spdx.library.model.v2.enumerations.RelationshipType, RelationshipType> relationshipTypeMap = new HashMap<>();
|
@@ -1377,44 +1379,74 @@ private void addPackageFileNameToPackage(String fileName,
|
1377 | 1379 | */
|
1378 | 1380 | private void addExternalRefToArtifact(org.spdx.library.model.v2.ExternalRef externalRef,
|
1379 | 1381 | SoftwareArtifact artifact) throws InvalidSPDXAnalysisException {
|
1380 |
| - org.spdx.library.model.v2.ReferenceType referenceType = externalRef.getReferenceType(); |
| 1382 | + addExternalRefToArtifact(externalRef, artifact, toModelStore); |
| 1383 | + } |
| 1384 | + |
| 1385 | + /** |
| 1386 | + * @param externalRef SPDX Spec version 2 External Ref to add to the package |
| 1387 | + * @param artifact SPDX Spec version 3 Artifact to add either an ExternalRef or ExternalId depending on the externalRef type |
| 1388 | + * @param modelStore modelStore to use for creating any SPDX objects |
| 1389 | + * @throws InvalidSPDXAnalysisException on any error in conversion |
| 1390 | + */ |
| 1391 | + public static void addExternalRefToArtifact(org.spdx.library.model.v2.ExternalRef externalRef, |
| 1392 | + SoftwareArtifact artifact, IModelStore modelStore) throws InvalidSPDXAnalysisException { |
| 1393 | + addExternalRefToArtifact(externalRef.getReferenceCategory(), externalRef.getReferenceType(), |
| 1394 | + externalRef.getReferenceLocator(), externalRef.getComment().orElse(null), artifact, modelStore); |
| 1395 | + } |
| 1396 | + |
| 1397 | + /** |
| 1398 | + * @param referenceCategory Reference category for external ref |
| 1399 | + * @param referenceType Reference type for external ref |
| 1400 | + * @param referenceLocator Locator for external ref |
| 1401 | + * @param comment External reference comment |
| 1402 | + * @param artifact Artifact which contains the external ref |
| 1403 | + * @param modelStore modelStore to use for creating any SPDX objects |
| 1404 | + * @throws InvalidSPDXAnalysisException on any error in conversion |
| 1405 | + */ |
| 1406 | + public static void addExternalRefToArtifact(org.spdx.library.model.v2.enumerations.ReferenceCategory referenceCategory, |
| 1407 | + org.spdx.library.model.v2.ReferenceType referenceType, String referenceLocator, @Nullable String comment, |
| 1408 | + SoftwareArtifact artifact, IModelStore modelStore) throws InvalidSPDXAnalysisException { |
1381 | 1409 | Objects.requireNonNull(referenceType);
|
1382 | 1410 | switch (referenceType.getIndividualURI()) {
|
1383 | 1411 | case SpdxConstantsCompatV2.SPDX_LISTED_REFERENCE_TYPES_PREFIX + "cpe22Type":
|
1384 | 1412 | case SpdxConstantsCompatV2.SPDX_LISTED_REFERENCE_TYPES_PREFIX + "cpe23Type":
|
1385 | 1413 | case SpdxConstantsCompatV2.SPDX_LISTED_REFERENCE_TYPES_PREFIX + "swid":
|
1386 |
| - artifact.getExternalIdentifiers().add(artifact.createExternalIdentifier(toModelStore.getNextId(IdType.Anonymous)) |
| 1414 | + artifact.getExternalIdentifiers().add(artifact.createExternalIdentifier(modelStore.getNextId(IdType.Anonymous)) |
1387 | 1415 | .setExternalIdentifierType(EXTERNAL_IDENTIFIER_TYPE_MAP.get(referenceType.getIndividualURI()))
|
1388 |
| - .setIdentifier(externalRef.getReferenceLocator()) |
| 1416 | + .setIdentifier(referenceLocator) |
| 1417 | + .setComment(comment) |
1389 | 1418 | .build()); break;
|
1390 | 1419 | case SpdxConstantsCompatV2.SPDX_LISTED_REFERENCE_TYPES_PREFIX + "purl": {
|
1391 | 1420 | if (artifact instanceof SpdxPackage) {
|
1392 |
| - ((SpdxPackage)artifact).setPackageUrl(externalRef.getReferenceLocator()); |
| 1421 | + ((SpdxPackage)artifact).setPackageUrl(referenceLocator); |
1393 | 1422 | } else {
|
1394 |
| - artifact.getExternalIdentifiers().add(artifact.createExternalIdentifier(toModelStore.getNextId(IdType.Anonymous)) |
| 1423 | + artifact.getExternalIdentifiers().add(artifact.createExternalIdentifier(modelStore.getNextId(IdType.Anonymous)) |
1395 | 1424 | .setExternalIdentifierType(EXTERNAL_IDENTIFIER_TYPE_MAP.get(referenceType.getIndividualURI()))
|
1396 |
| - .setIdentifier(externalRef.getReferenceLocator()) |
| 1425 | + .setIdentifier(referenceLocator) |
| 1426 | + .setComment(comment) |
1397 | 1427 | .build()); break;
|
1398 | 1428 | }
|
1399 | 1429 | } break;
|
1400 | 1430 | case SpdxConstantsCompatV2.SPDX_LISTED_REFERENCE_TYPES_PREFIX + "swh":
|
1401 | 1431 | case SpdxConstantsCompatV2.SPDX_LISTED_REFERENCE_TYPES_PREFIX + "gitoid":
|
1402 |
| - artifact.getContentIdentifiers().add(artifact.createContentIdentifier(toModelStore.getNextId(IdType.Anonymous)) |
| 1432 | + artifact.getContentIdentifiers().add(artifact.createContentIdentifier(modelStore.getNextId(IdType.Anonymous)) |
1403 | 1433 | .setContentIdentifierType(CONTENT_IDENTIFIER_TYPE_MAP.get(referenceType.getIndividualURI()))
|
1404 |
| - .setContentIdentifierValue(externalRef.getReferenceLocator()) |
| 1434 | + .setContentIdentifierValue(referenceLocator) |
| 1435 | + .setComment(comment) |
1405 | 1436 | .build()); break;
|
1406 | 1437 | default: {
|
1407 | 1438 | ExternalRefType externalRefType = EXTERNAL_REF_TYPE_MAP.get(referenceType.getIndividualURI());
|
1408 | 1439 | if (Objects.isNull(externalRefType)) {
|
1409 |
| - switch (externalRef.getReferenceCategory()) { |
| 1440 | + switch (referenceCategory) { |
1410 | 1441 | case PACKAGE_MANAGER: externalRefType = ExternalRefType.BUILD_SYSTEM; break;
|
1411 | 1442 | case SECURITY: externalRefType = ExternalRefType.SECURITY_OTHER; break;
|
1412 | 1443 | default: externalRefType = ExternalRefType.OTHER;
|
1413 | 1444 | }
|
1414 | 1445 | }
|
1415 |
| - artifact.getExternalRefs().add(artifact.createExternalRef(toModelStore.getNextId(IdType.Anonymous)) |
| 1446 | + artifact.getExternalRefs().add(artifact.createExternalRef(modelStore.getNextId(IdType.Anonymous)) |
1416 | 1447 | .setExternalRefType(externalRefType)
|
1417 |
| - .addLocator(externalRef.getReferenceLocator()) |
| 1448 | + .addLocator(referenceLocator) |
| 1449 | + .setComment(comment) |
1418 | 1450 | .build());
|
1419 | 1451 | }
|
1420 | 1452 | }
|
|
0 commit comments