public VocabBuilder(String filename, RDFFormat format) throws IOException, RDFParseException { Path file = Paths.get(filename); if (!Files.exists(file)) throw new FileNotFoundException(filename); if (format == null) { format = Rio.getParserFormatForFileName(filename); log.trace("detected input format from filename {}: {}", filename, format); } try (final InputStream inputStream = Files.newInputStream(file)) { log.trace("Loading input file"); model = Rio.parse(inputStream, "", format); } //import Set<Resource> owlOntologies = model.filter(null, RDF.TYPE, OWL.ONTOLOGY).subjects(); if (!owlOntologies.isEmpty()) { setPrefix(owlOntologies.iterator().next().stringValue()); } }
private Model execGraphQuery(java.net.URI target, String query) { try { log.debug("Executing SPARQL tuple query: {}", query.replaceAll("\\s*[\\r\\n]+\\s*", " ").trim()); RDFFormat format = RDFFormat.TURTLE; CloseableHttpResponse response = client.post(target, query, format.getDefaultMIMEType(),SPARQL_QUERY_MIME_TYPE); final int status = response.getStatusLine().getStatusCode(); log.debug("Request resolved with {} status code: {}", status, response.getStatusLine().getReasonPhrase()); try { if (status >= 200 && status < 300) { String entity = EntityUtils.toString(response.getEntity()); return Rio.parse(new StringReader(entity), target.toString(), RDFFormat.TURTLE, new ParserConfig(), ValueFactoryImpl.getInstance(), new ParseErrorLogger()); } else { // TODO: improve this feedback from the sdk (400, 500, etc) throw new RuntimeException("Query failed: HTTP error code " + status + ": " + response.getStatusLine().getReasonPhrase()); } } catch (RDFParseException e) { log.error("Error parsing query results: {}", e.getMessage(), e); throw new RuntimeException(e); } finally { response.close(); } } catch (IllegalArgumentException | IOException e) { throw new RuntimeException(e); } }
private Model getResource(UriBuilder uriBuilder) { RDFFormat format = RDFFormat.TURTLE; try { java.net.URI target = credentials.buildUrl(uriBuilder); log.debug("Exporting {} data from resource {}", format.getName(), target.toString()); String entity = client.get(target, format.getDefaultMIMEType()); return Rio.parse(new StringReader(entity), target.toString(), format, new ParserConfig(), ValueFactoryImpl.getInstance(), new ParseErrorLogger()); } catch (IllegalArgumentException | URISyntaxException | RDFParseException | IOException e) { if (e instanceof ClientProtocolException && "Unexpected response status: 404".compareTo(e.getMessage())==0) { //keeping old behavior, should not be silently fail (i.e. return empty model)? return new LinkedHashModel(); } else { throw new RuntimeException(e); } } }
@Override public Model exportDataset(String dataset) { RDFFormat format = RDFFormat.TURTLE; try { final java.net.URI target = credentials.buildUrl(getDatasetUriBuilder(dataset)); log.debug("Exporting {} data from dataset {}", format.getName(), dataset); final String entity = client.get(target, format.getDefaultMIMEType()); return Rio.parse(new StringReader(entity), target.toString(), format, new ParserConfig(), ValueFactoryImpl.getInstance(), new ParseErrorLogger()); } catch (IllegalArgumentException | URISyntaxException | RDFParseException | IOException e) { throw new RuntimeException(e); } }
throws IOException, RDFParseException, UnsupportedRDFormatException return parse(in, baseURI, dataFormat, new ParserConfig(), SimpleValueFactory.getInstance(), new ParseErrorLogger(), contexts);
throws IOException, RDFParseException, UnsupportedRDFormatException return parse(reader, baseURI, dataFormat, new ParserConfig(), SimpleValueFactory.getInstance(), new ParseErrorLogger(), contexts);
/** * Test method for * {@link RDFJSONUtility.kmr.scam.rest.util.RDFJSON#rdfJsonToGraph(java.lang.String)}. * * @throws IOException * @throws RDFParseException * @throws RDFHandlerException */ @Test public void testRdfJsonToGraph0() throws IOException, RDFParseException, RDFHandlerException { this.testInputFile = "example0.json"; final Model rdfJsonToGraph = Rio.parse(this.getClass().getResourceAsStream(this.testInputFile), "", RDFFormat.RDFJSON); // RDFJSONUtility.rdfJsonToHandler(new // InputStreamReader(this.getClass().getResourceAsStream(this.testInputFile), // StandardCharsets.UTF_8), new StatementCollector(rdfJsonToGraph), // ValueFactoryImpl.getInstance()); Assert.assertEquals(12, rdfJsonToGraph.size()); }
/** * Test method for * {@link RDFJSONUtility.kmr.scam.rest.util.RDFJSON#rdfJsonToGraph(java.lang.String)}. * * @throws IOException * @throws RDFParseException * @throws RDFHandlerException */ @Test public void testRdfJsonToGraph5() throws IOException, RDFParseException, RDFHandlerException { this.testInputFile = "example5.json"; final Model rdfJsonToGraph = Rio.parse(this.getClass().getResourceAsStream(this.testInputFile), "", RDFFormat.RDFJSON); // RDFJSONUtility.rdfJsonToHandler(new // InputStreamReader(this.getClass().getResourceAsStream(this.testInputFile), // StandardCharsets.UTF_8), new StatementCollector(rdfJsonToGraph), // ValueFactoryImpl.getInstance()); Assert.assertEquals(1, rdfJsonToGraph.size()); }
public static Map<String,Metadata> parseRDFJSON(InputStream data) throws ParseException { try { Model model = Rio.parse(data, "", RDFFormat.RDFJSON); // convert Sesame Model into a map to Metadata objects Map<String,Metadata> result = new HashMap<String, Metadata>(); for(Resource subject : model.subjects()) { Metadata m = new Metadata(subject.stringValue()); for(org.openrdf.model.URI property : model.filter(subject, null, null).predicates()) { Set<RDFNode> propValue = new HashSet<RDFNode>(); for(Value value : model.filter(subject, property, null).objects()) { propValue.add(parseRDFJSONNode(value)); } m.put(property.stringValue(),propValue); } result.put(subject.stringValue(),m); } return result; } catch (IOException e) { throw new ParseException("could not parse JSON data",e); } catch(RDFParseException e) { throw new ParseException("could not parse JSON data",e); } catch(UnsupportedRDFormatException e) { throw new ParseException("could not parse JSON data",e); } }
public static Map<String,Metadata> parseRDFJSON(InputStream data) throws ParseException { try { Model model = Rio.parse(data, "", RDFFormat.RDFJSON); // convert Sesame Model into a map to Metadata objects Map<String,Metadata> result = new HashMap<String, Metadata>(); for(Resource subject : model.subjects()) { Metadata m = new Metadata(subject.stringValue()); for(org.openrdf.model.URI property : model.filter(subject, null, null).predicates()) { Set<RDFNode> propValue = new HashSet<RDFNode>(); for(Value value : model.filter(subject, property, null).objects()) { propValue.add(parseRDFJSONNode(value)); } m.put(property.stringValue(),propValue); } result.put(subject.stringValue(),m); } return result; } catch (IOException e) { throw new ParseException("could not parse JSON data",e); } catch(RDFParseException e) { throw new ParseException("could not parse JSON data",e); } catch(UnsupportedRDFormatException e) { throw new ParseException("could not parse JSON data",e); } }
Rio.parse(inputReader, baseUri, Rio.getParserFormatForMIMEType(mimeType, RDFFormat.RDFXML));