Java Code Examples for org.apache.jena.util.iterator.ExtendedIterator#hasNext()

The following examples show how to use org.apache.jena.util.iterator.ExtendedIterator#hasNext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RdfModelObject.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * Finds all SPDX elements with a subject of this object
 * @param namespace
 * @param propertyName
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
protected SpdxElement[] findMultipleElementPropertyValues(String namespace,
		String propertyName) throws InvalidSPDXAnalysisException {
	if (this.model == null || this.node == null) {
		return null;
	}
	Node p = model.getProperty(namespace, propertyName).asNode();
	Triple m = Triple.createMatch(node, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	List<SpdxElement> retval = Lists.newArrayList();
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		retval.add(SpdxElementFactory.createElementFromModel(modelContainer, 
				t.getObject()));
	}
	return retval.toArray(new SpdxElement[retval.size()]);
}
 
Example 2
Source File: RdfModelObject.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @param nameSpace
 * @param propertyName
 * @param externalRef
 * @return all external references found as objects to the property
 * @throws InvalidSPDXAnalysisException
 */
protected ExternalRef[] findExternalRefPropertyValues(String nameSpace,
		String propertyName) throws InvalidSPDXAnalysisException {
	if (this.model == null || this.node == null) {
		return null;
	}
	Node p = model.getProperty(nameSpace, propertyName).asNode();
	Triple m = Triple.createMatch(node, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	List<ExternalRef> retval = Lists.newArrayList();
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		retval.add(new ExternalRef(modelContainer, t.getObject()));
	}
	return retval.toArray(new ExternalRef[retval.size()]);
}
 
Example 3
Source File: SPDXDocument.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * Find all property string values belonging to the subject
 * @param subject
 * @param nameSpace
 * @param propertyName
 * @return string values of the properties or null if the subject or propertyName is null
 */
private String[] findDocPropertieStringValues(Node subject, String nameSpace, String propertyName) {
	if (subject == null || propertyName == null) {
		return null;
	}
	List<String> alResult = Lists.newArrayList();
	Node p = model.getProperty(nameSpace, propertyName).asNode();
	Triple m = Triple.createMatch(subject, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		if (t.getObject().isURI()) {
			if (t.getObject().getURI().equals(SpdxRdfConstants.URI_VALUE_NONE)) {
				alResult.add(SpdxRdfConstants.NONE_VALUE);
			} else if (t.getObject().getURI().equals(SpdxRdfConstants.URI_VALUE_NOASSERTION)) {
				alResult.add(SpdxRdfConstants.NOASSERTION_VALUE);
			} else {
				alResult.add(t.getObject().toString(false));
			}
		} else {
			alResult.add(t.getObject().toString(false));
		}
	}
	String[] retval = new String[alResult.size()];
	return alResult.toArray(retval);
}
 
Example 4
Source File: RdfModelObject.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * Find all annotations with a subject of this object
 * @param nameSpace
 * @param propertyName
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
protected Relationship[] findRelationshipPropertyValues(String nameSpace,
		String propertyName) throws InvalidSPDXAnalysisException {
	if (this.model == null || this.node == null) {
		return null;
	}
	List<Relationship> retval = Lists.newArrayList();
	Node p = model.getProperty(nameSpace, propertyName).asNode();
	Triple m = Triple.createMatch(node, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		retval.add(new Relationship(this.modelContainer, t.getObject()));
	}
	return retval.toArray(new Relationship[retval.size()]);
}
 
