Skip to content

Commit 5421710

Browse files
committed
API changes to support the tag/value store
Signed-off-by: Gary O'Neall <gary@sourceauditor.com>
1 parent f590ccd commit 5421710

9 files changed

Lines changed: 76 additions & 49 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.slf4j.LoggerFactory;
3131
import org.spdx.core.InvalidSPDXAnalysisException;
3232
import org.spdx.library.model.v2.SpdxConstantsCompatV2;
33-
import org.spdx.library.model.v2.SpdxModelFactory;
33+
import org.spdx.library.model.v2.SpdxModelFactoryCompatV2;
3434
import org.spdx.library.model.v2.license.SpdxListedLicense;
3535
import org.spdx.library.model.v3.expandedlicensing.ListedLicense;
3636
import org.spdx.library.model.v3.expandedlicensing.ListedLicenseException;
@@ -199,7 +199,7 @@ public boolean isSpdxListedExceptionId(String exceptionId) {
199199
* @throws InvalidSPDXAnalysisException
200200
*/
201201
public SpdxListedLicense getListedLicenseByIdCompatV2(String licenseId) throws InvalidSPDXAnalysisException {
202-
return (SpdxListedLicense)SpdxModelFactory.createModelObjectV2(this.licenseStoreV2, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX, licenseId, SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE, null);
202+
return (SpdxListedLicense)SpdxModelFactoryCompatV2.createModelObjectV2(this.licenseStoreV2, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX, licenseId, SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE, null);
203203
}
204204

205205
/**
@@ -208,7 +208,7 @@ public SpdxListedLicense getListedLicenseByIdCompatV2(String licenseId) throws I
208208
* @throws InvalidSPDXAnalysisException
209209
*/
210210
public org.spdx.library.model.v2.license.ListedLicenseException getListedExceptionByIdCompatV2(String exceptionId) throws InvalidSPDXAnalysisException {
211-
return (org.spdx.library.model.v2.license.ListedLicenseException)SpdxModelFactory.createModelObjectV2(this.licenseStoreV2, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX, exceptionId, SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE_EXCEPTION, null);
211+
return (org.spdx.library.model.v2.license.ListedLicenseException)SpdxModelFactoryCompatV2.createModelObjectV2(this.licenseStoreV2, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX, exceptionId, SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE_EXCEPTION, null);
212212
}
213213

214214
/**

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,15 @@ public static void init() {
127127
* @param externalMap map of URI's to ExternalMaps for any external elements
128128
* @param specVersion version of the SPDX spec the object complies with
129129
* @param create if true, create the model object ONLY if it does not already exist
130+
* @param idPrefix optional prefix used for any new object URI's created in support of this model object
130131
* @return model object of type type
131132
* @throws InvalidSPDXAnalysisException
132133
*/
133134
public static CoreModelObject inflateModelObject(IModelStore modelStore, String objectUri,
134135
String type, IModelCopyManager copyManager,
135-
String specVersion, boolean create) throws InvalidSPDXAnalysisException {
136-
return ModelRegistry.getModelRegistry().inflateModelObject(modelStore, objectUri, type, copyManager, specVersion, create);
136+
String specVersion, boolean create, @Nullable String idPrefix) throws InvalidSPDXAnalysisException {
137+
return ModelRegistry.getModelRegistry().inflateModelObject(modelStore, objectUri, type,
138+
copyManager, specVersion, create, idPrefix);
137139
}
138140

139141
/**
@@ -148,12 +150,13 @@ public static CoreModelObject inflateModelObject(IModelStore modelStore, String
148150
* @param copyManager if non-null, implicitly copy any referenced properties from other model stores
149151
* @param externalMap map of URI's to ExternalMaps for any external elements
150152
* @param create if true, create the model object ONLY if it does not already exist
153+
* @param idPrefix optional prefix used for any new object URI's created in support of this model object
151154
* @return model object of type type
152155
* @throws InvalidSPDXAnalysisException
153156
*/
154157
public static CoreModelObject inflateModelObject(IModelStore modelStore, String objectUri,
155-
String type, IModelCopyManager copyManager, boolean create) throws InvalidSPDXAnalysisException {
156-
return inflateModelObject(modelStore, objectUri, type, copyManager, getLatestSpecVersion(), create);
158+
String type, IModelCopyManager copyManager, boolean create, @Nullable String idPrefix) throws InvalidSPDXAnalysisException {
159+
return inflateModelObject(modelStore, objectUri, type, copyManager, getLatestSpecVersion(), create, idPrefix);
157160
}
158161

159162
/**
@@ -212,16 +215,17 @@ public static Object getExternalElement(IModelStore store, String uri,
212215
* @param copyManager optional copy manager
213216
* @param typeFilter type to filter on
214217
* @param objectUriPrefixFilter only return objects with URI's starting with this string
218+
* @param idPrefix optional prefix used for any new object URI's created in support of this model object
215219
* @return stream of objects stored in the model store - an object being any non primitive type
216220
* @throws InvalidSPDXAnalysisException
217221
*/
218222
public static Stream<?> getSpdxObjects(IModelStore store, @Nullable IModelCopyManager copyManager,
219-
@Nullable String typeFilter, @Nullable String objectUriPrefixFilter) throws InvalidSPDXAnalysisException {
223+
@Nullable String typeFilter, @Nullable String objectUriPrefixFilter, @Nullable String idPrefix) throws InvalidSPDXAnalysisException {
220224
Objects.requireNonNull(store, "Store must not be null");
221225
return store.getAllItems(objectUriPrefixFilter, typeFilter).map(tv -> {
222226
//TODO: Change this a null namespace and filtering on anonomous or startswith document URI - this will catch the anon. types
223227
try {
224-
return inflateModelObject(store, tv.getObjectUri(), tv.getType(), copyManager, tv.getSpecVersion(), false);
228+
return inflateModelObject(store, tv.getObjectUri(), tv.getType(), copyManager, tv.getSpecVersion(), false, idPrefix);
225229
} catch (InvalidSPDXAnalysisException e) {
226230
throw new RuntimeException(e);
227231
}

src/main/java/org/spdx/utility/compare/SpdxComparer.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private void compareSnippets() throws SpdxCompareException {
216216
Stream<SpdxSnippet> snippetStreamA = null;
217217
try {
218218
snippetStreamA = (Stream<SpdxSnippet>)SpdxModelFactory.getSpdxObjects(spdxDocs.get(i).getModelStore(), null,
219-
SpdxConstantsCompatV2.CLASS_SPDX_SNIPPET, spdxDocs.get(i).getDocumentUri());
219+
SpdxConstantsCompatV2.CLASS_SPDX_SNIPPET, spdxDocs.get(i).getDocumentUri(), null);
220220
snippetsA = snippetStreamA.collect(Collectors.toList());
221221
} catch (InvalidSPDXAnalysisException e) {
222222
try {
@@ -244,7 +244,7 @@ private void compareSnippets() throws SpdxCompareException {
244244
Stream<SpdxSnippet> snippetStreamB = null;
245245
try {
246246
snippetStreamB = (Stream<SpdxSnippet>)SpdxModelFactory.getSpdxObjects(spdxDocs.get(j).getModelStore(),
247-
null, SpdxConstantsCompatV2.CLASS_SPDX_SNIPPET, spdxDocs.get(j).getDocumentUri());
247+
null, SpdxConstantsCompatV2.CLASS_SPDX_SNIPPET, spdxDocs.get(j).getDocumentUri(), null);
248248
snippetsB = snippetStreamB.collect(Collectors.toList());
249249
} catch (InvalidSPDXAnalysisException e) {
250250
try {
@@ -447,7 +447,8 @@ private void compareFiles() throws InvalidSPDXAnalysisException, SpdxCompareExce
447447
for (int i = 0; i < spdxDocs.size(); i++) {
448448
List<SpdxFile> filesListA;
449449
Stream<SpdxFile> fileStreamA = (Stream<SpdxFile>) SpdxModelFactory.getSpdxObjects(
450-
spdxDocs.get(i).getModelStore(), null, SpdxConstantsCompatV2.CLASS_SPDX_FILE, spdxDocs.get(i).getDocumentUri());
450+
spdxDocs.get(i).getModelStore(), null, SpdxConstantsCompatV2.CLASS_SPDX_FILE,
451+
spdxDocs.get(i).getDocumentUri(), null);
451452
filesListA = fileStreamA.collect(Collectors.toList());
452453
fileStreamA.close();
453454
// note - the file arrays MUST be sorted for the comparator methods to work
@@ -467,7 +468,8 @@ private void compareFiles() throws InvalidSPDXAnalysisException, SpdxCompareExce
467468
continue;
468469
}
469470
Stream<SpdxFile> fileStreamB = (Stream<SpdxFile>)SpdxModelFactory.getSpdxObjects(
470-
spdxDocs.get(j).getModelStore(), null, SpdxConstantsCompatV2.CLASS_SPDX_FILE, spdxDocs.get(j).getDocumentUri());
471+
spdxDocs.get(j).getModelStore(), null, SpdxConstantsCompatV2.CLASS_SPDX_FILE,
472+
spdxDocs.get(j).getDocumentUri(), null);
471473
List<SpdxFile> filesListB = (List<SpdxFile>)fileStreamB.collect(Collectors.toList());
472474
fileStreamB.close();
473475
//Note that the files arrays must be sorted for the find methods to work
@@ -505,7 +507,8 @@ private void compareFiles() throws InvalidSPDXAnalysisException, SpdxCompareExce
505507
@SuppressWarnings("unchecked")
506508
protected List<SpdxPackage> collectAllPackages(SpdxDocument spdxDocument) throws InvalidSPDXAnalysisException {
507509
Stream<SpdxPackage> packageStream = (Stream<SpdxPackage>) SpdxModelFactory.getSpdxObjects(
508-
spdxDocument.getModelStore(), null, SpdxConstantsCompatV2.CLASS_SPDX_PACKAGE, spdxDocument.getDocumentUri());
510+
spdxDocument.getModelStore(), null, SpdxConstantsCompatV2.CLASS_SPDX_PACKAGE,
511+
spdxDocument.getDocumentUri(), null);
509512
List<SpdxPackage> retval = packageStream.collect(Collectors.toList());
510513
packageStream.close();
511514
return retval;
@@ -521,7 +524,8 @@ protected List<SpdxPackage> collectAllPackages(SpdxDocument spdxDocument) throws
521524
@SuppressWarnings("unchecked")
522525
public List<SpdxFile> collectAllFiles(SpdxDocument spdxDocument) throws InvalidSPDXAnalysisException {
523526
Stream<SpdxFile> fileElementStream = (Stream<SpdxFile>) SpdxModelFactory.getSpdxObjects(
524-
spdxDocument.getModelStore(), null, SpdxConstantsCompatV2.CLASS_SPDX_FILE, spdxDocument.getDocumentUri());
527+
spdxDocument.getModelStore(), null, SpdxConstantsCompatV2.CLASS_SPDX_FILE,
528+
spdxDocument.getDocumentUri(), null);
525529
List<SpdxFile> retval = fileElementStream.collect(Collectors.toList());
526530
fileElementStream.close();
527531
return retval;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ public static org.spdx.library.model.v2.license.AnyLicenseInfo parseLicenseExpre
151151
Objects.requireNonNull(documentUri, "Document URI can not be null");
152152
String[] tokens = tokenizeExpression(expression);
153153
if (tokens.length == 1 && tokens[0].equals(SpdxConstantsCompatV2.NOASSERTION_VALUE)) {
154-
return new SpdxNoAssertionLicense();
154+
return new SpdxNoAssertionLicense(store, documentUri);
155155
} else if (tokens.length == 1 && tokens[0].equals(SpdxConstantsCompatV2.NONE_VALUE)) {
156-
return new SpdxNoneLicense();
156+
return new SpdxNoneLicense(store, documentUri);
157157
} else {
158158
try {
159159
return parseLicenseExpressionCompatV2(tokens, store, documentUri, copyManager);
@@ -339,7 +339,7 @@ private static org.spdx.library.model.v2.license.AnyLicenseInfo parseLicenseExpr
339339
} else if (token.startsWith(SpdxConstantsCompatV2.NON_STD_LICENSE_ID_PRENUM)) {
340340
throw new LicenseParserException("WITH must be followed by a license exception. "+token+" is a Listed License type.");
341341
} else {
342-
licenseException = (org.spdx.library.model.v2.license.ListedLicenseException) org.spdx.library.model.v2.SpdxModelFactory.createModelObjectV2(store,
342+
licenseException = (org.spdx.library.model.v2.license.ListedLicenseException) org.spdx.library.model.v2.SpdxModelFactoryCompatV2.createModelObjectV2(store,
343343
documentUri, token, SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE_EXCEPTION, copyManager);
344344
}
345345
org.spdx.library.model.v2.license.AnyLicenseInfo operand = operandStack.pop();
@@ -565,7 +565,7 @@ private static org.spdx.library.model.v2.license.AnyLicenseInfo parseSimpleLicen
565565
ModelObjectV2.LATEST_SPDX_2_VERSION, listedLicense.getDocumentUri());
566566
}
567567
}
568-
return (org.spdx.library.model.v2.license.AnyLicenseInfo) org.spdx.library.model.v2.SpdxModelFactory.getModelObjectV2(store, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX,
568+
return (org.spdx.library.model.v2.license.AnyLicenseInfo) org.spdx.library.model.v2.SpdxModelFactoryCompatV2.getModelObjectV2(store, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX,
569569
licenseId.get(), SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE, copyManager, true);
570570
} else {
571571
// LicenseRef
@@ -575,7 +575,7 @@ private static org.spdx.library.model.v2.license.AnyLicenseInfo parseSimpleLicen
575575
localLicense = new ExtractedLicenseInfo(store, documentUri, caseSensitiveId.get(), copyManager, false);
576576

577577
} else {
578-
localLicense = (ExtractedLicenseInfo) org.spdx.library.model.v2.SpdxModelFactory.createModelObjectV2(
578+
localLicense = (ExtractedLicenseInfo) org.spdx.library.model.v2.SpdxModelFactoryCompatV2.createModelObjectV2(
579579
store, documentUri, token, SpdxConstantsCompatV2.CLASS_SPDX_EXTRACTED_LICENSING_INFO, copyManager);
580580
localLicense.setExtractedText(UNINITIALIZED_LICENSE_TEXT);
581581
}

src/test/java/org/spdx/library/ModelCopyManagerTest.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void setUp() throws Exception {
6969
modelCopyManager = new ModelCopyManager();
7070
fromStore = new InMemSpdxStore();
7171
toStore = new InMemSpdxStore();
72-
hash = (Hash)SpdxModelFactory.inflateModelObject(fromStore, HASH_OBJECT_URI, HASH_TYPE, modelCopyManager, "3.0.0", true);
72+
hash = (Hash)SpdxModelFactory.inflateModelObject(fromStore, HASH_OBJECT_URI, HASH_TYPE, modelCopyManager, "3.0.0", true, null);
7373
hash.setAlgorithm(HASH_ALGORITHM);
7474
hash.setHashValue(HASH_VALUE);
7575
DateFormat format = new SimpleDateFormat(SpdxConstantsCompatV2.SPDX_DATE_FORMAT);
@@ -101,7 +101,7 @@ public void testGetCopiedObjectUri() {
101101
public void testCopyIModelStoreIModelStoreStringStringStringString() throws InvalidSPDXAnalysisException {
102102
modelCopyManager.copy(toStore, OBJECT_URI2, fromStore, HASH_OBJECT_URI, HASH_TYPE, "3.0.0", NAMESPACE);
103103
assertEquals(OBJECT_URI2, (modelCopyManager.getCopiedObjectUri(fromStore, HASH_OBJECT_URI, toStore)));
104-
Hash copiedHash = (Hash)SpdxModelFactory.inflateModelObject(toStore, OBJECT_URI2, HASH_TYPE, modelCopyManager, "3.0.0", false);
104+
Hash copiedHash = (Hash)SpdxModelFactory.inflateModelObject(toStore, OBJECT_URI2, HASH_TYPE, modelCopyManager, "3.0.0", false, null);
105105
assertTrue(hash.equivalent(copiedHash));
106106
assertEquals(HASH_ALGORITHM, copiedHash.getAlgorithm());
107107
assertEquals(HASH_VALUE, copiedHash.getHashValue());
@@ -145,7 +145,8 @@ public void testDeepComplexCopy() throws InvalidSPDXAnalysisException {
145145
assertEquals(doc.getObjectUri(), result.getObjectUri());
146146
assertEquals(doc.getType(), result.getType());
147147
assertEquals("3.0.0", result.getSpecVersion());
148-
SpdxDocument copiedDoc = (SpdxDocument)SpdxModelFactory.inflateModelObject(toStore, result.getObjectUri(), result.getType(), modelCopyManager, "3.0.0", false);
148+
SpdxDocument copiedDoc = (SpdxDocument)SpdxModelFactory.inflateModelObject(toStore, result.getObjectUri(), result.getType(), modelCopyManager,
149+
"3.0.0", false, null);
149150
assertTrue(copiedDoc.equivalent(doc));
150151

151152
}
@@ -158,7 +159,8 @@ public void testDeepComplexCopy() throws InvalidSPDXAnalysisException {
158159
public void testCopyIModelStoreStringIModelStoreStringStringStringString() throws InvalidSPDXAnalysisException {
159160
// URI does not exist
160161
TypedValue result = modelCopyManager.copy(toStore, fromStore, HASH_OBJECT_URI, HASH_TYPE, "3.0.0", NAMESPACE);
161-
Hash copiedHash = (Hash)SpdxModelFactory.inflateModelObject(toStore, HASH_OBJECT_URI, HASH_TYPE, modelCopyManager, "3.0.0", false);
162+
Hash copiedHash = (Hash)SpdxModelFactory.inflateModelObject(toStore, HASH_OBJECT_URI, HASH_TYPE, modelCopyManager,
163+
"3.0.0", false, null);
162164
assertTrue(hash.equivalent(copiedHash));
163165
assertEquals(HASH_ALGORITHM, copiedHash.getAlgorithm());
164166
assertEquals(HASH_VALUE, copiedHash.getHashValue());
@@ -167,30 +169,34 @@ public void testCopyIModelStoreStringIModelStoreStringStringStringString() throw
167169
assertEquals("3.0.0", result.getSpecVersion());
168170
// Anonymous fromUri
169171
String differentUri = fromStore.getNextId(IdType.Anonymous);
170-
Hash differentHash = (Hash)SpdxModelFactory.inflateModelObject(fromStore, differentUri, HASH_TYPE, modelCopyManager, "3.0.0", true);
172+
Hash differentHash = (Hash)SpdxModelFactory.inflateModelObject(fromStore, differentUri, HASH_TYPE, modelCopyManager,
173+
"3.0.0", true, null);
171174
differentHash.setAlgorithm(HashAlgorithm.BLAKE2B256);
172175
differentHash.setHashValue("asasdf1309u93u");
173176
result = modelCopyManager.copy(toStore, fromStore, differentUri, HASH_TYPE, "3.0.0", NAMESPACE);
174177
assertEquals(IdType.Anonymous, toStore.getIdType(result.getObjectUri()));
175178
assertEquals(HASH_TYPE, result.getType());
176179
assertEquals(HASH_TYPE, result.getType());assertEquals("3.0.0", result.getSpecVersion());
177-
Hash differentCopiedHash = (Hash)SpdxModelFactory.inflateModelObject(toStore, result.getObjectUri(), HASH_TYPE, modelCopyManager, "3.0.0", true);
180+
Hash differentCopiedHash = (Hash)SpdxModelFactory.inflateModelObject(toStore, result.getObjectUri(), HASH_TYPE,
181+
modelCopyManager, "3.0.0", true, null);
178182
assertTrue(differentHash.equivalent(differentCopiedHash));
179183
}
180184

181185
@Test
182186
public void testCopyToExistingUri() throws InvalidSPDXAnalysisException {
183187
String differentUri = "http://prefix/something#"+fromStore.getNextId(IdType.SpdxId);
184188
modelCopyManager.copy(toStore, differentUri, fromStore, HASH_OBJECT_URI, HASH_TYPE, "3.0.0", NAMESPACE);
185-
Hash differentHash = (Hash)SpdxModelFactory.inflateModelObject(fromStore, differentUri, HASH_TYPE, modelCopyManager, "3.0.0", true);
189+
Hash differentHash = (Hash)SpdxModelFactory.inflateModelObject(fromStore, differentUri, HASH_TYPE, modelCopyManager,
190+
"3.0.0", true, null);
186191
differentHash.setAlgorithm(HashAlgorithm.BLAKE2B256);
187192
differentHash.setHashValue("asasdf1309u93u");
188193
TypedValue result = modelCopyManager.copy(toStore, fromStore, differentUri, HASH_TYPE, "3.0.0", NAMESPACE);
189194
assertEquals(IdType.SpdxId, toStore.getIdType(result.getObjectUri()));
190195
assertTrue(result.getObjectUri().startsWith(NAMESPACE));
191196
assertEquals(HASH_TYPE, result.getType());
192197
assertEquals(HASH_TYPE, result.getType());assertEquals("3.0.0", result.getSpecVersion());
193-
Hash differentCopiedHash = (Hash)SpdxModelFactory.inflateModelObject(toStore, result.getObjectUri(), HASH_TYPE, modelCopyManager, "3.0.0", true);
198+
Hash differentCopiedHash = (Hash)SpdxModelFactory.inflateModelObject(toStore, result.getObjectUri(), HASH_TYPE,
199+
modelCopyManager, "3.0.0", true, null);
194200
assertTrue(differentHash.equivalent(differentCopiedHash));
195201
}
196202
}

0 commit comments

Comments
 (0)