Search in sources :

Example 51 with RDFFormat

use of org.eclipse.rdf4j.rio.RDFFormat in project inception by inception-project.

the class KnowledgeBaseServiceImpl method importData.

@SuppressWarnings("resource")
@Override
public void importData(KnowledgeBase kb, String aFilename, InputStream aIS) throws RDFParseException, RepositoryException, IOException {
    if (kb.isReadOnly()) {
        log.warn("Knowledge base [{}] is read only, will not import!", kb.getName());
        return;
    }
    InputStream is = new BufferedInputStream(aIS);
    try {
        // Stream is expected to be closed by caller of importData
        is = new CompressorStreamFactory().createCompressorInputStream(is);
    } catch (CompressorException e) {
        // Probably not compressed then or unknown format - just try as is.
        log.debug("Stream is not compressed, continue as is.");
    }
    // Detect the file format
    RDFFormat format = Rio.getParserFormatForFileName(aFilename).orElse(RDFFormat.RDFXML);
    // Load files into the repository
    try (RepositoryConnection conn = getConnection(kb)) {
        // If the RDF file contains relative URLs, then they probably start with a hash.
        // To avoid having two hashes here, we drop the hash from the base prefix configured
        // by the user.
        String prefix = StringUtils.removeEnd(kb.getBasePrefix(), "#");
        conn.add(is, prefix, format);
    }
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) CompressorException(org.apache.commons.compress.compressors.CompressorException) CompressorStreamFactory(org.apache.commons.compress.compressors.CompressorStreamFactory) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat)

Example 52 with RDFFormat

use of org.eclipse.rdf4j.rio.RDFFormat in project conformance-test-harness by solid.

the class RDFModel method parse.

/**
 * Returns a <code>RDFModel</code> instance containing the model for this input.
 * @param data the RDF data
 * @param contentType the content type for this data
 * @param baseUri the base URI for this data
 * @return the model
 */
public static RDFModel parse(final String data, final String contentType, final String baseUri) {
    try {
        if (!CONTENT_TYPES.containsKey(contentType)) {
            throw new IllegalArgumentException("contentType '" + contentType + "' is not supported");
        }
        final RDFFormat format = CONTENT_TYPES.get(contentType);
        if (format.equals(RDFFormat.RDFA)) {
            final ParserConfig parserConfig = new ParserConfig();
            parserConfig.set(RDFaParserSettings.RDFA_COMPATIBILITY, RDFaVersion.RDFA_1_1);
            return new RDFModel(Rio.parse(new StringReader(data), baseUri, RDFFormat.RDFA, parserConfig, SimpleValueFactory.getInstance(), new ParseErrorLogger()));
        } else {
            return new RDFModel(Rio.parse(new StringReader(data), baseUri, format));
        }
    } catch (Exception e) {
        throw new TestHarnessException("Failed to parse data", e);
    }
}
Also used : ParseErrorLogger(org.eclipse.rdf4j.rio.helpers.ParseErrorLogger) StringReader(java.io.StringReader) ParserConfig(org.eclipse.rdf4j.rio.ParserConfig) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat)

Example 53 with RDFFormat

use of org.eclipse.rdf4j.rio.RDFFormat in project corese by Wimmics.

the class Load method rdf4jModel.

