Search in sources :

Example 46 with RDFFormat

use of org.eclipse.rdf4j.rio.RDFFormat in project incubator-rya by apache.

the class RyaCommands method loadData.

@CliCommand(value = LOAD_DATA_CMD, help = "Loads RDF Statement data from a local file to the connected Rya instance.")
public String loadData(@CliOption(key = { "file" }, mandatory = true, help = "A local file containing RDF Statements that is to be loaded.") final String file, @CliOption(key = { "format" }, mandatory = false, help = "The format of the supplied RDF Statements file. [RDF/XML, N-Triples, Turtle, N3, TriX, TriG, BinaryRDF, N-Quads, JSON-LD, RDF/JSON, RDFa]") final String format) {
    // Fetch the command that is connected to the store.
    final ShellState shellState = state.getShellState();
    final RyaClient commands = shellState.getConnectedCommands().get();
    final Optional<String> ryaInstanceName = shellState.getRyaInstanceName();
    try {
        final long start = System.currentTimeMillis();
        // If the provided path is relative, then make it rooted in the user's home.
        // Make sure the path is formatted with Unix style file
        // separators('/') before using it as a regex replacement string.
        // Windows file separators('\') will not work unless escaped.
        final String userHome = FilenameUtils.separatorsToUnix(System.getProperty("user.home"));
        final Path rootedFile = Paths.get(file.replaceFirst("^~", userHome));
        RDFFormat rdfFormat = null;
        // If a format was provided, then go with that.
        if (format != null) {
            rdfFormat = RdfFormatUtils.getRdfFormatFromName(format);
            if (rdfFormat == null) {
                throw new RuntimeException("Unsupported RDF Statement data input format: " + format);
            }
        } else // Otherwise try to figure it out using the filename.
        if (rdfFormat == null) {
            rdfFormat = Rio.getParserFormatForFileName(rootedFile.getFileName().toString()).get();
            if (rdfFormat == null) {
                throw new RuntimeException("Unable to detect RDF Statement data input format for file: " + rootedFile);
            } else {
                consolePrinter.println("Detected RDF Format: " + rdfFormat);
                consolePrinter.flush();
            }
        }
        commands.getLoadStatementsFile().loadStatements(ryaInstanceName.get(), rootedFile, rdfFormat);
        final String seconds = new DecimalFormat("0.0##").format((System.currentTimeMillis() - start) / 1000.0);
        return "Loaded the file: '" + file + "' successfully in " + seconds + " seconds.";
    } catch (final RyaClientException | IOException e) {
        log.error("Error", e);
        throw new RuntimeException("Can not load the RDF Statement data. Reason: " + e.getMessage(), e);
    }
}
Also used : Path(java.nio.file.Path) RyaClientException(org.apache.rya.api.client.RyaClientException) ShellState(org.apache.rya.shell.SharedShellState.ShellState) DecimalFormat(java.text.DecimalFormat) RyaClient(org.apache.rya.api.client.RyaClient) IOException(java.io.IOException) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 47 with RDFFormat

use of org.eclipse.rdf4j.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 = RdfFormatUtils.getRdfFormatFromName(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.createIRI(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.eclipse.rdf4j.model.Resource) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) RdfCloudTripleStoreConnection(org.apache.rya.rdftriplestore.RdfCloudTripleStoreConnection) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 48 with RDFFormat

use of org.eclipse.rdf4j.rio.RDFFormat in project timbuctoo by HuygensING.

the class Rdf4jRdfParser method importRdf.

@Override
public void importRdf(CachedLog input, String baseUri, String defaultGraph, RdfProcessor rdfProcessor) throws RdfProcessingFailedException, RdfProcessingParseException {
    try {
        RDFFormat format = Rio.getParserFormatForMIMEType(input.getMimeType().toString()).orElseThrow(() -> new UnsupportedRDFormatException(input.getMimeType() + " is not a supported rdf type."));
        RDFParser rdfParser = Rio.createParser(format);
        rdfParser.setPreserveBNodeIDs(true);
        rdfParser.setRDFHandler(new TimRdfHandler(rdfProcessor, baseUri, defaultGraph, input.getFile().getName()));
        rdfParser.parse(input.getReader(), baseUri);
    } catch (IOException | UnsupportedRDFormatException e) {
        throw new RdfProcessingFailedException(e);
    } catch (RDFParseException e) {
        throw new Rdf4jRdfProcessingParseException(e, input);
    } catch (RDFHandlerException e) {
        if (e.getCause() instanceof RdfProcessingFailedException) {
            throw (RdfProcessingFailedException) e.getCause();
        } else {
            throw new RdfProcessingFailedException(e);
        }
    }
}
Also used : UnsupportedRDFormatException(org.eclipse.rdf4j.rio.UnsupportedRDFormatException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) TimRdfHandler(nl.knaw.huygens.timbuctoo.v5.rdfio.implementations.rdf4j.parsers.TimRdfHandler) IOException(java.io.IOException) RDFParser(org.eclipse.rdf4j.rio.RDFParser) RdfProcessingFailedException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.RdfProcessingFailedException) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 49 with RDFFormat

