-
Notifications
You must be signed in to change notification settings - Fork 10
/
AGNodeFactory.java
47 lines (41 loc) · 1.31 KB
/
AGNodeFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.franz.agraph.jena;
import org.openrdf.model.BNode;
import org.openrdf.model.Literal;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.AnonId;
public class AGNodeFactory {
public static Triple asTriple(Statement st) {
Node s = asNode(st.getSubject());
Node p = asNode(st.getPredicate());
Node o = asNode(st.getObject());
return new Triple(s,p,o);
}
public static Node asNode(Value v) {
Node node = null;
if (v==null) {
node = Node.ANY; // TODO or Node.NULL or null?
} else if (v instanceof URI) {
node = Node.createURI(v.stringValue());
} else if (v instanceof BNode) {
node = Node.createAnon(new AnonId(v.stringValue()));
} else if (v instanceof Literal) {
Literal lit = (Literal)v;
URI datatype = lit.getDatatype();
String lang = lit.getLanguage();
if (lang!=null) {
node = Node.createLiteral(lit.getLabel(), lang, null);
} else if (datatype!=null) {
node = Node.createLiteral(lit.getLabel(), null, Node.getType(datatype.toString()));
} else {
node = Node.createLiteral(lit.stringValue());
}
} else {
throw new IllegalArgumentException("Cannot create Node from Value: " + v);
}
return node;
}
}