From 88318a4522224fd7eb6f6e06b24159df3850bc04 Mon Sep 17 00:00:00 2001 From: Yev Bronshteyn Date: Mon, 12 Sep 2016 02:33:33 -0400 Subject: [PATCH] When document contains mulitple packages not indicated by describes relationsihp, these now appear in the package list. --- src/main/java/spdxedit/SpdxLogic.java | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/spdxedit/SpdxLogic.java b/src/main/java/spdxedit/SpdxLogic.java index ed83bfc..116e605 100644 --- a/src/main/java/spdxedit/SpdxLogic.java +++ b/src/main/java/spdxedit/SpdxLogic.java @@ -3,6 +3,10 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; import com.google.common.net.MediaType; +import com.hp.hpl.jena.rdf.model.Property; +import com.hp.hpl.jena.rdf.model.ResIterator; +import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.rdf.model.impl.PropertyImpl; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; @@ -18,7 +22,6 @@ import org.spdx.rdfparser.model.*; import org.spdx.rdfparser.model.Relationship.RelationshipType; import org.spdx.rdfparser.model.SpdxFile.FileType; -import org.spdx.rdfparser.referencetype.ListedReferenceTypes; import org.spdx.rdfparser.referencetype.ReferenceType; import java.io.IOException; @@ -62,11 +65,19 @@ public static void addPackageToDocument(SpdxDocument document, SpdxPackage pkg) } public static Stream getSpdxPackagesInDocument(SpdxDocument document) { - return Arrays.stream(document.getRelationships()) - .filter(relationship -> relationship.getRelationshipType() == RelationshipType.DESCRIBES) - // Just in case - .filter(relationship -> relationship.getRelatedSpdxElement() instanceof SpdxPackage) - .map(relationship -> (SpdxPackage) relationship.getRelatedSpdxElement()); + final Property rdfType = new PropertyImpl("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "type"); + final String packageTypeUri = "http://spdx.org/rdf/terms#Package"; + ResIterator resIterator = document.getDocumentContainer().getModel().listResourcesWithProperty(rdfType, document.getDocumentContainer().getModel().getResource(packageTypeUri)); + List result = new LinkedList(); + try { + while (resIterator.hasNext()) { + Resource resource = resIterator.nextResource(); + result.add(new SpdxPackage(document.getDocumentContainer(), resource.asNode())); + } + return result.stream(); + } catch (InvalidSPDXAnalysisException e) { + throw new RuntimeException(e); + } } public static SpdxDocument createDocumentWithPackages(Iterable packages) {