protected void parseSynchronusly()

in commons-rdf-jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/experimental/JsonLdParser.java [101:149]


    protected void parseSynchronusly() throws IOException {
        final Object json = readSource();
        final JsonLdOptions options = new JsonLdOptions();
        getBase().map(IRI::getIRIString).ifPresent(options::setBase);
        // TODO: base from readSource() (after redirection and Content-Location
        // header)
        // should be forwarded

        // TODO: Modify JsonLdProcessor to accept the target RDFDataset
        RDFDataset rdfDataset;
        try {
            rdfDataset = (RDFDataset) JsonLdProcessor.toRDF(json, options);
        } catch (final JsonLdError e) {
            throw new IOException("Could not parse Json-LD", e);
        }
        if (getTargetGraph().isPresent()) {
            final Graph intoGraph = getTargetGraph().get();
            if (intoGraph instanceof JsonLdGraph && !intoGraph.contains(null, null, null)) {
                // Empty graph, we can just move over the map content directly:
                final JsonLdGraph jsonLdGraph = (JsonLdGraph) intoGraph;
                jsonLdGraph.getRdfDataSet().putAll(rdfDataset);
                return;
                // otherwise we have to merge as normal
            }
            // TODO: Modify JsonLdProcessor to have an actual triple callback
            final Graph parsedGraph = getJsonLdFactory().asGraph(rdfDataset);
            // sequential() as we don't know if destination is thread safe :-/
            parsedGraph.stream().sequential().forEach(intoGraph::add);
        } else if (getTargetDataset().isPresent()) {
            final Dataset intoDataset = getTargetDataset().get();
            if (intoDataset instanceof JsonLdDataset && !intoDataset.contains(null, null, null, null)) {
                final JsonLdDataset jsonLdDataset = (JsonLdDataset) intoDataset;
                // Empty - we can just do a brave replace!
                jsonLdDataset.getRdfDataSet().putAll(rdfDataset);
                return;
                // otherwise we have to merge.. but also avoid duplicate
                // triples,
                // map blank nodes etc, so we'll fall back to normal Dataset
                // appending.
            }
            final Dataset fromDataset = getJsonLdFactory().asDataset(rdfDataset);
            // .sequential() as we don't know if destination is thread-safe :-/
            fromDataset.stream().sequential().forEach(intoDataset::add);
        } else {
            final Dataset fromDataset = getJsonLdFactory().asDataset(rdfDataset);
            // No need for .sequential() here
            fromDataset.stream().forEach(getTarget());
        }
    }