use of org.eclipse.rdf4j.rio.UnsupportedRDFormatException in project rdf4j by eclipse.
the class SPARQLProtocolSession method execute.
protected HttpResponse execute(HttpUriRequest method) throws IOException, RDF4JException {
boolean consume = true;
if (params != null) {
method.setParams(params);
}
HttpResponse response = httpClient.execute(method, httpContext);
try {
int httpCode = response.getStatusLine().getStatusCode();
if (httpCode >= 200 && httpCode < 300 || httpCode == HttpURLConnection.HTTP_NOT_FOUND) {
consume = false;
// everything OK, control flow can continue
return response;
} else {
switch(httpCode) {
case // 401
HttpURLConnection.HTTP_UNAUTHORIZED:
throw new UnauthorizedException();
case // 503
HttpURLConnection.HTTP_UNAVAILABLE:
throw new QueryInterruptedException();
default:
ErrorInfo errInfo = getErrorInfo(response);
// Throw appropriate exception
if (errInfo.getErrorType() == ErrorType.MALFORMED_DATA) {
throw new RDFParseException(errInfo.getErrorMessage());
} else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_FILE_FORMAT) {
throw new UnsupportedRDFormatException(errInfo.getErrorMessage());
} else if (errInfo.getErrorType() == ErrorType.MALFORMED_QUERY) {
throw new MalformedQueryException(errInfo.getErrorMessage());
} else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_QUERY_LANGUAGE) {
throw new UnsupportedQueryLanguageException(errInfo.getErrorMessage());
} else if (errInfo.toString().length() > 0) {
throw new RepositoryException(errInfo.toString());
} else {
throw new RepositoryException(response.getStatusLine().getReasonPhrase());
}
}
}
} finally {
if (consume) {
EntityUtils.consumeQuietly(response.getEntity());
}
}
}
use of org.eclipse.rdf4j.rio.UnsupportedRDFormatException in project rdf4j by eclipse.
the class LocalRepositoryManager method addRepositoryConfig.
private synchronized void addRepositoryConfig(RepositoryConfig config, boolean updateSystem) {
File dataDir = getRepositoryDir(config.getID());
if (!dataDir.exists()) {
dataDir.mkdirs();
}
if (!dataDir.isDirectory()) {
throw new RepositoryConfigException("Could not create directory: " + dataDir);
}
File configFile = new File(dataDir, CFG_FILE);
if (!upgraded && !configFile.exists()) {
upgraded = true;
upgrade();
}
Model model = new TreeModel();
String ns = configFile.toURI().toString() + "#";
config.export(model, SimpleValueFactory.getInstance().createIRI(ns, config.getID()));
File part = new File(configFile.getParentFile(), configFile.getName() + ".part");
try (OutputStream output = new FileOutputStream(part)) {
Rio.write(model, output, configFile.toURI().toString(), CONFIG_FORMAT, CFG_CONFIG);
} catch (IOException | RDFHandlerException | UnsupportedRDFormatException | URISyntaxException e) {
throw new RepositoryConfigException(e);
}
if (updateSystem) {
super.addRepositoryConfig(config);
}
part.renameTo(configFile);
}
use of org.eclipse.rdf4j.rio.UnsupportedRDFormatException in project opentheso by miledrousset.
the class ReadRdfFileTest method test4.
@Test
public void test4() {
// read the file 'example-data-artists.ttl' as an InputStream.
InputStream is = null;
try {
is = new FileInputStream("test_unesco - Copie.rdf");
} catch (FileNotFoundException ex) {
Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
}
Model model = null;
try {
model = Rio.parse(is, "", RDFFormat.RDFXML);
} catch (IOException ex) {
Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (RDFParseException ex) {
Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedRDFormatException ex) {
Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
}
for (Statement statement : model) {
System.out.println(statement);
}
System.out.println("\n----------------");
for (Statement st : model) {
// we want to see the object values of each statement
Value value = st.getObject();
IRI property = st.getPredicate();
if (property.getLocalName().equals("type"))
System.out.println("\n......\n");
if (property.getLocalName().equals("notation")) {
Literal title = (Literal) value;
System.out.println("notation: " + title.getLabel());
} else if (property.getLocalName().equals("lat")) {
Literal title = (Literal) value;
System.out.println("latitude : " + title.getLabel());
} else if (property.getLocalName().equals("long")) {
Literal title = (Literal) value;
System.out.println("longitude : " + title.getLabel());
} else if (property.getLocalName().equals("created")) {
Literal title = (Literal) value;
System.out.println("created : " + title.getLabel());
} else if (property.getLocalName().equals("modified")) {
Literal title = (Literal) value;
System.out.println("modified : " + title.getLabel());
} else if (value instanceof Literal) {
String pref = "";
if (property.getLocalName().equals("prefLabel"))
pref = " // pref label";
Literal title = (Literal) value;
System.out.println("language: " + title.getLanguage().orElse("unknown"));
System.out.println(" title: " + title.getLabel() + pref);
} else {
System.out.println(" **** " + value + " //// " + property.getLocalName());
if (property.getLocalName().equals("type")) {
System.out.println(" URL: " + st.getSubject());
}
}
}
Rio.write(model, System.out, RDFFormat.RDFXML);
}
use of org.eclipse.rdf4j.rio.UnsupportedRDFormatException 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);
}
}
use of org.eclipse.rdf4j.rio.UnsupportedRDFormatException in project timbuctoo by HuygensING.
the class Rdf4jRdfParser method importRdf.
@Override
public void importRdf(CachedLog input, String baseUri, String defaultGraph, RdfProcessor rdfProcessor) throws RdfProcessingFailedException {
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, defaultGraph, input.getFile().getName()));
rdfParser.parse(input.getReader(), baseUri);
} catch (IOException | RDFParseException | UnsupportedRDFormatException e) {
throw new RdfProcessingFailedException(e);
} catch (RDFHandlerException e) {
if (e.getCause() instanceof RdfProcessingFailedException) {
throw (RdfProcessingFailedException) e.getCause();
} else {
throw new RdfProcessingFailedException(e);
}
}
}
Aggregations