Java Code Examples for org.eclipse.rdf4j.model.Value#getClass()

The following examples show how to use org.eclipse.rdf4j.model.Value#getClass() . 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: FileIO.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void writeValue(Value value, DataOutputStream dataOut) throws IOException {
	if (value instanceof IRI) {
		dataOut.writeByte(URI_MARKER);
		writeString(((IRI) value).toString(), dataOut);
	} else if (value instanceof BNode) {
		dataOut.writeByte(BNODE_MARKER);
		writeString(((BNode) value).getID(), dataOut);
	} else if (value instanceof Literal) {
		Literal lit = (Literal) value;

		String label = lit.getLabel();
		IRI datatype = lit.getDatatype();

		if (Literals.isLanguageLiteral(lit)) {
			dataOut.writeByte(LANG_LITERAL_MARKER);
			writeString(label, dataOut);
			writeString(lit.getLanguage().get(), dataOut);
		} else {
			dataOut.writeByte(DATATYPE_LITERAL_MARKER);
			writeString(label, dataOut);
			writeValue(datatype, dataOut);
		}
	} else {
		throw new IllegalArgumentException("unexpected value type: " + value.getClass());
	}
}
 
Example 2
Source File: TriXWriter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Writes out the XML-representation for the supplied value.
 */
private void writeValue(Value value) throws IOException, RDFHandlerException {
	if (value instanceof IRI) {
		IRI uri = (IRI) value;
		xmlWriter.textElement(URI_TAG, uri.toString());
	} else if (value instanceof BNode) {
		BNode bNode = (BNode) value;
		xmlWriter.textElement(BNODE_TAG, bNode.getID());
	} else if (value instanceof Literal) {
		Literal literal = (Literal) value;
		IRI datatype = literal.getDatatype();

		if (Literals.isLanguageLiteral(literal)) {
			xmlWriter.setAttribute(LANGUAGE_ATT, literal.getLanguage().get());
			xmlWriter.textElement(PLAIN_LITERAL_TAG, literal.getLabel());
		} else {
			xmlWriter.setAttribute(DATATYPE_ATT, datatype.toString());
			xmlWriter.textElement(TYPED_LITERAL_TAG, literal.getLabel());
		}
	} else {
		throw new RDFHandlerException("Unknown value type: " + value.getClass());
	}
}
 
Example 3
Source File: FunctionAdapter.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Convert from OpenRDF to rdf4j value used by Geo Functions.
 * 
 * @param value
 *            Must be a URIImpl, Literal or a BooleanLiteralImpl, or throws error. Ignores language.
 * @param rdf4jValueFactory
 * @return an rdf4j Literal copied from the input
 */
public org.eclipse.rdf4j.model.Value adaptValue(Value value, org.eclipse.rdf4j.model.ValueFactory rdf4jValueFactory) {
    if (value instanceof URIImpl) {
        URIImpl uri = (URIImpl) value;
        return rdf4jValueFactory.createIRI(uri.stringValue());
    } else if (!(value instanceof Literal)) {
        throw new UnsupportedOperationException("Not supported, value must be literal type, it was: " + value.getClass() + " value=" + value);
    }
    if (value instanceof BooleanLiteralImpl) {
        BooleanLiteralImpl bl = (BooleanLiteralImpl) value;
        if (bl.booleanValue())
            return org.eclipse.rdf4j.model.impl.BooleanLiteral.TRUE;
        else
            return org.eclipse.rdf4j.model.impl.BooleanLiteral.FALSE;
    }
    final Literal literalValue = (Literal) value;
    org.eclipse.rdf4j.model.ValueFactory vf = org.eclipse.rdf4j.model.impl.SimpleValueFactory.getInstance();
    final String label = literalValue.getLabel();
    final IRI datatype = vf.createIRI(literalValue.getDatatype().stringValue());
    return vf.createLiteral(label, datatype);
}
 
Example 4
Source File: ValueStore.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public NativeValue getNativeValue(Value value) {
	if (value instanceof Resource) {
		return getNativeResource((Resource) value);
	} else if (value instanceof Literal) {
		return getNativeLiteral((Literal) value);
	} else {
		throw new IllegalArgumentException("Unknown value type: " + value.getClass());
	}
}
 
Example 5
Source File: BinaryQueryResultWriter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeValue(Value value) throws IOException {
	if (value instanceof IRI) {
		writeQName((IRI) value);
	} else if (value instanceof BNode) {
		writeBNode((BNode) value);
	} else if (value instanceof Literal) {
		writeLiteral((Literal) value);
	} else if (value instanceof Triple) {
		writeTriple((Triple) value);
	} else {
		throw new TupleQueryResultHandlerException("Unknown Value object type: " + value.getClass());
	}
}
 
