Search in sources :

Example 1 with JSONLDMode

use of org.eclipse.rdf4j.rio.helpers.JSONLDMode in project rdf4j by eclipse.

the class JSONLDWriter method endRDF.

@Override
public void endRDF() throws RDFHandlerException {
    final JSONLDInternalRDFParser serialiser = new JSONLDInternalRDFParser();
    try {
        Object output = JsonLdProcessor.fromRDF(model, serialiser);
        final JSONLDMode mode = getWriterConfig().get(JSONLDSettings.JSONLD_MODE);
        final JsonLdOptions opts = new JsonLdOptions();
        // opts.addBlankNodeIDs =
        // getWriterConfig().get(BasicParserSettings.PRESERVE_BNODE_IDS);
        opts.setUseRdfType(getWriterConfig().get(JSONLDSettings.USE_RDF_TYPE));
        opts.setUseNativeTypes(getWriterConfig().get(JSONLDSettings.USE_NATIVE_TYPES));
        if (baseURI != null && getWriterConfig().get(BasicWriterSettings.BASE_DIRECTIVE)) {
            opts.setBase(baseURI);
        }
        if (mode == JSONLDMode.EXPAND) {
            output = JsonLdProcessor.expand(output, opts);
        }
        // TODO: Implement inframe in JSONLDSettings
        final Object inframe = null;
        if (mode == JSONLDMode.FLATTEN) {
            output = JsonLdProcessor.flatten(output, inframe, opts);
        }
        if (mode == JSONLDMode.COMPACT) {
            final Map<String, Object> ctx = new LinkedHashMap<String, Object>();
            addPrefixes(ctx, model.getNamespaces());
            final Map<String, Object> localCtx = new HashMap<String, Object>();
            localCtx.put("@context", ctx);
            output = JsonLdProcessor.compact(output, localCtx, opts);
        }
        if (getWriterConfig().get(BasicWriterSettings.PRETTY_PRINT)) {
            JsonUtils.writePrettyPrint(writer, output);
        } else {
            JsonUtils.write(writer, output);
        }
    } catch (final JsonLdError e) {
        throw new RDFHandlerException("Could not render JSONLD", e);
    } catch (final JsonGenerationException e) {
        throw new RDFHandlerException("Could not render JSONLD", e);
    } catch (final JsonMappingException e) {
        throw new RDFHandlerException("Could not render JSONLD", e);
    } catch (final IOException e) {
        throw new RDFHandlerException("Could not render JSONLD", e);
    }
}
Also used : JSONLDMode(org.eclipse.rdf4j.rio.helpers.JSONLDMode) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IOException(java.io.IOException) JsonLdError(com.github.jsonldjava.core.JsonLdError) LinkedHashMap(java.util.LinkedHashMap) JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException)

Aggregations

JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 JsonLdError (com.github.jsonldjava.core.JsonLdError)1 JsonLdOptions (com.github.jsonldjava.core.JsonLdOptions)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)1 JSONLDMode (org.eclipse.rdf4j.rio.helpers.JSONLDMode)1