Skip to content

Commit

Permalink
Merge pull request #24 from spdx/v23
Browse files Browse the repository at this point in the history
Update tag/value parser for SPDX spec version 2.3
  • Loading branch information
goneall authored Aug 12, 2022
2 parents 5c7435d + b5a626c commit 4838c60
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 77 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.spdx</groupId>
<artifactId>spdx-tagvalue-store</artifactId>
<version>1.0.5-SNAPSHOT</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>spdx-tagvalue-store</name>
Expand Down Expand Up @@ -103,7 +103,7 @@
<dependency>
<groupId>org.spdx</groupId>
<artifactId>java-spdx-library</artifactId>
<version>1.0.10</version>
<version>1.1.0</version>
</dependency>
</dependencies>
<build>
Expand Down
30 changes: 27 additions & 3 deletions src/main/java/org/spdx/tag/BuildDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.spdx.library.model.enumerations.AnnotationType;
import org.spdx.library.model.enumerations.ChecksumAlgorithm;
import org.spdx.library.model.enumerations.FileType;
import org.spdx.library.model.enumerations.Purpose;
import org.spdx.library.model.enumerations.ReferenceCategory;
import org.spdx.library.model.enumerations.RelationshipType;
import org.spdx.library.model.license.AnyLicenseInfo;
Expand All @@ -71,9 +72,9 @@
/**
* Translates an tag-value file to a an SPDX Document.
*
* Supports SPDX version 2.0
* Supports SPDX version 2.3
*
* 2.0 changes made by Gary O'Neall
* 2.0, 2.1, 2.2 and 2.3 changes made by Gary O'Neall
*
* @author Rana Rahal, Protecode Inc.
*/
Expand Down Expand Up @@ -378,6 +379,10 @@ public BuildDocument(IModelStore modelStore, Properties constants, List<String>
this.PACKAGE_TAGS.add(constants.getProperty("PROP_EXTERNAL_REFERENCE_COMMENT").trim()+" ");
this.PACKAGE_TAGS.add(constants.getProperty("PROP_PACKAGE_FILES_ANALYZED").trim()+" ");
this.PACKAGE_TAGS.add(constants.getProperty("PROP_PACKAGE_ATTRIBUTION_TEXT").trim()+" ");
this.PACKAGE_TAGS.add(constants.getProperty("PROP_PRIMARY_PACKAGE_PURPOSE").trim() + " ");
this.PACKAGE_TAGS.add(constants.getProperty("PROP_PACKAGE_BUILT_DATE").trim() + " ");
this.PACKAGE_TAGS.add(constants.getProperty("PROP_PACKAGE_RELEASE_DATE").trim() + " ");
this.PACKAGE_TAGS.add(constants.getProperty("PROP_PACKAGE_VALID_UNTIL_DATE").trim() + " ");

this.EXTRACTED_LICENSE_TAGS.add(constants.getProperty("PROP_LICENSE_TEXT").trim()+" ");
this.EXTRACTED_LICENSE_TAGS.add(constants.getProperty("PROP_EXTRACTED_TEXT").trim()+" ");
Expand Down Expand Up @@ -938,6 +943,12 @@ private void buildPackage(SpdxPackage pkg, String tag, String value, int lineNum
pkg.setHomepage(value);
} else if (tag.equals(constants.getProperty("PROP_PACKAGE_SOURCE_INFO"))) {
pkg.setSourceInfo(value);
} else if (tag.equals(constants.getProperty("PROP_PACKAGE_BUILT_DATE"))) {
pkg.setBuiltDate(value);
} else if (tag.equals(constants.getProperty("PROP_PACKAGE_RELEASE_DATE"))) {
pkg.setReleaseDate(value);
} else if (tag.equals(constants.getProperty("PROP_PACKAGE_VALID_UNTIL_DATE"))) {
pkg.setValidUntilDate(value);
} else if (tag.equals(constants.getProperty("PROP_PACKAGE_CONCLUDED_LICENSE"))) {
AnyLicenseInfo licenseSet = LicenseInfoFactory.parseSPDXLicenseString(value, modelStore, documentNamespace, copyManager);
// can not verify any licenses at this point since the extracted license infos may not be set
Expand Down Expand Up @@ -1009,6 +1020,19 @@ private void buildPackage(SpdxPackage pkg, String tag, String value, int lineNum
pkg.setComment(value);
} else if (tag.equals(constants.getProperty("PROP_PACKAGE_ATTRIBUTION_TEXT"))) {
pkg.getAttributionText().add(value);
} else if (tag.equals(constants.getProperty("PROP_PRIMARY_PACKAGE_PURPOSE"))) {
Purpose purpose = null;
try {
purpose = Purpose.valueOf(value.trim());
} catch(IllegalArgumentException ex) {
try {
purpose = Purpose.valueOf(value.trim().toUpperCase());
this.warningMessages.add("Invalid Package Purpose - needs to be uppercased: "+value+" at line number "+lineNumber);
} catch(IllegalArgumentException ex2) {
throw(new InvalidSpdxTagFileException("Unknown Package Purpose: "+value+" at line number "+lineNumber));
}
}
pkg.setPrimaryPurpose(purpose);
} else if (tag.equals(constants.getProperty("PROP_PACKAGE_FILES_ANALYZED"))) {
if ("TRUE".equals(value.toUpperCase())) {
pkg.setFilesAnalyzed(true);
Expand Down Expand Up @@ -1081,7 +1105,7 @@ public static Checksum parseChecksum(String value, int lineNumber, SpdxDocument
throw(new InvalidSpdxTagFileException("Invalid checksum: "+value+" at line number "+lineNumber));
}
try {
ChecksumAlgorithm algorithm = ChecksumAlgorithm.valueOf(matcher.group(1));
ChecksumAlgorithm algorithm = ChecksumAlgorithm.valueOf(matcher.group(1).replaceAll("-","_"));
return document.createChecksum(algorithm, matcher.group(3));
} catch(IllegalArgumentException ex) {
throw(new InvalidSpdxTagFileException("Invalid checksum algorithm: "+value+" at line number "+lineNumber));
Expand Down
49 changes: 44 additions & 5 deletions src/main/java/org/spdx/tag/CommonCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.stream.Stream;

import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.SpdxConstants;
import org.spdx.library.model.Annotation;
import org.spdx.library.model.Checksum;
import org.spdx.library.model.ExternalDocumentRef;
Expand All @@ -58,6 +59,7 @@
import org.spdx.library.model.SpdxPackageVerificationCode;
import org.spdx.library.model.SpdxSnippet;
import org.spdx.library.model.enumerations.FileType;
import org.spdx.library.model.enumerations.Purpose;
import org.spdx.library.model.license.AnyLicenseInfo;
import org.spdx.library.model.license.ExtractedLicenseInfo;
import org.spdx.library.model.license.SimpleLicensingInfo;
Expand Down Expand Up @@ -510,6 +512,26 @@ private static void printPackage(SpdxPackage pkg, PrintWriter out,
constants.getProperty("PROP_PACKAGE_DOWNLOAD_URL")
+ downloadLocation.get());
}
// Primary Package Purpose
Optional<Purpose> purpose = pkg.getPrimaryPurpose();
if (purpose.isPresent()) {
println(out, constants.getProperty("PROP_PRIMARY_PACKAGE_PURPOSE") + purpose.get().toString());
}
// release date
Optional<String> releaseDate = pkg.getReleaseDate();
if (releaseDate.isPresent()) {
println(out, constants.getProperty("PROP_PACKAGE_RELEASE_DATE") + releaseDate.get());
}
// Built date
Optional<String> builtDate = pkg.getBuiltDate();
if (builtDate.isPresent()) {
println(out, constants.getProperty("PROP_PACKAGE_BUILT_DATE") + builtDate.get());
}
// Valid until date
Optional<String> validUntilDate = pkg.getValidUntilDate();
if (validUntilDate.isPresent()) {
println(out, constants.getProperty("PROP_PACKAGE_VALID_UNTIL_DATE") + validUntilDate.get());
}
// package verification code
Optional<SpdxPackageVerificationCode> verificationCode = pkg.getPackageVerificationCode();
if (verificationCode.isPresent()
Expand Down Expand Up @@ -585,11 +607,20 @@ private static void printPackage(SpdxPackage pkg, PrintWriter out,
constants.getProperty("PROP_END_TEXT"));
}
// Declared copyright
if (pkg.getCopyrightText() != null
String copyrightText = pkg.getCopyrightText();
if (copyrightText != null
&& !pkg.getCopyrightText().isEmpty()) {
println(out, constants.getProperty("PROP_PACKAGE_DECLARED_COPYRIGHT")
+ constants.getProperty("PROP_BEGIN_TEXT")
+ pkg.getCopyrightText() + constants.getProperty("PROP_END_TEXT"));
boolean encloseInText = !(SpdxConstants.NONE_VALUE.equals(copyrightText) ||
SpdxConstants.NOASSERTION_VALUE.equals(copyrightText));
print(out, constants.getProperty("PROP_PACKAGE_DECLARED_COPYRIGHT"));
if (encloseInText) {
print(out, constants.getProperty("PROP_BEGIN_TEXT"));
}
print(out, copyrightText);
if (encloseInText) {
print(out, constants.getProperty("PROP_END_TEXT"));
}
println(out, "");
}
// Short description
Optional<String> summary = pkg.getSummary();
Expand Down Expand Up @@ -708,7 +739,7 @@ private static void printExternalRef(PrintWriter out, Properties constants,
private static void printChecksum(Checksum checksum, PrintWriter out,
Properties constants, String checksumProperty) throws InvalidSPDXAnalysisException {
out.println(constants.getProperty(checksumProperty)
+ checksum.getAlgorithm().toString()
+ checksum.getAlgorithm().toString().replaceAll("_", "-")
+ ": " + checksum.getValue());
}

Expand Down Expand Up @@ -807,6 +838,14 @@ private static void println(PrintWriter out, String output) {
System.out.println(output);
}
}

private static void print(PrintWriter out, String output) {
if (out != null) {
out.print(output);
} else {
System.out.print(output);
}
}

public static Properties getTextFromProperties(final String path)
throws IOException {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/spdx/tag/SpdxTagValueConstants.properties
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ PROP_PACKAGE_FILES_ANALYZED=FilesAnalyzed:
PROP_EXTERNAL_REFERENCE=ExternalRef:
PROP_EXTERNAL_REFERENCE_COMMENT=ExternalRefComment:
PROP_PACKAGE_ATTRIBUTION_TEXT=PackageAttributionText:
PROP_PRIMARY_PACKAGE_PURPOSE=PrimaryPackagePurpose:
PROP_PACKAGE_BUILT_DATE=BuiltDate:
PROP_PACKAGE_RELEASE_DATE=ReleaseDate:
PROP_PACKAGE_VALID_UNTIL_DATE=ValidUntilDate:

#SPDX License Properties
PROP_LICENSE_ID=LicenseID:
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/spdx/tag/SpdxViewerConstants.properties
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ PROP_PACKAGE_FILES_ANALYZED=Files Analyzed:
PROP_EXTERNAL_REFERENCE=External Reference:
PROP_EXTERNAL_REFERENCE_COMMENT=\tReference Comment:
PROP_PACKAGE_ATTRIBUTION_TEXT=Attribution Text:
PROP_PRIMARY_PACKAGE_PURPOSE=Primary Package Purpose:
PROP_PACKAGE_BUILT_DATE=Built Date:
PROP_PACKAGE_RELEASE_DATE=Release Date:
PROP_PACKAGE_VALID_UNTIL_DATE=Valid Until Date:

#SPDX License Properties
PROP_LICENSE_ID=\tLicense ID:
Expand Down
Loading

0 comments on commit 4838c60

Please sign in to comment.