4242import org .apache .jena .util .iterator .ExtendedIterator ;
4343import org .slf4j .Logger ;
4444import org .slf4j .LoggerFactory ;
45+ import org .spdx .core .CoreModelObject ;
4546import org .spdx .core .DuplicateSpdxIdException ;
4647import org .spdx .core .InvalidSPDXAnalysisException ;
4748import org .spdx .core .TypedValue ;
49+ import org .spdx .library .SpdxModelFactory ;
4850import org .spdx .library .model .v2 .SpdxConstantsCompatV2 ;
51+ import org .spdx .library .model .v2 .SpdxDocument ;
4952import org .spdx .storage .CompatibleModelStoreWrapper ;
5053import org .spdx .storage .IModelStore ;
5154import 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
0 commit comments