Skip to content

Commit 6054e0b

Browse files
committedFeb 19, 2025
feat(spdx-utils): Expose simple mapping of licenses to SpdxExpression
Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
1 parent cf145cf commit 6054e0b

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed
 

‎utils/spdx/src/main/kotlin/SpdxExpression.kt

+12-10
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,12 @@ sealed class SpdxExpression {
107107
}
108108

109109
/**
110-
* Normalize all license IDs using [SpdxSimpleLicenseMapping]. If [mapDeprecated] is `true`, also deprecated IDs are
111-
* mapped to their current counterparts. The result of this function is not guaranteed to contain only valid IDs.
112-
* Use [validate] or [isValid] to check the returned [SpdxExpression] for validity afterwards.
110+
* Normalize all license IDs using [SpdxSimpleLicenseMapping]. If [mapDeprecated] is `true`, this involves mapping
111+
* deprecated IDs to their current counterparts. If [mapSimple] is `true`, also commonly known abbreviations or
112+
* aliases are mapped. The result of this function is not guaranteed to contain only valid IDs. Use [validate] or
113+
* [isValid] to check the returned [SpdxExpression] for validity afterwards.
113114
*/
114-
abstract fun normalize(mapDeprecated: Boolean = true): SpdxExpression
115+
abstract fun normalize(mapDeprecated: Boolean = true, mapSimple: Boolean = true): SpdxExpression
115116

116117
/**
117118
* Return a simplified expression that has e.g. redundancies removed.
@@ -246,8 +247,8 @@ class SpdxCompoundExpression(
246247

247248
override fun decompose(): Set<SpdxSingleLicenseExpression> = children.flatMapTo(mutableSetOf()) { it.decompose() }
248249

249-
override fun normalize(mapDeprecated: Boolean) =
250-
SpdxCompoundExpression(operator, children.map { it.normalize(mapDeprecated) })
250+
override fun normalize(mapDeprecated: Boolean, mapSimple: Boolean) =
251+
SpdxCompoundExpression(operator, children.map { it.normalize(mapDeprecated, mapSimple) })
251252

252253
private fun flatten(): SpdxExpression {
253254
val flattenedChildren = children.flatMapTo(mutableSetOf()) { child ->
@@ -460,10 +461,10 @@ class SpdxLicenseWithExceptionExpression(
460461

461462
override fun exception() = exception
462463

463-
override fun normalize(mapDeprecated: Boolean) =
464+
override fun normalize(mapDeprecated: Boolean, mapSimple: Boolean) =
464465
// Manually cast to SpdxSingleLicenseExpression as the type resolver does not recognize that in all subclasses
465466
// of SpdxSimpleExpression normalize() returns an SpdxSingleLicenseExpression.
466-
when (val normalizedLicense = license.normalize(mapDeprecated) as SpdxSingleLicenseExpression) {
467+
when (val normalizedLicense = license.normalize(mapDeprecated, mapSimple) as SpdxSingleLicenseExpression) {
467468
is SpdxSimpleExpression -> SpdxLicenseWithExceptionExpression(normalizedLicense, exception)
468469

469470
// This case happens if a deprecated license identifier that contains an exception is used together with
@@ -554,7 +555,8 @@ class SpdxLicenseIdExpression(
554555

555556
override fun exception(): String? = null
556557

557-
override fun normalize(mapDeprecated: Boolean) = SpdxSimpleLicenseMapping.map(toString(), mapDeprecated) ?: this
558+
override fun normalize(mapDeprecated: Boolean, mapSimple: Boolean) =
559+
SpdxSimpleLicenseMapping.map(toString(), mapDeprecated, mapSimple) ?: this
558560

559561
override fun validate(strictness: Strictness) {
560562
val isValid = SpdxConstants.isNotPresent(id) || when (strictness) {
@@ -607,7 +609,7 @@ data class SpdxLicenseReferenceExpression(
607609

608610
override fun exception(): String? = null
609611

610-
override fun normalize(mapDeprecated: Boolean) = this
612+
override fun normalize(mapDeprecated: Boolean, mapSimple: Boolean) = this
611613

612614
override fun validate(strictness: Strictness) {
613615
val isLicenseRef = id.startsWith(LICENSE_REF_PREFIX)

0 commit comments

Comments
 (0)
Please sign in to comment.