use of org.eclipse.rdf4j.rio.RDFFormat in project opentheso by miledrousset.
the class ExportJsonTest method getJson.
// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
//
@Test
public void getJson() {
String idConcept1 = "14767";
String idConcept2 = "36825";
String idTheso = "TH_1";
int type = 3;
RDFFormat format = null;
String extention;
switch(type) {
case 0:
format = RDFFormat.RDFXML;
extention = "_skos.xml";
break;
case 1:
format = RDFFormat.JSONLD;
extention = "_json-ld.json";
break;
case 2:
format = RDFFormat.TURTLE;
extention = "_turtle.ttl";
break;
case 3:
format = RDFFormat.RDFJSON;
extention = "_json.json";
break;
}
HikariDataSource ds;
ConnexionTest connexionTest = new ConnexionTest();
ds = connexionTest.getConnexionPool();
NodePreference nodePreference = new PreferencesHelper().getThesaurusPreferences(ds, idTheso);
if (nodePreference != null) {
ExportRdf4jHelper exportRdf4jHelper = new ExportRdf4jHelper();
exportRdf4jHelper.setNodePreference(nodePreference);
exportRdf4jHelper.setInfos(ds, "dd-mm-yyyy", false, idTheso, nodePreference.getCheminSite());
exportRdf4jHelper.setNodePreference(nodePreference);
exportRdf4jHelper.addSignleConcept(idTheso, idConcept1);
exportRdf4jHelper.addSignleConcept(idTheso, idConcept2);
WriteRdf4j writeRdf4j = new WriteRdf4j(exportRdf4jHelper.getSkosXmlDocument());
ByteArrayOutputStream out;
out = new ByteArrayOutputStream();
Rio.write(writeRdf4j.getModel(), out, format);
System.out.println(out.toString());
// file = new ByteArrayContent(out.toByteArray(), "application/xml", idTheso + " " + extention);
}
ds.close();
}
use of org.eclipse.rdf4j.rio.RDFFormat in project nanopub-server by tkuhn.
the class NanopubPage method show.
public void show() throws IOException {
Nanopub nanopub;
String ac = getReq().getArtifactCode();
try {
nanopub = NanopubDb.get().getNanopub(ac);
} catch (Exception ex) {
getResp().sendError(500, "Internal error: " + ex.getMessage());
logger.error(ex.getMessage(), ex);
return;
}
if (nanopub == null) {
getResp().sendError(404, "Nanopub not found: " + ac);
return;
}
setCanonicalLink("/" + ac);
boolean isIndexNanopub = IndexUtils.isIndex(nanopub);
String ext = getReq().getExtension();
String rf = getReq().getPresentationFormat();
RDFFormat format = null;
if (ext != null) {
format = Rio.getParserFormatForFileName("np." + ext).orElse(null);
if (ext.equals("stnp")) {
format = TrustyNanopubUtils.STNP_FORMAT;
}
if (format == null) {
getResp().sendError(400, "Unknown format: " + ext);
return;
}
} else if (rf == null) {
String suppFormats = "text/html,application/trig,application/x-trig,text/x-nquads,application/trix,application/ld+json";
if (isIndexNanopub) {
suppFormats += ",text/html";
} else {
suppFormats += ",text/plain";
}
String mimeType = Utils.getMimeType(getHttpReq(), suppFormats);
if (isIndexNanopub && "text/html".equals(mimeType) && rf == null) {
// Show index-specific HTML reprensetation when HTML is requested by content negotiation,
// but not if ".html" ending is used (in the latter case, use the general HTML view).
showIndex(nanopub);
return;
}
if ("text/plain".equals(mimeType)) {
rf = "text/plain";
} else {
format = Rio.getParserFormatForMIMEType(mimeType).orElse(null);
}
}
if (format == null) {
format = HtmlWriter.HTML_FORMAT;
}
if (!format.supportsContexts()) {
getResp().sendError(400, "Unsuitable RDF format: " + ext);
return;
}
if (rf != null) {
getResp().setContentType(rf);
} else {
getResp().setContentType(format.getDefaultMIMEType());
getResp().addHeader("Content-Disposition", "filename=\"" + ac + "." + format.getDefaultFileExtension() + "\"");
}
OutputStream out = null;
try {
out = getResp().getOutputStream();
if (format == HtmlWriter.HTML_FORMAT) {
Nanopub2Html.createHtml(nanopub, out, true);
} else {
NanopubUtils.writeToStream(nanopub, out, format);
}
} catch (Exception ex) {
getResp().sendError(500, "Internal error: " + ex.getMessage());
logger.error(ex.getMessage(), ex);
} finally {
if (out != null)
out.close();
}
}
use of org.eclipse.rdf4j.rio.RDFFormat in project rdf4j by eclipse.
the class HTTPRepositoryConnection method add.
public void add(URL url, String baseURI, RDFFormat dataFormat, Resource... contexts) throws IOException, RDFParseException, RepositoryException {
if (baseURI == null) {
baseURI = url.toExternalForm();
}
URLConnection con = url.openConnection();
// Set appropriate Accept headers
if (dataFormat != null) {
for (String mimeType : dataFormat.getMIMETypes()) {
con.addRequestProperty("Accept", mimeType);
}
} else {
Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys();
List<String> acceptParams = RDFFormat.getAcceptParams(rdfFormats, true, null);
for (String acceptParam : acceptParams) {
con.addRequestProperty("Accept", acceptParam);
}
}
InputStream in = con.getInputStream();
if (dataFormat == null) {
// Try to determine the data's MIME type
String mimeType = con.getContentType();
int semiColonIdx = mimeType.indexOf(';');
if (semiColonIdx >= 0) {
mimeType = mimeType.substring(0, semiColonIdx);
}
dataFormat = Rio.getParserFormatForMIMEType(mimeType).orElse(Rio.getParserFormatForFileName(url.getPath()).orElseThrow(Rio.unsupportedFormat(mimeType)));
}
try {
add(in, baseURI, dataFormat, contexts);
} finally {
in.close();
}
}
use of org.eclipse.rdf4j.rio.RDFFormat in project rdf4j by eclipse.
the class RDFLoader method loadZip.
private void loadZip(InputStream in, String baseURI, RDFFormat dataFormat, RDFHandler rdfHandler) throws IOException, RDFParseException, RDFHandlerException {
try (ZipInputStream zipIn = new ZipInputStream(in)) {
for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn.getNextEntry()) {
if (entry.isDirectory()) {
continue;
}
try {
RDFFormat format = Rio.getParserFormatForFileName(entry.getName()).orElse(dataFormat);
// Prevent parser (Xerces) from closing the input stream
UncloseableInputStream wrapper = new UncloseableInputStream(zipIn);
load(wrapper, baseURI, format, rdfHandler);
} catch (RDFParseException e) {
String msg = e.getMessage() + " in " + entry.getName();
RDFParseException pe = new RDFParseException(msg, e.getLineNumber(), e.getColumnNumber());
pe.initCause(e);
throw pe;
} finally {
zipIn.closeEntry();
}
}
// end for
}
}
use of org.eclipse.rdf4j.rio.RDFFormat in project rdf4j by eclipse.
the class RDFLoader method load.
/**
* Parses the RDF data that can be found at the specified URL to the
* RDFHandler.
*
* @param url
* The URL of the RDF data.
* @param baseURI
* The base URI to resolve any relative URIs that are in the data
* against. This defaults to the value of
* {@link java.net.URL#toExternalForm() url.toExternalForm()} if
* the value is set to <tt>null</tt>.
* @param dataFormat
* The serialization format of the data. If set to <tt>null</tt>,
* the format will be automatically determined by examining the
* content type in the HTTP response header, and failing that,
* the file name extension of the supplied URL.
* @param rdfHandler
* Receives RDF parser events.
* @throws IOException
* If an I/O error occurred while reading from the URL.
* @throws UnsupportedRDFormatException
* If no parser is available for the specified RDF format, or
* the RDF format could not be automatically determined.
* @throws RDFParseException
* If an error was found while parsing the RDF data.
* @throws RDFHandlerException
* If thrown by the RDFHandler
*/
public void load(URL url, String baseURI, RDFFormat dataFormat, RDFHandler rdfHandler) throws IOException, RDFParseException, RDFHandlerException {
if (baseURI == null) {
baseURI = url.toExternalForm();
}
URLConnection con = url.openConnection();
// Set appropriate Accept headers
if (dataFormat != null) {
for (String mimeType : dataFormat.getMIMETypes()) {
con.addRequestProperty("Accept", mimeType);
}
} else {
Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys();
List<String> acceptParams = RDFFormat.getAcceptParams(rdfFormats, true, null);
for (String acceptParam : acceptParams) {
con.addRequestProperty("Accept", acceptParam);
}
}
try (InputStream in = con.getInputStream()) {
if (dataFormat == null) {
// Try to determine the data's MIME type
String mimeType = con.getContentType();
int semiColonIdx = mimeType.indexOf(';');
if (semiColonIdx >= 0) {
mimeType = mimeType.substring(0, semiColonIdx);
}
dataFormat = Rio.getParserFormatForMIMEType(mimeType).orElseGet(() -> Rio.getParserFormatForFileName(url.getPath()).orElseThrow(() -> new UnsupportedRDFormatException("Could not find RDF format for URL: " + url.getPath())));
}
load(in, baseURI, dataFormat, rdfHandler);
}
}
Aggregations