Skip to content

Commit f590ccd

Browse files
committed
Updated to support API changes needed for the RDF store
Signed-off-by: Gary O'Neall <gary@sourceauditor.com>
1 parent d01ec70 commit f590ccd

10 files changed

Lines changed: 318 additions & 202 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>org.spdx</groupId>
66
<artifactId>java-spdx-library</artifactId>
7-
<version>1.1.8-SNAPSHOT</version>
7+
<version>2.0.0-SNAPSHOT</version>
88
<packaging>jar</packaging>
99

1010
<name>java-spdx-library</name>

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

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private void copyCollectionProperty(IModelStore toStore, String toObjectUri, IMo
244244
* @param sourceUri URI for the Source object
245245
* @param type Type to copy
246246
* @param toSpecVersion Version of the SPDX spec the to value complies with
247-
* @param toNamespace Namespace to use if an ID needs to be generated for the to object
247+
* @param toNamespace Namespace to use if an ID needs to be generated for the to object - must be a unique prefix to the store
248248
* @return Object URI for the copied object
249249
* @throws InvalidSPDXAnalysisException
250250
*/
@@ -258,30 +258,95 @@ public TypedValue copy(IModelStore toStore, IModelStore fromStore,
258258

259259
String toObjectUri = getCopiedObjectUri(fromStore, sourceUri, toStore);
260260
if (Objects.isNull(toObjectUri)) {
261-
if (toStore.exists(sourceUri) || IdType.Anonymous.equals(fromStore.getIdType(sourceUri))) {
262-
// TODO - the toNamspace will never be used - we need to add a from namespace
263-
if (Objects.nonNull(toNamespace)) {
264-
if (SpdxConstantsCompatV2.CLASS_EXTERNAL_DOC_REF.equals(type)) {
265-
toObjectUri = toNamespace + toStore.getNextId(IdType.DocumentRef);
266-
} else {
267-
switch (fromStore.getIdType(sourceUri)) {
268-
case Anonymous: toObjectUri = toStore.getNextId(IdType.Anonymous); break;
269-
case LicenseRef: toObjectUri = toNamespace + toStore.getNextId(IdType.LicenseRef); break;
270-
case DocumentRef: toObjectUri = toNamespace + toStore.getNextId(IdType.DocumentRef); break;
271-
case SpdxId: toObjectUri = toNamespace + toStore.getNextId(IdType.SpdxId); break;
272-
case ListedLicense:
273-
case Unkown:
274-
default: toObjectUri = toStore.getNextId(IdType.Anonymous);
275-
}
276-
}
277-
} else {
278-
toObjectUri = toStore.getNextId(IdType.Anonymous);
279-
}
280-
} else {
281-
toObjectUri = sourceUri;
282-
}
261+
toObjectUri = toSpecVersion.startsWith("SPDX-2") ? sourceUriToObjectUriV2Compat(sourceUri,
262+
fromStore.getIdType(sourceUri), toStore, toNamespace, SpdxConstantsCompatV2.CLASS_EXTERNAL_DOC_REF.equals(type)) :
263+
sourceUriToObjectUri(sourceUri, fromStore.getIdType(sourceUri), toStore, toNamespace);
283264
copy(toStore, toObjectUri, fromStore, sourceUri, type, toSpecVersion, toNamespace);
284265
}
285266
return new TypedValue(toObjectUri, type, toSpecVersion);
286267
}
268+
269+
/**
270+
* @param sourceUri source URI copied from
271+
* @param idType idType from the sourceUri
272+
* @param toStore model store to store the copied item
273+
* @param toNamespace namespace for the generated elements for "to"
274+
* @return an object URI suitable for SPDX V3 and later
275+
* @throws InvalidSPDXAnalysisException
276+
*/
277+
private String sourceUriToObjectUri(String sourceUri, IdType idType, IModelStore toStore,
278+
String toNamespace) throws InvalidSPDXAnalysisException {
279+
if (IdType.Anonymous.equals(idType)) {
280+
return toStore.getNextId(IdType.Anonymous);
281+
}
282+
if (!toStore.exists(sourceUri)) {
283+
return sourceUri;
284+
}
285+
if (Objects.isNull(toNamespace) || toNamespace.isEmpty() ||
286+
sourceUri.startsWith(toNamespace)) {
287+
logger.warn(sourceUri + " already exists - possibly overwriting properties due to a copy from a different model store.");
288+
return sourceUri;
289+
}
290+
switch (idType) {
291+
case LicenseRef: return toNamespace + toStore.getNextId(IdType.LicenseRef);
292+
case DocumentRef: return toNamespace + toStore.getNextId(IdType.DocumentRef);
293+
case SpdxId: return toNamespace + toStore.getNextId(IdType.SpdxId);
294+
case ListedLicense: return sourceUri;
295+
case Anonymous:
296+
case Unkown:
297+
default: return toStore.getNextId(IdType.Anonymous);
298+
}
299+
}
300+
301+
/**
302+
* @param sourceUri source URI copied from
303+
* @param idType idType from the sourceUri
304+
* @param toStore model store to store the copied item
305+
* @param toNamespace namespace for the generated elements for "to"
306+
* @param isExternalDocRef true if the type of the value to be copied is an ExternalDocRef
307+
* @return an object URI suitable for SPDX V2
308+
* @throws InvalidSPDXAnalysisException
309+
*/
310+
private String sourceUriToObjectUriV2Compat(String sourceUri, IdType idType,
311+
IModelStore toStore, String toNamespace, boolean isExternalDocRef) throws InvalidSPDXAnalysisException {
312+
if ((isExternalDocRef || !(IdType.Anonymous.equals(idType) ||
313+
IdType.ListedLicense.equals(idType) || IdType.Unkown.equals(idType)))
314+
&& (Objects.isNull(toNamespace) || toNamespace.isEmpty())) {
315+
throw new InvalidSPDXAnalysisException("A to namespace or document URI must be provided to copy SPDX element for SPDX spec version 2");
316+
}
317+
if (sourceUri.startsWith(toNamespace) && !toStore.exists(sourceUri)) {
318+
return sourceUri;
319+
}
320+
if (IdType.ListedLicense.equals(idType)) {
321+
return sourceUri;
322+
}
323+
String toUri = null;
324+
if (Objects.nonNull(toNamespace)) {
325+
int poundIndex = sourceUri.lastIndexOf('#');
326+
if (poundIndex > 0) {
327+
toUri = toNamespace + sourceUri.substring(poundIndex + 1);
328+
}
329+
}
330+
331+
boolean notNullAndNotExists = Objects.nonNull(toUri) && !toStore.exists(toUri); // notExists and nonNull
332+
if (isExternalDocRef) {
333+
if (!toStore.exists(toUri) && IdType.DocumentRef.equals(toStore.getIdType(toUri))) {
334+
return toUri;
335+
} else {
336+
return toNamespace + toStore.getNextId(IdType.DocumentRef);
337+
}
338+
}
339+
switch (idType) {
340+
case LicenseRef: return notNullAndNotExists && IdType.LicenseRef.equals(toStore.getIdType(toUri)) ? toUri :
341+
toNamespace + toStore.getNextId(IdType.LicenseRef);
342+
case DocumentRef: return notNullAndNotExists && IdType.DocumentRef.equals(toStore.getIdType(toUri)) ? toUri :
343+
toNamespace + toStore.getNextId(IdType.DocumentRef);
344+
case SpdxId: return notNullAndNotExists && IdType.SpdxId.equals(toStore.getIdType(toUri)) ? toUri :
345+
toNamespace + toStore.getNextId(IdType.SpdxId);
346+
case ListedLicense: return sourceUri;
347+
case Anonymous:
348+
case Unkown:
349+
default: return toStore.getNextId(IdType.Anonymous);
350+
}
351+
}
287352
}