Example 5
Source File: TestSpdxFile.java    From tools with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoassertionCopyright() throws InvalidSPDXAnalysisException {
	model = ModelFactory.createDefaultModel();
	IModelContainer modelContainer = new ModelContainerForTest(model, "http://somethingunique.com/something");
	SpdxFile file = new SpdxFile("filename", null, null, null, 
			COMPLEX_LICENSE, CONJUNCTIVE_LICENSES, SpdxRdfConstants.NOASSERTION_VALUE, null,
			null, new Checksum[] {new Checksum(ChecksumAlgorithm.checksumAlgorithm_sha1,
					"1123456789abcdef0123456789abcdef01234567")}, null, null, null);
	Resource fileResource = file.createResource(modelContainer);
	Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_COPYRIGHT).asNode();
	Triple m = Triple.createMatch(null, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		assertTrue(t.getObject().isURI());
		assertEquals(SpdxRdfConstants.URI_VALUE_NOASSERTION, t.getObject().getURI());
	}
	SpdxFile file2 = new SpdxFile(modelContainer, fileResource.asNode());
	assertEquals(SpdxRdfConstants.NOASSERTION_VALUE, file2.getCopyrightText());
}
 
Example 6
Source File: RdfModelObject.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @param nameSpace
 * @param propertyName
 * @param checksumValue
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
protected DoapProject[] findMultipleDoapPropertyValues(String nameSpace,
		String propertyName) throws InvalidSPDXAnalysisException {
	if (this.model == null || this.node == null) {
		return new DoapProject[0];
	}
	List<DoapProject> retval = Lists.newArrayList();
	Node p = model.getProperty(nameSpace, propertyName).asNode();
	Triple m = Triple.createMatch(node, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		retval.add(new DoapProject(modelContainer, t.getObject()));
	}
	return retval.toArray(new DoapProject[retval.size()]);
}
 
Example 7
Source File: localname.java    From xcurator with Apache License 2.0 6 votes vote down vote up
private QueryIterator execAllNodes(Var subjVar, Node nodeLocalname,  Binding input, ExecutionContext execCxt)
{
    if ( ! nodeLocalname.isVariable() )
    {
        if ( ! nodeLocalname.isLiteral() )
            // Not a variable, not a literal=> can't match
            return QueryIterNullIterator.create(execCxt) ;
    
        if( ! NodeUtils.isSimpleString(nodeLocalname) )
            return QueryIterNullIterator.create(execCxt) ;
    }
    
    //Set bindings = new HashSet() ;    // Use a Set if you want unique results. 
    List<Binding> bindings = new ArrayList<Binding>() ;   // Use a list if you want counting results. 
    Graph graph = execCxt.getActiveGraph() ;
    
    ExtendedIterator<Triple>iter = graph.find(Node.ANY, Node.ANY, Node.ANY) ;
    for ( ; iter.hasNext() ; )
    {
        Triple t = iter.next() ;
        slot(bindings, input, t.getSubject(),   subjVar, nodeLocalname) ;
        slot(bindings, input, t.getPredicate(), subjVar, nodeLocalname) ;
        slot(bindings, input, t.getObject(),    subjVar, nodeLocalname) ;
    }
    return new QueryIterPlainWrapper(bindings.iterator(), execCxt) ;
}
 
Example 8
Source File: RdfModelObject.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @param nameSpace
 * @param propertyName
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
protected Checksum[] findMultipleChecksumPropertyValues(String nameSpace,
		String propertyName) throws InvalidSPDXAnalysisException {
	if (this.model == null || this.node == null) {
		return new Checksum[0];
	}
	List<Checksum> retval = Lists.newArrayList();
	Node p = model.getProperty(nameSpace, propertyName).asNode();
	Triple m = Triple.createMatch(node, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		retval.add(new Checksum(modelContainer, t.getObject()));
	}
	return retval.toArray(new Checksum[retval.size()]);
}
 
