/** * Create an HTML Data object. * * @param namespaceService the namespace service * @param subject the subject * @param triples the triples * @param css the stylesheets * @param js the javascripts * @param icon the icon */ public HtmlData(final NamespaceService namespaceService, final String subject, final List<Triple> triples, final List<String> css, final List<String> js, final String icon) { this.css = requireNonNull(css, "The CSS list may not be null!"); this.js = requireNonNull(js, "The JS list may not be null!"); this.triples = triples; this.subject = ofNullable(subject).orElse(""); this.icon = ofNullable(icon).orElse("//www.trellisldp.org/assets/img/trellis.png"); this.prefixMapping.setNsPrefixes(namespaceService.getNamespaces()); }
@Override public Stream<Triple> read(final InputStream input, final RDFSyntax syntax, final String base) { requireNonNull(input, "The input stream may not be null!"); requireNonNull(syntax, "The syntax value may not be null!"); try { final org.apache.jena.graph.Graph graph = createDefaultGraph(); final Lang lang = rdf.asJenaLang(syntax).orElseThrow(() -> new RuntimeTrellisException("Unsupported RDF Syntax: " + syntax.mediaType())); RDFParser.source(input).lang(lang).base(base).parse(graph); // Check the graph for any new namespace definitions final Set<String> namespaces = nsService.getNamespaces().entrySet().stream().map(Map.Entry::getValue) .collect(toSet()); graph.getPrefixMapping().getNsPrefixMap().forEach((prefix, namespace) -> { if (!namespaces.contains(namespace)) { LOGGER.debug("Setting prefix ({}) for namespace {}", prefix, namespace); nsService.setPrefix(prefix, namespace); } }); return rdf.asGraph(graph).stream().map(Triple.class::cast); } catch (final RiotException | AtlasException | IllegalArgumentException ex) { throw new RuntimeTrellisException(ex); } }
@Override public Stream<Triple> read(final InputStream input, final RDFSyntax syntax, final String base) { requireNonNull(input, "The input stream may not be null!"); requireNonNull(syntax, "The syntax value may not be null!"); try { final org.apache.jena.graph.Graph graph = createDefaultGraph(); final Lang lang = rdf.asJenaLang(syntax).orElseThrow(() -> new RuntimeTrellisException("Unsupported RDF Syntax: " + syntax.mediaType())); RDFParser.source(input).lang(lang).base(base).parse(graph); // Check the graph for any new namespace definitions final Set<String> namespaces = nsService.getNamespaces().entrySet().stream().map(Map.Entry::getValue) .collect(toSet()); graph.getPrefixMapping().getNsPrefixMap().forEach((prefix, namespace) -> { if (!namespaces.contains(namespace)) { LOGGER.debug("Setting prefix ({}) for namespace {}", prefix, namespace); nsService.setPrefix(prefix, namespace); } }); return rdf.asGraph(graph).stream().map(Triple.class::cast); } catch (final RiotException | AtlasException | IllegalArgumentException ex) { throw new RuntimeTrellisException(ex); } }
@Test public void noAction() { final NamespaceService testService = new NoopNamespaceService(); testService.setPrefix("foo", "http://bar/"); assertTrue(testService.getNamespaces().isEmpty(), "Namespace list not empty in no-op service!"); } }
/** * Create an HTML Data object. * * @param namespaceService the namespace service * @param subject the subject * @param triples the triples * @param css the stylesheets * @param js the javascripts * @param icon the icon */ public HtmlData(final NamespaceService namespaceService, final String subject, final List<Triple> triples, final List<String> css, final List<String> js, final String icon) { this.css = requireNonNull(css, "The CSS list may not be null!"); this.js = requireNonNull(js, "The JS list may not be null!"); this.triples = triples; this.subject = ofNullable(subject).orElse(""); this.icon = ofNullable(icon).orElse("//www.trellisldp.org/assets/img/trellis.png"); this.prefixMapping.setNsPrefixes(namespaceService.getNamespaces()); }
@BeforeEach @SuppressWarnings("unchecked") public void setUp() { initMocks(this); final Map<String, String> namespaces = new HashMap<>(); namespaces.put("dc", DCTerms.NS); namespaces.put("rdf", RDF.uri); namespaces.put("dcmitype", "http://purl.org/dc/dcmitype/"); when(mockNamespaceService.getNamespaces()).thenReturn(namespaces); service = new HtmlSerializer(mockNamespaceService); }
final StreamRDF stream = getWriterStream(output, format); stream.start(); ofNullable(nsService).ifPresent(svc -> svc.getNamespaces().forEach(stream::prefix)); triples.map(rdf::asJenaTriple).forEachOrdered(stream::triple); stream.finish();
final StreamRDF stream = getWriterStream(output, format); stream.start(); ofNullable(nsService).ifPresent(svc -> svc.getNamespaces().forEach(stream::prefix)); triples.map(rdf::asJenaTriple).forEachOrdered(stream::triple); stream.finish();
@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); }); }