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