/** * Create an HTML Serializer object. * * @param namespaceService a namespace service * @param template the template location * @param css the css to use (comma-delimited for multiple css documents) * @param js the js to use (comma-delimited for multiple js documents) * @param icon an icon, may be {@code null} */ public HtmlSerializer(final NamespaceService namespaceService, final String template, final String css, final String js, final String icon) { this(namespaceService, template, intoList(css), intoList(js), icon); }
/** * Get the label for the predicate. * * @return the predicate label */ public String getPredicateLabel() { if (nonNull(predLabel)) { return predLabel; } return getPredicate(); }
/** * Get the label for the object. * * @return the object label */ public String getObjectLabel() { if (nonNull(objLabel)) { return objLabel; } return getObject(); }
@Test public void testOrdinaryLabelledTriple() { final Triple triple = rdf.createTriple( rdf.createIRI("test:value"), DC.title, rdf.createLiteral("A title")); final LabelledTriple t = new LabelledTriple(triple, "title", null); assertEquals("title", t.getPredicateLabel(), "Predicate label doesn't match!"); assertEquals(DC.title.getIRIString(), t.getPredicate(), "Predicate value doesn't match!"); assertEquals("A title", t.getObjectLabel(), "Object label doesn't match!"); assertEquals("A title", t.getObject(), "Object value doesn't match!"); }
@Test public void testBnodes() { final BlankNode bn1 = rdf.createBlankNode(); final BlankNode bn2 = rdf.createBlankNode(); final Triple triple = rdf.createTriple(bn1, DC.subject, bn2); final LabelledTriple t = new LabelledTriple(triple, null, null); assertEquals(bn1.ntriplesString(), t.getSubject(), "Subject bnode value doesn't match!"); assertEquals(bn2.ntriplesString(), t.getObject(), "Object bnode value doesn't match!"); assertEquals(bn2.ntriplesString(), t.getObjectLabel(), "Object bnode label doesn't match!"); } }
private LabelledTriple labelTriple(final Triple triple) { final String pred = triple.getPredicate().getIRIString(); if (triple.getObject() instanceof IRI) { return new LabelledTriple(triple, getLabel(pred), getLabel(((IRI) triple.getObject()).getIRIString())); } else if (triple.getObject() instanceof Literal) { return new LabelledTriple(triple, getLabel(pred), ((Literal) triple.getObject()).getLexicalForm()); } return new LabelledTriple(triple, getLabel(pred), triple.getObject().ntriplesString()); } }
@Test public void testDefaultSerializer() { final RDFaWriterService svc = new HtmlSerializer(); final ByteArrayOutputStream out = new ByteArrayOutputStream(); svc.write(getTriples2(), out, "http://example.com/"); assertAll("HTML check", checkHtmlWithoutNamespaces(new String(out.toByteArray(), UTF_8))); }
@Test public void testHtmlSerializer3() { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final RDFaWriterService service4 = new HtmlSerializer(mockNamespaceService, "/resource-test.mustache", "//www.trellisldp.org/assets/css/trellis.css", "", "//www.trellisldp.org/assets/img/trellis.png"); service4.write(getTriples(), out, "http://example.com/"); assertAll("HTML check", checkHtmlFromTriples(new String(out.toByteArray(), UTF_8))); }
@Test public void testHtmlSerializer2() { final ByteArrayOutputStream out = new ByteArrayOutputStream(); service.write(getTriples(), out, "http://example.com/"); assertAll("HTML check", checkHtmlFromTriples(new String(out.toByteArray(), UTF_8))); }
/** * Create an HTML Serializer object. * * @param namespaceService a namespace service * @param template the template location * @param css the css to use (comma-delimited for multiple css documents) * @param js the js to use (comma-delimited for multiple js documents) * @param icon an icon, may be {@code null} */ public HtmlSerializer(final NamespaceService namespaceService, final String template, final List<String> css, final List<String> js, final String icon) { this.namespaceService = requireNonNull(namespaceService, "NamespaceService may not be null!"); final String templatePath = ofNullable(template).orElse("org/trellisldp/rdfa/resource.mustache"); final File tpl = new File(templatePath); this.template = tpl.exists() ? mf.compile(templatePath) : mf.compile(getReader(templatePath), templatePath); this.css = ofNullable(css).orElseGet(Collections::emptyList); this.js = ofNullable(js).orElseGet(Collections::emptyList); this.icon = icon; }
@Test public void testWriteError() throws IOException { doThrow(new IOException()).when(mockOutputStream).write(any(byte[].class), anyInt(), anyInt()); assertThrows(UncheckedIOException.class, () -> service.write(getTriples(), mockOutputStream, ""), "IOException in write operation doesn't cause failure!"); }
@Test public void testOrdinaryLabelledTriple3() { final Triple triple = rdf.createTriple( rdf.createIRI("test:value"), DC.title, rdf.createLiteral("A title")); final LabelledTriple t = new LabelledTriple(triple, null, null); assertEquals("test:value", t.getSubject(), "Subject value doesn't match!"); assertEquals(DC.title.getIRIString(), t.getPredicateLabel(), "Predicate label doesn't match!"); assertEquals(DC.title.getIRIString(), t.getPredicate(), "Predicate value doesn't match!"); assertEquals("A title", t.getObjectLabel(), "Object label doesn't match!"); assertEquals("A title", t.getObject(), "Object value doesn't match!"); }
private LabelledTriple labelTriple(final Triple triple) { final String pred = triple.getPredicate().getIRIString(); if (triple.getObject() instanceof IRI) { return new LabelledTriple(triple, getLabel(pred), getLabel(((IRI) triple.getObject()).getIRIString())); } else if (triple.getObject() instanceof Literal) { return new LabelledTriple(triple, getLabel(pred), ((Literal) triple.getObject()).getLexicalForm()); } return new LabelledTriple(triple, getLabel(pred), triple.getObject().ntriplesString()); } }
@Test public void testHtmlSerializer5() throws Exception { final String path = getClass().getResource("/resource-test.mustache").toURI().getPath(); final ByteArrayOutputStream out = new ByteArrayOutputStream(); final RDFaWriterService service4 = new HtmlSerializer(new NoopNamespaceService(), path, "//www.trellisldp.org/assets/css/trellis.css", "", null); service4.write(getTriples2(), out, "http://example.com/"); assertAll("HTML check", checkHtmlWithoutNamespaces(new String(out.toByteArray(), UTF_8))); }
@Test public void testHtmlSerializer4() throws Exception { final String path = getClass().getResource("/resource-test.mustache").toURI().getPath(); final ByteArrayOutputStream out = new ByteArrayOutputStream(); final RDFaWriterService service4 = new HtmlSerializer(mockNamespaceService, path, "//www.trellisldp.org/assets/css/trellis.css", "", null); service4.write(getTriples(), out, "http://example.com/"); assertAll("HTML check", checkHtmlFromTriples(new String(out.toByteArray(), UTF_8))); }
/** * Get the label for the predicate. * * @return the predicate label */ public String getPredicateLabel() { if (nonNull(predLabel)) { return predLabel; } return getPredicate(); }
/** * Get the label for the object. * * @return the object label */ public String getObjectLabel() { if (nonNull(objLabel)) { return objLabel; } return getObject(); }
/** * Create an HTML Serializer object. * * @param namespaceService a namespace service * @param template the template location * @param css the css to use (comma-delimited for multiple css documents) * @param js the js to use (comma-delimited for multiple js documents) * @param icon an icon, may be {@code null} */ public HtmlSerializer(final NamespaceService namespaceService, final String template, final String css, final String js, final String icon) { this(namespaceService, template, intoList(css), intoList(js), icon); }
@Test public void testHtmlSerializer() { final ByteArrayOutputStream out = new ByteArrayOutputStream(); service.write(getTriples(), out, null); assertAll("HTML check", checkHtmlFromTriples(new String(out.toByteArray(), UTF_8))); }
@Test public void testOrdinaryLabelledTriple2() { final Triple triple = rdf.createTriple( rdf.createIRI("test:value"), DC.title, rdf.createLiteral("A title")); final LabelledTriple t = new LabelledTriple(triple, null, null); assertEquals(DC.title.getIRIString(), t.getPredicateLabel(), "Predicate label doesn't match!"); assertEquals(DC.title.getIRIString(), t.getPredicate(), "Predicate value doesn't match!"); assertEquals("A title", t.getObjectLabel(), "Object label doesn't match!"); assertEquals("A title", t.getObject(), "Object value doesn't match!"); }