Skip to content

Commit 5502454

Browse files
committed
Update API's to accept type hints for external
Signed-off-by: Gary O'Neall <gary@sourceauditor.com>
1 parent cbebf8d commit 5502454

2 files changed

Lines changed: 44 additions & 58 deletions

File tree

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,10 @@ public static void init() {
120120
* If the object does not exist AND the create parameter is true, a new object will be created and
121121
* its inflated form will be returned
122122
* @param modelStore store to use for the inflated object
123-
* @param objectUri URI of the external element
124-
* @param documentUri URI for the SPDX document to store the external element reference - used for compatibility with SPDX 2.X model stores
123+
* @param objectUri URI of the element or SPDX object
125124
* @param type Type of the object to create
126125
* @param copyManager if non-null, implicitly copy any referenced properties from other model stores
127-
* @param externalMap map of URI's to ExternalMaps for any external elements
128-
* @param specVersion version of the SPDX spec the object complies with
126+
*@param specVersion version of the SPDX spec the object complies with
129127
* @param create if true, create the model object ONLY if it does not already exist
130128
* @param idPrefix optional prefix used for any new object URI's created in support of this model object
131129
* @return model object of type type
@@ -145,10 +143,8 @@ public static CoreModelObject inflateModelObject(IModelStore modelStore, String
145143
* its inflated form will be returned
146144
* @param modelStore store to use for the inflated object
147145
* @param objectUri URI of the external element
148-
* @param documentUri URI for the SPDX document to store the external element reference - used for compatibility with SPDX 2.X model stores
149146
* @param type Type of the object to create
150147
* @param copyManager if non-null, implicitly copy any referenced properties from other model stores
151-
* @param externalMap map of URI's to ExternalMaps for any external elements
152148
* @param create if true, create the model object ONLY if it does not already exist
153149
* @param idPrefix optional prefix used for any new object URI's created in support of this model object
154150
* @return model object of type type
@@ -163,30 +159,28 @@ public static CoreModelObject inflateModelObject(IModelStore modelStore, String
163159
* @param store store to use for the inflated object
164160
* @param uri URI of the external element
165161
* @param copyManager if non-null, implicitly copy any referenced properties from other model stores
166-
* @param documentUri URI for the SPDX document to store the external element reference - used for compatibility with SPDX 2.X model stores
167-
* @param externalMap Map of URI's of elements referenced but not present in the store
162+
* @param type type hint for creating the correct external element
168163
* @param specVersion version of the SPDX spec the object complies with
169164
* @return a java object representing an SPDX element external to model store, collection or document
170165
* @throws InvalidSPDXAnalysisException
171166
*/
172167
public static Object getExternalElement(IModelStore store, String uri,
173-
@Nullable IModelCopyManager copyManager,
168+
@Nullable IModelCopyManager copyManager, Class<?> type,
174169
String specVersion) throws InvalidSPDXAnalysisException {
175-
return ModelRegistry.getModelRegistry().getExternalElement(store, uri, copyManager, specVersion);
170+
return ModelRegistry.getModelRegistry().getExternalElement(store, uri, copyManager, type, specVersion);
176171
}
177172

178173
/**
179174
* @param store store to use for the inflated object
180175
* @param uri URI of the external element
181176
* @param copyManager if non-null, implicitly copy any referenced properties from other model stores
182-
* @param documentUri URI for the SPDX document to store the external element reference - used for compatibility with SPDX 2.X model stores
183-
* @param externalMap Map of URI's of elements referenced but not present in the store
177+
* @param type type hint for creating the correct external element
184178
* @return a java object representing an SPDX element external to model store, collection or document for the most recent version of the spec supported
185179
* @throws InvalidSPDXAnalysisException
186180
*/
187181
public static Object getExternalElement(IModelStore store, String uri,
188-
@Nullable IModelCopyManager copyManager) throws InvalidSPDXAnalysisException {
189-
return getExternalElement(store, uri, copyManager, getLatestSpecVersion());
182+
@Nullable IModelCopyManager copyManager, Class<?> type) throws InvalidSPDXAnalysisException {
183+
return getExternalElement(store, uri, copyManager, type, getLatestSpecVersion());
190184
}
191185

192186
/**

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

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -412,51 +412,22 @@ public boolean isCollectionMembersAssignableTo(PropertyDescriptor propertyDescri
412412
ConcurrentHashMap<String, List<Object>> idValueMap = (ConcurrentHashMap<String, List<Object>>)map;
413413
for (List<Object> valueList:idValueMap.values()) {
414414
for (Object value:valueList) {
415-
if (!clazz.isAssignableFrom(value.getClass())) {
416-
if (value instanceof IndividualUriValue) {
417-
String uri = ((IndividualUriValue)value).getIndividualURI();
418-
Enum<?> spdxEnum = ModelRegistry.getModelRegistry().uriToEnum(uri, getSpecVersion());
419-
if (Objects.nonNull(spdxEnum)) {
420-
if (!clazz.isAssignableFrom(spdxEnum.getClass())) {
421-
return false;
422-
}
423-
} else if (Objects.isNull(ModelRegistry.getModelRegistry().uriToIndividual(uri, getSpecVersion(), clazz))) {
424-
return false;
425-
//TODO: Test for type of individual
426-
}
427-
} else if (value instanceof TypedValue) {
428-
TypedValue typedValue = (TypedValue)value;
429-
try {
430-
if (clazz != TypedValue.class && !clazz.isAssignableFrom(ModelRegistry.getModelRegistry().typeToClass(typedValue.getType(), typedValue.getSpecVersion()))) {
431-
return false;
432-
}
433-
} catch (InvalidSPDXAnalysisException e) {
434-
logger.error("Error converting typed value to class",e);
435-
return false;
436-
}
437-
} else {
438-
return false;
439-
}
415+
if (!isAssignableTo(value, clazz, getSpecVersion())) {
416+
return false;
440417
}
441418
}
442419
}
443420
return true;
444421
}
445422

446423
/**
447-
* @param propertyDescriptor descriptor for the property
448-
* @param clazz class to test against
449-
* @param specVersion Version of the spec to test for
450-
* @return true if the property can be assigned to type clazz for the latest SPDX spec version
451-
* @throws ModelRegistryException
424+
* @param value value to test
425+
* @param clazz class to see if the value can be assigned to
426+
* @param specVersion version of the spec
427+
* @return true if value can be assigned to clazz
428+
* @throws ModelRegistryException if the model registry is not property initialized
452429
*/
453-
public boolean isPropertyValueAssignableTo(PropertyDescriptor propertyDescriptor, Class<?> clazz, String specVersion) throws ModelRegistryException {
454-
Objects.requireNonNull(propertyDescriptor, "Property descriptor can not be null");
455-
Objects.requireNonNull(clazz, "Class can not be null");
456-
Object value = properties.get(propertyDescriptor);
457-
if (value == null) {
458-
return false;
459-
}
430+
private boolean isAssignableTo(Object value, Class<?> clazz, String specVersion) throws ModelRegistryException {
460431
if (clazz.isAssignableFrom(value.getClass())) {
461432
return true;
462433
}
@@ -471,18 +442,39 @@ public boolean isPropertyValueAssignableTo(PropertyDescriptor propertyDescriptor
471442
}
472443
if (value instanceof IndividualUriValue) {
473444
String uri = ((IndividualUriValue)value).getIndividualURI();
474-
if (Objects.nonNull(ModelRegistry.getModelRegistry().uriToEnum(uri, specVersion))) {
475-
return true;
476-
}
477-
//TODO: Check for individual URI types
478-
Enum<?> spdxEnum = ModelRegistry.getModelRegistry().uriToEnum(uri, specVersion);
445+
446+
Enum<?> spdxEnum = ModelRegistry.getModelRegistry().uriToEnum(uri, getSpecVersion());
479447
if (Objects.nonNull(spdxEnum)) {
480448
return clazz.isAssignableFrom(spdxEnum.getClass());
481449
} else {
482-
return false;
450+
Object individual = ModelRegistry.getModelRegistry().uriToIndividual(uri, getSpecVersion(), clazz);
451+
if (Objects.nonNull(individual)) {
452+
return clazz.isAssignableFrom(individual.getClass());
453+
} else {
454+
// Assume this is external
455+
return ModelRegistry.getModelRegistry().canBeExternal(clazz, specVersion);
456+
}
483457
}
458+
} else {
459+
return false;
484460
}
485-
return false;
461+
}
462+
463+
/**
464+
* @param propertyDescriptor descriptor for the property
465+
* @param clazz class to test against
466+
* @param specVersion Version of the spec to test for
467+
* @return true if the property can be assigned to type clazz for the latest SPDX spec version
468+
* @throws ModelRegistryException if the registry is not property initialized
469+
*/
470+
public boolean isPropertyValueAssignableTo(PropertyDescriptor propertyDescriptor, Class<?> clazz, String specVersion) throws ModelRegistryException {
471+
Objects.requireNonNull(propertyDescriptor, "Property descriptor can not be null");
472+
Objects.requireNonNull(clazz, "Class can not be null");
473+
Object value = properties.get(propertyDescriptor);
474+
if (value == null) {
475+
return false;
476+
}
477+
return isAssignableTo(value, clazz, specVersion);
486478
}
487479

488480
/**

0 commit comments

Comments
 (0)