use of org.eclipse.rdf4j.rio.RDFFormat in project ldp-coap-framework by sisinflab-swot.

the class CoAPLDPBasicContainer method addNewResource.

protected void addNewResource(CoapExchange exchange, int ct, String rt, String childName, String title) throws RDFParseException, RepositoryException, IOException, CoAPLDPException {
    if (ct == MediaTypeRegistry.TEXT_TURTLE || ct == MediaTypeRegistry.APPLICATION_LD_JSON) {
        RDFFormat f;
        if (ct == MediaTypeRegistry.TEXT_TURTLE)
            f = RDFFormat.TURTLE;
        else
            f = RDFFormat.JSONLD;
        String body = exchange.getRequestText();
        mng.postRDFSource(mng.getBaseURI() + childName, body, f);
        if ((rt == null) || (rt.equals(LDP.LINK_LDP + ":" + LDP.CLASS_LNAME_RESOURCE))) {
            /**
             * Add LDP-RDFSource **
             */
            add(new CoAPLDPRDFSource(title, getFullName(), mng));
        } else if (rt.equals(LDP.LINK_LDP + ":" + LDP.CLASS_LNAME_BASIC_CONTAINER)) {
            /**
             * Add LDP-BasicContainer **
             */
            CoAPLDPBasicContainer bc = new CoAPLDPBasicContainer(title, getFullName(), mng);
            bc.setRDFCreated();
            add(bc);
        } else if (rt.equals(LDP.LINK_LDP + ":" + LDP.CLASS_LNAME_DIRECT_CONTAINER)) {
            String memberRel = mng.getMemberRelation(body, f);
            String isMemberOfRel = mng.getIsMemberOfRelation(body, f);
            String resName = mng.getMemberResource(body, f);
            if (resName.equals(mng.getBaseURI())) {
                mng.deleteMemberResourceStatement(childName);
                resName = "resource";
            }
            /**
             * Add LDP-DirectContainer **
             */
            CoAPLDPRDFSource memberRes = new CoAPLDPRDFSource(resName, childName, mng);
            CoAPLDPDirectContainer dc = new CoAPLDPDirectContainer(title, getFullName(), mng, memberRes, memberRel, isMemberOfRel);
            dc.setRDFCreated();
            add(dc);
        } else
            throw new CoAPLDPException("Invalid RT query parameter.");
        exchange.setLocationQuery(LinkFormat.RESOURCE_TYPE + "=" + LDP.LINK_LDP + ":" + LDP.CLASS_LNAME_RESOURCE);
    } else if (options.getAcceptedPostTypes().contains(ct)) {
        /**
         * Add LDP-NonRDFSource **
         */
        CoAPLDPNonRDFSource nRDF = new CoAPLDPNonRDFSource(title, getFullName(), mng, ct);
        nRDF.setData(exchange.getRequestPayload());
        add(nRDF);
        String type = LinkFormat.RESOURCE_TYPE + "=" + LDP.LINK_LDP + ":" + LDP.CLASS_LNAME_RESOURCE;
        String meta = LDP.LINK_REL_DESCRIBEDBY + "=" + mng.getBaseURI() + nRDF.getFullName() + "/meta";
        exchange.setLocationQuery(type + "&" + meta);
    } else
        throw new CoAPLDPContentFormatException("Content-Format (CT) Not Accepted.");
}
Also used : CoAPLDPException(it.poliba.sisinflab.coap.ldp.exception.CoAPLDPException) CoAPLDPContentFormatException(it.poliba.sisinflab.coap.ldp.exception.CoAPLDPContentFormatException) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat)

Example 50 with RDFFormat

use of org.eclipse.rdf4j.rio.RDFFormat in project ldp-coap-framework by sisinflab-swot.

the class CoAPLDPIndirectContainer method postResource.

