/** * 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); }
@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; }
@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; }
@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; }
@Override protected List<FileStatus> listStatus(JobContext job) throws IOException { List<FileStatus> filteredList = new ArrayList<>(); for (FileStatus fs : super.listStatus(job)) { if (Rio.getParserFormatForFileName(fs.getPath().getName()) != null) { filteredList.add(fs); } } return filteredList; }
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())); } InputStream in = new FileInputStream(file); try { add(in, baseURI, dataFormat, contexts); } finally { in.close(); } }
@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); } }
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())); } InputStream in = new FileInputStream(file); try { add(in, baseURI, dataFormat, contexts); } finally { in.close(); } }
public static void main(String[] args) throws Exception { //aGraph.read(new URL("http://xmlns.com/foaf/spec/index.rdf").openStream()); // File aOut = new File("/Users/mhgrove/work/GitHub/empire/core/src/com/clarkparsia/empire/codegen/test/"); // // generateSourceFiles("com.clarkparsia.empire.codegen.test", new File("test/data/nasa.nt").toURI().toURL(), RDFFormat.NTRIPLES, aOut); if (args.length < 4) { System.err.println("Must provide three arguments to the program, the package name, ontology URL, and the output directory for the source code.\n"); System.err.println("For example:\n"); System.err.println("\tBeanGenerator my.package.domain /usr/local/files/myontology.ttl /usr/local/code/src/my/package/domain"); return; } URL aURL; if (EmpireUtil.isURI(args[1])) { aURL = new URL(args[1]); } else { aURL = new File(args[1]).toURI().toURL(); } generateSourceFiles(args[0], aURL, Rio.getParserFormatForFileName(args[1]).orElse(null), new File(args[2])); } }
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 MIMEType guessMIMEType( String fileName, InputStream input, MIMEType mimeTypeFromMetadata ) { if (mimeTypeFromMetadata != null) { return mimeTypeFromMetadata; } final Optional<RDFFormat> parserFormatForFileName = Rio.getParserFormatForFileName(fileName); if (parserFormatForFileName.isPresent()) { return MIMEType.parse(parserFormatForFileName.get().getDefaultMIMEType()); } String extension = getExtension(fileName); if (extension == null) { // Assume index file on web server. extension = "html"; } if (extensions.containsKey(extension)) { return MIMEType.parse(extensions.get(extension)); } return null; }
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 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); } }
private void loadZip(InputStream in, String baseURI, RDFFormat dataFormat, RDFHandler rdfHandler) throws IOException, RDFParseException, RDFHandlerException { try (ZipInputStream zipIn = new ZipInputStream(in);) { for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn.getNextEntry()) { if (entry.isDirectory()) { continue; } try { RDFFormat format = Rio.getParserFormatForFileName(entry.getName()).orElse(dataFormat); // Prevent parser (Xerces) from closing the input stream UncloseableInputStream wrapper = new UncloseableInputStream(zipIn); load(wrapper, baseURI, format, rdfHandler); } catch (RDFParseException e) { String msg = e.getMessage() + " in " + entry.getName(); RDFParseException pe = new RDFParseException(msg, e.getLineNumber(), e.getColumnNumber()); pe.initCause(e); throw pe; } finally { zipIn.closeEntry(); } } // end for } }
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(); } } }
private void loadZip(InputStream in, String baseURI, RDFFormat dataFormat, RDFHandler rdfHandler) throws IOException, RDFParseException, RDFHandlerException { try (ZipInputStream zipIn = new ZipInputStream(in);) { for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn.getNextEntry()) { if (entry.isDirectory()) { continue; } try { RDFFormat format = Rio.getParserFormatForFileName(entry.getName()).orElse(dataFormat); // Prevent parser (Xerces) from closing the input stream UncloseableInputStream wrapper = new UncloseableInputStream(zipIn); load(wrapper, baseURI, format, rdfHandler); } catch (RDFParseException e) { String msg = e.getMessage() + " in " + entry.getName(); RDFParseException pe = new RDFParseException(msg, e.getLineNumber(), e.getColumnNumber()); pe.initCause(e); throw pe; } finally { zipIn.closeEntry(); } } // end for } }
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(); } }
@SuppressWarnings("resource") @Override public void importData(KnowledgeBase kb, String aFilename, InputStream aIS) throws RDFParseException, RepositoryException, IOException { if (kb.isReadOnly()) { log.warn("Knowledge base [{}] is read only, will not import!", kb.getName()); return; } InputStream is = new BufferedInputStream(aIS); try { // Stream is expected to be closed by caller of importData is = new CompressorStreamFactory().createCompressorInputStream(is); } catch (CompressorException e) { // Probably not compressed then or unknown format - just try as is. log.debug("Stream is not compressed, continue as is."); } // Detect the file format RDFFormat format = Rio.getParserFormatForFileName(aFilename).orElse(RDFFormat.RDFXML); // Load files into the repository try (RepositoryConnection conn = getConnection(kb)) { // If the RDF file contains relative URLs, then they probably start with a hash. // To avoid having two hashes here, we drop the hash from the base prefix configured // by the user. String prefix = StringUtils.removeEnd(kb.getBasePrefix(), "#"); conn.add(is, prefix, format); } }
private Set<Statement> readExpectedGraphQueryResult() throws Exception Optional<RDFFormat> rdfFormat = Rio.getParserFormatForFileName(resultFileURL);
private static void processRDF(AbstractRDFHandler rdfHandler, IRI graphURL) throws SemanticIndexException { RDFFormat rdfFormat = Rio.getParserFormatForFileName(graphURL.toString()).get(); RDFParser rdfParser = Rio.createParser(rdfFormat); ParserConfig config = rdfParser.getParserConfig(); // To emulate DatatypeHandling.IGNORE config.addNonFatalError(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES); config.addNonFatalError(BasicParserSettings.VERIFY_DATATYPE_VALUES); config.addNonFatalError(BasicParserSettings.NORMALIZE_DATATYPE_VALUES); rdfParser.setRDFHandler(rdfHandler); InputStream in = null; try { URL url = new URL(graphURL.toString()); in = url.openStream(); rdfParser.parse(in, graphURL.toString()); } catch (IOException e) { throw new SemanticIndexException(e.getMessage()); } finally { try { if (in != null) in.close(); } catch (IOException e) { throw new SemanticIndexException(e.getMessage()); } } }