Skip to content

Commit 95aba1f

Browse files
goneallbact
andauthored
Improve license parsing error handling (#309)
* Improve license parsing error handling Checks simple license tokens to make sure they are either a listed license or a LicenseRef- Fixes #227 * Fix typos in description Co-authored-by: Arthit Suriyawongkul <[email protected]> Signed-off-by: Gary O'Neall <[email protected]> * Add check for addition ref syntax * Fix unit tests for AdditionRefs * Update src/main/java/org/spdx/utility/license/LicenseExpressionParser.java Signed-off-by: Arthit Suriyawongkul <[email protected]> * Update src/main/java/org/spdx/utility/license/LicenseExpressionParser.java Signed-off-by: Arthit Suriyawongkul <[email protected]> * Update src/main/java/org/spdx/utility/license/LicenseExpressionParser.java Signed-off-by: Arthit Suriyawongkul <[email protected]> * Update src/test/java/org/spdx/utility/license/LicenseExpressionParserTest.java Signed-off-by: Arthit Suriyawongkul <[email protected]> --------- Signed-off-by: Gary O'Neall <[email protected]> Signed-off-by: Arthit Suriyawongkul <[email protected]> Co-authored-by: Arthit Suriyawongkul <[email protected]>
1 parent 15a7461 commit 95aba1f

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

src/main/java/org/spdx/utility/license/LicenseExpressionParser.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ private static LicenseAddition parseSimpleLicenseAdditionToken(String token, IMo
440440
}
441441
return new ListedLicenseException(store, listedException.getObjectUri(), copyManager,
442442
true, customLicenseUriPrefix);
443-
} else {
443+
} else if (token.toLowerCase().startsWith("additionref-")) {
444444
// custom addition
445445
String objectUri = customLicenseUriPrefix + token;
446446
CustomLicenseAddition localAddition;
@@ -451,12 +451,13 @@ private static LicenseAddition parseSimpleLicenseAdditionToken(String token, IMo
451451
localAddition.setAdditionText(UNINITIALIZED_LICENSE_TEXT);
452452
}
453453
return localAddition;
454+
} else {
455+
throw new LicenseParserException(String.format("Invalid license addition %s. Must be either a listed license exception or be prefixed with 'AdditionRef-'", token));
454456
}
455457
}
456458

