Search in sources :

Example 1 with JsonLdTripleCallback

use of com.github.jsonldjava.core.JsonLdTripleCallback 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();
}
Also used : RDFDataset(com.github.jsonldjava.core.RDFDataset) Quad(org.apache.jena.sparql.core.Quad) Node(org.apache.jena.graph.Node) JsonLdTripleCallback(com.github.jsonldjava.core.JsonLdTripleCallback) JsonLdError(com.github.jsonldjava.core.JsonLdError) Triple(org.apache.jena.graph.Triple) JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) List(java.util.List) Map(java.util.Map)

Aggregations

JsonLdError (com.github.jsonldjava.core.JsonLdError)1 JsonLdOptions (com.github.jsonldjava.core.JsonLdOptions)1 JsonLdTripleCallback (com.github.jsonldjava.core.JsonLdTripleCallback)1 RDFDataset (com.github.jsonldjava.core.RDFDataset)1 List (java.util.List)1 Map (java.util.Map)1 Node (org.apache.jena.graph.Node)1 Triple (org.apache.jena.graph.Triple)1 Quad (org.apache.jena.sparql.core.Quad)1