public static void main(String[] args) throws IOException, RDFParseException, RDFHandlerException, UnsupportedRDFormatException { if (args.length < 2) { System.out.println("Usage: java org.eclipse.rdf4j.rio.Rio <inputFile> <outputFile>"); System.exit(1); return; } String inputFile = args[0]; String outputFile = args[1]; try (FileOutputStream outStream = new FileOutputStream(outputFile); FileInputStream inStream = new FileInputStream(inputFile);) { createParser(getParserFormatForFileName(inputFile).orElse(RDFFormat.RDFXML)).setRDFHandler( createWriter(getWriterFormatForFileName(outputFile).orElse(RDFFormat.RDFXML), outStream)).parse(inStream, "file:" + inputFile); } }
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); } } }
@Override public void add(File file, String baseURI, RDFFormat dataFormat, Resource... contexts) throws IOException, RDFParseException, RepositoryException { if (baseURI == null) { // default baseURI to file baseURI = file.toURI().toString(); } if (dataFormat == null) { dataFormat = Rio.getParserFormatForFileName(file.getName()).orElseThrow( Rio.unsupportedFormat(file.getName())); } try (InputStream in = new FileInputStream(file)) { add(in, baseURI, dataFormat, contexts); } }
/** * Creates a new {@link RDFParser} instance. * * @param format parser format. * @return parser instance. * @throws IllegalArgumentException if format is not supported. */ public static RDFParser getParser(RDFFormat format) { return Rio.createParser(format); }
mimeType = mimeType.substring(0, semiColonIdx); dataFormat = Rio.getParserFormatForMIMEType(mimeType).orElse( Rio.getParserFormatForFileName(url.getPath()).orElseThrow( Rio.unsupportedFormat(mimeType)));
private void translateRDFDocument(final InputStream in, final OutputStream out, final RDFFormat inFormat, final RDFFormat outFormat, final String baseURI) throws SailException, IOException, RDFHandlerException, RDFParseException { RDFParser p = Rio.createParser(inFormat); RDFWriter w = Rio.createWriter(outFormat, out); p.setRDFHandler(w); p.parse(in, baseURI); } }
private Model loadModel(String fileName) throws Exception { URL modelURL = this.getClass().getResource(TESTCASES_DIR + fileName); assertNotNull("Test file not found: " + fileName, modelURL); Model model = createEmptyModel(); Optional<RDFFormat> rdfFormat = Rio.getParserFormatForFileName(fileName); assertTrue("Unable to determine RDF format for file: " + fileName, rdfFormat.isPresent()); RDFParser parser = Rio.createParser(rdfFormat.get()); parser.setDatatypeHandling(DatatypeHandling.IGNORE); parser.setPreserveBNodeIDs(true); parser.setRDFHandler(new StatementCollector(model)); InputStream in = modelURL.openStream(); try { parser.parse(in, modelURL.toString()); return model; } finally { in.close(); } }
/** * Creates a new {@link RDFWriter} instance. * * @param format output format. * @param os output stream. * @return writer instance. * @throws IllegalArgumentException if format is not supported. */ public static RDFWriter getWriter(RDFFormat format, OutputStream os) { return Rio.createWriter(format, os); }
/** * Returns a parser type from the given extension. * * @param ext input extension. * @return parser matching the extension. * @throws IllegalArgumentException if no extension matches. */ public static Optional<RDFFormat> getFormatByExtension(String ext) { if( ! ext.startsWith(".") ) ext = "." + ext; return Rio.getParserFormatForFileName(ext); }
private void init(Result<Description> descriptionResult, Interpreter interpreter) { if (descriptionResult.getContent().isPresent()) { Description description = descriptionResult.getContent().get(); String mimeType = description.getDescribedByLink().getType().orElse(null); Optional<RDFFormat> maybeFormat = Rio.getParserFormatForMIMEType(mimeType); if (!maybeFormat.isPresent()) { String filename = descriptionResult.getUri().toString(); maybeFormat = Rio.getParserFormatForFileName(filename); } if (maybeFormat.isPresent()) { createDescriptionNode(description, maybeFormat.get(), interpreter); } else { rawContent = description.getRawContent(); } } }
@Override public void importRdf(CachedLog input, String baseUri, String defaultGraph, RdfProcessor rdfProcessor) throws RdfProcessingFailedException, RdfProcessingParseException { 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 e) { throw new RdfProcessingFailedException(e); } catch (RDFParseException e) { throw new Rdf4jRdfProcessingParseException(e, input); } catch (UnsupportedRDFormatException e) { throw new RdfProcessingFailedException(e); } catch (RDFHandlerException e) { if (e.getCause() instanceof RdfProcessingFailedException) { throw (RdfProcessingFailedException) e.getCause(); } else { throw new RdfProcessingFailedException(e); } } } }
public static String asString(Graph graph) throws RDFHandlerException { OutputStream stream = new ByteArrayOutputStream(); Rio.write(graph, stream, RDFFormat.JSONLD); return stream.toString(); }
/** * Parses the content of the <code>resource</code> file * guessing the content format from the extension. * * @param resource resource name. * @return the statements declared within the resource file. * @throws java.io.IOException if an error occurs while reading file. */ public static Statement[] parseRDF(String resource) throws IOException { final int extIndex = resource.lastIndexOf('.'); if(extIndex == -1) throw new IllegalArgumentException("Error while detecting the extension in resource name " + resource); final String extension = resource.substring(extIndex + 1); return parseRDF( getFormatByExtension(extension).orElseThrow(Rio.unsupportedFormat(extension)) , RDFUtils.class.getResourceAsStream(resource) ); }
switch (action) { case ADD: format = Rio.getParserFormatForMIMEType(request.getContentType()).orElseThrow( Rio.unsupportedFormat(request.getContentType())); transaction.add(request.getInputStream(), baseURI, format, preserveNodeIds, contexts); break; case DELETE: format = Rio.getParserFormatForMIMEType(request.getContentType()).orElseThrow( Rio.unsupportedFormat(request.getContentType())); transaction.delete(format, request.getInputStream(), baseURI);
RDFFormat rdfFormat = Rio.getParserFormatForMIMEType(mimeType).orElseThrow( () -> new ClientHTTPException(SC_UNSUPPORTED_MEDIA_TYPE, "Unsupported MIME type: " + mimeType));
private RDFParser getParser(RdfFormat rdfFormat) { if (rdfFormat == RdfFormat.RDFXML) return Rio.createParser(RDFFormat.RDFXML); else if (rdfFormat == RdfFormat.JSONLD) return Rio.createParser(RDFFormat.JSONLD); else if (rdfFormat == RdfFormat.TURTLE) return Rio.createParser(RDFFormat.TURTLE); else if (rdfFormat == RdfFormat.RDFA) return Rio.createParser(RDFFormat.RDFA); else return Rio.createParser(RDFFormat.N3); }
mimeType = mimeType.substring(0, semiColonIdx); dataFormat = Rio.getParserFormatForMIMEType(mimeType).orElse( Rio.getParserFormatForFileName(url.getPath()).orElseThrow( Rio.unsupportedFormat(mimeType)));
/** * Creates a new {@link RDFWriter} instance. * * @param format output format. * @param writer data output writer. * @return writer instance. * @throws IllegalArgumentException if format is not supported. */ public static RDFWriter getWriter(RDFFormat format, Writer writer) { return Rio.createWriter(format, writer); }
@Override public String probeContentType(Path path) throws IOException { Optional<RDFFormat> result = Rio.getParserFormatForFileName(path.getFileName().toString()); if (result.isPresent()) { return result.get().getDefaultMIMEType(); } // Specification says to return null if we could not // conclusively determine the file type return null; }