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);
}
}
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);
}
}
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;
}
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"));
}
}
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);
}
Aggregations