Search in sources :

Example 11 with RDFFormat

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();
        }
    }
}
Also used : Resource(org.openrdf.model.Resource) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) RdfCloudTripleStoreConnection(org.apache.rya.rdftriplestore.RdfCloudTripleStoreConnection) RDFFormat(org.openrdf.rio.RDFFormat) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with RDFFormat

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;
}
Also used : StringWriter(java.io.StringWriter) RDFHandlerException(org.openrdf.rio.RDFHandlerException) Statement(org.openrdf.model.Statement) ArrayList(java.util.ArrayList) RDFWriter(org.openrdf.rio.RDFWriter) RDFFormat(org.openrdf.rio.RDFFormat)

Example 13 with RDFFormat

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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType) RDFFormat(org.openrdf.rio.RDFFormat) Activate(org.apache.felix.scr.annotations.Activate)

Aggregations

RDFFormat (org.openrdf.rio.RDFFormat)13 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Statement (org.openrdf.model.Statement)3 RepositoryConnection (org.openrdf.repository.RepositoryConnection)3 RDFParseException (org.openrdf.rio.RDFParseException)3 RDFParser (org.openrdf.rio.RDFParser)3 File (java.io.File)2 InputStreamReader (java.io.InputStreamReader)2 LinkedList (java.util.LinkedList)2 Configuration (org.apache.hadoop.conf.Configuration)2 Repository (org.openrdf.repository.Repository)2 RepositoryException (org.openrdf.repository.RepositoryException)2 SailRepository (org.openrdf.repository.sail.SailRepository)2 SailRepositoryConnection (org.openrdf.repository.sail.SailRepositoryConnection)2 RDFHandlerException (org.openrdf.rio.RDFHandlerException)2 MemoryStore (org.openrdf.sail.memory.MemoryStore)2 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1