src/main/java/org/spdx/storage/listedlicense/SpdxListedLicenseModelStore.java

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@
4141
import org.slf4j.Logger;
4242
import org.slf4j.LoggerFactory;
4343
import org.spdx.core.DuplicateSpdxIdException;
44-
import org.spdx.core.IExternalElementInfo;
4544
import org.spdx.core.InvalidSPDXAnalysisException;
4645
import org.spdx.core.SpdxIdNotFoundException;
4746
import org.spdx.core.TypedValue;
48-
import org.spdx.library.LicenseInfoFactory;
4947
import org.spdx.library.model.v2.ModelObjectV2;
5048
import org.spdx.library.model.v2.SpdxConstantsCompatV2;
5149
import org.spdx.library.model.v2.license.SpdxListedLicenseException;
@@ -156,12 +154,6 @@ public SpdxListedLicenseModelStore() throws InvalidSPDXAnalysisException {
156154
* @throws IOException
157155
*/
158156
abstract InputStream getExceptionInputStream(String exceptionId) throws IOException;
159-
160-
/**
161-
* Map of an objectUri to a map of documentUri's to external element information
162-
*/
163-
protected Map<String, Map<String, IExternalElementInfo>> objectUriExternalMap = new HashMap<>();
164-
165157
/**
166158
* Loads all license and exception ID's from the appropriate JSON files
167159
* @throws InvalidSPDXAnalysisException
@@ -249,6 +241,9 @@ private String objectUriToId(String objectUri) throws InvalidSPDXAnalysisExcepti
249241
String id;
250242
if (objectUri.startsWith(LISTED_LICENSE_NAMESPACE)) {
251243
id = objectUri.substring(LISTED_LICENSE_NAMESPACE.length());
244+
} else if (objectUri.startsWith(SpdxConstantsCompatV2.LISTED_LICENSE_URL)) {
245+
logger.warn("SPDX listed license URL was used instead of the required namespace ('https:' rather than 'http:'");
246+
id = objectUri.substring(SpdxConstantsCompatV2.LISTED_LICENSE_URL.length());
252247
} else if (getIdType(objectUri) == IdType.Anonymous ||
253248
licenseCreator.getObjectUri().equals(objectUri) ||
254249
LicenseCreationInfo.CREATION_INFO_URI.equals(objectUri)) {
@@ -1194,19 +1189,24 @@ public boolean isCollectionProperty(String objectUri, PropertyDescriptor propert
11941189
}
11951190

11961191
@Override
1197-
public IdType getIdType(String id) {
1198-
Objects.requireNonNull(id, "ID must not be null");
1199-
if (LicenseInfoFactory.isSpdxListedLicenseId(id) || LicenseInfoFactory.isSpdxListedExceptionId(id)) {
1192+
public IdType getIdType(String objectUri) {
1193+
Objects.requireNonNull(objectUri, "Object URI must not be null");
1194+
if (objectUri.startsWith(LISTED_LICENSE_NAMESPACE)) {
12001195
return IdType.ListedLicense;
1201-
} else if (id.startsWith(ANONYMOUS_ID_PREFIX) || LicenseCreationInfo.CREATION_INFO_URI.equals(id)) {
1196+
} else if (objectUri.startsWith(ANONYMOUS_ID_PREFIX) || LicenseCreationInfo.CREATION_INFO_URI.equals(objectUri)) {
12021197
return IdType.Anonymous;
1203-
} else if (id.startsWith(LicenseCreatorAgent.OBJECT_URI_PREFIX)) {
1198+
} else if (objectUri.startsWith(LicenseCreatorAgent.OBJECT_URI_PREFIX)) {
12041199
return IdType.SpdxId;
12051200
} else {
12061201
return IdType.Unkown;
12071202
}
12081203
}
12091204

1205+
@Override
1206+
public boolean isAnon(String objectUri) {
1207+
return objectUri.startsWith(ANONYMOUS_ID_PREFIX);
1208+
}
1209+
12101210

12111211
@Override
12121212
public IModelStoreLock enterCriticalSection(boolean readLockRequested) {
@@ -1284,44 +1284,4 @@ public void delete(String objectUri) throws InvalidSPDXAnalysisException {
12841284
public void close() throws Exception {
12851285
// Nothing to do for the either the in-memory or the web store
12861286
}
1287-
1288-
/**
1289-
* Adds an external reference for a given collection
1290-
* @param externalObjectUri URI of the external SPDX Element or License
1291-
* @param collectionUri URI of the SPDX document or collection
1292-
* @param externalElementInfo info about the external element
1293-
* @return the previous external mapping for the collection, null if no previous value is present
1294-
*/
1295-
@Override
1296-
public synchronized @Nullable IExternalElementInfo addExternalReference(String externalObjectUri, String collectionUri, IExternalElementInfo externalElementInfo) {
1297-
Map<String, IExternalElementInfo> externalObjectToCollectionMap = objectUriExternalMap.get(externalObjectUri);
1298-
if (Objects.isNull(externalObjectToCollectionMap)) {
1299-
externalObjectToCollectionMap = new HashMap<>();
1300-
}
1301-
return externalObjectToCollectionMap.put(collectionUri, externalElementInfo);
1302-
}
1303-
1304-
/**
1305-
* @param externalObjectUri object URI for an element external to a collection
1306-
* @return a map of collection (or document) URI's mapped to their external element info for the given object URI
1307-
*/
1308-
@Override
1309-
public synchronized @Nullable Map<String, IExternalElementInfo> getExternalReferenceMap(String externalObjectUri) {
1310-
return objectUriExternalMap.get(externalObjectUri);
1311-
}
1312-
1313-
/**
1314-
* @param externalObjectUri URI of the external SPDX Element or License
1315-
* @param collectionUri URI of the SPDX document or collection
1316-
* @return the externalElementInfo associated with the collection for a given external element
1317-
*/
1318-
@Override
1319-
public synchronized @Nullable IExternalElementInfo getExternalElementInfo(String externalObjectUri, String collectionUri) {
1320-
Map<String, IExternalElementInfo> externalObjectToCollectionMap = objectUriExternalMap.get(externalObjectUri);
1321-
if (Objects.isNull(externalObjectToCollectionMap)) {
1322-
return null;
1323-
} else {
1324-
return externalObjectToCollectionMap.get(collectionUri);
1325-
}
1326-
}
13271287
}

