use of com.github.jsonldjava.core.JsonLdError in project stanbol by apache.
the class JsonLdSerializingProvider method serialize.
@Override
public void serialize(OutputStream serializedGraph, Graph tc, String formatIdentifier) {
ClerezzaRDFParser serializer = new ClerezzaRDFParser();
try {
long start = System.currentTimeMillis();
Object output = JsonLdProcessor.fromRDF(tc, serializer);
if (MODE_EXPAND.equalsIgnoreCase(mode)) {
logger.debug(" - mode: {}", MODE_EXPAND);
output = JsonLdProcessor.expand(output, opts);
}
if (MODE_FLATTEN.equalsIgnoreCase(mode)) {
logger.debug(" - mode: {}", MODE_FLATTEN);
// TODO: Allow inframe config
final Object inframe = null;
output = JsonLdProcessor.flatten(output, inframe, opts);
}
if (MODE_COMPACT.equalsIgnoreCase(mode)) {
logger.debug(" - mode: {}", MODE_COMPACT);
// TODO: collect namespaces used in the triples in the ClerezzaRDFParser
final Map<String, Object> localCtx = new HashMap<String, Object>();
localCtx.put("@context", DEFAULT_NAMESPACES);
output = JsonLdProcessor.compact(output, localCtx, opts);
}
Writer writer = new OutputStreamWriter(serializedGraph, UTF8);
logger.debug(" - prettyPrint: {}", prettyPrint);
if (prettyPrint) {
JsonUtils.writePrettyPrint(writer, output);
} else {
JsonUtils.write(writer, output);
}
if (logger.isDebugEnabled()) {
logger.debug(" - serialized {} triples in {}ms", serializer.getCount(), System.currentTimeMillis() - start);
}
} catch (JsonLdError e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
use of com.github.jsonldjava.core.JsonLdError in project jena by apache.
the class JsonLDReader method read$.
private void read$(Object jsonObject, JsonLdOptions options, final StreamRDF output) {
output.start();
try {
JsonLdTripleCallback callback = new JsonLdTripleCallback() {
@Override
public Object call(RDFDataset dataset) {
// Copy across namespaces
for (Entry<String, String> namespace : dataset.getNamespaces().entrySet()) {
output.prefix(namespace.getKey(), namespace.getValue());
}
// Copy triples and quads
for (String gn : dataset.keySet()) {
Object x = dataset.get(gn);
if ("@default".equals(gn)) {
@SuppressWarnings("unchecked") List<Map<String, Object>> triples = (List<Map<String, Object>>) x;
for (Map<String, Object> t : triples) {
Node s = createNode(t, "subject");
Node p = createNode(t, "predicate");
Node o = createNode(t, "object");
Triple triple = profile.createTriple(s, p, o, -1, -1);
output.triple(triple);
}
} else {
@SuppressWarnings("unchecked") List<Map<String, Object>> quads = (List<Map<String, Object>>) x;
Node g = createURI(gn);
for (Map<String, Object> q : quads) {
Node s = createNode(q, "subject");
Node p = createNode(q, "predicate");
Node o = createNode(q, "object");
Quad quad = profile.createQuad(g, s, p, o, -1, -1);
output.quad(quad);
}
}
}
return null;
}
};
JsonLdProcessor.toRDF(jsonObject, callback, options);
} catch (JsonLdError e) {
errorHandler.error(e.getMessage(), -1, -1);
throw new RiotException(e);
}
output.finish();
}
use of com.github.jsonldjava.core.JsonLdError in project aic-praise by aic-sri-international.
the class JSONLDJavaUtil method getFlattenedExpandedJSONLDJavaObject.
private static Object getFlattenedExpandedJSONLDJavaObject(Object jsonldJavaObject) throws Error {
Map context = new HashMap();
JsonLdOptions options = new JsonLdOptions();
Object flattened;
try {
Object expanded = JsonLdProcessor.expand(jsonldJavaObject, options);
flattened = JsonLdProcessor.flatten(expanded, context, options);
} catch (JsonLdError e) {
throw new Error(e);
}
return flattened;
}
use of com.github.jsonldjava.core.JsonLdError in project jena by apache.
the class JsonLDReader method read$.
private void read$(Object jsonObject, String baseURI, ContentType ct, final StreamRDF output, Context context) {
output.start();
try {
JsonLdTripleCallback callback = new JsonLdTripleCallback() {
@Override
public Object call(RDFDataset dataset) {
// Copy across namespaces
for (Entry<String, String> namespace : dataset.getNamespaces().entrySet()) {
output.prefix(namespace.getKey(), namespace.getValue());
}
// Copy triples and quads
for (String gn : dataset.keySet()) {
Object x = dataset.get(gn);
if ("@default".equals(gn)) {
@SuppressWarnings("unchecked") List<Map<String, Object>> triples = (List<Map<String, Object>>) x;
for (Map<String, Object> t : triples) {
Node s = createNode(t, "subject");
Node p = createNode(t, "predicate");
Node o = createNode(t, "object");
Triple triple = profile.createTriple(s, p, o, -1, -1);
output.triple(triple);
}
} else {
@SuppressWarnings("unchecked") List<Map<String, Object>> quads = (List<Map<String, Object>>) x;
Node g = createURI(gn);
for (Map<String, Object> q : quads) {
Node s = createNode(q, "subject");
Node p = createNode(q, "predicate");
Node o = createNode(q, "object");
Quad quad = profile.createQuad(g, s, p, o, -1, -1);
output.quad(quad);
}
}
}
return null;
}
};
JsonLdOptions options = new JsonLdOptions(baseURI);
options.useNamespaces = true;
JsonLdProcessor.toRDF(jsonObject, callback, options);
} catch (JsonLdError e) {
errorHandler.error(e.getMessage(), -1, -1);
throw new RiotException(e);
}
output.finish();
}
Aggregations