Example 9
Source File: SpdxSnippet.java    From tools with Apache License 2.0 6 votes vote down vote up
@Override
public Resource findDuplicateResource(IModelContainer modelContainer, String uri) throws InvalidSPDXAnalysisException {
	if (this.snippetFromFile == null) {
		return null;
	}
	if (this.byteRange == null) {
		return null;
	}
	Resource snippetFromFileResource = SpdxFile.findFileResource(modelContainer, this.snippetFromFile);
	if (snippetFromFileResource == null) {
		return null;
	}
	Model model = modelContainer.getModel();
	Node snippetFromFileProperty = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_SNIPPET_FROM_FILE).asNode();
	Triple fileMatch = Triple.createMatch(null, snippetFromFileProperty, snippetFromFileResource.asNode());
	
	ExtendedIterator<Triple> fileMatchIter = model.getGraph().find(fileMatch);	
	while (fileMatchIter.hasNext()) {
		Triple fileMatchTriple = fileMatchIter.next();
		SpdxSnippet localSnippet = new SpdxSnippet(modelContainer, fileMatchTriple.getSubject());
		if (this.byteRange.equivalent(localSnippet.getByteRange())) {
			return model.asRDFNode(fileMatchTriple.getSubject()).asResource();
		}
	}
	return null;
}
 
Example 10
Source File: SPDXChecksum.java    From tools with Apache License 2.0 5 votes vote down vote up
protected static Resource findSpdxChecksum(Model model, SPDXChecksum checksum) throws InvalidSPDXAnalysisException {
	// find any matching checksum values
	Node checksumValueProperty = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_CHECKSUM_VALUE).asNode();
	Triple checksumValueMatch = Triple.createMatch(null, checksumValueProperty, NodeFactory.createLiteral(checksum.getValue()));
	ExtendedIterator<Triple> checksumMatchIter = model.getGraph().find(checksumValueMatch);	
	while (checksumMatchIter.hasNext()) {
		Triple checksumMatchTriple = checksumMatchIter.next();
		Node checksumNode = checksumMatchTriple.getSubject();
		// check the algorithm
		Node algorithmProperty = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_CHECKSUM_ALGORITHM).asNode();
		Triple algorithmMatch = Triple.createMatch(checksumNode, algorithmProperty, null);
		ExtendedIterator<Triple> algorithmMatchIterator = model.getGraph().find(algorithmMatch);
		if (algorithmMatchIterator.hasNext()) {
			String algorithm = "UNKNOWN";
			Triple algorithmMatchTriple = algorithmMatchIterator.next();
			if (algorithmMatchTriple.getObject().isLiteral()) {
				// The following is for compatibility with rdf generated with older
				// versions of the tool
				algorithm = algorithmMatchTriple.getObject().toString(false);
			} else if (algorithmMatchTriple.getObject().isURI()) {
				algorithm = URI_TO_ALGORITHM.get(algorithmMatchTriple.getObject().getURI());
				if (algorithm == null) {
					algorithm = "UNKNOWN";
				}
			}
			if (algorithm.equals(checksum.getAlgorithm())) {
				return RdfParserHelper.convertToResource(model, checksumNode);
			}
		}
	}
	// if we get to here, we did not find a match
	return null;
}
 
Example 11
Source File: SPDXDocument.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * @return the spdx doc node from the model
 */
private Node getSpdxDocNode() {
	Node spdxDocNode = null;
	Node rdfTypePredicate = this.model.getProperty(RDF_NAMESPACE, RDF_PROP_TYPE).asNode();
	Node spdxDocObject = this.model.getProperty(SPDX_NAMESPACE, CLASS_SPDX_DOCUMENT).asNode();
	Triple m = Triple.createMatch(null, rdfTypePredicate, spdxDocObject);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	// find the document
	while (tripleIter.hasNext()) {
		Triple docTriple = tripleIter.next();
		spdxDocNode = docTriple.getSubject();
	}
	return spdxDocNode;
}
 
Example 12
Source File: SPDXFile.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * @return contributors to the file
 */
public String[] getContributors() {
	if (this.model != null && this.resource != null) {
		List<String> alContributors = Lists.newArrayList();
		Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_CONTRIBUTOR).asNode();
		Triple m = Triple.createMatch(this.resource.asNode(), p, null);
		ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
		while (tripleIter.hasNext()) {
			Triple t = tripleIter.next();
			alContributors.add(t.getObject().toString(false));
		}
		this.contributors = alContributors.toArray(new String[alContributors.size()]);
	}
	return this.contributors;
}
 