src/main/java/org/spdx/storage/listedlicense/SpdxV2ListedLicenseModelStore.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@
2121
import java.util.HashSet;
2222
import java.util.Iterator;
2323
import java.util.List;
24-
import java.util.Map;
2524
import java.util.Objects;
2625
import java.util.Optional;
2726
import java.util.Set;
2827
import java.util.stream.Stream;
2928

30-
import org.spdx.core.IExternalElementInfo;
3129
import org.spdx.core.InvalidSPDXAnalysisException;
3230
import org.spdx.core.TypedValue;
3331
import org.spdx.library.model.v2.ModelObjectV2;
@@ -374,30 +372,11 @@ public void delete(String objectUri) throws InvalidSPDXAnalysisException {
374372
}
375373

376374
/* (non-Javadoc)
377-
* @see org.spdx.storage.IModelStore#addExternalReference(java.lang.String, java.lang.String, org.spdx.core.IExternalElementInfo)
375+
* @see org.spdx.storage.IModelStore#isAnon(java.lang.String)
378376
*/
379377
@Override
380-
public IExternalElementInfo addExternalReference(String externalObjectUri,
381-
String collectionUri, IExternalElementInfo externalElementInfo) {
382-
return baseStore.addExternalReference(externalObjectUri, collectionUri, externalElementInfo);
383-
}
384-
385-
/* (non-Javadoc)
386-
* @see org.spdx.storage.IModelStore#getExternalReferenceMap(java.lang.String)
387-
*/
388-
@Override
389-
public Map<String, IExternalElementInfo> getExternalReferenceMap(
390-
String externalObjectUri) {
391-
return baseStore.getExternalReferenceMap(externalObjectUri);
392-
}
393-
394-
/* (non-Javadoc)
395-
* @see org.spdx.storage.IModelStore#getExternalElementInfo(java.lang.String, java.lang.String)
396-
*/
397-
@Override
398-
public IExternalElementInfo getExternalElementInfo(String externalObjectUri,
399-
String collectionUri) {
400-
return baseStore.getExternalElementInfo(externalObjectUri, collectionUri);
378+
public boolean isAnon(String objectUri) {
379+
return baseStore.isAnon(objectUri);
401380
}
402381

403382
}

