Search in sources :

Example 1 with StatementCollector

use of org.openrdf.rio.helpers.StatementCollector in project vcell by virtualcell.

the class JDOM2Model method addJDOM.

public void addJDOM(Element element, String baseURI) throws IOException, RDFParseException, RDFHandlerException {
    Document document = new Document((Element) element.clone());
    StringWriter stringWriter = new StringWriter();
    new XMLOutputter().output(document, stringWriter);
    StringReader stringReader = new StringReader(stringWriter.getBuffer().toString());
    RDFParser rdfParser = Rio.createParser(RDFFormat.RDFXML);
    rdfParser.setRDFHandler(new StatementCollector(model));
    rdfParser.parse(stringReader, baseURI);
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) StringWriter(java.io.StringWriter) StatementCollector(org.openrdf.rio.helpers.StatementCollector) StringReader(java.io.StringReader) Document(org.jdom.Document) RDFParser(org.openrdf.rio.RDFParser)

Example 2 with StatementCollector

use of org.openrdf.rio.helpers.StatementCollector in project vcell by virtualcell.

the class SesameRioUtil method createRDFParser.

public static RDFParser createRDFParser(Graph graph, Map<String, String> nsMap, RDFFormat format) {
    RDFParser parser = Rio.createParser(format);
    StatementCollector statementCollector = new StatementCollector(graph, nsMap);
    parser.setRDFHandler(statementCollector);
    return parser;
}
Also used : StatementCollector(org.openrdf.rio.helpers.StatementCollector) RDFParser(org.openrdf.rio.RDFParser)

Example 3 with StatementCollector

use of org.openrdf.rio.helpers.StatementCollector in project wikidata-query-rdf by wikimedia.

the class WikibaseRepository method fetchRdfForEntity.

/**
 * Fetch the RDF for some entity.
 *
 * @throws RetryableException thrown if there is an error communicating with
 *             wikibase
 */
public Collection<Statement> fetchRdfForEntity(String entityId) throws RetryableException {
    // TODO handle ?flavor=dump or whatever parameters we need
    URI uri = uris.rdf(entityId);
    long start = System.currentTimeMillis();
    log.debug("Fetching rdf from {}", uri);
    RDFParser parser = Rio.createParser(RDFFormat.TURTLE);
    StatementCollector collector = new StatementCollector();
    parser.setRDFHandler(new NormalizingRdfHandler(collector));
    HttpGet request = new HttpGet(uri);
    request.setConfig(configWithTimeout);
    try {
        try (CloseableHttpResponse response = client.execute(request)) {
            if (response.getStatusLine().getStatusCode() == 404) {
                // A delete/nonexistent page
                return Collections.emptyList();
            }
            if (response.getStatusLine().getStatusCode() >= 300) {
                throw new ContainedException("Unexpected status code fetching RDF for " + uri + ":  " + response.getStatusLine().getStatusCode());
            }
            parser.parse(new InputStreamReader(response.getEntity().getContent(), Charsets.UTF_8), uri.toString());
        }
    } catch (UnknownHostException | SocketException | SSLHandshakeException e) {
        // We want to bail on this, since it happens to be sticky for some reason
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RetryableException("Error fetching RDF for " + uri, e);
    } catch (RDFParseException | RDFHandlerException e) {
        throw new ContainedException("RDF parsing error for " + uri, e);
    }
    log.debug("Done in {} ms", System.currentTimeMillis() - start);
    return collector.getStatements();
}
Also used : SocketException(java.net.SocketException) InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) StatementCollector(org.openrdf.rio.helpers.StatementCollector) HttpGet(org.apache.http.client.methods.HttpGet) ContainedException(org.wikidata.query.rdf.tool.exception.ContainedException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) RDFParser(org.openrdf.rio.RDFParser) URI(java.net.URI) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) NormalizingRdfHandler(org.wikidata.query.rdf.tool.rdf.NormalizingRdfHandler) RetryableException(org.wikidata.query.rdf.tool.exception.RetryableException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) RDFParseException(org.openrdf.rio.RDFParseException)

Example 4 with StatementCollector

use of org.openrdf.rio.helpers.StatementCollector in project incubator-rya by apache.