Example 13
Source File: OWLClassPropertyMetadataPlugin.java    From shacl with Apache License 2.0 5 votes vote down vote up
@Override
public void init(ClassPropertyMetadata cpm, Node classNode, Graph graph) {
	ExtendedIterator<Triple> it = graph.find(classNode, RDFS.subClassOf.asNode(), Node.ANY);
	while(it.hasNext()) {
		Node superClass = it.next().getObject();
		if(superClass.isBlank() && graph.contains(superClass, OWL.onProperty.asNode(), cpm.getPredicate())) {
			if(cpm.getLocalRange() == null) {
				Node localRange = JenaNodeUtil.getObject(superClass, OWL.allValuesFrom.asNode(), graph);
				if(localRange != null) {
					cpm.setLocalRange(localRange);
					it.close();
					break;
				}
			}
			if(cpm.getMaxCount() == null) {
				Node maxCountNode = JenaNodeUtil.getObject(superClass, OWL.maxCardinality.asNode(), graph);
				if(maxCountNode == null) {
					maxCountNode = JenaNodeUtil.getObject(superClass, OWL.cardinality.asNode(), graph);
				}
				if(maxCountNode != null && maxCountNode.isLiteral()) {
					Object value = maxCountNode.getLiteralValue();
					if(value instanceof Number) {
						cpm.setMaxCount(((Number) value).intValue());
					}
				}
			}
		}
	}
}
 
Example 14
Source File: SPDXFile.java    From tools with Apache License 2.0 5 votes vote down vote up
public String getComment() {
	if (this.model != null && this.resource != null) {
		Node p = model.getProperty(SpdxRdfConstants.RDFS_NAMESPACE, SpdxRdfConstants.RDFS_PROP_COMMENT).asNode();
		Triple m = Triple.createMatch(this.resource.asNode(), p, null);
		ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
		while (tripleIter.hasNext()) {
			this.comment = tripleIter.next().getObject().toString(false);
		}
	}
	return this.comment;
}
 
