Java Code Examples for org.eclipse.rdf4j.rio.RDFWriter#startRDF()
The following examples show how to use
org.eclipse.rdf4j.rio.RDFWriter#startRDF() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InferencingTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
private File dumpStatements(String name, Collection<? extends Statement> statements) throws Exception { // Dump results to tmp file for debugging String tmpDir = System.getProperty("java.io.tmpdir"); name = name.replace("/", "_"); File tmpFile = new File(tmpDir, "junit-" + name + ".nt"); tmpFile.createNewFile(); try (OutputStream export = new BufferedOutputStream(new FileOutputStream(tmpFile))) { RDFWriter writer = Rio.createWriter(RDFFormat.NTRIPLES, export); writer.startRDF(); for (Statement st : statements) { writer.handleStatement(st); } writer.endRDF(); } return tmpFile; }
Example 2
Source File: SpinRendererTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
private static String toRDF(StatementCollector stmts) throws RDFHandlerException { WriterConfig config = new WriterConfig(); config.set(BasicWriterSettings.PRETTY_PRINT, false); StringBuilderWriter writer = new StringBuilderWriter(); final RDFWriter rdfWriter = Rio.createWriter(RDFFormat.TURTLE, writer); rdfWriter.setWriterConfig(config); rdfWriter.startRDF(); for (Map.Entry<String, String> entry : stmts.getNamespaces().entrySet()) { rdfWriter.handleNamespace(entry.getKey(), entry.getValue()); } for (final Statement st : stmts.getStatements()) { rdfWriter.handleStatement(st); } rdfWriter.endRDF(); writer.close(); return writer.toString(); }
Example 3
Source File: RDFXMLWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Write a statement with a literal that needs escaping as RDF/XML to a string * * @param useSingle use single quotes * @param withinText quotes to entity within text * @return RDF/XML output as string */ private String writeQuotable(boolean useSingle, boolean withinText) { Statement st = vf.createStatement(vf.createBNode(), DCTERMS.TITLE, vf.createLiteral("Single ' quote / Double \" quote")); StringWriter outputWriter = new StringWriter(); RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter); rdfWriter.getWriterConfig() .set(BasicWriterSettings.PRETTY_PRINT, false) .set(XMLWriterSettings.INCLUDE_XML_PI, true) .set(XMLWriterSettings.USE_SINGLE_QUOTES, useSingle) .set(XMLWriterSettings.QUOTES_TO_ENTITIES_IN_TEXT, withinText); rdfWriter.startRDF(); rdfWriter.handleStatement(st); rdfWriter.endRDF(); return outputWriter.toString(); }
Example 4
Source File: RDFXMLPrettyWriterBackgroundTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void sequenceItemsAreAbbreviated() throws RDFHandlerException, IOException { StringWriter writer = new StringWriter(); RDFWriter rdfWriter = rdfWriterFactory.getWriter(writer); rdfWriter.startRDF(); Resource res = vf.createIRI("http://example.com/#"); rdfWriter.handleStatement(vf.createStatement(res, RDF.TYPE, RDF.BAG)); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_1"), vf.createIRI("http://example.com/#1"))); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_2"), vf.createIRI("http://example.com/#2"))); rdfWriter.endRDF(); List<String> rdfLines = rdfOpenTags(writer.toString()); assertEquals(Arrays.asList("<rdf:RDF", "<rdf:Bag", "<rdf:li", "<rdf:li"), rdfLines); }
Example 5
Source File: RDFXMLPrettyWriterBackgroundTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void outOfSequenceItemsAreNotAbbreviated() throws RDFHandlerException, IOException { StringWriter writer = new StringWriter(); RDFWriter rdfWriter = rdfWriterFactory.getWriter(writer); rdfWriter.startRDF(); Resource res = vf.createIRI("http://example.com/#"); rdfWriter.handleStatement(vf.createStatement(res, RDF.TYPE, RDF.BAG)); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_0"), vf.createIRI("http://example.com/#0"))); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_2"), vf.createIRI("http://example.com/#2"))); rdfWriter.endRDF(); List<String> rdfLines = rdfOpenTags(writer.toString()); assertEquals(Arrays.asList("<rdf:RDF", "<rdf:Bag", "<rdf:_0", "<rdf:_2"), rdfLines); }
Example 6
Source File: RDFXMLPrettyWriterBackgroundTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void inSequenceItemsMixedWithOtherElementsAreAbbreviated() throws RDFHandlerException, IOException { StringWriter writer = new StringWriter(); RDFWriter rdfWriter = rdfWriterFactory.getWriter(writer); rdfWriter.startRDF(); Resource res = vf.createIRI("http://example.com/#"); rdfWriter.handleStatement(vf.createStatement(res, RDF.TYPE, RDF.BAG)); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_2"), vf.createIRI("http://example.com/#2"))); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_1"), vf.createIRI("http://example.com/#1"))); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_3"), vf.createIRI("http://example.com/#3"))); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_2"), vf.createIRI("http://example.com/#2"))); rdfWriter.endRDF(); List<String> rdfLines = rdfOpenTags(writer.toString()); assertEquals(Arrays.asList("<rdf:RDF", "<rdf:Bag", "<rdf:_2", "<rdf:li", "<rdf:_3", "<rdf:li"), rdfLines); }
Example 7
Source File: RDFXMLPrettyWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void sequenceItemsAreAbbreviated() throws RDFHandlerException, IOException { StringWriter writer = new StringWriter(); RDFWriter rdfWriter = rdfWriterFactory.getWriter(writer); rdfWriter.startRDF(); Resource res = vf.createIRI("http://example.com/#"); rdfWriter.handleStatement(vf.createStatement(res, RDF.TYPE, RDF.BAG)); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_1"), vf.createIRI("http://example.com/#1"))); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_2"), vf.createIRI("http://example.com/#2"))); rdfWriter.endRDF(); List<String> rdfLines = rdfOpenTags(writer.toString()); assertEquals(Arrays.asList("<rdf:RDF", "<rdf:Bag", "<rdf:li", "<rdf:li"), rdfLines); }
Example 8
Source File: RDFXMLPrettyWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void outOfSequenceItemsAreNotAbbreviated() throws RDFHandlerException, IOException { StringWriter writer = new StringWriter(); RDFWriter rdfWriter = rdfWriterFactory.getWriter(writer); rdfWriter.startRDF(); Resource res = vf.createIRI("http://example.com/#"); rdfWriter.handleStatement(vf.createStatement(res, RDF.TYPE, RDF.BAG)); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_0"), vf.createIRI("http://example.com/#0"))); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_2"), vf.createIRI("http://example.com/#2"))); rdfWriter.endRDF(); List<String> rdfLines = rdfOpenTags(writer.toString()); assertEquals(Arrays.asList("<rdf:RDF", "<rdf:Bag", "<rdf:_0", "<rdf:_2"), rdfLines); }
Example 9
Source File: RDFXMLPrettyWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void inSequenceItemsMixedWithOtherElementsAreAbbreviated() throws RDFHandlerException, IOException { StringWriter writer = new StringWriter(); RDFWriter rdfWriter = rdfWriterFactory.getWriter(writer); rdfWriter.startRDF(); Resource res = vf.createIRI("http://example.com/#"); rdfWriter.handleStatement(vf.createStatement(res, RDF.TYPE, RDF.BAG)); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_2"), vf.createIRI("http://example.com/#2"))); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_1"), vf.createIRI("http://example.com/#1"))); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_3"), vf.createIRI("http://example.com/#3"))); rdfWriter.handleStatement( vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_2"), vf.createIRI("http://example.com/#2"))); rdfWriter.endRDF(); List<String> rdfLines = rdfOpenTags(writer.toString()); assertEquals(Arrays.asList("<rdf:RDF", "<rdf:Bag", "<rdf:_2", "<rdf:li", "<rdf:_3", "<rdf:li"), rdfLines); }
Example 10
Source File: JSONLDWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testEmptyNamespace() throws Exception { IRI uri1 = vf.createIRI(exNs, "uri1"); IRI uri2 = vf.createIRI(exNs, "uri2"); StringWriter w = new StringWriter(); RDFWriter rdfWriter = rdfWriterFactory.getWriter(w); rdfWriter.getWriterConfig().set(JSONLDSettings.JSONLD_MODE, JSONLDMode.COMPACT); rdfWriter.startRDF(); rdfWriter.handleNamespace("", exNs); rdfWriter.handleNamespace(DCTERMS.PREFIX, DCTERMS.NAMESPACE); rdfWriter.handleStatement(vf.createStatement(uri1, DCTERMS.TITLE, vf.createBNode())); rdfWriter.handleStatement(vf.createStatement(uri1, uri2, vf.createBNode())); rdfWriter.endRDF(); assertTrue("Does not contain @vocab", w.toString().contains("@vocab")); }
Example 11
Source File: TextOutputExample.java From rya with Apache License 2.0 | 6 votes |
@Override protected void map(Text key, RyaStatementWritable value, Context context) throws IOException, InterruptedException { // receives a RyaStatementWritable; convert to a Statement RyaStatement rstmt = value.getRyaStatement(); Statement st = RyaToRdfConversions.convertStatement(rstmt); logger.info("Mapper receives: " + rstmt); // then convert to an RDF string StringWriter writer = new StringWriter(); try { RDFWriter rdfWriter = Rio.createWriter(rdfFormat, writer); rdfWriter.startRDF(); rdfWriter.handleStatement(st); rdfWriter.endRDF(); } catch (RDFHandlerException e) { throw new IOException("Error writing RDF data", e); } // Write the string to the output String line = writer.toString().trim(); logger.info("Serialized to RDF: " + line); textOut.set(line); context.write(NullWritable.get(), textOut); }
Example 12
Source File: StatementsSerializer.java From rya with Apache License 2.0 | 6 votes |
@Override public byte[] serialize(final String topic, final Set<Statement> data) { if(data == null) { // Returning null because that is the contract of this method. return null; } // Write the statements using a Binary RDF Writer. final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final RDFWriter writer = WRITER_FACTORY.getWriter(baos); writer.startRDF(); for(final Statement stmt : data) { // Write the statement. log.debug("Writing Statement: " + stmt); writer.handleStatement(stmt); } writer.endRDF(); // Return the byte[] version of the data. return baos.toByteArray(); }
Example 13
Source File: QueryResultsOutputUtil.java From rya with Apache License 2.0 | 6 votes |
/** * Writes the results of a {@link QueryResultStream} to the output stream as NTriples until the * shutdown signal is set. * * @param out - The stream the NTriples data will be written to. (not null) * @param resultsStream - The results stream that will be polled for results to * write to {@code out}. (not null) * @param shutdownSignal - Setting this signal will cause the thread that * is processing this function to finish and leave. (not null) * @throws RDFHandlerException A problem was encountered while * writing the NTriples to the output stream. * @throws IllegalStateException The {@code resultsStream} is closed. * @throws RyaStreamsException Could not fetch the next set of results. */ public static void toNtriplesFile( final OutputStream out, final QueryResultStream<VisibilityStatement> resultsStream, final AtomicBoolean shutdownSignal) throws RDFHandlerException, IllegalStateException, RyaStreamsException { requireNonNull(out); requireNonNull(resultsStream); requireNonNull(shutdownSignal); final RDFWriter writer = Rio.createWriter(RDFFormat.NTRIPLES, out); writer.startRDF(); while(!shutdownSignal.get()) { final Iterable<VisibilityStatement> it = resultsStream.poll(1000); for(final VisibilityStatement result : it) { writer.handleStatement(result); } } writer.endRDF(); }
Example 14
Source File: JSONLDWriterBackgroundTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testRoundTripNamespaces() throws Exception { String exNs = "http://example.org/"; IRI uri1 = vf.createIRI(exNs, "uri1"); IRI uri2 = vf.createIRI(exNs, "uri2"); Literal plainLit = vf.createLiteral("plain", XMLSchema.STRING); Statement st1 = vf.createStatement(uri1, uri2, plainLit); ByteArrayOutputStream out = new ByteArrayOutputStream(); RDFWriter rdfWriter = rdfWriterFactory.getWriter(out); rdfWriter.getWriterConfig().set(JSONLDSettings.JSONLD_MODE, JSONLDMode.COMPACT); rdfWriter.startRDF(); rdfWriter.handleNamespace("ex", exNs); rdfWriter.handleStatement(st1); rdfWriter.endRDF(); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); RDFParser rdfParser = rdfParserFactory.getParser(); ParserConfig config = new ParserConfig(); config.set(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES, true); config.set(BasicParserSettings.FAIL_ON_UNKNOWN_LANGUAGES, true); rdfParser.setParserConfig(config); rdfParser.setValueFactory(vf); Model model = new LinkedHashModel(); rdfParser.setRDFHandler(new StatementCollector(model)); rdfParser.parse(in, "foo:bar"); assertEquals("Unexpected number of statements, found " + model.size(), 1, model.size()); assertTrue("missing namespaced statement", model.contains(st1)); if (rdfParser.getRDFFormat().supportsNamespaces()) { assertTrue("Expected at least one namespace, found " + model.getNamespaces().size(), model.getNamespaces().size() >= 1); assertEquals(exNs, model.getNamespace("ex").get().getName()); } }
Example 15
Source File: JSONLDWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testRoundTripNamespaces() throws Exception { IRI uri1 = vf.createIRI(exNs, "uri1"); IRI uri2 = vf.createIRI(exNs, "uri2"); Literal plainLit = vf.createLiteral("plain", XMLSchema.STRING); Statement st1 = vf.createStatement(uri1, uri2, plainLit); ByteArrayOutputStream out = new ByteArrayOutputStream(); RDFWriter rdfWriter = rdfWriterFactory.getWriter(out); rdfWriter.getWriterConfig().set(JSONLDSettings.JSONLD_MODE, JSONLDMode.COMPACT); rdfWriter.startRDF(); rdfWriter.handleNamespace("ex", exNs); rdfWriter.handleStatement(st1); rdfWriter.endRDF(); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); RDFParser rdfParser = rdfParserFactory.getParser(); ParserConfig config = new ParserConfig(); config.set(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES, true); config.set(BasicParserSettings.FAIL_ON_UNKNOWN_LANGUAGES, true); rdfParser.setParserConfig(config); rdfParser.setValueFactory(vf); Model model = new LinkedHashModel(); rdfParser.setRDFHandler(new StatementCollector(model)); rdfParser.parse(in, "foo:bar"); assertEquals("Unexpected number of statements, found " + model.size(), 1, model.size()); assertTrue("missing namespaced statement", model.contains(st1)); if (rdfParser.getRDFFormat().supportsNamespaces()) { assertTrue("Expected at least one namespace, found " + model.getNamespaces().size(), model.getNamespaces().size() >= 1); assertEquals(exNs, model.getNamespace("ex").get().getName()); } }