public static Model parse(InputStream inputStream, RDFFormat format) { try (InputStream is = inputStream) { return Rio.parse(is, "http://none.com/", format); } catch (IOException e) { throw new RuntimeException("failed to parse input stream [" + inputStream + "] as [" + format + "]", e); } } }
public static Model parse(String resource, RDFFormat format) { try (InputStream input = IoUtils.class.getClassLoader().getResourceAsStream(resource)) { return Rio.parse(input, "http://none.com/", format); } catch (IOException e) { throw new RuntimeException("failed to parse resource [" + resource + "] as [" + format + "]", e); } }
public static void main(String[] args) throws Exception { InputStream resourceAsStream = new FileInputStream(args[0]); Model model = Rio.parse(resourceAsStream, "", RDFFormat.NQUADS); Rio.write(model, new FileOutputStream(args[1]), RDFFormat.TURTLE); } }
protected static void read(Model repos, String ontology) throws RDFParseException, IOException { String filename = "onto/" + ontology; InputStream ins = OntoReader.class.getClassLoader() .getResourceAsStream(filename); if(filename.endsWith(".ttl")) { repos.addAll(Rio.parse(ins, "",RDFFormat.TURTLE)); } else { repos.addAll(Rio.parse(ins, "",RDFFormat.RDFXML)); } } }
protected static void read(Model repos, String ontology) throws RDFParseException, IOException { String filename = "onto/" + ontology; InputStream ins = OntoReader.class.getClassLoader() .getResourceAsStream(filename); if(filename.endsWith(".ttl")) { repos.addAll(Rio.parse(ins, "",RDFFormat.TURTLE)); } else { repos.addAll(Rio.parse(ins, "",RDFFormat.RDFXML)); } } }
public RdfDescriptionSaver(File descriptionFile, String baseUri, ImportStatus importStatus) throws IOException, ParserConfigurationException, SAXException { this.baseUri = baseUri; descriptionFile.createNewFile(); this.descriptionFile = descriptionFile; inputStream = new FileInputStream(descriptionFile); if (inputStream.available() > 0) { model = Rio.parse(inputStream, baseUri, RDFFormat.RDFXML); } else { model = new TreeModel(); } this.importStatus = importStatus; }
private RepositoryConfig getSystemConfig() { URL ttl = this.getClass().getClassLoader().getResource(CONFIG_SYSTEM_TTL); if (ttl == null) { return null; } try (InputStream in = ttl.openStream()) { Model model = Rio.parse(in, ttl.toString(), RDFFormat.TURTLE); return RepositoryConfigUtil.getRepositoryConfig(model, ID); } catch (IOException e) { throw new RepositoryConfigException(e); } }
private RepositoryConfig getSystemConfig() { URL ttl = this.getClass().getClassLoader().getResource(CONFIG_SYSTEM_TTL); if (ttl == null) { return null; } try (InputStream in = ttl.openStream()) { Model model = Rio.parse(in, ttl.toString(), RDFFormat.TURTLE); return RepositoryConfigUtil.getRepositoryConfig(model, ID); } catch (IOException e) { throw new RepositoryConfigException(e); } }
public static Model extractStatements(String graphData) throws SepaParseException { try { return Rio.parse(getGraphDataAsStream(graphData), "", RDFFormat.JSONLD); } catch (RDFParseException | UnsupportedRDFormatException | IOException e) { throw new SepaParseException(); } }
@Override public <T> T fromJsonLd(String json, Class<T> destination) throws RDFParseException, UnsupportedRDFormatException, IOException, RepositoryException { InputStream stream = new ByteArrayInputStream( json.getBytes(StandardCharsets.UTF_8)); Model statements; statements = Rio.parse(stream, "", RDFFormat.JSONLD); return makeRdfMapper() .readValue(statements, destination, getResource(statements)); }
public static Model extractStatements(String graphData) throws SepaParseException { try { return Rio.parse(getGraphDataAsStream(graphData), "", RDFFormat.JSONLD); } catch (RDFParseException | UnsupportedRDFormatException | IOException e) { throw new SepaParseException(); } }
private void createDescriptionNode(Description description, RDFFormat parseFormat, Interpreter interpreter) { try { Model model = Rio.parse(IOUtils.toInputStream(description.getRawContent()), "", parseFormat); ByteArrayOutputStream out = new ByteArrayOutputStream(); Rio.write(model, out, RDFFormat.JSONLD); ObjectMapper mapper = new ObjectMapper(); content = mapper.readTree(out.toString()); } catch (Exception e) { // catch all e.g. org.xml.sax.SAXParseException rawContent = description.getRawContent(); error = new ErrorView(e, interpreter); } } }
File configFile = new File(dataDir, CFG_FILE); try (InputStream input = new FileInputStream(configFile)) { Model model = Rio.parse(input, configFile.toURI().toString(), CONFIG_FORMAT); Set<String> repositoryIDs = RepositoryConfigUtil.getRepositoryIDs(model); if (repositoryIDs.isEmpty()) {
/** * Adds RDF data from an {@link InputStream} to a {@link Model}, optionally to one or more named contexts. * * @param in * An InputStream from which RDF data can be read. * @param baseURI * The base URI to resolve any relative URIs that are in the data against. * @param dataFormat * The serialization format of the data. * @param contexts * The contexts to add the data to. If one or more contexts are supplied the method ignores * contextual information in the actual data. If no contexts are supplied the contextual * information in the input stream is used, if no context information is available the data is * added without any context. * @return A {@link Model} containing the parsed statements. * @throws IOException * If an I/O error occurred while reading from the input stream. * @throws UnsupportedRDFormatException * If no {@link RDFParser} is available for the specified RDF format. * @throws RDFParseException * If an error was found while parsing the RDF data. */ public static Model parse(InputStream in, String baseURI, RDFFormat dataFormat, Resource... contexts) throws IOException, RDFParseException, UnsupportedRDFormatException { return parse(in, baseURI, dataFormat, new ParserConfig(), SimpleValueFactory.getInstance(), new ParseErrorLogger(), contexts); }
/** * Adds RDF data from a {@link Reader} to a {@link Model}, optionally to one or more named contexts. * <b>Note: using a Reader to upload byte-based data means that you have to be careful not to destroy the * data's character encoding by enforcing a default character encoding upon the bytes. If possible, adding * such data using an InputStream is to be preferred.</b> * * @param reader * A Reader from which RDF data can be read. * @param baseURI * The base URI to resolve any relative URIs that are in the data against. * @param dataFormat * The serialization format of the data. * @param contexts * The contexts to add the data to. If one or more contexts are specified the data is added to * these contexts, ignoring any context information in the data itself. * @return A {@link Model} containing the parsed statements. * @throws IOException * If an I/O error occurred while reading from the reader. * @throws UnsupportedRDFormatException * If no {@link RDFParser} is available for the specified RDF format. * @throws RDFParseException * If an error was found while parsing the RDF data. */ public static Model parse(Reader reader, String baseURI, RDFFormat dataFormat, Resource... contexts) throws IOException, RDFParseException, UnsupportedRDFormatException { return parse(reader, baseURI, dataFormat, new ParserConfig(), SimpleValueFactory.getInstance(), new ParseErrorLogger(), contexts); }
/** * Adds RDF data from a {@link Reader} to a {@link Model}, optionally to one or more named contexts. * <b>Note: using a Reader to upload byte-based data means that you have to be careful not to destroy the * data's character encoding by enforcing a default character encoding upon the bytes. If possible, adding * such data using an InputStream is to be preferred.</b> * * @param reader * A Reader from which RDF data can be read. * @param baseURI * The base URI to resolve any relative URIs that are in the data against. * @param dataFormat * The serialization format of the data. * @param contexts * The contexts to add the data to. If one or more contexts are specified the data is added to * these contexts, ignoring any context information in the data itself. * @return A {@link Model} containing the parsed statements. * @throws IOException * If an I/O error occurred while reading from the reader. * @throws UnsupportedRDFormatException * If no {@link RDFParser} is available for the specified RDF format. * @throws RDFParseException * If an error was found while parsing the RDF data. */ public static Model parse(Reader reader, String baseURI, RDFFormat dataFormat, Resource... contexts) throws IOException, RDFParseException, UnsupportedRDFormatException { return parse(reader, baseURI, dataFormat, new ParserConfig(), SimpleValueFactory.getInstance(), new ParseErrorLogger(), contexts); }
/** * Adds RDF data from an {@link InputStream} to a {@link Model}, optionally to one or more named contexts. * * @param in * An InputStream from which RDF data can be read. * @param baseURI * The base URI to resolve any relative URIs that are in the data against. * @param dataFormat * The serialization format of the data. * @param contexts * The contexts to add the data to. If one or more contexts are supplied the method ignores * contextual information in the actual data. If no contexts are supplied the contextual * information in the input stream is used, if no context information is available the data is * added without any context. * @return A {@link Model} containing the parsed statements. * @throws IOException * If an I/O error occurred while reading from the input stream. * @throws UnsupportedRDFormatException * If no {@link RDFParser} is available for the specified RDF format. * @throws RDFParseException * If an error was found while parsing the RDF data. */ public static Model parse(InputStream in, String baseURI, RDFFormat dataFormat, Resource... contexts) throws IOException, RDFParseException, UnsupportedRDFormatException { return parse(in, baseURI, dataFormat, new ParserConfig(), SimpleValueFactory.getInstance(), new ParseErrorLogger(), contexts); }
/** * Adds RDF data from an {@link InputStream} to a {@link Model}, optionally to one or more named contexts. * * @param in * An InputStream from which RDF data can be read. * @param baseURI * The base URI to resolve any relative URIs that are in the data against. * @param dataFormat * The serialization format of the data. * @param contexts * The contexts to add the data to. If one or more contexts are supplied the method ignores * contextual information in the actual data. If no contexts are supplied the contextual * information in the input stream is used, if no context information is available the data is * added without any context. * @return A {@link Model} containing the parsed statements. * @throws IOException * If an I/O error occurred while reading from the input stream. * @throws UnsupportedRDFormatException * If no {@link RDFParser} is available for the specified RDF format. * @throws RDFParseException * If an error was found while parsing the RDF data. */ public static Model parse(InputStream in, String baseURI, RDFFormat dataFormat, Resource... contexts) throws IOException, RDFParseException, UnsupportedRDFormatException { return parse(in, baseURI, dataFormat, new ParserConfig(), SimpleValueFactory.getInstance(), new ParseErrorLogger(), contexts); }
/** * Adds RDF data from a {@link Reader} to a {@link Model}, optionally to one or more named contexts. * <b>Note: using a Reader to upload byte-based data means that you have to be careful not to destroy the * data's character encoding by enforcing a default character encoding upon the bytes. If possible, adding * such data using an InputStream is to be preferred.</b> * * @param reader * A Reader from which RDF data can be read. * @param baseURI * The base URI to resolve any relative URIs that are in the data against. * @param dataFormat * The serialization format of the data. * @param contexts * The contexts to add the data to. If one or more contexts are specified the data is added to * these contexts, ignoring any context information in the data itself. * @return A {@link Model} containing the parsed statements. * @throws IOException * If an I/O error occurred while reading from the reader. * @throws UnsupportedRDFormatException * If no {@link RDFParser} is available for the specified RDF format. * @throws RDFParseException * If an error was found while parsing the RDF data. */ public static Model parse(Reader reader, String baseURI, RDFFormat dataFormat, Resource... contexts) throws IOException, RDFParseException, UnsupportedRDFormatException { return parse(reader, baseURI, dataFormat, new ParserConfig(), SimpleValueFactory.getInstance(), new ParseErrorLogger(), contexts); }
public static void assertRDFFilesIsomorphic(File expected, File actual, RDFFormat format) throws Exception { final RDFFormat expectedFormat; final RDFFormat actualFormat; if (format == null) { final RDFParserRegistry registry = RDFParserRegistry.getInstance(); expectedFormat = registry.getFileFormatForFileName(expected.getName()).orElse(null); actualFormat = registry.getFileFormatForFileName(actual.getName()).orElse(null); assertNotNull("Can't guess the expected file format", expectedFormat); assertNotNull("Can't guess the actual file format", actualFormat); } else { expectedFormat = format; actualFormat = format; } final Model expectedModel; final Model actualModel; try (final InputStream expectedStream = new FileInputStream(expected); final InputStream actualStream = new FileInputStream(actual)) { expectedModel = Rio.parse(expectedStream, "base://", expectedFormat); actualModel = Rio.parse(actualStream, "base://", actualFormat); } assertTrue("RDF files are not isomorphic", Models.isomorphic(expectedModel, actualModel)); }