Skip to content

Commit caad98b

Browse files
committed
Add creationInfo during license parsing
Previous SPDX version 3 license parsing results produced invalid license due to missing creation informations. This PR introduces a potential incompatibility by requiring a creationInfo parameter to the license parser. This was intentional so that any callers of the method would be required to consider the correct creationInfo to be used. Signed-off-by: Gary O'Neall <[email protected]>
1 parent 33b9bff commit caad98b

File tree

3 files changed

+125
-50
lines changed

3 files changed

+125
-50
lines changed

src/main/java/org/spdx/library/LicenseInfoFactory.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.spdx.core.InvalidSPDXAnalysisException;
3232
import org.spdx.library.model.v2.license.InvalidLicenseStringException;
3333
import org.spdx.library.model.v2.license.SpdxListedLicense;
34+
import org.spdx.library.model.v3_0_1.SpdxModelClassFactoryV3;
3435
import org.spdx.library.model.v3_0_1.core.CreationInfo;
3536
import org.spdx.library.model.v3_0_1.core.DictionaryEntry;
3637
import org.spdx.library.model.v3_0_1.expandedlicensing.ListedLicense;
@@ -143,14 +144,17 @@ public static org.spdx.library.model.v2.license.AnyLicenseInfo parseSPDXLicenseS
143144
* none exist for an ID, they will be added. If null, the default model store will be used.
144145
* @param customLicensePrefix Prefix to use for any custom licenses or addition IDs found in the string. If the resultant object URI does not exist
145146
* for an ID, they will be added. If null, the default model document URI + "#" will be used.
147+
* @param creationInfo Creation information to use for newly created elements. If null, the default
146148
* @param copyManager allows for copying of any properties set which use other model stores or document URI's. If null, the default will be used.
147149
* @param customIdToUri Mapping of the id prefixes used in the license expression to the namespace preceding the external ID
148150
* @return an SPDXLicenseInfo created from the string. If the license expression is not parseable, a <code>InvalidLicenseExpression</code> is returned.
149151
* @throws DefaultStoreNotInitializedException if the default model store is not initialized
150152
*/
151-
public static AnyLicenseInfo parseSPDXLicenseString(String licenseString, @Nullable IModelStore store,
152-
@Nullable String customLicensePrefix, @Nullable IModelCopyManager copyManager,
153-
@Nullable List<DictionaryEntry> customIdToUri) throws InvalidLicenseStringException, DefaultStoreNotInitializedException {
153+
public static AnyLicenseInfo parseSPDXLicenseString(String licenseString, @Nullable IModelStore store,
154+
@Nullable String customLicensePrefix,
155+
@Nullable CreationInfo creationInfo,
156+
@Nullable IModelCopyManager copyManager,
157+
@Nullable List<DictionaryEntry> customIdToUri) throws InvalidLicenseStringException, DefaultStoreNotInitializedException {
154158
if (Objects.isNull(store)) {
155159
store = DefaultModelStore.getDefaultModelStore();
156160
}
@@ -160,9 +164,18 @@ public static AnyLicenseInfo parseSPDXLicenseString(String licenseString, @Nulla
160164
if (Objects.isNull(copyManager)) {
161165
copyManager = DefaultModelStore.getDefaultCopyManager();
162166
}
167+
if (Objects.isNull(creationInfo)) {
168+
try {
169+
creationInfo = SpdxModelClassFactoryV3.createCreationInfo(
170+
store, customLicensePrefix + "licenseinfo-creator", "LicenseInfoFactory",
171+
copyManager);
172+
} catch (InvalidSPDXAnalysisException e) {
173+
throw new RuntimeException(e);
174+
}
175+
}
163176
try {
164177
return LicenseExpressionParser.parseLicenseExpression(licenseString, store, customLicensePrefix,
165-
copyManager, customIdToUri);
178+
creationInfo, copyManager, customIdToUri);
166179
} catch (LicenseParserException e) {
167180
try {
168181
InvalidLicenseExpression retval = new InvalidLicenseExpression(store, store.getNextId(IModelStore.IdType.Anonymous),
@@ -185,6 +198,34 @@ public static AnyLicenseInfo parseSPDXLicenseString(String licenseString, @Nulla
185198

186199
}
187200

201+
/**
202+
* Parses a license string and converts it into a SPDXLicenseInfo object using the default creation information
203+
* Syntax - A license set must start and end with a parenthesis "("
204+
* A conjunctive license set will have and AND after the first
205+
* licenseInfo term
206+
* A disjunctive license set will have an OR after the first
207+
* licenseInfo term
208+
* If there is no And or Or, then it is converted to a simple
209+
* license type
210+
* A space or tab must be used between license ID's and the
211+
* keywords AND and OR
212+
* A licenseID must NOT be "AND" or "OR"
213+
* @param licenseString String conforming to the syntax
214+
* @param store Store containing any extractedLicenseInfos - if any extractedLicenseInfos by ID already exist, they will be used. If
215+
* none exist for an ID, they will be added. If null, the default model store will be used.
216+
* @param customLicensePrefix Prefix to use for any custom licenses or addition IDs found in the string. If the resultant object URI does not exist
217+
* for an ID, they will be added. If null, the default model document URI + "#" will be used.
218+
* @param copyManager allows for copying of any properties set which use other model stores or document URI's. If null, the default will be used.
219+
* @param customIdToUri Mapping of the id prefixes used in the license expression to the namespace preceding the external ID
220+
* @return an SPDXLicenseInfo created from the string. If the license expression is not parseable, a <code>InvalidLicenseExpression</code> is returned.
221+
* @throws DefaultStoreNotInitializedException if the default model store is not initialized
222+
*/
223+
public static AnyLicenseInfo parseSPDXLicenseString(String licenseString, @Nullable IModelStore store,
224+
@Nullable String customLicensePrefix,
225+
@Nullable IModelCopyManager copyManager,
226+
@Nullable List<DictionaryEntry> customIdToUri) throws InvalidLicenseStringException, DefaultStoreNotInitializedException {
227+
return parseSPDXLicenseString(licenseString, store, customLicensePrefix, null, copyManager, customIdToUri);
228+
}
188229
/**
189230
* Parses a license string and converts it into a SPDXLicenseInfo object
190231
* Syntax - A license set must start and end with a parenthesis "("
@@ -203,7 +244,7 @@ public static AnyLicenseInfo parseSPDXLicenseString(String licenseString, @Nulla
203244
* @throws DefaultStoreNotInitializedException if the default model store is not initialized
204245
*/
205246
public static AnyLicenseInfo parseSPDXLicenseString(String licenseString) throws InvalidLicenseStringException, DefaultStoreNotInitializedException {
206-
return parseSPDXLicenseString(licenseString, null, null, null, null);
247+
return parseSPDXLicenseString(licenseString, null, null, null, null, null);
207248
}
208249

209250
/**

0 commit comments

Comments
 (0)