Skip to content

Commit 43903ca

Browse files
committed
Handle InvalidLicenseExpression types converting from SPDX 2 to 3
Fixes 406 Signed-off-by: Gary O'Neall <gary@sourceauditor.com>
1 parent 7baa445 commit 43903ca

1 file changed

Lines changed: 38 additions & 8 deletions

File tree

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

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import org.spdx.library.model.v3_0_1.expandedlicensing.OrLaterOperator;
7979
import org.spdx.library.model.v3_0_1.expandedlicensing.WithAdditionOperator;
8080
import org.spdx.library.model.v3_0_1.simplelicensing.AnyLicenseInfo;
81+
import org.spdx.library.model.v3_0_1.simplelicensing.InvalidLicenseExpression;
8182
import org.spdx.library.model.v3_0_1.simplelicensing.LicenseExpression;
8283
import org.spdx.library.model.v3_0_1.software.ContentIdentifierType;
8384
import org.spdx.library.model.v3_0_1.software.Snippet;
@@ -844,6 +845,33 @@ public OrLaterOperator convertAndStore(org.spdx.library.model.v2.license.OrLater
844845
toOrLaterOperator.setSubjectLicense((License)convertAndStore(fromOrLaterOperator.getLicense()));
845846
return toOrLaterOperator;
846847
}
848+
849+
/**
850+
* Converts an SPDX spec version 2 SPDX InvalidLicenseExpression to an SPDX spec version 3 SPDX InvalidLicenseExpression and store the result
851+
* @param fromInvalidExpression an SPDX spec version 2 InvalidLicenseExpression
852+
* @return an SPDX spec version 3 InvalidLicenseExpression
853+
* @throws InvalidSPDXAnalysisException on any errors converting
854+
*/
855+
public InvalidLicenseExpression convertAndStore(org.spdx.library.model.v2.license.InvalidLicenseExpression fromInvalidExpression) throws InvalidSPDXAnalysisException {
856+
Optional<ModelObjectV3> existing = getExistingObject(fromInvalidExpression.getObjectUri(),
857+
"SimpleLicensing.InvalidLicenseExpression"); //TODO: This should be included in the SPDXV3 Constants file
858+
if (existing.isPresent()) {
859+
return (InvalidLicenseExpression)existing.get();
860+
}
861+
String toObjectUri = toModelStore.getNextId(IdType.Anonymous);
862+
String existingUri = this.alreadyConverted.putIfAbsent(fromInvalidExpression.getObjectUri(), toObjectUri);
863+
if (Objects.nonNull(existingUri)) {
864+
// small window if conversion occurred since the last check already converted
865+
return (InvalidLicenseExpression)getExistingObject(fromInvalidExpression.getObjectUri(),
866+
"SimpleLicensing.InvalidLicenseExpression").get();
867+
}
868+
InvalidLicenseExpression toInvalidLicExpression = (InvalidLicenseExpression)SpdxModelClassFactoryV3.getModelObject(toModelStore,
869+
toObjectUri, "SimpleLicensing.InvalidLicenseExpression", copyManager, true, defaultUriPrefix);
870+
toInvalidLicExpression.setCreationInfo(defaultCreationInfo);
871+
toInvalidLicExpression.setMessage(fromInvalidExpression.getMessage());
872+
toInvalidLicExpression.setLicenseExpression(fromInvalidExpression.getMessage());
873+
return toInvalidLicExpression;
874+
}
847875

848876
/**
849877
* Converts an SPDX spec version 2 SPDX SpdxListedLicense to an SPDX spec version 3 SPDX ListedLicense and store the result
@@ -1043,26 +1071,28 @@ public AnyLicenseInfo convertAndStore(org.spdx.library.model.v2.license.AnyLicen
10431071
if (!complexLicenses) {
10441072
return convertToLicenseExpression(fromLicense);
10451073
} else if (fromLicense instanceof org.spdx.library.model.v2.license.ConjunctiveLicenseSet) {
1046-
return convertAndStore((org.spdx.library.model.v2.license.ConjunctiveLicenseSet)fromLicense);
1074+
return convertAndStore((org.spdx.library.model.v2.license.ConjunctiveLicenseSet) fromLicense);
10471075
} else if (fromLicense instanceof org.spdx.library.model.v2.license.DisjunctiveLicenseSet) {
1048-
return convertAndStore((org.spdx.library.model.v2.license.DisjunctiveLicenseSet)fromLicense);
1076+
return convertAndStore((org.spdx.library.model.v2.license.DisjunctiveLicenseSet) fromLicense);
10491077
} else if (fromLicense instanceof org.spdx.library.model.v2.license.ExternalExtractedLicenseInfo) {
1050-
String externalUri = ((org.spdx.library.model.v2.license.ExternalExtractedLicenseInfo)fromLicense).getIndividualURI();
1051-
logger.warn("Referencing an external SPDX 2 element with URI {} while converting from SPDX 2 to 3", externalUri);
1078+
String externalUri = ((org.spdx.library.model.v2.license.ExternalExtractedLicenseInfo) fromLicense).getIndividualURI();
1079+
logger.warn("Referencing an external SPDX 2 element with URI {} while converting from SPDX 2 to 3", externalUri);
10521080
addExternalMapInfo(externalUri);
10531081
return new ExternalCustomLicense(externalUri);
10541082
} else if (fromLicense instanceof org.spdx.library.model.v2.license.ExtractedLicenseInfo) {
1055-
return convertAndStore((org.spdx.library.model.v2.license.ExtractedLicenseInfo)fromLicense);
1083+
return convertAndStore((org.spdx.library.model.v2.license.ExtractedLicenseInfo) fromLicense);
10561084
} else if (fromLicense instanceof org.spdx.library.model.v2.license.OrLaterOperator) {
1057-
return convertAndStore((org.spdx.library.model.v2.license.OrLaterOperator)fromLicense);
1085+
return convertAndStore((org.spdx.library.model.v2.license.OrLaterOperator) fromLicense);
10581086
} else if (fromLicense instanceof org.spdx.library.model.v2.license.SpdxListedLicense) {
1059-
return convertAndStore((org.spdx.library.model.v2.license.SpdxListedLicense)fromLicense);
1087+
return convertAndStore((org.spdx.library.model.v2.license.SpdxListedLicense) fromLicense);
10601088
} else if (fromLicense instanceof org.spdx.library.model.v2.license.SpdxNoneLicense) {
10611089
return new NoneLicense();
10621090
} else if (fromLicense instanceof org.spdx.library.model.v2.license.SpdxNoAssertionLicense) {
10631091
return new NoAssertionLicense();
10641092
} else if (fromLicense instanceof org.spdx.library.model.v2.license.WithExceptionOperator) {
1065-
return convertAndStore((org.spdx.library.model.v2.license.WithExceptionOperator)fromLicense);
1093+
return convertAndStore((org.spdx.library.model.v2.license.WithExceptionOperator) fromLicense);
1094+
} else if (fromLicense instanceof org.spdx.library.model.v2.license.InvalidLicenseExpression) {
1095+
return convertAndStore((org.spdx.library.model.v2.license.InvalidLicenseExpression) fromLicense);
10661096
} else {
10671097
throw new InvalidSPDXAnalysisException("Can not convert the from AnyLicenseInfo type "+fromLicense.getType());
10681098
}

0 commit comments

Comments
 (0)