src/main/java/org/spdx/storage/listedlicense/SpdxV3ListedLicenseModelStore.java

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
import java.util.HashSet;
2222
import java.util.Iterator;
2323
import java.util.List;
24-
import java.util.Map;
2524
import java.util.Optional;
2625
import java.util.Set;
2726
import java.util.stream.Stream;
2827

29-
import org.spdx.core.IExternalElementInfo;
3028
import org.spdx.core.InvalidSPDXAnalysisException;
3129
import org.spdx.core.TypedValue;
3230
import org.spdx.library.model.v2.SpdxConstantsCompatV2;
@@ -380,30 +378,10 @@ public void delete(String objectUri) throws InvalidSPDXAnalysisException {
380378
}
381379

382380
/* (non-Javadoc)
383-
* @see org.spdx.storage.IModelStore#addExternalReference(java.lang.String, java.lang.String, org.spdx.core.IExternalElementInfo)
381+
* @see org.spdx.storage.IModelStore#isAnon(java.lang.String)
384382
*/
385383
@Override
386-
public IExternalElementInfo addExternalReference(String externalObjectUri,
387-
String collectionUri, IExternalElementInfo externalElementInfo) {
388-
return baseStore.addExternalReference(externalObjectUri, collectionUri, externalElementInfo);
384+
public boolean isAnon(String objectUri) {
385+
return baseStore.isAnon(objectUri);
389386
}
390-
391-
/* (non-Javadoc)
392-
* @see org.spdx.storage.IModelStore#getExternalReferenceMap(java.lang.String)
393-
*/
394-
@Override
395-
public Map<String, IExternalElementInfo> getExternalReferenceMap(
396-
String externalObjectUri) {
397-
return baseStore.getExternalReferenceMap(externalObjectUri);
398-
}
399-
400-
/* (non-Javadoc)
401-
* @see org.spdx.storage.IModelStore#getExternalElementInfo(java.lang.String, java.lang.String)
402-
*/
403-
@Override
404-
public IExternalElementInfo getExternalElementInfo(String externalObjectUri,
405-
String collectionUri) {
406-
return baseStore.getExternalElementInfo(externalObjectUri, collectionUri);
407-
}
408-
409387
}

