Skip to content

Commit 7fd2435

Browse files
committed
Make public static access to SPDX Converter
This makes some of the logic accessible to code outside the java library (e.g. the spdx-maven-plugin).
1 parent fdbd7be commit 7fd2435

File tree

1 file changed

+52
-20
lines changed

1 file changed

+52
-20
lines changed

src/main/java/org/spdx/library/conversion/Spdx2to3Converter.java

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.regex.Pattern;
3232
import java.util.stream.Collectors;
3333

34+
import javax.annotation.Nullable;
35+
3436
import org.slf4j.Logger;
3537
import org.slf4j.LoggerFactory;
3638
import org.spdx.core.IModelCopyManager;
@@ -106,23 +108,23 @@ public class Spdx2to3Converter implements ISpdxConverter {
106108

107109
static final Pattern SPDX_2_CREATOR_PATTERN = Pattern.compile("(Person|Organization):\\s*([^(]+)\\s*(\\(([^)]*)\\))?");
108110

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;
110112

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;
112114

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;
114116

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;
116118

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;
118120

119-
private static final Map<String, ContentIdentifierType> CONTENT_IDENTIFIER_TYPE_MAP;
121+
public static final Map<String, ContentIdentifierType> CONTENT_IDENTIFIER_TYPE_MAP;
120122

121-
private static final Map<String, ExternalIdentifierType> EXTERNAL_IDENTIFIER_TYPE_MAP;
123+
public static final Map<String, ExternalIdentifierType> EXTERNAL_IDENTIFIER_TYPE_MAP;
122124

123-
private static final Map<String, ExternalRefType> EXTERNAL_REF_TYPE_MAP;
125+
public static final Map<String, ExternalRefType> EXTERNAL_REF_TYPE_MAP;
124126

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;
126128

127129
static {
128130
Map<org.spdx.library.model.v2.enumerations.RelationshipType, RelationshipType> relationshipTypeMap = new HashMap<>();
@@ -1377,44 +1379,74 @@ private void addPackageFileNameToPackage(String fileName,
13771379
*/
13781380
private void addExternalRefToArtifact(org.spdx.library.model.v2.ExternalRef externalRef,
13791381
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 {
13811409
Objects.requireNonNull(referenceType);
13821410
switch (referenceType.getIndividualURI()) {
13831411
case SpdxConstantsCompatV2.SPDX_LISTED_REFERENCE_TYPES_PREFIX + "cpe22Type":
13841412
case SpdxConstantsCompatV2.SPDX_LISTED_REFERENCE_TYPES_PREFIX + "cpe23Type":
13851413
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))
13871415
.setExternalIdentifierType(EXTERNAL_IDENTIFIER_TYPE_MAP.get(referenceType.getIndividualURI()))
1388-
.setIdentifier(externalRef.getReferenceLocator())
1416+
.setIdentifier(referenceLocator)
1417+
.setComment(comment)
13891418
.build()); break;
13901419
case SpdxConstantsCompatV2.SPDX_LISTED_REFERENCE_TYPES_PREFIX + "purl": {
13911420
if (artifact instanceof SpdxPackage) {
1392-
((SpdxPackage)artifact).setPackageUrl(externalRef.getReferenceLocator());
1421+
((SpdxPackage)artifact).setPackageUrl(referenceLocator);
13931422
} else {
1394-
artifact.getExternalIdentifiers().add(artifact.createExternalIdentifier(toModelStore.getNextId(IdType.Anonymous))
1423+
artifact.getExternalIdentifiers().add(artifact.createExternalIdentifier(modelStore.getNextId(IdType.Anonymous))
13951424
.setExternalIdentifierType(EXTERNAL_IDENTIFIER_TYPE_MAP.get(referenceType.getIndividualURI()))
1396-
.setIdentifier(externalRef.getReferenceLocator())
1425+
.setIdentifier(referenceLocator)
1426+
.setComment(comment)
13971427
.build()); break;
13981428
}
13991429
} break;
14001430
case SpdxConstantsCompatV2.SPDX_LISTED_REFERENCE_TYPES_PREFIX + "swh":
14011431
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))
14031433
.setContentIdentifierType(CONTENT_IDENTIFIER_TYPE_MAP.get(referenceType.getIndividualURI()))
1404-
.setContentIdentifierValue(externalRef.getReferenceLocator())
1434+
.setContentIdentifierValue(referenceLocator)
1435+
.setComment(comment)
14051436
.build()); break;
14061437
default: {
14071438
ExternalRefType externalRefType = EXTERNAL_REF_TYPE_MAP.get(referenceType.getIndividualURI());
14081439
if (Objects.isNull(externalRefType)) {
1409-
switch (externalRef.getReferenceCategory()) {
1440+
switch (referenceCategory) {
14101441
case PACKAGE_MANAGER: externalRefType = ExternalRefType.BUILD_SYSTEM; break;
14111442
case SECURITY: externalRefType = ExternalRefType.SECURITY_OTHER; break;
14121443
default: externalRefType = ExternalRefType.OTHER;
14131444
}
14141445
}
1415-
artifact.getExternalRefs().add(artifact.createExternalRef(toModelStore.getNextId(IdType.Anonymous))
1446+
artifact.getExternalRefs().add(artifact.createExternalRef(modelStore.getNextId(IdType.Anonymous))
14161447
.setExternalRefType(externalRefType)
1417-
.addLocator(externalRef.getReferenceLocator())
1448+
.addLocator(referenceLocator)
1449+
.setComment(comment)
14181450
.build());
14191451
}
14201452
}

0 commit comments

Comments
 (0)