* RDFTriple in each of the given contexts.
*/
public static Collection<Statement> tripleAsStatements(
final RDFTriple triple, final Resource... contexts) {
OpenRDFUtil.verifyContextNotNull(contexts);
final ValueFactoryImpl vf = ValueFactoryImpl.getInstance();
Resource subject;
URI predicate;
Value object;
if (triple.getSubject() instanceof RDFResourceIRI) {
try {
subject = vf.createURI(triple.getSubject().getIRI().toString());
} catch (IllegalArgumentException iae) {
LOGGER.error("Subject URI was invalid: {}", triple);
return Collections.emptyList();
}
} else {
// FIXME: When blank nodes are no longer represented as IRIs
// internally, need to fix
// this
if (triple.getSubject().getIRI().toString().startsWith("_:")) {
subject = vf.createBNode(triple.getSubject().getIRI()
.toString().substring(2));
} else {
subject = vf.createBNode(triple.getSubject().getIRI()
.toString());
}
}
predicate = vf.createURI(triple.getPredicate().getIRI().toString());
if (triple.getObject() instanceof RDFResourceIRI) {
try {
object = vf.createURI(triple.getObject().getIRI().toString());
} catch (IllegalArgumentException iae) {
LOGGER.error("Object URI was invalid: {}", triple);
return Collections.emptyList();
}
} else if (triple.getObject() instanceof RDFLiteral) {
final RDFLiteral literalObject = (RDFLiteral) triple.getObject();
// TODO: When updating to Sesame-2.8 the following may need to be
// rewritten
if (literalObject.isPlainLiteral()) {
if (literalObject.hasLang()) {
object = vf.createLiteral(literalObject.getLexicalValue(),
literalObject.getLang());
} else {
object = vf.createLiteral(literalObject.getLexicalValue());
}
} else {
object = vf.createLiteral(literalObject.getLexicalValue(),
vf.createURI(literalObject.getDatatype().toString()));
}
} else {
// FIXME: When blank nodes are no longer represented as IRIs
// internally, need to fix
// this
if (triple.getObject().getIRI().toString().startsWith("_:")) {
object = vf.createBNode(triple.getObject().getIRI().toString()
.substring(2));
} else {
object = vf.createBNode(triple.getObject().getIRI().toString());
}
}
if (contexts == null || contexts.length == 0) {
return Collections.singletonList(vf.createStatement(subject,
predicate, object));
} else {
final ArrayList<Statement> results = new ArrayList<>(
contexts.length);
for (final Resource nextContext : contexts) {
results.add(vf.createStatement(subject, predicate, object,
nextContext));
}
return results;
}
}