Skip to content

Commit 7bd86c2

Browse files
committed
Add InvalidLicenseExpression class
Supports improvements to the license parsing error handling. See spdx/Spdx-Java-Library#339 for reference.
1 parent 7ee8808 commit 7bd86c2

File tree

5 files changed

+222
-2
lines changed

5 files changed

+222
-2
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) 2025 Source Auditor Inc.
3+
* SPDX-FileType: SOURCE
4+
* SPDX-License-Identifier: Apache-2.0
5+
* <p>
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* <p>
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* <p>
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.spdx.library.model.{{versionSuffix}}.simplelicensing;
20+
21+
import org.spdx.core.DefaultModelStore;
22+
import org.spdx.core.IModelCopyManager;
23+
import org.spdx.core.IndividualUriValue;
24+
import org.spdx.core.InvalidSPDXAnalysisException;
25+
import org.spdx.library.model.{{versionSuffix}}.SpdxConstantsV3;
26+
import org.spdx.library.model.{{versionSuffix}}.core.CreationInfo;
27+
import org.spdx.storage.IModelStore;
28+
import org.spdx.storage.PropertyDescriptor;
29+
30+
import javax.annotation.Nullable;
31+
32+
import java.util.*;
33+
import java.util.regex.Pattern;
34+
35+
import static org.spdx.library.model.{{versionSuffix}}.SpdxConstantsV3.SIMPLE_LICENSING_NAMESPACE;
36+
37+
/**
38+
* DO NOT EDIT - this file is generated by the Owl to Java Utility
39+
* See: <a href="https://github.com/spdx/tools-java">tools-java</a>
40+
* <p>
41+
* Represents a license expression that can not be parsed
42+
*/
43+
public class InvalidLicenseExpression extends LicenseExpression {
44+
45+
public static final PropertyDescriptor MESSAGE_PROPERTY = new PropertyDescriptor("SimpleLicensing.invalidLicenseMessage", SIMPLE_LICENSING_NAMESPACE);
46+
public static final String INVALID_LICENSE_EXPRESSION_TYPE = "SimpleLicensing.InvalidLicenseExpression";
47+
48+
/**
49+
* Create the InvalidLicenseExpression with default model store and generated anonymous ID
50+
* @throws InvalidSPDXAnalysisException when unable to create the LicenseExpression
51+
*/
52+
public InvalidLicenseExpression() throws InvalidSPDXAnalysisException {
53+
super();
54+
}
55+
56+
/**
57+
* @param objectUri URI or anonymous ID for the LicenseExpression
58+
* @throws InvalidSPDXAnalysisException when unable to create the LicenseExpression
59+
*/
60+
public InvalidLicenseExpression(String objectUri) throws InvalidSPDXAnalysisException {
61+
this(DefaultModelStore.getDefaultModelStore(), objectUri, DefaultModelStore.getDefaultCopyManager(),
62+
true, DefaultModelStore.getDefaultDocumentUri() + "#");
63+
}
64+
65+
/**
66+
* @param modelStore Model store where the LicenseExpression is to be stored
67+
* @param objectUri URI or anonymous ID for the LicenseExpression
68+
* @param copyManager Copy manager for the LicenseExpression - can be null if copying is not required
69+
* @param create true if LicenseExpression is to be created
70+
* @param idPrefix - prefix to be used when generating new SPDX IDs
71+
* @throws InvalidSPDXAnalysisException when unable to create the LicenseExpression
72+
*/
73+
public InvalidLicenseExpression(IModelStore modelStore, String objectUri, @Nullable IModelCopyManager copyManager,
74+
boolean create, String idPrefix) throws InvalidSPDXAnalysisException {
75+
super(modelStore, objectUri, copyManager, create, idPrefix);
76+
}
77+
78+
@Override
79+
public String getType() {
80+
return INVALID_LICENSE_EXPRESSION_TYPE;
81+
}
82+
83+
84+
// Getters and Setters
85+
86+
/**
87+
* @param creationInfo the creationInfo to set
88+
* @return this to chain setters
89+
* @throws InvalidSPDXAnalysisException on model store exceptions
90+
*/
91+
@Override
92+
public InvalidLicenseExpression setCreationInfo(@Nullable CreationInfo creationInfo) throws InvalidSPDXAnalysisException {
93+
super.setCreationInfo(creationInfo);
94+
return this;
95+
}
96+
/**
97+
* @param summary the summary to set
98+
* @return this to chain setters
99+
* @throws InvalidSPDXAnalysisException on model store exceptions
100+
*/
101+
@Override
102+
public InvalidLicenseExpression setSummary(@Nullable String summary) throws InvalidSPDXAnalysisException {
103+
super.setSummary(summary);
104+
return this;
105+
}
106+
/**
107+
* @param description the description to set
108+
* @return this to chain setters
109+
* @throws InvalidSPDXAnalysisException on model store exceptions
110+
*/
111+
@Override
112+
public InvalidLicenseExpression setDescription(@Nullable String description) throws InvalidSPDXAnalysisException {
113+
super.setDescription(description);
114+
return this;
115+
}
116+
117+
/**
118+
* @param licenseExpression the licenseExpression to set
119+
* @return this to chain setters
120+
* @throws InvalidSPDXAnalysisException on model store exceptions
121+
*/
122+
public InvalidLicenseExpression setLicenseExpression(@Nullable String licenseExpression) throws InvalidSPDXAnalysisException {
123+
if (isStrict() && Objects.isNull(licenseExpression)) {
124+
throw new InvalidSPDXAnalysisException("licenseExpression is a required property");
125+
}
126+
setPropertyValue(SpdxConstantsV3.PROP_LICENSE_EXPRESSION, licenseExpression);
127+
return this;
128+
}
129+
130+
/**
131+
* @param licenseListVersion the licenseListVersion to set
132+
* @return this to chain setters
133+
* @throws InvalidSPDXAnalysisException on model store exceptions
134+
*/
135+
public InvalidLicenseExpression setLicenseListVersion(@Nullable String licenseListVersion) throws InvalidSPDXAnalysisException {
136+
if (isStrict() && Objects.nonNull(licenseListVersion) && !Pattern.matches("^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$", licenseListVersion)) {
137+
throw new InvalidSPDXAnalysisException("licenseListVersion value '" + licenseListVersion +
138+
"' does not match the pattern '^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$'");
139+
}
140+
setPropertyValue(SpdxConstantsV3.PROP_LICENSE_LIST_VERSION, licenseListVersion);
141+
return this;
142+
}
143+
/**
144+
* @param comment the comment to set
145+
* @return this to chain setters
146+
* @throws InvalidSPDXAnalysisException on model store exceptions
147+
*/
148+
@Override
149+
public InvalidLicenseExpression setComment(@Nullable String comment) throws InvalidSPDXAnalysisException {
150+
super.setComment(comment);
151+
return this;
152+
}
153+
/**
154+
* @param name the name to set
155+
* @return this to chain setters
156+
* @throws InvalidSPDXAnalysisException on model store exceptions
157+
*/
158+
@Override
159+
public InvalidLicenseExpression setName(@Nullable String name) throws InvalidSPDXAnalysisException {
160+
super.setName(name);
161+
return this;
162+
}
163+
164+
/**
165+
* @return the licenseExpression
166+
*/
167+
public @Nullable String getMessage() throws InvalidSPDXAnalysisException {
168+
Optional<String> retval = getStringPropertyValue(MESSAGE_PROPERTY);
169+
return retval.orElse(null);
170+
}
171+
/**
172+
* @param message the message to set
173+
* @return this to chain setters
174+
* @throws InvalidSPDXAnalysisException on invalid message
175+
*/
176+
public LicenseExpression setMessage(@Nullable String message) throws InvalidSPDXAnalysisException {
177+
if (isStrict() && Objects.isNull(message)) {
178+
throw new InvalidSPDXAnalysisException("Message is a required property");
179+
}
180+
setPropertyValue(MESSAGE_PROPERTY, message);
181+
return this;
182+
}
183+
184+
/* (non-Javadoc)
185+
* @see org.spdx.library.model.v3.ModelObjectV3#_verify(java.util.List)
186+
*/
187+
@Override
188+
public List<String> _verify(Set<String> verifiedIds, String specVersionForVerify, List<IndividualUriValue> profiles) {
189+
List<String> retval = new ArrayList<>();
190+
try {
191+
retval.add(String.format("Invalid license expression '%s': %s",
192+
getLicenseExpression(), getMessage()));
193+
} catch(Exception e) {
194+
retval.add(String.format("Error getting properties: %s", e.getMessage()));
195+
}
196+
return retval;
197+
}
198+
}

