Java Code Examples for org.apache.jena.graph.Node#isURI()

The following examples show how to use org.apache.jena.graph.Node#isURI() . 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: IsValidForDatatypeFunction.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
protected NodeValue exec(Node literalNode, Node datatypeNode, FunctionEnv env) {
	
	if(literalNode == null || !literalNode.isLiteral()) {
		throw new ExprEvalException();
	}
	String lex = literalNode.getLiteralLexicalForm();
	
	if(!datatypeNode.isURI()) {
		throw new ExprEvalException();
	}
	RDFDatatype datatype = TypeMapper.getInstance().getTypeByName(datatypeNode.getURI());
	
	if(datatype == null) {
		return NodeValue.TRUE;
	}
	else {
		boolean valid = datatype.isValid(lex);
		return NodeValue.makeBoolean(valid);
	}
}
 
Example 2
Source File: LicenseInfoFactory.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * Create the appropriate SPDXLicenseInfo from the model and node provided.
 * The appropriate SPDXLicenseInfo subclass object will be chosen based on
 * the class (rdf type) of the node.  If there is no rdf type, then the
 * license ID is parsed to determine the type
 * @param modelContainer
 * @param node
 * @return
 */
public static AnyLicenseInfo getLicenseInfoFromModel(IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException {
	if (!node.isURI() && !node.isBlank()) {
		throw(new InvalidSPDXAnalysisException("Can not create a LicenseInfo from a literal node"));
	}
	AnyLicenseInfo retval = null;
	// check to see if it is a "simple" type of license (NONESEEN, NONE, NOTANALYZED, or SPDX_LISTED_LICENSE)
	if (node.isURI()) {
		retval = getLicenseInfoByUri(modelContainer, node);
	}
	if (retval == null) {	// try by type
		retval = getLicenseInfoByType(modelContainer, node);
	}
	if (retval == null) {	// try by ID
		retval = getLicenseInfoById(modelContainer, node);
	}
	if (retval == null) {	// OK, we give up
		logger.error("Could not determine the type for a license");
		throw(new InvalidSPDXAnalysisException("Could not determine the type for a license"));
	}
	return retval;
}
 
Example 3
Source File: LicenseInfoFactory.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * Obtains an SPDX license by a URI - could be a listed license or a predefined license type
 * @param document
 * @param node
 * @return License Info for the license or NULL if no external listed license info could be found
 * @throws InvalidSPDXAnalysisException 
 */
private static AnyLicenseInfo getLicenseInfoByUri(IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException {
	if (!node.isURI()) {
		return null;
	}
	if (node.getURI().equals(SpdxRdfConstants.SPDX_NAMESPACE+SpdxRdfConstants.TERM_LICENSE_NONE)) {
		return new SpdxNoneLicense(modelContainer, node);
	} else if (node.getURI().equals(SpdxRdfConstants.SPDX_NAMESPACE+SpdxRdfConstants.TERM_LICENSE_NOASSERTION)) {
		return new SpdxNoAssertionLicense(modelContainer, node);
	} else if (node.getURI().startsWith(ListedLicenses.LISTED_LICENSE_ID_URL)) {
		// try to fetch the listed license from the model
		try {
			return ListedLicenses.getListedLicenses().getLicenseFromStdLicModel(modelContainer, node);
		} catch (Exception ex) {
			logger.warn("Unable to get license from SPDX listed license model for "+node.getURI());
			return null;
		}
	} else {
		return null;
	}
}
 
Example 4
Source File: EvalUtils.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
/**
 * The query name may be an expression, which evaluates differently
 * depending on the input bindings. This method groups the bindings for
 * which the query name evaluation is the same.
 *
 * @param expr the expression for the query name
 * @param bindings
 * @param env
 * @return
 */
public static Map<String, List<Binding>> splitBindingsForQuery(
        final Expr expr,
        final List<Binding> bindings,
        final FunctionEnv env) {
    final Map<String, List<Binding>> calls = new HashMap<>();
    for (Binding binding : bindings) {
        final Node n = eval(expr, binding, env);
        if (n == null) {
            continue;
        }
        if (!n.isURI()) {
            LOG.warn("Name of sub query resolved to something else than a"
                    + " URI: " + n);
            continue;
        }
        String queryName = n.getURI();
        List<Binding> call = calls.get(queryName);
        if (call == null) {
            call = new ArrayList<>();
            calls.put(queryName, call);
        }
        call.add(binding);
    }
    return calls;
}
 
Example 5
Source File: OWLQLCompiler.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
protected String getKey(Triple t) {
	Node subj = t.getSubject();
	Node obj = t.getObject();
	Node pred = t.getPredicate();
	if (!pred.isURI()) {
		return null;
	}
	if (!subj.isVariable() && !obj.isVariable()) {
		return null;
	}
	if (pred.getURI().equals(RDFConstants.RDF_TYPE)) {
		return obj.isURI()? obj.getURI() : null;
	} else {
		return pred.getURI();
	}
	
}
 
Example 6
Source File: SourcePlan.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param binding -
 * @return the actual URI that represents the location of the query to be
 * fetched.
 */
private String getActualSource(
        final Binding binding) {
    if (node.isVariable()) {
        Node actualSource = binding.get((Var) node);
        if (actualSource == null) {
            return null;
        }
        if (!actualSource.isURI()) {
            throw new SPARQLExtException("Variable " + node.getName()
                    + " must be bound to a IRI that represents the location"
                    + " of the query to be fetched.");
        }
        return actualSource.getURI();
    } else {
        if (!node.isURI()) {
            throw new SPARQLExtException("The source must be a IRI"
                    + " that represents the location of the query to be"
                    + " fetched. Got " + node.getURI());
        }
        return node.getURI();
    }
}
 
Example 7
Source File: localname.java    From xcurator with Apache License 2.0 6 votes vote down vote up
private QueryIterator execFixedSubject(Node nodeURI, Node nodeLocalname, Binding binding, ExecutionContext execCxt)
{
    if ( ! nodeURI.isURI() )
        // Subject bound but not a URI
        return QueryIterNullIterator.create(execCxt) ;

    // Subject is bound and a URI - get the localname as a Node 
    Node localname = NodeFactory.createLiteral(nodeURI.getLocalName()) ;
    
    // Object - unbound variable or a value? 
    if ( ! nodeLocalname.isVariable() )
    {
        // Object bound or a query constant.  Is it the same as the calculated value?
        if ( nodeLocalname.equals(localname) )
            // Same
            return QueryIterSingleton.create(binding, execCxt) ;
        // No - different - no match.
        return QueryIterNullIterator.create(execCxt) ;
    }
    
    // Object unbound variable - assign the localname to it.
    return QueryIterSingleton.create(binding, Var.alloc(nodeLocalname), localname, execCxt) ;
}
 
Example 8
Source File: SourcePlan.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * The generation plan of a <code>{@code SOURCE <node> ACCEPT <mime> AS
 * <var>}</code> clause.
 *
 * @param node The IRI or the Variable node where a GET must be operated.
 * Must not be null.
 * @param accept The IRI or the Variable node that represent the accepted
 * Internet Media Type. May be null.
 * @param var The variable to bound the potentially retrieved document.
 * Must not be null.
 */
public SourcePlan(
        final Node node,
        final Node accept,
        final Var var) {
    super(var);
    Objects.requireNonNull(node, "Node must not be null");
    Objects.requireNonNull(var, "Var must not be null");
    if (!node.isURI() && !node.isVariable()) {
        throw new IllegalArgumentException("Source node must be a IRI or a"
                + " Variable. got " + node);
    }
    this.node = node;
    this.accept = accept;
}
 
Example 9
Source File: SPDXDocument.java    From tools with Apache License 2.0 5 votes vote down vote up
private Resource getResource(Node node) throws InvalidSPDXAnalysisException {
	Resource s;
	if (node.isURI()) {
		s = model.createResource(node.getURI());
	} else if (node.isBlank()) {
		s = model.createResource(new AnonId(node.getBlankNodeId()));
	} else {
		throw(new InvalidSPDXAnalysisException("Node can not be a literal"));
	}
	return s;
}
 
Example 10
Source File: Node2String.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public static short getType(Node n)
{
if (n.isURI())
   {
   return TypeMap.IRI_ID;
   }
if (n.isBlank())
   {
   return TypeMap.BLANK_NODE_ID;
   }

if (n.isLiteral())
   {

   if (n.getLiteral().getDatatype() != null)
      {
      return TypeMap.getDatatypeType(n.getLiteralDatatypeURI());
      }
   else
      {
      String lang = n.getLiteralLanguage();
      if (lang != null && lang.length() > 0)
         {
         return TypeMap.getLanguageType(lang);
         }
      else
         {
         return TypeMap.SIMPLE_LITERAL_ID;
         }
      }
   }

throw new RdfStoreException("Unknown RDFterm");
}
 
Example 11
Source File: ClassMetadata.java    From shacl with Apache License 2.0 5 votes vote down vote up
private void addGroupProperties(Node nodeShape, Graph graph, Node systemPredicate) {
	ExtendedIterator<Triple> it = graph.find(nodeShape, systemPredicate, Node.ANY);
	while(it.hasNext()) {
		Node propertyShape = it.next().getObject();
		if(!graph.contains(propertyShape, SH.deactivated.asNode(), JenaDatatypes.TRUE.asNode())) {
			Node group = JenaNodeUtil.getObject(propertyShape, SH.group.asNode(), graph);
			if(group != null) {
				Node path = JenaNodeUtil.getObject(propertyShape, SH.path.asNode(), graph);
				if(path != null) {
					Set<PathMetadata> paths = groupPaths.get(group);
					if(paths == null) {
						paths = new HashSet<>();
						groupPaths.put(group, paths);
					}
					if(path.isURI()) {
						paths.add(new PathMetadata(path, false));
					}
					else {
						Node inverse = JenaNodeUtil.getObject(path, SH.inversePath.asNode(), graph);
						if(inverse != null && inverse.isURI()) {
							paths.add(new PathMetadata(inverse, true));
						}
					}
				}
			}
		}
	}
}
 
Example 12
Source File: RdfModelObject.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * Create an RDF Model Object based on an existing Node
 * @param modelContainer Container containing the RDF Model
 * @param node Node describing this object
 * @throws InvalidSPDXAnalysisException
 */
public RdfModelObject(IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException {
	this.modelContainer = modelContainer;
	this.model = modelContainer.getModel();
	this.node = node;
	this.refreshOnGet = modelContainer.addCheckNodeObject(node, this);
	if (node.isBlank()) {
		resource = model.createResource(new AnonId(node.getBlankNodeId()));
	} else if (node.isURI()) {
		resource = model.createResource(node.getURI());
	} else {
		throw(new InvalidSPDXAnalysisException("Can not have an model node as a literal"));
	}
}
 
Example 13
Source File: JSFactory.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static JSTerm asJSTerm(Node node) {
	if(node.isURI()) {
		return new JSNamedNode(node);
	}
	else if(node.isBlank()) {
		return new JSBlankNode(node);
	}
	else if(node.isLiteral()) {
		return new JSLiteral(node);
	}
	else {
		throw new IllegalArgumentException("Unsupported node type " + node);
	}
}
 
Example 14
Source File: SPDXDocument.java    From tools with Apache License 2.0 5 votes vote down vote up
private Resource getResource(Node node) throws InvalidSPDXAnalysisException {
	Resource s;
	if (node.isURI()) {
		s = model.createResource(node.getURI());
	} else if (node.isBlank()) {
		s = model.createResource(node.getBlankNodeId());
	} else {
		throw(new InvalidSPDXAnalysisException("Node can not be a literal"));
	}
	return s;
}
 
Example 15
Source File: PointerFactory.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * Get the single pointer class based on the element type specified in the model
 * @param modelContainer
 * @param node
 * @return
 * @throws InvalidSPDXAnalysisException 
 */
private static SinglePointer 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 a SinglePointer"));
		}
		Node typeNode = triple.getObject();
		if (!typeNode.isURI()) {
			throw(new InvalidSPDXAnalysisException("Invalid type for a SinglePointer - not a URI"));
		}
		// need to parse the URI
		String typeUri = typeNode.getURI();
		if (!typeUri.startsWith(SpdxRdfConstants.RDF_POINTER_NAMESPACE)) {
			throw(new InvalidSPDXAnalysisException("Invalid type for a SinglePointer - not an RDF Pointer type (namespace must begin with "+
						SpdxRdfConstants.RDF_POINTER_NAMESPACE));
		}
		String type = typeUri.substring(SpdxRdfConstants.RDF_POINTER_NAMESPACE.length());
		if (type.equals(SpdxRdfConstants.CLASS_POINTER_BYTE_OFFSET_POINTER)) {
			return new ByteOffsetPointer(modelContainer, node);
		} else if (type.equals(SpdxRdfConstants.CLASS_POINTER_LINE_CHAR_POINTER)) {
			return new LineCharPointer(modelContainer, node);
		} else {
			throw(new InvalidSPDXAnalysisException("Unsupported type for SinglePointer '"+type+"'"));
		}
	} else {
		return null;
	}
}
 
