Skip to content

Commit 650fe11

Browse files
committed
Intermediate checkin - serializable interface changes
Signed-off-by: Gary O'Neall <gary@sourceauditor.com>
1 parent 528339c commit 650fe11

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

src/main/java/org/spdx/spdxRdfStore/RdfStore.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@
4242
import org.apache.jena.util.iterator.ExtendedIterator;
4343
import org.slf4j.Logger;
4444
import org.slf4j.LoggerFactory;
45+
import org.spdx.core.CoreModelObject;
4546
import org.spdx.core.DuplicateSpdxIdException;
4647
import org.spdx.core.InvalidSPDXAnalysisException;
4748
import org.spdx.core.TypedValue;
49+
import org.spdx.library.SpdxModelFactory;
4850
import org.spdx.library.model.v2.SpdxConstantsCompatV2;
51+
import org.spdx.library.model.v2.SpdxDocument;
4952
import org.spdx.storage.CompatibleModelStoreWrapper;
5053
import org.spdx.storage.IModelStore;
5154
import org.spdx.storage.ISerializableModelStore;
@@ -500,8 +503,8 @@ public String loadModelFromFile(String fileNameOrUrl, boolean overwrite) throws
500503
throw new FileNotFoundException(fileNameOrUrl + " not found.");
501504
}
502505
try {
503-
deSerialize(spdxRdfInput, overwrite);
504-
return this.documentUri;
506+
SpdxDocument doc = deSerialize(spdxRdfInput, overwrite);
507+
return doc.getDocumentUri();
505508
} finally {
506509
try {
507510
spdxRdfInput.close();
@@ -569,9 +572,25 @@ public void serialize(OutputStream stream) throws InvalidSPDXAnalysisException,
569572
checkClosed();
570573
modelManager.serialize(stream, outputFormat);
571574
}
575+
576+
@Override
577+
public void serialize(OutputStream stream, @Nullable CoreModelObject spdxDocument) throws InvalidSPDXAnalysisException, IOException {
578+
checkClosed();
579+
if (Objects.nonNull(spdxDocument)) {
580+
if (!(spdxDocument instanceof SpdxDocument)) {
581+
logger.error("Attempting to serialize "+spdxDocument.getClass().getName()+" which is not an SpdxDocument");
582+
throw new InvalidSPDXAnalysisException("Attempting to serialize "+spdxDocument.getClass().getName()+" which is not an SpdxDocument");
583+
}
584+
if (!this.documentUri.equals(((SpdxDocument)spdxDocument).getDocumentUri())) {
585+
logger.error(((SpdxDocument)spdxDocument).getDocumentUri() + " not found in model store");
586+
throw new InvalidSPDXAnalysisException(((SpdxDocument)spdxDocument).getDocumentUri() + " not found in model store");
587+
}
588+
}
589+
modelManager.serialize(stream, outputFormat);
590+
}
572591

573592
@Override
574-
public void deSerialize(InputStream stream, boolean overwrite) throws InvalidSPDXAnalysisException, IOException {
593+
public SpdxDocument deSerialize(InputStream stream, boolean overwrite) throws InvalidSPDXAnalysisException, IOException {
575594
Model model = ModelFactory.createDefaultModel();
576595
model.read(stream, null, this.outputFormat.getType());
577596
List<String> documentNamespaces = getDocumentNamespaces(model);
@@ -592,6 +611,16 @@ public void deSerialize(InputStream stream, boolean overwrite) throws InvalidSPD
592611
}
593612
this.modelManager = new RdfSpdxModelManager(documentNamespace, model);
594613
this.documentUri = documentNamespace;
614+
615+
@SuppressWarnings("unchecked")
616+
Stream<SpdxDocument> documentStream = (Stream<SpdxDocument>)SpdxModelFactory.getSpdxObjects(this, null, SpdxConstantsCompatV2.CLASS_SPDX_DOCUMENT,
617+
documentNamespace + "#" + SpdxConstantsCompatV2.SPDX_DOCUMENT_ID, documentNamespace);
618+
List<SpdxDocument> documents = documentStream.collect(Collectors.toList());
619+
if (documents.isEmpty()) {
620+
logger.error("No SPDX document found in file");;
621+
throw new InvalidSPDXAnalysisException("No SPDX document was found in file");
622+
}
623+
return documents.get(0);
595624
}
596625

597626

src/test/java/org/spdx/spdxRdfStore/RdfStoreTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
*/
44
package org.spdx.spdxRdfStore;
55

6+
import java.io.ByteArrayOutputStream;
67
import java.io.File;
78
import java.io.IOException;
9+
import java.io.InputStream;
810
import java.util.ArrayList;
911
import java.util.Collection;
1012
import java.util.List;
@@ -16,6 +18,7 @@
1618
import org.apache.jena.query.QueryExecutionFactory;
1719
import org.apache.jena.query.ResultSet;
1820
import org.apache.jena.rdf.model.Model;
21+
import org.apache.jena.riot.RDFDataMgr;
1922
import org.spdx.core.InvalidSPDXAnalysisException;
2023
import org.spdx.core.ModelRegistry;
2124
import org.spdx.core.TypedValue;
@@ -227,5 +230,25 @@ public void testDuplicateHasFiles() throws InvalidSPDXAnalysisException, IOExcep
227230
}
228231
assertTrue(found);
229232
}
230-
233+
234+
public void testSerializeDeserialize() throws Exception {
235+
try (RdfStore rdfStore = new RdfStore()) {
236+
SpdxDocument result = null;
237+
try (InputStream spdxRdfInput = RDFDataMgr.open(TEST_FILE_NAME)) {
238+
result = rdfStore.deSerialize(spdxRdfInput, false);
239+
assertEquals("http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301", result.getDocumentUri());
240+
assertEquals("http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301", rdfStore.getDocumentUri());
241+
List<String> verify = result.verify();
242+
assertTrue(verify.isEmpty());
243+
}
244+
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
245+
rdfStore.serialize(output);
246+
assertTrue(output.size() > 0);
247+
}
248+
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
249+
rdfStore.serialize(output, result);
250+
assertTrue(output.size() > 0);
251+
}
252+
}
253+
}
231254
}

0 commit comments

Comments
 (0)