Example 15
Source File: SPDXLicenseInfoFactory.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * @param model
 * @param node
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
private static SPDXLicenseInfo getLicenseInfoByType(Model model, Node node) throws InvalidSPDXAnalysisException {
	// find the subclass
	Node rdfTypePredicate = model.getProperty(SpdxRdfConstants.RDF_NAMESPACE, 
			SpdxRdfConstants.RDF_PROP_TYPE).asNode();
	Triple m = Triple.createMatch(node, rdfTypePredicate, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	// find the type(s)
	if (tripleIter.hasNext()) {
		Triple triple = tripleIter.next();
		if (tripleIter.hasNext()) {
			throw(new InvalidSPDXAnalysisException("More than one type associated with a licenseInfo"));
		}
		Node typeNode = triple.getObject();
		if (!typeNode.isURI()) {
			throw(new InvalidSPDXAnalysisException("Invalid type for licenseInfo - not a URI"));
		}
		// need to parse the URI
		String typeUri = typeNode.getURI();
		if (!typeUri.startsWith(SpdxRdfConstants.SPDX_NAMESPACE)) {
			throw(new InvalidSPDXAnalysisException("Invalid type for licenseInfo - not an SPDX type"));
		}
		String type = typeUri.substring(SpdxRdfConstants.SPDX_NAMESPACE.length());
		if (type.equals(SpdxRdfConstants.CLASS_SPDX_CONJUNCTIVE_LICENSE_SET)) {
			return new SPDXConjunctiveLicenseSet(model, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_DISJUNCTIVE_LICENSE_SET)) {
			return new SPDXDisjunctiveLicenseSet(model, node);
		}else if (type.equals(SpdxRdfConstants.CLASS_SPDX_EXTRACTED_LICENSING_INFO)) {
			return new SPDXNonStandardLicense(model, node);
		}else if (type.equals(SpdxRdfConstants.CLASS_SPDX_STANDARD_LICENSE)) {
			return new SPDXStandardLicense(model, node);
		} else {
			throw(new InvalidSPDXAnalysisException("Invalid type for licenseInfo '"+type+"'"));
		}
	} else {
		return null;
	}
}
 
Example 16
Source File: LicenseException.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * Get the exception text from the model
 * @param model
 * @param exceptionNode
 * @return
 */
public static String getExceptionTextFromModel(Model model, Node exceptionNode) {
	Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_EXCEPTION_TEXT).asNode();
	Triple m = Triple.createMatch(exceptionNode, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	if (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		return t.getObject().toString(false);
	}
	return null;
}
 
Example 17
Source File: SPDXDocument.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * @return the files
 * @throws InvalidSPDXAnalysisException 
 */
public SPDXFile[] getFiles() throws InvalidSPDXAnalysisException {
	// files
	List<SPDXFile> alFiles = Lists.newArrayList();
	Node p = model.getProperty(SPDX_NAMESPACE, PROP_PACKAGE_FILE).asNode();
	Triple m = Triple.createMatch(this.node, p, null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		alFiles.add(new SPDXFile(enclosingSpdxDocument, t.getObject()));
	}
	SPDXFile[] retval = new SPDXFile[alFiles.size()];
	return alFiles.toArray(retval);
}
 
Example 18
Source File: LicenseInfoFactory.java    From tools with Apache License 2.0 4 votes vote down vote up
/**
 * @param modelContainer
 * @param node
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
private static AnyLicenseInfo getLicenseInfoByType(IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException {
	// find the subclass
	Node rdfTypePredicate = modelContainer.getModel().getProperty(SpdxRdfConstants.RDF_NAMESPACE, 
			SpdxRdfConstants.RDF_PROP_TYPE).asNode();
	Triple m = Triple.createMatch(node, rdfTypePredicate, null);
	ExtendedIterator<Triple> tripleIter = modelContainer.getModel().getGraph().find(m);	// find the type(s)
	if (tripleIter.hasNext()) {
		Triple triple = tripleIter.next();
		if (tripleIter.hasNext()) {
			throw(new InvalidSPDXAnalysisException("More than one type associated with a licenseInfo"));
		}
		Node typeNode = triple.getObject();
		if (!typeNode.isURI()) {
			throw(new InvalidSPDXAnalysisException("Invalid type for licenseInfo - not a URI"));
		}
		// need to parse the URI
		String typeUri = typeNode.getURI();
		if (!typeUri.startsWith(SpdxRdfConstants.SPDX_NAMESPACE)) {
			throw(new InvalidSPDXAnalysisException("Invalid type for licenseInfo - not an SPDX type"));
		}
		String type = typeUri.substring(SpdxRdfConstants.SPDX_NAMESPACE.length());
		if (type.equals(SpdxRdfConstants.CLASS_SPDX_CONJUNCTIVE_LICENSE_SET)) {
			return new ConjunctiveLicenseSet(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_DISJUNCTIVE_LICENSE_SET)) {
			return new DisjunctiveLicenseSet(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_EXTRACTED_LICENSING_INFO)) {
			return new ExtractedLicenseInfo(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_LICENSE)) {
			return new SpdxListedLicense(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_OR_LATER_OPERATOR)) {
			return new OrLaterOperator(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_WITH_EXCEPTION_OPERATOR)) {
			return new WithExceptionOperator(modelContainer, node);
		} else {
			throw(new InvalidSPDXAnalysisException("Invalid type for licenseInfo '"+type+"'"));
		}
	} else {
		return null;
	}
}
 
Example 19
Source File: ClassPropertyMetadata.java    From shacl with Apache License 2.0 4 votes vote down vote up
private void initFromShape(Node shape, Node systemPredicate, Graph graph) {
	ExtendedIterator<Triple> it = graph.find(shape, systemPredicate, Node.ANY);
	while(it.hasNext()) {
		Node propertyShape = it.next().getObject();
		if(!propertyShape.isLiteral()) {
			if(hasMatchingPath(propertyShape, graph)) {
				if(!graph.contains(propertyShape, SH.deactivated.asNode(), JenaDatatypes.TRUE.asNode())) {
					if(description == null) {
						description = JenaNodeUtil.getObject(propertyShape, SH.description.asNode(), graph);
					}
					if(editWidget == null) {
						editWidget = JenaNodeUtil.getObject(propertyShape, TOSH.editWidget.asNode(), graph);
					}
					if(localRange == null) {
						if(inverse) {
							// Maybe: support inverse ranges
						}
						else {
							localRange = SHACLUtil.walkPropertyShapesHelper(propertyShape, graph);
						}
					}
					if(maxCount == null) {
						Node maxCountNode = JenaNodeUtil.getObject(propertyShape, SH.maxCount.asNode(), graph);
						if(maxCountNode != null && maxCountNode.isLiteral()) {
							Object value = maxCountNode.getLiteralValue();
							if(value instanceof Number) {
								maxCount = ((Number) value).intValue();
							}
						}
					}
					if(name == null) {
						name = JenaNodeUtil.getObject(propertyShape, SH.name.asNode(), graph);
					}
					if(order == null) {
						order = JenaNodeUtil.getObject(propertyShape, SH.order.asNode(), graph);
					}
					if(viewWidget == null) {
						viewWidget = JenaNodeUtil.getObject(propertyShape, TOSH.viewWidget.asNode(), graph);
					}
				}
			}
		}
	}
}
 
Example 20
Source File: SpdxElementFactory.java    From tools with Apache License 2.0 4 votes vote down vote up
/**
 * @param modelContainer
 * @param node
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
private static SpdxElement getElementByType(IModelContainer modelContainer,
		Node node) throws InvalidSPDXAnalysisException {
	Node rdfTypePredicate = modelContainer.getModel().getProperty(SpdxRdfConstants.RDF_NAMESPACE, 
			SpdxRdfConstants.RDF_PROP_TYPE).asNode();
	Triple m = Triple.createMatch(node, rdfTypePredicate, null);
	ExtendedIterator<Triple> tripleIter = modelContainer.getModel().getGraph().find(m);	// find the type(s)
	if (tripleIter.hasNext()) {
		Triple triple = tripleIter.next();
		if (tripleIter.hasNext()) {
			throw(new InvalidSPDXAnalysisException("More than one type associated with an SPDX Element"));
		}
		Node typeNode = triple.getObject();
		if (!typeNode.isURI()) {
			throw(new InvalidSPDXAnalysisException("Invalid type for an SPDX Element - not a URI"));
		}
		// need to parse the URI
		String typeUri = typeNode.getURI();
		if (!typeUri.startsWith(SpdxRdfConstants.SPDX_NAMESPACE)) {
			throw(new InvalidSPDXAnalysisException("Invalid type for an SPDX Element - not an SPDX type"));
		}
		String type = typeUri.substring(SpdxRdfConstants.SPDX_NAMESPACE.length());
		if (type.equals(SpdxRdfConstants.CLASS_SPDX_FILE)) {
			return new SpdxFile(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_PACKAGE)) {
			return new SpdxPackage(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_SNIPPET)) {
			return new SpdxSnippet(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_ITEM)) {
			return new SpdxItem(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_ELEMENT)) {
			return new SpdxElement(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_DOCUMENT)) {
			return new SpdxDocumentContainer(modelContainer.getModel()).getSpdxDocument();
		} else {
			throw(new InvalidSPDXAnalysisException("Invalid type for element '"+type+"'"));
		}
	} else {
		return null;
	}
}