org.eclipse.rdf4j.rio.Rio.getParserFormatForFileName()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(12.4k)|赞(0)|评价(0)|浏览(126)

本文整理了Java中org.eclipse.rdf4j.rio.Rio.getParserFormatForFileName方法的一些代码示例,展示了Rio.getParserFormatForFileName的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Rio.getParserFormatForFileName方法的具体详情如下:
包路径:org.eclipse.rdf4j.rio.Rio
类名称:Rio
方法名:getParserFormatForFileName

Rio.getParserFormatForFileName介绍

[英]Tries to match the extension of a file name against the list of RDF formats that can be parsed.
[中]尝试将文件名的扩展名与可解析的RDF格式列表相匹配。

代码示例

代码示例来源:origin: org.apache.any23/apache-any23-core

/**
 * 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);
}

代码示例来源:origin: org.eclipse.rdf4j/rdf4j-client

@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;
}

代码示例来源:origin: eclipse/rdf4j

@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;
}

代码示例来源:origin: org.eclipse.rdf4j/rdf4j-rio-api

@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;
}

代码示例来源:origin: Merck/Halyard

@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;
}

代码示例来源:origin: org.eclipse.rdf4j/rdf4j-client

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();
  }
}

代码示例来源:origin: eclipse/rdf4j

@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);
  }
}

代码示例来源:origin: org.eclipse.rdf4j/rdf4j-repository-http

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();
  }
}

代码示例来源:origin: org.streampipes/streampipes-empire-core

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]));
  }
}

代码示例来源:origin: org.eclipse.rdf4j/rdf4j-rio-api

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);
  }
}

代码示例来源:origin: org.apache.any23/apache-any23-mime

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;
}

代码示例来源:origin: org.eclipse.rdf4j/rdf4j-client

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);
  }
}

代码示例来源:origin: eclipse/rdf4j

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);
  }
}

代码示例来源:origin: eclipse/rdf4j

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
  }
}

代码示例来源:origin: HuygensING/timbuctoo

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();
  }
 }
}

代码示例来源:origin: org.eclipse.rdf4j/rdf4j-client

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
  }
}

代码示例来源:origin: org.eclipse.rdf4j/rdf4j-model-testsuite

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();
  }
}

代码示例来源:origin: de.tudarmstadt.ukp.inception.app/inception-kb

@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);
  }
}

代码示例来源:origin: ontop/ontop

private Set<Statement> readExpectedGraphQueryResult()
  throws Exception
  Optional<RDFFormat> rdfFormat = Rio.getParserFormatForFileName(resultFileURL);

代码示例来源:origin: ontop/ontop

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());
    }
  }
}

相关文章