private static Model rdf4jModel(Model model, LoadableFile... load_files) {
    for (LoadableFile load_file : load_files) {
        InputStream input = null;
        try {
            input = new FileInputStream(load_file.getFilePath());
        } catch (FileNotFoundException e1) {
            System.err.println("Error : File not found : " + load_file);
            e1.printStackTrace();
        }
        RDFFormat format = load_file.getFormatRdf4j();
        IRI[] contexts = load_file.getContextsRDF4J();
        try {
            model.addAll(Rio.parse(input, format, contexts));
        } catch (RDFParseException e) {
            System.err.println("Error : Impossible to parse " + load_file);
            e.printStackTrace();
        } catch (UnsupportedRDFormatException e) {
            System.err.println("Error : Unsupported RDF format " + load_file);
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("Error : Impossible to read " + load_file);
            e.printStackTrace();
        }
    }
    return model;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) UnsupportedRDFormatException(org.eclipse.rdf4j.rio.UnsupportedRDFormatException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 54 with RDFFormat

use of org.eclipse.rdf4j.rio.RDFFormat in project FAIRDataPoint by FAIRDataTeam.

the class HarvesterService method makeRequest.

private Model makeRequest(String uri) {
    log.info(format("Making request to '%s'", uri));
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(List.of(MediaType.parseMediaType(RDFFormat.TURTLE.getDefaultMIMEType())));
    HttpEntity<Void> entity = new HttpEntity<>(null, headers);
    try {
        ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
        if (!response.getStatusCode().is2xxSuccessful()) {
            log.info(format("Request to '%s' failed", uri));
            throw new HttpClientErrorException(response.getStatusCode());
        }
        RDFFormat rdfContentType = getRdfContentType(response.getHeaders().getContentType().getType());
        log.info(format("Request to '%s' successfully received", uri));
        Model result = read(response.getBody(), uri, rdfContentType);
        log.info(format("Request to '%s' successfully parsed", uri));
        return result;
    } catch (RestClientException e) {
        log.info(format("Request to '%s' failed", uri));
        throw new HttpClientErrorException(HttpStatus.BAD_GATEWAY, ofNullable(e.getMessage()).orElse("HTTP request failed to proceed"));
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Model(org.eclipse.rdf4j.model.Model) RestClientException(org.springframework.web.client.RestClientException) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat)

Example 55 with RDFFormat

use of org.eclipse.rdf4j.rio.RDFFormat in project FAIRDataPoint by FAIRDataTeam.

the class GenericController method updateMetaData.

@Operation(hidden = true)
@PutMapping(path = { "", "{oUrlPrefix:[^.]+}/{oRecordId:[^.]+}" }, produces = { "!application/json" })
public ResponseEntity<Model> updateMetaData(@PathVariable final Optional<String> oUrlPrefix, @PathVariable final Optional<String> oRecordId, @RequestBody String reqBody, @RequestHeader(value = "Content-Type", required = false) String contentType) throws MetadataServiceException {
    // 1. Init
    String urlPrefix = oUrlPrefix.orElse("");
    String recordId = oRecordId.orElse("");
    MetadataService metadataService = metadataServiceFactory.getMetadataServiceByUrlPrefix(urlPrefix);
    ResourceDefinition rd = resourceDefinitionService.getByUrlPrefix(urlPrefix);
    // 2. Extract URI
    IRI uri = getMetadataIRI(persistentUrl, urlPrefix, recordId);
    // 3. Parse reqDto
    RDFFormat rdfContentType = getRdfContentType(contentType);
    Model reqDto = read(reqBody, uri.stringValue(), rdfContentType);
    for (ResourceDefinitionChild child : rd.getChildren()) {
        org.eclipse.rdf4j.model.Value childEntity = getObjectBy(reqDto, null, i(child.getRelationUri()));
        if (childEntity != null) {
            reqDto.remove(i(childEntity.stringValue()), null, null);
        }
    }
    // 4. Store metadata
    Model metadata = metadataService.update(reqDto, uri, rd);
    // 5. Create response
    return ResponseEntity.ok(metadata);
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) ResourceDefinition(nl.dtls.fairdatapoint.entity.resource.ResourceDefinition) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) ResourceDefinitionChild(nl.dtls.fairdatapoint.entity.resource.ResourceDefinitionChild) MetadataService(nl.dtls.fairdatapoint.service.metadata.common.MetadataService) Value(org.eclipse.rdf4j.model.Value) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) Operation(io.swagger.v3.oas.annotations.Operation)

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