private void postResource(CoapExchange exchange, boolean putToCreate) {
    Request req = exchange.advanced().getCurrentRequest();
    HashMap<String, String> atts = serializeAttributes(req.getOptions().getUriQuery());
    String title = atts.get(LinkFormat.TITLE);
    if (title == null) {
        title = getAnonymousResource();
    }
    int ct = exchange.getRequestOptions().getContentFormat();
    if ((ct != -1) && (title != null)) {
        String body = exchange.getRequestText();
        try {
            String childName = resource.getURI() + "/" + title;
            if (mng.isDeleted(childName)) {
                if (!putToCreate) {
                    title = getAnonymousResource();
                    childName = resource.getURI() + "/" + title;
                } else {
                    throw new CoAPLDPException("LDP Resource previously deleted!");
                }
            }
            if (!existChild(childName)) {
                if (ct == MediaTypeRegistry.TEXT_TURTLE || ct == MediaTypeRegistry.APPLICATION_LD_JSON) {
                    RDFFormat f;
                    if (ct == MediaTypeRegistry.TEXT_TURTLE)
                        f = RDFFormat.TURTLE;
                    else
                        f = RDFFormat.JSONLD;
                    String indirectResource = mng.postIndirectRDFSource(mng.getBaseURI() + childName, body, this.insertedContentRelation, f);
                    String rt = atts.get(LinkFormat.RESOURCE_TYPE);
                    if ((rt == null) || (rt.equals(LDP.LINK_LDP + ":" + LDP.CLASS_LNAME_RESOURCE))) {
                        /**
                         * Add LDP-RDFSource **
                         */
                        CoAPLDPRDFSource s = new CoAPLDPRDFSource(title, resource.getURI(), mng);
                        if (indirectResource != null)
                            this.addRDFResource(s, mng.createIRI(indirectResource.replaceAll("<>", "")));
                        else
                            this.addRDFResource(s);
                    } else if (rt.equals(LDP.LINK_LDP + ":" + LDP.CLASS_LNAME_BASIC_CONTAINER)) {
                        /**
                         * Add LDP-BasicContainer **
                         */
                        CoAPLDPBasicContainer bc = new CoAPLDPBasicContainer(title, mng);
                        bc.setRDFCreated();
                        this.addRDFResource(bc, mng.createIRI(indirectResource));
                    } else
                        throw new CoAPLDPException("Invalid RT query parameter.");
                } else {
                    throw new CoAPLDPContentFormatException("Content-Format (CT) Not Accepted.");
                }
                exchange.setLocationPath(mng.getBaseURI() + childName);
                exchange.setLocationQuery(LinkFormat.RESOURCE_TYPE + "=" + LDP.LINK_LDP + ":" + LDP.CLASS_LNAME_RESOURCE);
                exchange.respond(ResponseCode.CREATED);
            } else
                exchange.respond(ResponseCode.FORBIDDEN);
        } catch (CoAPLDPContentFormatException e) {
            e.printStackTrace();
            exchange.respond(ResponseCode.UNSUPPORTED_CONTENT_FORMAT);
        } catch (RDFParseException | CoAPLDPException e) {
            e.printStackTrace();
            exchange.respond(ResponseCode.BAD_REQUEST);
        } catch (RepositoryException | IOException e) {
            e.printStackTrace();
            exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR);
        }
    } else {
        exchange.respond(ResponseCode.BAD_REQUEST);
    }
}
Also used : Request(org.eclipse.californium.core.coap.Request) CoAPLDPContentFormatException(it.poliba.sisinflab.coap.ldp.exception.CoAPLDPContentFormatException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) IOException(java.io.IOException) CoAPLDPException(it.poliba.sisinflab.coap.ldp.exception.CoAPLDPException) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Aggregations

RDFFormat (org.eclipse.rdf4j.rio.RDFFormat)62 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 IOException (java.io.IOException)17 WriteRdf4j (mom.trd.opentheso.core.exports.rdf4j.WriteRdf4j)14 InputStream (java.io.InputStream)12 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)11 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)8 FileInputStream (java.io.FileInputStream)7 Model (org.eclipse.rdf4j.model.Model)6 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)6 RDFParser (org.eclipse.rdf4j.rio.RDFParser)5 UnsupportedRDFormatException (org.eclipse.rdf4j.rio.UnsupportedRDFormatException)5 Rdf2GoCore (de.knowwe.rdf2go.Rdf2GoCore)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 File (java.io.File)4 NodePreference (mom.trd.opentheso.bdd.helper.nodes.NodePreference)4 ExportRdf4jHelper (mom.trd.opentheso.core.exports.rdf4j.ExportRdf4jHelper)4 IRI (org.eclipse.rdf4j.model.IRI)4 Statement (org.eclipse.rdf4j.model.Statement)4 Test (org.junit.Test)4