3131import java .util .stream .Stream ;
3232import java .util .stream .StreamSupport ;
3333
34+ import javax .annotation .Nullable ;
35+
3436import org .apache .jena .graph .Node ;
3537import org .apache .jena .graph .Triple ;
3638import org .apache .jena .query .ARQ ;
@@ -67,6 +69,7 @@ public class RdfStore implements IModelStore, ISerializableModelStore {
6769 static Pattern ANON_ID_PATTERN = Pattern .compile (ANON_PREFIX +"(.+)$" );
6870 RdfSpdxModelManager modelManager ;
6971 String documentUri ;
72+ boolean dontStoreLicenseDetails = false ;
7073
7174 private OutputFormat outputFormat = OutputFormat .XML_ABBREV ;
7275
@@ -85,6 +88,14 @@ public RdfStore(InputStream stream) throws InvalidSPDXAnalysisException, IOExcep
8588 deSerialize (stream , false );
8689 }
8790
91+ /**
92+ * Creates an uninitialized RDF store - the documentUri must be set or a stream deserialized before any other methods are called
93+ */
94+ public RdfStore () {
95+ this .modelManager = null ;
96+ this .documentUri = null ;
97+ }
98+
8899 /**
89100 * Create an RDF store and initialize it with an data deserialized from stream using the documentUri
90101 * for ID prefixes
@@ -111,13 +122,55 @@ public RdfStore(String documentUri) {
111122 public OutputFormat getOutputFormat () {
112123 return outputFormat ;
113124 }
125+
126+ /**
127+ * @return the document URI
128+ */
129+ public @ Nullable String getDocumentUri () {
130+ return this .documentUri ;
131+ }
132+
133+ /**
134+ * @param documentUri document URI to set
135+ * @param overwrite setting a different document URI will overwrite an existing model - this flag will allow it to be overwritten
136+ * @throws InvalidSPDXAnalysisException if the document URI already exists and override is set to false
137+ */
138+ public void setDocumentUri (@ Nullable String documentUri , boolean overwrite ) throws InvalidSPDXAnalysisException {
139+ if (Objects .nonNull (this .documentUri ) && !overwrite && !Objects .equals (this .documentUri , documentUri )) {
140+ throw new InvalidSPDXAnalysisException ("Document URI " +this .documentUri +" already exists" );
141+ }
142+ if (!Objects .equals (this .documentUri , documentUri )) {
143+ this .documentUri = documentUri ;
144+ if (Objects .nonNull (documentUri )) {
145+ modelManager = createModelManager (documentUri );
146+ } else {
147+ modelManager = null ;
148+ }
149+ }
150+ }
114151
115152 /**
116153 * @param outputFormat the outputFormat to set
117154 */
118155 public void setOutputFormat (OutputFormat outputFormat ) {
119156 this .outputFormat = outputFormat ;
120157 }
158+
159+
160+
161+ /**
162+ * @return the dontStoreLicenseDetails - if true, listed license properties will not be stored in the RDF store
163+ */
164+ public boolean isDontStoreLicenseDetails () {
165+ return dontStoreLicenseDetails ;
166+ }
167+
168+ /**
169+ * @param dontStoreLicenseDetails the dontStoreLicenseDetails to set - if true, listed license properties will not be stored in the RDF store
170+ */
171+ public void setDontStoreLicenseDetails (boolean dontStoreLicenseDetails ) {
172+ this .dontStoreLicenseDetails = dontStoreLicenseDetails ;
173+ }
121174
122175 /* (non-Javadoc)
123176 * @see org.spdx.storage.IModelStore#exists(java.lang.String, java.lang.String)
@@ -237,10 +290,22 @@ public void setValue(String objectUri, PropertyDescriptor prop, Object value)
237290 checkClosed ();
238291 Objects .requireNonNull (objectUri );
239292 Objects .requireNonNull (prop );
293+ if (isListedLicenseOrException (objectUri ) && dontStoreLicenseDetails ) {
294+ return ;
295+ }
240296 String id = CompatibleModelStoreWrapper .objectUriToId (this , objectUri , documentUri );
241297 modelManager .setValue (id , prop .getName (), value );
242298 }
243299
300+ /**
301+ * @param objectUri URI or temp ID of an SPDX object
302+ * @return true if the object URI is associated with a listed license or a listed exception
303+ */
304+ private boolean isListedLicenseOrException (String objectUri ) {
305+ return Objects .isNull (objectUri ) || objectUri .startsWith (SpdxConstantsCompatV2 .LISTED_LICENSE_NAMESPACE_PREFIX ) ||
306+ objectUri .startsWith (SpdxConstantsCompatV2 .LISTED_LICENSE_URL );
307+ }
308+
244309 /* (non-Javadoc)
245310 * @see org.spdx.storage.IModelStore#getValue(java.lang.String, java.lang.String, java.lang.String)
246311 */
@@ -280,10 +345,9 @@ public void removeProperty(String objectUri, PropertyDescriptor prop) throws Inv
280345 * @see org.spdx.storage.IModelStore#getAllItems(java.lang.String, java.util.Optional)
281346 */
282347 @ Override
283- public Stream <TypedValue > getAllItems (String documentUri , String typeFilter )
348+ public Stream <TypedValue > getAllItems (@ Nullable String prefix , String typeFilter )
284349 throws InvalidSPDXAnalysisException {
285350 checkClosed ();
286- Objects .requireNonNull (documentUri , "Missing required document URI" );
287351 return modelManager .getAllItems (typeFilter );
288352 }
289353
@@ -296,6 +360,9 @@ public boolean removeValueFromCollection(String objectUri, PropertyDescriptor pr
296360 Objects .requireNonNull (objectUri , "Missing required object URI" );
297361 Objects .requireNonNull (propertyDescriptor , "Missing required property descriptor" );
298362 Objects .requireNonNull (value , "Mising required value" );
363+ if (isListedLicenseOrException (objectUri ) && dontStoreLicenseDetails ) {
364+ return false ;
365+ }
299366 String id = CompatibleModelStoreWrapper .objectUriToId (this , objectUri , documentUri );
300367 return modelManager .removeValueFromCollection (id , propertyDescriptor .getName (), value );
301368 }
@@ -344,6 +411,9 @@ public boolean addValueToCollection(String objectUri, PropertyDescriptor propert
344411 Objects .requireNonNull (objectUri , "Missing required Object URI" );
345412 Objects .requireNonNull (propertyDescriptor , "Missing required property descriptor" );
346413 checkClosed ();
414+ if (isListedLicenseOrException (objectUri ) && dontStoreLicenseDetails ) {
415+ return false ;
416+ }
347417 String id = CompatibleModelStoreWrapper .objectUriToId (this , objectUri , documentUri );
348418 return modelManager .addValueToCollection (id , propertyDescriptor .getName (), value );
349419 }
0 commit comments