457459
/**
458-
* Converts a string token into its equivalent license
459-
* checking for a listed license
460+
* Converts a string token into its equivalent license - either a listed license or a LicenseRef
460461
* @param token Token to translate to the equivalent license
461462
* @param store Store for the licenses
462463
* @param customLicenseUriPrefix Prefix to use for any created local licenses or additions
@@ -490,7 +491,7 @@ private static AnyLicenseInfo parseSimpleLicenseToken(String token, IModelStore
490491
}
491492
}
492493
return new ListedLicense(store, listedLicense.getObjectUri(), copyManager, true, SpdxConstantsV3.SPDX_LISTED_LICENSE_NAMESPACE);
493-
} else {
494+
} else if (token.toLowerCase().startsWith("licenseref-")) {
494495
// LicenseRef
495496
String objectUri = customLicenseUriPrefix + token;
496497
CustomLicense localLicense;
@@ -501,6 +502,10 @@ private static AnyLicenseInfo parseSimpleLicenseToken(String token, IModelStore
501502
localLicense.setLicenseText(UNINITIALIZED_LICENSE_TEXT);
502503
}
503504
return localLicense;
505+
} else if (LicenseInfoFactory.isSpdxListedExceptionId(token)) {
506+
throw new LicenseParserException(String.format("Unexpected listed license exception %s. Must be a listed license or a LicenseRef", token));
507+
} else {
508+
throw new LicenseParserException(String.format("Unknown license %s. Must be a listed license or have the syntax %s", token, SpdxConstantsCompatV2.LICENSE_ID_PATTERN));
504509
}
505510
}
506511

@@ -520,26 +525,23 @@ private static String convertToExternalObjectUri(String externalReference, @Null
520525
throw new LicenseParserException("Invalid external ID: "+externalReference);
521526
}
522527
String namespace = null;
523-
if (Objects.nonNull(customIdToUri)) {
524-
for (DictionaryEntry entry : customIdToUri) {
525-
if (refParts[0].equals(entry.getIdPrefix())) {
526-
Optional<String> entryValue = entry.getValue();
527-
if (!entryValue.isPresent()) {
528-
throw new LicenseParserException("No associated namespace for license ID prefix "+entry.getIdPrefix());
529-
}
530-
namespace = entryValue.get();
531-
}
532-
}
533-
}
534-
if (Objects.isNull(namespace)) {
528+
for (DictionaryEntry entry : customIdToUri) {
529+
if (refParts[0].equals(entry.getIdPrefix())) {
530+
Optional<String> entryValue = entry.getValue();
531+
if (!entryValue.isPresent()) {
532+
throw new LicenseParserException("No associated namespace for license ID prefix " + entry.getIdPrefix());
533+
}
534+
namespace = entryValue.get();
535+
}
536+
}
537+
if (Objects.isNull(namespace)) {
535538
throw new LicenseParserException("No ID Prefix "+refParts[0]+" found in the customIdToUri map");
536539
}
537540
return namespace + refParts[1];
538541
}
539542

540543
/**
541-
* Converts a string token into its equivalent license
542-
* checking for a listed license
544+
* Converts a string token into its equivalent license - either a listed license or a LicenseRef
543545
* @param token license ID token
544546
* @param store model store for non-listed licenses
545547
* @param documentUri document URI for non-listed licenses
@@ -568,14 +570,13 @@ private static org.spdx.library.model.v2.license.AnyLicenseInfo parseSimpleLicen
568570
SpdxListedLicense listedLicense = LicenseInfoFactory.getListedLicenseByIdCompatV2(licenseId.get());
569571
if (Objects.nonNull(copyManager)) {
570572
// copy to the local store
571-
copyManager.copy(store, listedLicense.getObjectUri(), listedLicense.getModelStore(),
573+
copyManager.copy(store, listedLicense.getObjectUri(), listedLicense.getModelStore(),
572574
listedLicense.getObjectUri(), ModelObjectV2.LATEST_SPDX_2_VERSION, listedLicense.getDocumentUri());
573575
}
574576
}
575-
return (org.spdx.library.model.v2.license.AnyLicenseInfo) org.spdx.library.model.v2.SpdxModelFactoryCompatV2.getModelObjectV2(store, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX,
577+
return (org.spdx.library.model.v2.license.AnyLicenseInfo) org.spdx.library.model.v2.SpdxModelFactoryCompatV2.getModelObjectV2(store, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX,
576578
licenseId.get(), SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE, copyManager, true);
577-
} else {
578-
// LicenseRef
579+
} else if (token.toLowerCase().startsWith("licenseref-")) {
579580
Optional<String> caseSensitiveId = store.getCaseSensitiveId(documentUri, token);
580581
ExtractedLicenseInfo localLicense;
581582
if (caseSensitiveId.isPresent()) {
@@ -587,6 +588,10 @@ private static org.spdx.library.model.v2.license.AnyLicenseInfo parseSimpleLicen
587588
localLicense.setExtractedText(UNINITIALIZED_LICENSE_TEXT);
588589
}
589590
return localLicense;
591+
} else if (LicenseInfoFactory.isSpdxListedExceptionId(token)) {
592+
throw new LicenseParserException(String.format("Unexpected listed license exception %s. Must be a listed license or a LicenseRef", token));
593+
} else {
594+
throw new LicenseParserException(String.format("Unknown license %s. Must be a listed license or have the syntax %s", token, SpdxConstantsCompatV2.LICENSE_ID_PATTERN));
590595
}
591596
}
592597

src/test/java/org/spdx/utility/license/LicenseExpressionParserTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public class LicenseExpressionParserTest extends TestCase {
6161
static final String[] STD_TEXTS = new String[] {"Academic Free License (", "CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B",
6262
"European Union Public Licence", "Afmparse License"};
6363
static final String[] NONSTD_TEXTS = new String[] {"text1", "text2", "text3", "text4"};
64-
static final String[] NONSTD_ADDITION_IDS = new String[] {"exception-1","exception-2", "exception-3", "exception-4"};
64+
static final String[] NONSTD_ADDITION_IDS = new String[] {"AdditionRef-exception-1", "AdditionRef-exception-2",
65+
"AdditionRef-exception-3", "AdditionRef-exception-4"};
6566
static final String[] STD_EXCEPTION_IDS = new String[] {"389-exception", "Autoconf-exception-2.0"};
6667
static final String[] NON_STD_ADDITION_NAMES = new String[] {"exName-1", "exName-2", "exName-3", "exName-4"};
6768
static final String[] NON_STD_ADDITION_TEXTS = new String[] {"Ex text 1", "Ex text 2", "Ex text 3", "Ex text 4"};

0 commit comments

Comments
 (0)