public void toRDF(File file, RDFFormat format) throws RDFHandlerException, IOException { Writer writer = Files.newWriter(file, Charset.forName("UTF-8")); Rio.write(model, writer, format); writer.flush(); writer.close(); }
@Override public boolean importDataset(Model data, String dataset, boolean cleanBefore) throws RDFHandlerException { RDFFormat format = RDFFormat.TURTLE; ByteArrayOutputStream out = new ByteArrayOutputStream(); Rio.write(data, out, format); return importDataset(new ByteArrayInputStream(out.toByteArray()), format, dataset, cleanBefore); }
/** * @param data * @param out * @param mediaType */ private void writeRdf(Model data, OutputStream out, MediaType mediaType) { RDFFormat rdfFormat = Rio.getWriterFormatForMIMEType(mediaType.toString()); if(rdfFormat == null){ throw new IllegalStateException("JAX-RS called for unsupported mediaType '" + mediaType +"'! If this is a valid RDF type this indicates a missing " + "Sesame Serializer implementation. Otherwise please report this " + "as a bug for the Stanbol Issue Tracker."); } try { Rio.write(data, out, rdfFormat); } catch (RDFHandlerException e) { throw new WebApplicationException("Unable to serialize QueryResultList with requested Format '" + rdfFormat +"'!", e); } }
private static void write(Model statements, OutputStream out) { try (PrintStream pout = new PrintStream(out)) { RDFWriter w = new RDFXMLPrettyWriter(pout); Rio.write(statements, w); } catch (Exception x) { throw new TripleStoreException("Writing model statements", x); } }
/** * Writes the given statement to the given {@link OutputStream} in the given * format. * <p> * * @param st * The statement to be written. * @param output * The {@link OutputStream} to write the statement to. * @param dataFormat * The {@link RDFFormat} to use when writing the statement. * @throws RDFHandlerException * Thrown if there is an error writing the statement. * @throws UnsupportedRDFormatException * If no {@link RDFWriter} is available for the specified RDF format. * @since 2.8.0 */ public static void write(Statement st, OutputStream output, RDFFormat dataFormat) throws RDFHandlerException { write(st, output, dataFormat, new WriterConfig()); }
/** * Writes the given single statement to the given {@link Writer} in the given * format. * <p> * * @param statement * A statement to be written. * @param output * The {@link Writer} to write the statement to. * @param dataFormat * The {@link RDFFormat} to use when writing the statement. * @throws RDFHandlerException * Thrown if there is an error writing the statement. * @throws UnsupportedRDFormatException * If no {@link RDFWriter} is available for the specified RDF format. * @since 2.8.0 */ public static void write(Statement statement, Writer output, RDFFormat dataFormat) throws RDFHandlerException { write(statement, output, dataFormat, new WriterConfig()); }
/** * Writes the given statements to the given {@link Writer} in the given * format. * <p> * If the collection is a {@link Model}, its namespaces will also be written. * * @param model * A collection of statements, such as a {@link Model}, to be written. * @param output * The {@link Writer} to write the statements to. * @param dataFormat * The {@link RDFFormat} to use when writing the statements. * @throws RDFHandlerException * Thrown if there is an error writing the statements. * @throws UnsupportedRDFormatException * If no {@link RDFWriter} is available for the specified RDF format. * @since 2.7.1 */ public static void write(Iterable<Statement> model, Writer output, RDFFormat dataFormat) throws RDFHandlerException { write(model, output, dataFormat, new WriterConfig()); }
/** * Writes the given statements to the given {@link OutputStream} in the given * format. * <p> * If the collection is a {@link Model}, its namespaces will also be written. * * @param model * A collection of statements, such as a {@link Model}, to be written. * @param output * The {@link OutputStream} to write the statements to. * @param dataFormat * The {@link RDFFormat} to use when writing the statements. * @throws RDFHandlerException * Thrown if there is an error writing the statements. * @throws UnsupportedRDFormatException * If no {@link RDFWriter} is available for the specified RDF format. * @since 2.7.1 */ public static void write(Iterable<Statement> model, OutputStream output, RDFFormat dataFormat) throws RDFHandlerException { write(model, output, dataFormat, new WriterConfig()); }
@Override public boolean importResource(String resource, Model data, String dataset, boolean cleanBefore) { RDFFormat format = RDFFormat.TURTLE; log.debug("Importing {} data for resource {} in {}", format.getName(), resource, dataset); try { java.net.URI target = credentials.buildUrl(getResourceUriBuilder(dataset, resource)); ByteArrayOutputStream out = new ByteArrayOutputStream(); Rio.write(data, out, format); InputStream in = new ByteArrayInputStream(out.toByteArray()); CloseableHttpResponse response; if (cleanBefore) { response = client.put(target, in, format); } else { response = client.post(target, in, format); } try { log.debug("Request resolved with {} status code: {}", response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); return (response.getStatusLine().getStatusCode() == 200); } finally { response.close(); } } catch (IllegalArgumentException | URISyntaxException | RDFHandlerException | IOException e) { log.error("Error importing resource: {}", e.getMessage(), e); throw new RuntimeException(e); } }
/** * Writes the given single statement to the given {@link OutputStream} in the * given format. * * @param st * The statement to be written. * @param output * The {@link OutputStream} to write the statement to. * @param dataFormat * The {@link RDFFormat} to use when writing the statement. * @param settings * The {@link WriterConfig} containing setting for configuring the * writer. * @throws RDFHandlerException * Thrown if there is an error writing the statement. * @throws UnsupportedRDFormatException * If no {@link RDFWriter} is available for the specified RDF format. * @since 2.8.0 */ public static void write(Statement st, OutputStream output, RDFFormat dataFormat, WriterConfig settings) throws RDFHandlerException { final RDFWriter writer = Rio.createWriter(dataFormat, output); writer.setWriterConfig(settings); write(st, writer); }
/** * Writes the given single statement to the given {@link Writer} in the given * format. * <p> * * @param statement * A statement to be written. * @param output * The {@link Writer} to write the statement to. * @param dataFormat * The {@link RDFFormat} to use when writing the statement. * @param settings * The {@link WriterConfig} containing settings for configuring the * writer. * @throws RDFHandlerException * Thrown if there is an error writing the statement. * @throws UnsupportedRDFormatException * If no {@link RDFWriter} is available for the specified RDF format. * @since 2.8.0 */ public static void write(Statement statement, Writer output, RDFFormat dataFormat, WriterConfig settings) throws RDFHandlerException { final RDFWriter writer = Rio.createWriter(dataFormat, output); writer.setWriterConfig(settings); write(statement, writer); }
/** * Writes the given statements to the given {@link OutputStream} in the given * format. * <p> * If the collection is a {@link Model}, its namespaces will also be written. * * @param model * A collection of statements, such as a {@link Model}, to be written. * @param output * The {@link OutputStream} to write the statements to. * @param dataFormat * The {@link RDFFormat} to use when writing the statements. * @param settings * The {@link WriterConfig} containing settings for configuring the * writer. * @throws RDFHandlerException * Thrown if there is an error writing the statements. * @throws UnsupportedRDFormatException * If no {@link RDFWriter} is available for the specified RDF format. * @since 2.7.3 */ public static void write(Iterable<Statement> model, OutputStream output, RDFFormat dataFormat, WriterConfig settings) throws RDFHandlerException { final RDFWriter writer = Rio.createWriter(dataFormat, output); writer.setWriterConfig(settings); write(model, writer); }
/** * the method to write the R2RML mappings * from an rdf Model to a file * @param os the output target */ public void write(OutputStream os) throws Exception { try { R2RMLMappingManager mm = new SesameR2RMLMappingManagerFactory().getR2RMLMappingManager(); Collection<TriplesMap> coll = getTripleMaps(); Model out = mm.exportMappings(coll, Model.class); Rio.write(out, os, RDFFormat.TURTLE); os.close(); } catch (Exception e) { e.printStackTrace(); throw e; } }
/** * Writes the given statements to the given {@link Writer} in the given * format. * <p> * If the collection is a {@link Model}, its namespaces will also be written. * * @param model * A collection of statements, such as a {@link Model}, to be written. * @param output * The {@link Writer} to write the statements to. * @param dataFormat * The {@link RDFFormat} to use when writing the statements. * @param settings * The {@link WriterConfig} containing settings for configuring the * writer. * @throws RDFHandlerException * Thrown if there is an error writing the statements. * @throws UnsupportedRDFormatException * If no {@link RDFWriter} is available for the specified RDF format. * @since 2.7.3 */ public static void write(Iterable<Statement> model, Writer output, RDFFormat dataFormat, WriterConfig settings) throws RDFHandlerException { final RDFWriter writer = Rio.createWriter(dataFormat, output); writer.setWriterConfig(settings); write(model, writer); }
Rio.write(myModel, writer, outputFormat);
Rio.write(model, bufferedWriter, RDFFormat.TRIG, writerConfig); } catch (RDFHandlerException ex) {
private void testSES2030BNodeCollisionsInternal(boolean preserveBNodeIDs) throws Exception { ByteArrayOutputStream output = new ByteArrayOutputStream(); RDFWriter rdfWriter = rdfWriterFactory.getWriter(output); setupWriterConfig(rdfWriter.getWriterConfig()); rdfWriter.startRDF(); int count = 18; for (int i = 0; i < count; i++) { BNode bNode2 = vf.createBNode("a" + Integer.toHexString(i).toUpperCase()); // System.out.println(bNode2.getID()); rdfWriter.handleStatement(vf.createStatement(uri1, uri2, bNode2)); } rdfWriter.endRDF(); RDFParser rdfParser = rdfParserFactory.getParser(); setupParserConfig(rdfParser.getParserConfig()); if (preserveBNodeIDs) { rdfParser.getParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true); } Model parsedModel = new LinkedHashModel(); rdfParser.setRDFHandler(new StatementCollector(parsedModel)); rdfParser.parse(new ByteArrayInputStream(output.toByteArray()), ""); // if(potentialObjects.size() != parsedModel.size()) { if (count != parsedModel.size()) { Rio.write(parsedModel, System.out, RDFFormat.NQUADS); } assertEquals(count, parsedModel.size()); // assertEquals(potentialObjects.size(), parsedModel.size()); }
Rio.write(results, out, RDFFormat.RDFJSON); } catch(RDFHandlerException e) { throw new IOException(e);
Rio.write(model, bufferedWriter, RDFFormat.JSONLD, writerConfig); } catch (RDFHandlerException ex) {
Rio.write(results, out, RDFFormat.RDFJSON); } catch(RDFHandlerException e) { throw new IOException(e);