Skip to content

Commit

Permalink
When document contains mulitple packages not indicated by describes r…
Browse files Browse the repository at this point in the history
…elationsihp, these now appear in the package list.
  • Loading branch information
yevster committed Sep 12, 2016
1 parent 16e3027 commit 88318a4
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/main/java/spdxedit/SpdxLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -62,11 +65,19 @@ public static void addPackageToDocument(SpdxDocument document, SpdxPackage pkg)
}

public static Stream<SpdxPackage> 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<SpdxPackage> result = new LinkedList<SpdxPackage>();
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<SpdxPackage> packages) {
Expand Down

0 comments on commit 88318a4

Please sign in to comment.