/** * Create a serialization service. * @param namespaceService the namespace service */ public JenaIOService(final NamespaceService namespaceService) { this(namespaceService, findFirst(RDFaWriterService.class).orElse(null)); }
/** * Create a serialization service. * * @param namespaceService the namespace service * @param htmlSerializer the HTML serializer service * @param cache a cache for custom JSON-LD profile resolution * @param whitelist a whitelist of JSON-LD profiles * @param whitelistDomains a whitelist of JSON-LD profile domains */ public JenaIOService(final NamespaceService namespaceService, final RDFaWriterService htmlSerializer, final CacheService<String, String> cache, final String whitelist, final String whitelistDomains) { this(namespaceService, htmlSerializer, cache, intoSet(whitelist), intoSet(whitelistDomains)); }
private static RDFFormat getJsonLdProfile(final IRI... profiles) { return JSONLD_FORMATS.get(mergeProfiles(profiles)); }
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); }
writeHTML(triples, output, profiles.length > 0 ? profiles[0].getIRIString() : null); } else { final Lang lang = rdf.asJenaLang(syntax).orElseThrow(() -> triples.map(rdf::asJenaTriple).forEachOrdered(graph::add); if (JSONLD.equals(lang)) { writeJsonLd(output, DatasetGraphFactory.create(graph), profiles); } else { RDFDataMgr.write(output, graph, lang);
@BeforeEach @SuppressWarnings("unchecked") public void setUp() { initMocks(this); final Map<String, String> namespaces = new HashMap<>(); namespaces.put("dcterms", DCTerms.NS); namespaces.put("rdf", RDF.uri); service = new JenaIOService(mockNamespaceService, null, mockCache, "http://www.w3.org/ns/anno.jsonld,,,", "http://www.trellisldp.org/ns/"); service2 = new JenaIOService(mockNamespaceService, mockHtmlSerializer, mockCache, emptySet(), singleton("http://www.w3.org/ns/")); service3 = new JenaIOService(mockNamespaceService, null, mockCache, emptySet(), emptySet()); when(mockNamespaceService.getNamespaces()).thenReturn(namespaces); when(mockCache.get(anyString(), any(Function.class))).thenAnswer(inv -> { final String key = inv.getArgument(0); final Function<String, String> mapper = inv.getArgument(1); return mapper.apply(key); }); }
private void writeHTML(final Stream<Triple> triples, final OutputStream output, final String subject) { if (nonNull(htmlSerializer)) { htmlSerializer.write(triples, output, subject); } else { write(triples, output, TURTLE); } }
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); }
writeHTML(triples, output, profiles.length > 0 ? profiles[0].getIRIString() : null); } else { final Lang lang = rdf.asJenaLang(syntax).orElseThrow(() -> triples.map(rdf::asJenaTriple).forEachOrdered(graph::add); if (JSONLD.equals(lang)) { writeJsonLd(output, DatasetGraphFactory.create(graph), profiles); } else { RDFDataMgr.write(output, graph, lang);
@Test public void testHtmlSerializer2() throws Exception { final IOService service4 = new JenaIOService(mockNamespaceService, mockHtmlSerializer); final ByteArrayOutputStream out = new ByteArrayOutputStream(); service4.write(getComplexTriples(), out, RDFA); verify(mockHtmlSerializer).write(any(), eq(out), eq(null)); }
private void writeHTML(final Stream<Triple> triples, final OutputStream output, final String subject) { if (nonNull(htmlSerializer)) { htmlSerializer.write(triples, output, subject); } else { write(triples, output, TURTLE); } }
@Test public void testHtmlSerializer() throws Exception { final IOService service4 = new JenaIOService(mockNamespaceService, mockHtmlSerializer); final ByteArrayOutputStream out = new ByteArrayOutputStream(); service4.write(getComplexTriples(), out, RDFA, rdf.createIRI("http://example.org/")); verify(mockHtmlSerializer).write(any(), eq(out), eq("http://example.org/")); }
/** * Create a serialization service. * @param namespaceService the namespace service */ public JenaIOService(final NamespaceService namespaceService) { this(namespaceService, findFirst(RDFaWriterService.class).orElse(null)); }
/** * Create a serialization service. * * @param namespaceService the namespace service * @param htmlSerializer the HTML serializer service * @param cache a cache for custom JSON-LD profile resolution * @param whitelist a whitelist of JSON-LD profiles * @param whitelistDomains a whitelist of JSON-LD profile domains */ public JenaIOService(final NamespaceService namespaceService, final RDFaWriterService htmlSerializer, final CacheService<String, String> cache, final String whitelist, final String whitelistDomains) { this(namespaceService, htmlSerializer, cache, intoSet(whitelist), intoSet(whitelistDomains)); }
private static RDFFormat getJsonLdProfile(final IRI... profiles) { return JSONLD_FORMATS.get(mergeProfiles(profiles)); }
@Test public void testJsonLdNullCache() throws UnsupportedEncodingException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final IOService myservice = new JenaIOService(); myservice.write(getTriples(), out, JSONLD, rdf.createIRI("http://www.w3.org/ns/anno.jsonld")); final String output = out.toString("UTF-8"); final Graph graph = rdf.createGraph(); myservice.read(new ByteArrayInputStream(output.getBytes(UTF_8)), JSONLD, null).forEach(graph::add); assertAll("Check compact serialization", checkCompactSerialization(output, graph)); }
/** * Create a serialization service. */ @Inject public JenaIOService() { this(findFirst(NamespaceService.class).orElseGet(NoopNamespaceService::new)); }
private static IOService buildIoService(final AppConfiguration config) { final long cacheSize = config.getJsonld().getCacheSize(); final long hours = config.getJsonld().getCacheExpireHours(); final Cache<String, String> cache = newBuilder().maximumSize(cacheSize).expireAfterAccess(hours, HOURS).build(); final TrellisCache<String, String> profileCache = new TrellisCache<>(cache); final NamespaceService namespaceService = new NamespacesJsonContext(config.getNamespaces()); final RDFaWriterService htmlSerializer = new HtmlSerializer(namespaceService, config.getAssets().getTemplate(), config.getAssets().getCss(), config.getAssets().getJs(), config.getAssets().getIcon()); return new JenaIOService(namespaceService, htmlSerializer, profileCache, config.getJsonld().getContextWhitelist(), config.getJsonld().getContextDomainWhitelist()); }
/** * Create a serialization service. */ @Inject public JenaIOService() { this(findFirst(NamespaceService.class).orElseGet(NoopNamespaceService::new)); }
@Test public void testJsonLdCustomSerializerNoopCache() throws UnsupportedEncodingException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final IOService svc = new JenaIOService(mockNamespaceService, null, new NoopProfileCache(), "http://www.w3.org/ns/anno.jsonld,,,", "http://www.trellisldp.org/ns/"); svc.write(getTriples(), out, JSONLD, rdf.createIRI("http://www.w3.org/ns/anno.jsonld")); final String output = out.toString("UTF-8"); final Graph graph = rdf.createGraph(); service.read(new ByteArrayInputStream(output.getBytes(UTF_8)), JSONLD, null).forEach(graph::add); assertTrue(validateGraph(graph), "Not all triples present in output graph!"); }