org.eclipse.rdf4j.rio.Rio类的使用及代码示例

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

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

Rio介绍

[英]Static methods for parsing and writing RDF for all available syntaxes.

It includes methods for searching for RDFFormats based on MIME types and file extensions, creating RDFParsers and RDFWriters, and directly parsing and writing.
[中]用于解析和编写所有可用语法的RDF的静态方法。
它包括基于MIME类型和文件扩展名搜索RDF格式、创建RDFParser和RDFWWriter以及直接解析和写入的方法。

代码示例

代码示例来源: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: carml/carml

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

代码示例来源: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: HuygensING/timbuctoo

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

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

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

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

mimeType = mimeType.substring(0, semiColonIdx);
dataFormat = Rio.getParserFormatForMIMEType(mimeType).orElse(
    Rio.getParserFormatForFileName(url.getPath()).orElseThrow(
        Rio.unsupportedFormat(mimeType)));

代码示例来源:origin: joshsh/sesametools

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

代码示例来源: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: org.apache.any23/apache-any23-core

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

代码示例来源: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: 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: HuygensING/timbuctoo

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

代码示例来源:origin: streampipes/streampipes-ce

public static String asString(Graph graph) throws RDFHandlerException
{
  OutputStream stream = new ByteArrayOutputStream();
  Rio.write(graph, stream, RDFFormat.JSONLD);
  return stream.toString();
}

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

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

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

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

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

RDFFormat rdfFormat = Rio.getParserFormatForMIMEType(mimeType).orElseThrow(
    () -> new ClientHTTPException(SC_UNSUPPORTED_MEDIA_TYPE,
        "Unsupported MIME type: " + mimeType));

代码示例来源:origin: org.streampipes/streampipes-storage-rdf4j

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

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

mimeType = mimeType.substring(0, semiColonIdx);
dataFormat = Rio.getParserFormatForMIMEType(mimeType).orElse(
    Rio.getParserFormatForFileName(url.getPath()).orElseThrow(
        Rio.unsupportedFormat(mimeType)));

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

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

代码示例来源: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;
}

相关文章