Example 6
Source File: AbstractSPARQLJSONWriter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void writeValue(Value value) throws IOException, QueryResultHandlerException {
	jg.writeStartObject();

	if (value instanceof IRI) {
		jg.writeStringField("type", "uri");
		jg.writeStringField("value", ((IRI) value).toString());
	} else if (value instanceof BNode) {
		jg.writeStringField("type", "bnode");
		jg.writeStringField("value", ((BNode) value).getID());
	} else if (value instanceof Literal) {
		Literal lit = (Literal) value;

		if (Literals.isLanguageLiteral(lit)) {
			jg.writeObjectField("xml:lang", lit.getLanguage().orElse(null));
		} else {
			IRI datatype = lit.getDatatype();
			boolean ignoreDatatype = datatype.equals(XMLSchema.STRING) && xsdStringToPlainLiteral();
			if (!ignoreDatatype) {
				jg.writeObjectField("datatype", lit.getDatatype().stringValue());
			}
		}

		jg.writeObjectField("type", "literal");

		jg.writeObjectField("value", lit.getLabel());
	} else {
		throw new TupleQueryResultHandlerException("Unknown Value object type: " + value.getClass());
	}
	jg.writeEndObject();
}
 
Example 7
Source File: TransactionSAXParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Resource[] createContexts(int startIdx) throws SAXException {
	List<Resource> contexts = new ArrayList<>();

	for (int i = startIdx; i < parsedValues.size(); i++) {
		Value contextCandidate = parsedValues.get(i);

		if (contextCandidate == null || contextCandidate instanceof Resource) {
			contexts.add((Resource) contextCandidate);
		} else {
			throw new SAXException("Invalid context value: " + contextCandidate.getClass());
		}
	}

	return contexts.toArray(new Resource[contexts.size()]);
}
 
Example 8
Source File: NTriplesUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates an N-Triples string for the supplied value.If the supplied value is a {@link Literal}, it optionally
 * ignores the xsd:string datatype, since this datatype is implicit in RDF-1.1.
 *
 * @param value                   The value to write.
 * @param xsdStringToPlainLiteral True to omit serialising the xsd:string datatype and false to always serialise the
 *                                datatype for literals.
 * @return string
 */
public static String toNTriplesString(Value value, boolean xsdStringToPlainLiteral) {
	if (value instanceof Resource) {
		return toNTriplesString((Resource) value);
	} else if (value instanceof Literal) {
		return toNTriplesString((Literal) value, xsdStringToPlainLiteral);
	} else {
		throw new IllegalArgumentException("Unknown value type: " + value.getClass());
	}
}
 
Example 9
Source File: BinaryRDFWriter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeValue(Value value) throws RDFHandlerException, IOException {
	if (value instanceof IRI) {
		writeURI((IRI) value);
	} else if (value instanceof BNode) {
		writeBNode((BNode) value);
	} else if (value instanceof Literal) {
		writeLiteral((Literal) value);
	} else if (value instanceof Triple) {
		writeTriple((Triple) value);
	} else {
		throw new RDFHandlerException("Unknown Value object type: " + value.getClass());
	}
}
 
Example 10
Source File: NTriplesWriter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Writes the N-Triples representation of the given {@link Value}.
 *
 * @param value The value to write.
 * @throws IOException
 */
protected void writeValue(Value value) throws IOException {
	if (value instanceof IRI) {
		writeIRI((IRI) value);
	} else if (value instanceof BNode) {
		writeBNode((BNode) value);
	} else if (value instanceof Literal) {
		writeLiteral((Literal) value);
	} else {
		throw new IllegalArgumentException("Unknown value type: " + value.getClass());
	}
}
 
Example 11
Source File: NTriplesUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Appends the N-Triples representation of the given {@link Value} to the given {@link Appendable}, optionally not
 * serializing the datatype a {@link Literal} with the xsd:string datatype as it is implied for RDF-1.1.
 *
 * @param value                   The value to write.
 * @param appendable              The object to append to.
 * @param xsdStringToPlainLiteral True to omit serializing the xsd:string datatype and false to always serialize the
 *                                datatype for literals.
 * @param escapeUnicode
 * @throws IOException
 */
public static void append(Value value, Appendable appendable, boolean xsdStringToPlainLiteral,
		boolean escapeUnicode) throws IOException {
	if (value instanceof Resource) {
		append((Resource) value, appendable);
	} else if (value instanceof Literal) {
		append((Literal) value, appendable, xsdStringToPlainLiteral, escapeUnicode);
	} else {
		throw new IllegalArgumentException("Unknown value type: " + value.getClass());
	}
}