Skip to content

Commit 118cb55

Browse files
committed
Fix issues with String to Agent
Signed-off-by: Gary O'Neall <gary@sourceauditor.com>
1 parent 5fba1fc commit 118cb55

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public class Spdx2to3Converter implements ISpdxConverter {
100100

101101
static final Logger logger = LoggerFactory.getLogger(Spdx2to3Converter.class);
102102

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

105105
private static final Map<org.spdx.library.model.v2.enumerations.RelationshipType, RelationshipType> RELATIONSHIP_TYPE_MAP;
106106

@@ -314,6 +314,7 @@ public static CreationInfo convertCreationInfo(
314314
.setCreated(creationInfoV2.getCreated())
315315
.setSpecVersion(SpdxConstantsV3.MODEL_SPEC_VERSION)
316316
.build();
317+
retval.setIdPrefix(uriPrefix);
317318
for (String docCreator:creationInfoV2.getCreators()) {
318319
if (docCreator.startsWith(SpdxConstantsCompatV2.CREATOR_PREFIX_TOOL)) {
319320
Tool tool = (Tool)SpdxModelClassFactoryV3.getModelObject(modelStore,
@@ -337,6 +338,10 @@ public static CreationInfo convertCreationInfo(
337338
* @throws InvalidSPDXAnalysisException on any error in conversion
338339
*/
339340
public static Agent stringToAgent(String spdx2personOrgString, CreationInfo creationInfo) throws InvalidSPDXAnalysisException {
341+
Objects.requireNonNull(spdx2personOrgString, "Person/org string can not be null");
342+
Objects.requireNonNull(creationInfo, "Creation info required for stringToAgent");
343+
String idPrefix = creationInfo.getIdPrefix();
344+
Objects.requireNonNull(idPrefix, "Creation info must have an idPrefix to accurately generate SPDX IDs");
340345
Matcher matcher = SPDX_2_CREATOR_PATTERN.matcher(spdx2personOrgString);
341346
if (!matcher.matches()) {
342347
// return a generic Agent
@@ -348,18 +353,19 @@ public static Agent stringToAgent(String spdx2personOrgString, CreationInfo crea
348353
return agent;
349354
} else if (matcher.group(1).trim().equals("Person")) {
350355
Person person = (Person)SpdxModelClassFactoryV3.getModelObject(creationInfo.getModelStore(),
351-
creationInfo.getIdPrefix() + creationInfo.getModelStore().getNextId(IdType.SpdxId),
356+
idPrefix + creationInfo.getModelStore().getNextId(IdType.SpdxId),
352357
SpdxConstantsV3.CORE_PERSON, creationInfo.getCopyManager(), true, creationInfo.getIdPrefix());
353358
person.setCreationInfo(creationInfo);
354359
if (matcher.groupCount() > 1) {
355-
person.setName(matcher.group(2).trim());
360+
String personName = matcher.group(2).trim();
361+
person.setName(personName);
356362
} else {
357363
logger.warn("Missing person name in createdBy");
358364
person.setName("[MISSING]");
359365
}
360366
if (matcher.groupCount() > 3) {
361367
String email = matcher.group(4);
362-
if (Objects.nonNull(email)) {
368+
if (Objects.nonNull(email) && !email.isEmpty()) {
363369
person.getExternalIdentifiers().add(person.createExternalIdentifier(creationInfo.getModelStore().getNextId(IdType.Anonymous))
364370
.setExternalIdentifierType(ExternalIdentifierType.EMAIL)
365371
.setIdentifier(email)
@@ -373,14 +379,15 @@ public static Agent stringToAgent(String spdx2personOrgString, CreationInfo crea
373379
SpdxConstantsV3.CORE_ORGANIZATION, creationInfo.getCopyManager(), true, creationInfo.getIdPrefix());
374380
organization.setCreationInfo(creationInfo);
375381
if (matcher.groupCount() > 1) {
376-
organization.setName(matcher.group(2).trim());
382+
String origanizationName = matcher.group(2).trim();
383+
organization.setName(origanizationName);
377384
} else {
378385
logger.warn("Missing organization name");
379386
organization.setName("[MISSING]");
380387
}
381388
if (matcher.groupCount() > 3) {
382389
String email = matcher.group(4);
383-
if (Objects.nonNull(email)) {
390+
if (Objects.nonNull(email) && !email.isEmpty()) {
384391
organization.getExternalIdentifiers().add(organization.createExternalIdentifier(creationInfo.getModelStore().getNextId(IdType.Anonymous))
385392
.setExternalIdentifierType(ExternalIdentifierType.EMAIL)
386393
.setIdentifier(email)
@@ -547,6 +554,7 @@ public Annotation convertAndStore(org.spdx.library.model.v2.Annotation fromAnnot
547554
.setSpecVersion(SpdxConstantsV3.MODEL_SPEC_VERSION)
548555
.addAllCreatedUsing(defaultCreationInfo.getCreatedUsings())
549556
.build();
557+
creationInfo.setIdPrefix(defaultUriPrefix);
550558
creationInfo.getCreatedBys().add(stringToAgent(fromAnnotation.getAnnotator(), creationInfo));
551559
toAnnotation.setCreationInfo(creationInfo);
552560
return toAnnotation;
@@ -642,7 +650,7 @@ public ConjunctiveLicenseSet convertAndStore(org.spdx.library.model.v2.license.C
642650
if (existing.isPresent()) {
643651
return (ConjunctiveLicenseSet)existing.get();
644652
}
645-
String toObjectUri = toModelStore.getNextId(IdType.Anonymous);
653+
String toObjectUri = defaultUriPrefix + toModelStore.getNextId(IdType.SpdxId);
646654
String existingUri = this.alreadyConverted.putIfAbsent(fromConjunctiveLicenseSet.getObjectUri(), toObjectUri);
647655
if (Objects.nonNull(existingUri)) {
648656
// small window if conversion occurred since the last check already converted
@@ -669,7 +677,7 @@ public DisjunctiveLicenseSet convertAndStore(org.spdx.library.model.v2.license.D
669677
if (existing.isPresent()) {
670678
return (DisjunctiveLicenseSet)existing.get();
671679
}
672-
String toObjectUri = toModelStore.getNextId(IdType.Anonymous);
680+
String toObjectUri = defaultUriPrefix + toModelStore.getNextId(IdType.SpdxId);
673681
String existingUri = this.alreadyConverted.putIfAbsent(fromDisjunctiveLicenseSet.getObjectUri(), toObjectUri);
674682
if (Objects.nonNull(existingUri)) {
675683
// small window if conversion occurred since the last check already converted
@@ -724,7 +732,7 @@ public OrLaterOperator convertAndStore(org.spdx.library.model.v2.license.OrLater
724732
if (existing.isPresent()) {
725733
return (OrLaterOperator)existing.get();
726734
}
727-
String toObjectUri = toModelStore.getNextId(IdType.Anonymous);
735+
String toObjectUri = defaultUriPrefix + toModelStore.getNextId(IdType.SpdxId);
728736
String existingUri = this.alreadyConverted.putIfAbsent(fromOrLaterOperator.getObjectUri(), toObjectUri);
729737
if (Objects.nonNull(existingUri)) {
730738
// small window if conversion occurred since the last check already converted
@@ -793,7 +801,7 @@ public WithAdditionOperator convertAndStore(org.spdx.library.model.v2.license.Wi
793801
if (existing.isPresent()) {
794802
return (WithAdditionOperator)existing.get();
795803
}
796-
String toObjectUri = toModelStore.getNextId(IdType.Anonymous);
804+
String toObjectUri = defaultUriPrefix + toModelStore.getNextId(IdType.SpdxId);
797805
String existingUri = this.alreadyConverted.putIfAbsent(fromWithExceptionOperator.getObjectUri(), toObjectUri);
798806
if (Objects.nonNull(existingUri)) {
799807
// small window if conversion occurred since the last check already converted
@@ -823,7 +831,7 @@ public LicenseAddition convertAndStore(org.spdx.library.model.v2.license.License
823831
if (existing.isPresent()) {
824832
return (CustomLicenseAddition)existing.get();
825833
}
826-
String toObjectUri = toModelStore.getNextId(IdType.Anonymous);
834+
String toObjectUri = defaultUriPrefix + toModelStore.getNextId(IdType.SpdxId);
827835
String existingUri = this.alreadyConverted.putIfAbsent(fromException.getObjectUri(), toObjectUri);
828836
if (Objects.nonNull(existingUri)) {
829837
// small window if conversion occurred since the last check already converted

0 commit comments

Comments
 (0)