src/main/java/org/spdx/storage/simple/ExtendedSpdxStore.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@
1919

2020
import java.util.Iterator;
2121
import java.util.List;
22-
import java.util.Map;
2322
import java.util.Optional;
2423
import java.util.stream.Collectors;
2524
import java.util.stream.Stream;
2625

27-
import javax.annotation.Nullable;
28-
29-
import org.spdx.core.IExternalElementInfo;
3026
import org.spdx.core.InvalidSPDXAnalysisException;
3127
import org.spdx.core.TypedValue;
3228
import org.spdx.storage.IModelStore;
@@ -223,6 +219,14 @@ public boolean isCollectionProperty(String objectUri, PropertyDescriptor propert
223219
public IdType getIdType(String id) {
224220
return baseStore.getIdType(id);
225221
}
222+
223+
/* (non-Javadoc)
224+
* @see org.spdx.storage.IModelStore#isAnon(java.lang.String)
225+
*/
226+
@Override
227+
public boolean isAnon(String objectUri) {
228+
return baseStore.isAnon(objectUri);
229+
}
226230

227231
/* (non-Javadoc)
228232
* @see org.spdx.storage.IModelStore#getCaseSensisitiveId(java.lang.String, java.lang.String)
@@ -268,20 +272,4 @@ public void delete(String objectUri) throws InvalidSPDXAnalysisException {
268272
public void close() throws Exception {
269273
baseStore.close();
270274
}
271-
272-
@Override
273-
public synchronized @Nullable IExternalElementInfo getExternalElementInfo(String externalObjectUri, String collectionUri) {
274-
return baseStore.getExternalElementInfo(externalObjectUri, collectionUri);
275-
}
276-
277-
@Override
278-
public synchronized @Nullable Map<String, IExternalElementInfo> getExternalReferenceMap(String externalObjectUri) {
279-
return baseStore.getExternalReferenceMap(externalObjectUri);
280-
}
281-
282-
@Override
283-
public synchronized @Nullable IExternalElementInfo addExternalReference(String externalObjectUri, String collectionUri, IExternalElementInfo externalElementInfo) {
284-
return baseStore.addExternalReference(externalObjectUri, collectionUri, externalElementInfo);
285-
}
286-
287275
}

0 commit comments

Comments
 (0)