Example 16
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 17
Source File: SubjectMappingStreamRDF.java    From fcrepo-import-export with Apache License 2.0 5 votes vote down vote up
private Node rebase(final Node node) {
    if (node.isURI() && sourceURI != null && destinationURI != null
            && node.getURI().startsWith(sourceURI)) {
        return createURI(node.getURI().replaceFirst(sourceURI, destinationURI));
    }

    return node;
}
 
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: SpdxElementFactory.java    From tools with Apache License 2.0 4 votes vote down vote up
public static synchronized SpdxElement createElementFromModel(IModelContainer modelContainer,
		Node node) throws InvalidSPDXAnalysisException {
	Map<Node, SpdxElement> containerNodes = createdElements.get(modelContainer);
	if (containerNodes == null) {
		containerNodes = Maps.newHashMap();
		createdElements.put(modelContainer, containerNodes);
	}
	SpdxElement retval = containerNodes.get(node);
	if (retval != null) {
		return retval;
	}
	if (node.isBlank() ||
			(node.isURI() && node.getURI().startsWith(modelContainer.getDocumentNamespace()))) {
		// SPDX element local to this document
		retval = getElementByType(modelContainer, node);
		if (retval == null) {
			retval = guessElementByProperties(modelContainer, node);
			if (retval == null) {
				throw(new InvalidSPDXAnalysisException("Unable to determine the SPDX element type from the model"));
			}
		}
		containerNodes.put(node, retval);
		return retval;
	} else if (node.isURI()) {
		if (SpdxNoneElement.NONE_ELEMENT_URI.equals(node.getURI())) {
			return new SpdxNoneElement();
		} else if (SpdxNoAssertionElement.NOASSERTION_ELEMENT_URI.equals(node.getURI())) {
			return new SpdxNoAssertionElement();
		} else {
			// assume this is an external document reference
			String[] uriParts = node.getURI().split("#");
			if (uriParts.length != 2) {
				throw(new InvalidSPDXAnalysisException("Invalid element URI: "+node.getURI()));
			}
			String docId = modelContainer.documentNamespaceToId(uriParts[0]);
			if (docId == null) {
				throw(new InvalidSPDXAnalysisException("No external document reference was found for URI "+node.getURI()));
			}
			String externalId = docId + ":" + uriParts[1];
			return new ExternalSpdxElement(externalId);
		}
	} else {
		throw(new InvalidSPDXAnalysisException("Can not create an SPDX Element from a literal node"));
	}
}
 
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;
	}
}