use of org.openrdf.rio.RDFFormat in project incubator-rya by apache.
the class RdfController method loadRdf.
@RequestMapping(value = "/loadrdf", method = RequestMethod.POST)
public void loadRdf(@RequestParam(required = false) final String format, @RequestParam(value = RdfCloudTripleStoreConfiguration.CONF_CV, required = false) final String cv, @RequestParam(required = false) final String graph, @RequestBody final String body, final HttpServletResponse response) throws RepositoryException, IOException, RDFParseException {
RDFFormat format_r = RDFFormat.RDFXML;
if (format != null) {
format_r = RDFFormat.valueOf(format);
if (format_r == null) {
throw new RuntimeException("RDFFormat[" + format + "] not found");
}
}
// add named graph as context (if specified).
final List<Resource> contextList = new ArrayList<Resource>();
if (graph != null) {
contextList.add(VALUE_FACTORY.createURI(graph));
}
SailRepositoryConnection conn = null;
try {
conn = repository.getConnection();
if (conn.getSailConnection() instanceof RdfCloudTripleStoreConnection && cv != null) {
final RdfCloudTripleStoreConnection<?> sailConnection = (RdfCloudTripleStoreConnection<?>) conn.getSailConnection();
sailConnection.getConf().set(RdfCloudTripleStoreConfiguration.CONF_CV, cv);
}
conn.add(new StringReader(body), "", format_r, contextList.toArray(new Resource[contextList.size()]));
conn.commit();
} finally {
if (conn != null) {
conn.close();
}
}
}
use of org.openrdf.rio.RDFFormat in project wikidata-query-rdf by wikimedia.
the class RDFChunkSerializer method serializeAsChunks.
public List<RDFDataChunk> serializeAsChunks(Collection<Statement> statements, String mime, int softMaxSize) {
List<RDFDataChunk> chunks = new ArrayList<>();
StringWriter stringWriter = new StringWriter();
RDFFormat fileFormatForMIMEType = writerRegistry.getFileFormatForMIMEType(mime);
if (fileFormatForMIMEType == null) {
throw new IllegalArgumentException("Unsupported mime type [" + mime + "]");
}
RDFWriter writer = writerRegistry.get(fileFormatForMIMEType).getWriter(stringWriter);
try {
boolean pending = false;
writer.startRDF();
for (Statement s : statements) {
writer.handleStatement(s);
pending = true;
if (stringWriter.getBuffer().length() > softMaxSize) {
writer.endRDF();
chunks.add(new RDFDataChunk(stringWriter.toString(), mime));
stringWriter.getBuffer().setLength(0);
pending = false;
writer.startRDF();
}
}
if (pending) {
writer.endRDF();
chunks.add(new RDFDataChunk(stringWriter.toString(), mime));
}
} catch (RDFHandlerException e) {
throw new IllegalStateException("Cannot write RDF chunks", e);
}
return chunks;
}
use of org.openrdf.rio.RDFFormat in project stanbol by apache.
the class SesameModelWriter method activate.
@Activate
protected void activate(ComponentContext ctx) {
// parse the supported RDF formats
Collection<String> mts = new LinkedHashSet<String>();
for (RDFFormat format : RDFFormat.values()) {
mts.addAll(format.getMIMETypes());
}
List<MediaType> formats = new ArrayList<MediaType>(mts.size());
for (String format : mts) {
try {
formats.add(MediaType.valueOf(format));
} catch (IllegalArgumentException e) {
log.error("Unable to parse MediaType for Sesame RDF format '" + format + "'!", e);
}
}
supportedRrfFormats = Collections.unmodifiableList(formats);
}
Aggregations