resources/javaTemplates/ModelClassFactoryTemplate.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class SpdxModelClassFactoryV3 {
4343
{{#typeToClass}}
4444
typeToClassV3.put(SpdxConstantsV3.{{{classConstant}}}, {{{classPath}}}.class);
4545
{{/typeToClass}}
46+
typeToClassV3.put(org.spdx.library.model.{{versionSuffix}}.simplelicensing.InvalidLicenseExpression.INVALID_LICENSE_EXPRESSION_TYPE, org.spdx.library.model.{{versionSuffix}}.simplelicensing.InvalidLicenseExpression.class);
4647

4748
SPDX_TYPE_TO_CLASS_V3 = Collections.unmodifiableMap(typeToClassV3);
4849

resources/javaTemplates/PomTemplate.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>org.spdx</groupId>
44
<artifactId>spdx-java-model-3_0</artifactId>
5-
<version>1.0.0-RC2</version>
5+
<version>1.0.1</version>
66
<name>spdx-java-model-3</name>
77
<description>Generated java model source code</description>
88
<url>https://github.com/spdx/spdx-java-model-3_0</url>
@@ -107,7 +107,7 @@
107107
<dependency>
108108
<groupId>org.spdx</groupId>
109109
<artifactId>spdx-java-core</artifactId>
110-
<version>1.0.0-RC2</version>
110+
<version>1.0.0</version>
111111
</dependency>
112112
<dependency>
113113
<groupId>org.slf4j</groupId>

src/main/java/org/spdx/tools/model2java/ShaclToJava.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ public List<String> generate(File dir) throws IOException, ShaclToJavaException
320320
generateIndividualFactory(dir);
321321
//TODO: Get the version from the SHACL file
322322
generateMockFiles(dir);
323+
generateInvalidLicenseExpression(dir);
323324
return warnings;
324325
}
325326

@@ -474,6 +475,25 @@ private void generatePomFile(File dir) throws IOException {
474475
writeMustacheFile(ShaclToJavaConstants.POM_TEMPLATE, file, new HashMap<>());
475476
}
476477

478+
/**
479+
* @param dir
480+
* @throws IOException
481+
*/
482+
private void generateInvalidLicenseExpression(File dir) throws IOException {
483+
Path path = dir.toPath().resolve("src").resolve("main").resolve("java").resolve("org")
484+
.resolve("spdx").resolve("library").resolve("model").resolve(versionSuffix)
485+
.resolve("simplelicensing");
486+
Files.createDirectories(path);
487+
File file = path.resolve("InvalidLicenseExpression.java").toFile();
488+
if (!file.createNewFile()) {
489+
throw new IOException(String.format("Unable to create %s", file.toString()));
490+
}
491+
Map<String, Object> mustacheMap = new HashMap<>();
492+
mustacheMap.put("versionSuffix", versionSuffix);
493+
mustacheMap.put("versionSemVer", versionSemVer);
494+
writeMustacheFile(ShaclToJavaConstants.INVALID_LICENSE_EXPRESSION_TEMPLATE, file, mustacheMap);
495+
}
496+
477497
/**
478498
* @param dir
479499
* @throws IOException

src/main/java/org/spdx/tools/model2java/ShaclToJavaConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class ShaclToJavaConstants {
6868
public static final String UNIT_TEST_HELPER_TEMPLATE = "UnitTestHelperTemplate.txt";
6969
public static final String TEST_VALUES_GENERATOR_TEMPLATE = "TestValuesGeneratorTemplate.txt";
7070
public static final String TEST_MODEL_INFO_TEMPLATE = "TestModelInfoTemplate.txt";
71+
public static final String INVALID_LICENSE_EXPRESSION_TEMPLATE = "InvalidLicenseExpressionTemplate.txt";
7172

7273
public static Set<String> INTEGER_TYPES = new HashSet<>();
7374
static {

0 commit comments

Comments
 (0)