@Override public void handleComment(String comment) throws RDFHandlerException { trace("Discarded comment '%s'.",comment); }
@Test public void testWithbase$simpleDoc$UnusedNamespacesAreDiscarded() throws Exception { withBase.startRDF(); withBase.handleNamespace(NS1_PX1, NS1_NAMESPACE); withBase.handleNamespace(NS2_PX, NS2_NAMESPACE); withBase.handleNamespace(NO_PX,NO_NAMESPACE); withBase.handleStatement(statement(uriRef(NS1_NAMESPACE,URI1),uriRef(NS2_NAMESPACE,URI2),blankNode(BN_FOLDED))); withBase.endRDF(); assertThat(outWithBase.toString(),containsString(BASE_DIRECTIVE)); assertThat(outWithBase.toString(),containsString(prefixDirective(NS1_PX1, NS1_NAMESPACE))); assertThat(outWithBase.toString(),containsString(prefixDirective(NS2_PX, NS2_NAMESPACE))); assertThat(outWithBase.toString(),containsString(qname(NS1_PX1,URI1))); assertThat(outWithBase.toString(),containsString(qname(NS2_PX,URI2))); assertThat(outWithBase.toString(),containsString(blankNode(BN_FOLDED).toString())); assertThat(outWithBase.toString(),not(containsString(prefixDirective(NO_PX, NO_NAMESPACE)))); assertThat(outWithBase.toString(),not(containsString(uriRef(NS1_NAMESPACE,URI1).toString()))); assertThat(outWithBase.toString(),not(containsString(uriRef(NS2_NAMESPACE,URI2).toString()))); }
public static String prettyPrint(RepositoryConnection connection, Resource... contexts) throws SesameUtilsException { try { StringWriter writer = new StringWriter(); connection.export(new TurtlePrettyPrinter(writer), contexts); return writer.toString(); } catch(RepositoryException e) { throw new SesameRepositoryFailure(COULD_NOT_SERIALIZE_CONTENTS,e); } catch (RDFHandlerException e) { throw new SesameUtilsAssertionError("Unexpected pretty printer failure",e); } catch(UnsupportedRDFormatException e) { throw new SesameUtilsAssertionError(TURTLE_FORMAT_SHOULD_BE_SUPPORTED, e); } }
@Test public void testNobase$emptuDoc$NamespacesAreDiscarded() throws Exception { noBase.startRDF(); noBase.handleNamespace(NS1_PX1, NS1_NAMESPACE); noBase.endRDF(); assertThat(outNoBase.toString(),isEmptyString()); }
@Test public void testNoBase$emptyDoc$CommentsAreDiscarded() throws Exception { noBase.startRDF(); noBase.handleComment(COMMENT); noBase.endRDF(); assertThat(outNoBase.toString(),isEmptyString()); }
@Test public void testWithbase$simpleDoc$NamespacePrefixesOverlap() throws Exception { withBase.startRDF(); withBase.handleNamespace(NS1_PX1, NS1_NAMESPACE); withBase.handleNamespace(NS2_PX, NS2_NAMESPACE); withBase.handleNamespace(NS1_PX2,NS1_NAMESPACE); withBase.handleStatement(statement(uriRef(NS1_NAMESPACE,URI1),uriRef(NS2_NAMESPACE,URI2),blankNode(BN_FOLDED))); withBase.endRDF(); assertThat(outWithBase.toString(),containsString(BASE_DIRECTIVE)); assertThat(outWithBase.toString(),containsString(prefixDirective(NS1_PX2, NS1_NAMESPACE))); assertThat(outWithBase.toString(),containsString(prefixDirective(NS2_PX, NS2_NAMESPACE))); assertThat(outWithBase.toString(),containsString(qname(NS1_PX2,URI1))); assertThat(outWithBase.toString(),containsString(qname(NS2_PX,URI2))); assertThat(outWithBase.toString(),containsString(blankNode(BN_FOLDED).toString())); assertThat(outWithBase.toString(),not(containsString(prefixDirective(NS1_PX1, NS1_NAMESPACE)))); assertThat(outWithBase.toString(),not(containsString(qname(NS1_PX1,URI1)))); assertThat(outWithBase.toString(),not(containsString(uriRef(NS1_NAMESPACE,URI1).toString()))); assertThat(outWithBase.toString(),not(containsString(uriRef(NS2_NAMESPACE,URI2).toString()))); }
@Before public void setUp() { vf = new MemValueFactory(); outNoBase = new StringWriter(); outWithBase = new StringWriter(); noBase = new TurtlePrettyPrinter(outNoBase); withBase = new TurtlePrettyPrinter(vf.createURI(BASE),outWithBase); }
@Test public void testWithbase$emptyDoc$NamespacesAreDiscarded() throws Exception { withBase.startRDF(); withBase.handleNamespace(NS1_PX1, NS1_NAMESPACE); withBase.endRDF(); assertThat(outWithBase.toString(),containsString(BASE_DIRECTIVE)); assertThat(outWithBase.toString(),not(containsString(prefixDirective(NS1_PX1, NS1_NAMESPACE)))); }
@Test public void testWithBase$emptyDoc$CommentsAreDiscarded() throws Exception { withBase.startRDF(); withBase.handleComment(COMMENT); withBase.endRDF(); assertThat(outWithBase.toString(),containsString(BASE_DIRECTIVE)); assertThat(outWithBase.toString(),not(containsString(comment(COMMENT)))); }
private RDFWriter createWriter(StringWriter writer) { RDFWriter result=null; if(format.equals(Format.TURTLE)) { result=new TurtlePrettyPrinter(new MemValueFactory().createURI(baseURI.toString()),writer); } else { RDFWriterRegistry registry=RDFWriterRegistry.getInstance(); RDFFormat rawFormat=Rio.getWriterFormatForMIMEType(format.getMime(),RDFFormat.RDFXML); RDFWriterFactory factory=registry.get(rawFormat); result=factory.getWriter(writer); if(format.equals(Format.JSON_LD)) { result.getWriterConfig().set(JSONLDSettings.JSONLD_MODE,JSONLDMode.FLATTEN); result.getWriterConfig().set(BasicWriterSettings.PRETTY_PRINT,true); } } return result; }
@Override public void handleNamespace(String prefix, String uri) throws RDFHandlerException { graph.addNamespace(prefix, uri); trace("Added prefix '%s' for namespace '%s'.",prefix,uri); }
@Override public void startRDF() throws RDFHandlerException { trace("Started RDF processing..."); graph=new GraphImpl(base); }
@Override public void endRDF() throws RDFHandlerException { GraphRenderer renderer=new GraphRenderer(graph,writerConfig.get(ENABLE_FOLDING)); out.append(renderer.render()); out.flush(); trace("Completed RDF processing."); }
@Override public void handleStatement(Statement st) throws RDFHandlerException { Resource subject = st.getSubject(); URI predicate = st.getPredicate(); Value object = st.getObject(); trace("Added triple (%s,%s,%s).",subject,predicate,object); graph.add(subject, predicate, object); }