Skip to content

Commit d4d4cd8

Browse files
author
Gaurav SinghaRoy
committed
typed-literal in sesame type output
1 parent 11231ae commit d4d4cd8

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

linda/src/main/java/de/unibonn/iai/eis/linda/converters/impl/JSONConverter.java

+13
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ public void addResultSetRowToOutput(QuerySolution row) {
8989
"value",
9090
literal.toString().substring(0,
9191
languageIdentifierPoint));
92+
} else {
93+
if (((Literal) literal).getDatatypeURI() == null) {
94+
// plain literal
95+
columnEntry.put("type", "literal");
96+
columnEntry.put("value", literal.toString());
97+
} else {
98+
// typed literal
99+
columnEntry.put("type", "typed-literal");
100+
columnEntry.put("value",
101+
SPARQLHandler.getLiteralValue(literal));
102+
columnEntry.put("datatype",
103+
SPARQLHandler.getLiteralDataType(literal));
104+
}
92105
}
93106

94107
} else {

linda/src/main/java/de/unibonn/iai/eis/linda/converters/impl/results/sesame/JSONSesameOutputResultBinding.java

+47-20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.hp.hpl.jena.rdf.model.Literal;
1111
import com.hp.hpl.jena.rdf.model.RDFNode;
1212

13+
import de.unibonn.iai.eis.linda.helper.SPARQLHandler;
14+
1315
public class JSONSesameOutputResultBinding {
1416
public List<Object> binding;
1517

@@ -20,29 +22,54 @@ public JSONSesameOutputResultBinding(QuerySolution row,
2022
Map<String, Object> varHash = new HashMap<String, Object>();
2123
varHash.put("@name", resultVar);
2224
RDFNode node = row.get(resultVar);
23-
if (node instanceof Literal) {
24-
// section where the node is a literal
25-
Integer languageIdentifierPoint = node.toString().length() - 3;
26-
if (node.toString().charAt(languageIdentifierPoint) == '@') {
27-
// section if it is language literal
28-
Map<String, String> langLiteralHash = new HashMap<String, String>();
29-
langLiteralHash.put(
30-
"@xml:lang",
31-
node.toString().substring(
32-
languageIdentifierPoint + 1));
33-
langLiteralHash.put(
34-
"#text",
35-
node.toString().substring(0,
36-
languageIdentifierPoint));
37-
varHash.put("uri", langLiteralHash);
25+
if (node != null && !node.toString().equals("")) {
26+
if (node instanceof Literal) {
27+
// section where the node is a literal
28+
if (node.toString().length() > 3) {
29+
Integer languageIdentifierPoint = node.toString()
30+
.length() - 3;
31+
if (node.toString().charAt(languageIdentifierPoint) == '@') {
32+
// section if it is language literal
33+
Map<String, String> langLiteralHash = new HashMap<String, String>();
34+
langLiteralHash.put("@xml:lang", node.toString()
35+
.substring(languageIdentifierPoint + 1));
36+
langLiteralHash.put("#text", node.toString()
37+
.substring(0, languageIdentifierPoint));
38+
varHash.put("literal", langLiteralHash);
39+
} else {
40+
if (((Literal) node).getDatatypeURI() == null) {
41+
// plain literal
42+
varHash.put("literal", node.toString());
43+
} else {
44+
// typed literal
45+
Map<String, String> langLiteralHash = new HashMap<String, String>();
46+
langLiteralHash.put("datatype",
47+
SPARQLHandler.getLiteralDataType(node));
48+
langLiteralHash.put("#text",
49+
SPARQLHandler.getLiteralValue(node));
50+
varHash.put("typed-literal", langLiteralHash);
51+
}
52+
}
53+
} else {
54+
if (((Literal) node).getDatatypeURI() == null) {
55+
// plain literal
56+
varHash.put("literal", node.toString());
57+
} else {
58+
// typed literal
59+
Map<String, String> langLiteralHash = new HashMap<String, String>();
60+
langLiteralHash.put("datatype",
61+
SPARQLHandler.getLiteralDataType(node));
62+
langLiteralHash.put("#text",
63+
SPARQLHandler.getLiteralValue(node));
64+
varHash.put("typed-literal", langLiteralHash);
65+
}
66+
}
3867
} else {
39-
varHash.put("literal", node.toString());
68+
// section where the node is a uri
69+
varHash.put("uri", node.toString());
4070
}
41-
} else {
42-
// section where the node is a uri
43-
varHash.put("uri", node.toString());
71+
this.binding.add(varHash);
4472
}
45-
this.binding.add(varHash);
4673
}
4774
}
4875
}

0 commit comments

Comments
 (0)