private void writeJsonLd(final OutputStream output, final DatasetGraph graph, final IRI... profiles) { final String profile = getCustomJsonLdProfile(profiles); final RDFFormat format = canUseCustomJsonLdProfile(profile) ? JSONLD_COMPACT_FLAT : getJsonLdProfile(profiles); final WriterDatasetRIOT writer = RDFDataMgr.createDatasetWriter(format); final PrefixMap pm = RiotLib.prefixMap(graph); final String base = null; final JsonLDWriteContext ctx = new JsonLDWriteContext(); if (canUseCustomJsonLdProfile(profile)) { LOGGER.debug("Setting JSON-LD context with profile: {}", profile); final String c = cache.get(profile, p -> { try (final TypedInputStream res = HttpOp.execHttpGet(profile)) { return IOUtils.toString(res.getInputStream(), UTF_8); } catch (final IOException | HttpException ex) { LOGGER.warn("Error fetching profile {}: {}", p, ex.getMessage()); return null; } }); if (nonNull(c)) { ctx.setJsonLDContext(c); ctx.setJsonLDContextSubstitution("\"" + profile + "\""); } } writer.write(output, graph, pm, base, ctx); }
private void writeJsonLd(final OutputStream output, final DatasetGraph graph, final IRI... profiles) { final String profile = getCustomJsonLdProfile(profiles); final RDFFormat format = canUseCustomJsonLdProfile(profile) ? JSONLD_COMPACT_FLAT : getJsonLdProfile(profiles); final WriterDatasetRIOT writer = RDFDataMgr.createDatasetWriter(format); final PrefixMap pm = RiotLib.prefixMap(graph); final String base = null; final JsonLDWriteContext ctx = new JsonLDWriteContext(); if (canUseCustomJsonLdProfile(profile)) { LOGGER.debug("Setting JSON-LD context with profile: {}", profile); final String c = cache.get(profile, p -> { try (final TypedInputStream res = HttpOp.execHttpGet(profile)) { return IOUtils.toString(res.getInputStream(), UTF_8); } catch (final IOException | HttpException ex) { LOGGER.warn("Error fetching profile {}: {}", p, ex.getMessage()); return null; } }); if (nonNull(c)) { ctx.setJsonLDContext(c); ctx.setJsonLDContextSubstitution("\"" + profile + "\""); } } writer.write(output, graph, pm, base, ctx); }
/** * Test using a context to compute the output, and replacing the @context with a given value */ @Test public void testSubstitutingContext() { Model m = ModelFactory.createDefaultModel(); String ns = "http://schema.org/"; Resource person = m.createResource(ns + "Person"); Resource s = m.createResource(); m.add(s, m.createProperty(ns + "name"), "Jane Doe"); m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com"); m.add(s, m.createProperty(ns + "jobTitle"), "Professor"); m.add(s, RDF.type, person); // change @context to a URI JsonLDWriteContext jenaCtx = new JsonLDWriteContext(); jenaCtx.setJsonLDContextSubstitution((new JsonString(ns)).toString()); String jsonld; jsonld = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, jenaCtx); String c = "\"@context\":\"http://schema.org/\""; assertTrue(jsonld.contains(c)); // change @context to a given ctx String ctx = "{\"jobTitle\":{\"@id\":\"http://ex.com/jobTitle\"},\"url\":{\"@id\":\"http://ex.com/url\"},\"name\":{\"@id\":\"http://ex.com/name\"}}"; jenaCtx.setJsonLDContextSubstitution(ctx); jsonld = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, jenaCtx); assertTrue(jsonld.contains("http://ex.com/name")); }