Search in sources :

Example 1 with Rio

use of org.eclipse.rdf4j.rio.Rio in project commons-rdf by apache.

the class RDF4JParser method parseSynchronusly.

@Override
protected void parseSynchronusly() throws IOException {
    final Optional<RDFFormat> formatByMimeType = getContentType().flatMap(Rio::getParserFormatForMIMEType);
    final String base = getBase().map(IRI::getIRIString).orElse(null);
    final ParserConfig parserConfig = getParserConfig();
    // TODO: Should we need to set anything?
    final RDFLoader loader = new RDFLoader(parserConfig, rdf4jTermFactory.getValueFactory());
    final RDFHandler rdfHandler = makeRDFHandler();
    if (getSourceFile().isPresent()) {
        // NOTE: While we could have used
        // loader.load(sourcePath.toFile()
        // if the path fs provider == FileSystems.getDefault(),
        // that RDFLoader method does not use absolute path
        // as the base URI, so to be consistent
        // we'll always do it with our own input stream
        // 
        // That means we may have to guess format by extensions:
        final Optional<RDFFormat> formatByFilename = getSourceFile().map(Path::getFileName).map(Path::toString).flatMap(Rio::getParserFormatForFileName);
        // TODO: for the excited.. what about the extension after following
        // symlinks?
        final RDFFormat format = formatByMimeType.orElse(formatByFilename.orElse(null));
        try (InputStream in = Files.newInputStream(getSourceFile().get())) {
            loader.load(in, base, format, rdfHandler);
        }
    } else if (getSourceIri().isPresent()) {
        try {
            // TODO: Handle international IRIs properly
            // (Unicode support for for hostname, path and query)
            final URL url = new URL(getSourceIri().get().getIRIString());
            // TODO: This probably does not support https:// -> http://
            // redirections
            loader.load(url, base, formatByMimeType.orElse(null), makeRDFHandler());
        } catch (final MalformedURLException ex) {
            throw new IOException("Can't handle source URL: " + getSourceIri().get(), ex);
        }
    }
    // must be getSourceInputStream then, this is guaranteed by
    // super.checkSource();
    loader.load(getSourceInputStream().get(), base, formatByMimeType.orElse(null), rdfHandler);
}
Also used : Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) Rio(org.eclipse.rdf4j.rio.Rio) IOException(java.io.IOException) ParserConfig(org.eclipse.rdf4j.rio.ParserConfig) RDFLoader(org.eclipse.rdf4j.repository.util.RDFLoader) URL(java.net.URL) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) AbstractRDFHandler(org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler) RDFHandler(org.eclipse.rdf4j.rio.RDFHandler)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 RDFLoader (org.eclipse.rdf4j.repository.util.RDFLoader)1 ParserConfig (org.eclipse.rdf4j.rio.ParserConfig)1 RDFFormat (org.eclipse.rdf4j.rio.RDFFormat)1 RDFHandler (org.eclipse.rdf4j.rio.RDFHandler)1 Rio (org.eclipse.rdf4j.rio.Rio)1 AbstractRDFHandler (org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler)1