the class RyaAccumuloSailFactoryTest method testCreateFromTemplateName.

@Test
public void testCreateFromTemplateName() throws Exception {
    LocalRepositoryManager repoman = new LocalRepositoryManager(Files.createTempDir());
    repoman.initialize();
    try (InputStream templateStream = RepositoryConfig.class.getResourceAsStream("RyaAccumuloSail.ttl")) {
        String template = IOUtils.toString(templateStream);
        final ConfigTemplate configTemplate = new ConfigTemplate(template);
        final Map<String, String> valueMap = ImmutableMap.<String, String>builder().put("Repository ID", "RyaAccumuloSail").put("Repository title", "RyaAccumuloSail Store").put("Rya Accumulo user", "root").put("Rya Accumulo password", "").put("Rya Accumulo instance", "dev").put("Rya Accumulo zookeepers", "zoo1,zoo2,zoo3").put("Rya Accumulo is mock", "true").build();
        final String configString = configTemplate.render(valueMap);
        // final Repository systemRepo = this.state.getManager().getSystemRepository();
        final Graph graph = new LinkedHashModel();
        final RDFParser rdfParser = Rio.createParser(RDFFormat.TURTLE);
        rdfParser.setRDFHandler(new StatementCollector(graph));
        rdfParser.parse(new StringReader(configString), RepositoryConfigSchema.NAMESPACE);
        final Resource repositoryNode = GraphUtil.getUniqueSubject(graph, RDF.TYPE, RepositoryConfigSchema.REPOSITORY);
        final RepositoryConfig repConfig = RepositoryConfig.create(graph, repositoryNode);
        repConfig.validate();
        repoman.addRepositoryConfig(repConfig);
        Repository r = repoman.getRepository("RyaAccumuloSail");
        r.initialize();
    }
}
Also used : RepositoryConfig(org.openrdf.repository.config.RepositoryConfig) SailRepositoryConfig(org.openrdf.repository.sail.config.SailRepositoryConfig) InputStream(java.io.InputStream) StatementCollector(org.openrdf.rio.helpers.StatementCollector) Resource(org.openrdf.model.Resource) RDFParser(org.openrdf.rio.RDFParser) ConfigTemplate(org.openrdf.repository.config.ConfigTemplate) Repository(org.openrdf.repository.Repository) Graph(org.openrdf.model.Graph) LocalRepositoryManager(org.openrdf.repository.manager.LocalRepositoryManager) StringReader(java.io.StringReader) LinkedHashModel(org.openrdf.model.impl.LinkedHashModel) Test(org.junit.Test)

Example 5 with StatementCollector

use of org.openrdf.rio.helpers.StatementCollector in project eol-globi-data by jhpoelen.

the class LinkerTrustyNanoPubsTest method toTrigString.

private String toTrigString(InputStream rdfIn) throws IOException, RDFParseException, RDFHandlerException {
    TriGParser rdfParser = new TriGParser();
    Model model = new LinkedHashModel();
    rdfParser.setRDFHandler(new StatementCollector(model));
    rdfParser.parse(rdfIn, "http://example.com");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Rio.write(model, out, RDFFormat.TRIG);
    return new String(out.toByteArray(), "UTF-8");
}
Also used : TriGParser(org.openrdf.rio.trig.TriGParser) StatementCollector(org.openrdf.rio.helpers.StatementCollector) Model(org.openrdf.model.Model) LinkedHashModel(org.openrdf.model.impl.LinkedHashModel) LinkedHashModel(org.openrdf.model.impl.LinkedHashModel) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

StatementCollector (org.openrdf.rio.helpers.StatementCollector)5 RDFParser (org.openrdf.rio.RDFParser)4 StringReader (java.io.StringReader)2 LinkedHashModel (org.openrdf.model.impl.LinkedHashModel)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 InterruptedIOException (java.io.InterruptedIOException)1 StringWriter (java.io.StringWriter)1 SocketException (java.net.SocketException)1 URI (java.net.URI)1 UnknownHostException (java.net.UnknownHostException)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 Document (org.jdom.Document)1 XMLOutputter (org.jdom.output.XMLOutputter)1 Test (org.junit.Test)1 Graph (org.openrdf.model.Graph)1