Java Code Examples for com.hp.hpl.jena.graph.Node#isVariable()

The following examples show how to use com.hp.hpl.jena.graph.Node#isVariable() . 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: PrettyPrinter.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
/**
 * Pretty-prints an RDF node and shortens URIs into QNames according to a
 * {@link PrefixMapping}.
 * @param n An RDF node
 * @return An N-Triples style textual representation with URIs shortened to QNames
 */
public static String toString(Node n, PrefixMapping prefixes) {
	if (n.isURI()) {
		return qNameOrURI(n.getURI(), prefixes);
	}
	if (n.isBlank()) {
		return "_:" + n.getBlankNodeLabel();
	}
	if (n.isVariable()) {
		return "?" + n.getName();
	}
	if (Node.ANY.equals(n)) {
		return "?ANY";
	}
	// Literal
	String s = "\"" + n.getLiteralLexicalForm() + "\"";
	if (!"".equals(n.getLiteralLanguage())) {
		s += "@" + n.getLiteralLanguage();
	}
	if (n.getLiteralDatatype() != null) {
		s += "^^" + qNameOrURI(n.getLiteralDatatypeURI(), prefixes);
	}
	return s;
}
 
Example 2
Source File: TypeConversion.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Transforms a Jena node into a java object. Possible node types: uri,
 * variable, literal (datatype, languageTag), blank node
 * 
 * @param n The node to transform
 * @return A specific java object
 * @throws ModelRuntimeException from the underlying model
 */
public static org.ontoware.rdf2go.model.node.Node toRDF2Go(Node n) throws ModelRuntimeException {
	// A return of null indicates that the variable is not present in this
	// solution.
	if(n == null)
		return null;
	
	if(n.isURI())
		return new URIImpl(n.getURI());
	
	if(n.isVariable())
		throw new RuntimeException("Cannot convert a Jena variable to an RDF2Go node");
	
	if(n.isLiteral()) {
		LiteralLabel lit = n.getLiteral();
		// datatype
		if(lit.getDatatypeURI() != null) {
			return new DatatypeLiteralImpl(lit.getLexicalForm(), new URIImpl(
			        lit.getDatatypeURI()));
		}
		
		// language tagged
		if(lit.language() != null && !lit.language().equals(""))
			return new LanguageTagLiteralImpl(lit.getLexicalForm(), lit.language());
		
		// plain
		return new PlainLiteralImpl(lit.getLexicalForm());
	}
	
	if(n.isBlank())
		return new JenaBlankNode(n);
	
	// none of the above - don't know how to transform that
	throw new RuntimeException("no transformation defined from " + n + " to java");
}