Class Rio


  • public class Rio
    extends Object
    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.

    Author:
    Arjohn Kampman, Peter Ansell
    • Constructor Detail

      • Rio

        public Rio()
    • Method Detail

      • getParserFormatForMIMEType

        public static Optional<RDFFormat> getParserFormatForMIMEType​(String mimeType)
        Tries to match a MIME type against the list of RDF formats that can be parsed.
        Parameters:
        mimeType - A MIME type, e.g. "application/rdf+xml".
        Returns:
        An RDFFormat object if a match was found, or Optional.empty() otherwise.
      • getParserFormatForFileName

        public static Optional<RDFFormat> getParserFormatForFileName​(String fileName)
        Tries to match the extension of a file name against the list of RDF formats that can be parsed.
        Parameters:
        fileName - A file name.
        Returns:
        An RDFFormat object if a match was found, or Optional.empty() otherwise.
      • getWriterFormatForMIMEType

        public static Optional<RDFFormat> getWriterFormatForMIMEType​(String mimeType)
        Tries to match a MIME type against the list of RDF formats that can be written.
        Parameters:
        mimeType - A MIME type, e.g. "application/rdf+xml".
        Returns:
        An RDFFormat object if a match was found, or Optional.empty() otherwise.
      • getWriterFormatForFileName

        public static Optional<RDFFormat> getWriterFormatForFileName​(String fileName)
        Tries to match the extension of a file name against the list of RDF formats that can be written.
        Parameters:
        fileName - A file name.
        Returns:
        An RDFFormat object if a match was found, or Optional.empty() otherwise.
      • parse

        public static org.eclipse.rdf4j.model.Model parse​(InputStream in,
                                                          RDFFormat dataFormat,
                                                          org.eclipse.rdf4j.model.Resource... contexts)
                                                   throws IOException,
                                                          RDFParseException,
                                                          UnsupportedRDFormatException
        Adds RDF data from an InputStream to a Model, optionally to one or more named contexts.
        Parameters:
        in - An InputStream from which RDF data can be read.
        dataFormat - The serialization format of the data.
        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.
        Returns:
        A Model containing the parsed statements.
        Throws:
        IOException - If an I/O error occurred while reading from the input stream.
        UnsupportedRDFormatException - If no RDFParser is available for the specified RDF format.
        RDFParseException - If an error was found while parsing the RDF data.
        Since:
        3.5.0
      • parse

        public static org.eclipse.rdf4j.model.Model parse​(InputStream in,
                                                          RDFFormat dataFormat,
                                                          ParserConfig settings,
                                                          org.eclipse.rdf4j.model.Resource... contexts)
                                                   throws IOException,
                                                          RDFParseException,
                                                          UnsupportedRDFormatException
        Adds RDF data from an InputStream to a Model, optionally to one or more named contexts.
        Parameters:
        in - An InputStream from which RDF data can be read.
        dataFormat - The serialization format of the data.
        settings - The ParserConfig containing settings for configuring the parser.
        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.
        Returns:
        A Model containing the parsed statements.
        Throws:
        IOException - If an I/O error occurred while reading from the input stream.
        UnsupportedRDFormatException - If no RDFParser is available for the specified RDF format.
        RDFParseException - If an error was found while parsing the RDF data.
        Since:
        4.0.0
      • parse

        public static org.eclipse.rdf4j.model.Model parse​(InputStream in,
                                                          String baseURI,
                                                          RDFFormat dataFormat,
                                                          org.eclipse.rdf4j.model.Resource... contexts)
                                                   throws IOException,
                                                          RDFParseException,
                                                          UnsupportedRDFormatException
        Adds RDF data from an InputStream to a Model, optionally to one or more named contexts.
        Parameters:
        in - An InputStream from which RDF data can be read.
        baseURI - The base URI to resolve any relative URIs that are in the data against. May be null.
        dataFormat - The serialization format of the data.
        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.
        Returns:
        A Model containing the parsed statements.
        Throws:
        IOException - If an I/O error occurred while reading from the input stream.
        UnsupportedRDFormatException - If no RDFParser is available for the specified RDF format.
        RDFParseException - If an error was found while parsing the RDF data.
      • parse

        public static org.eclipse.rdf4j.model.Model parse​(InputStream in,
                                                          String baseURI,
                                                          RDFFormat dataFormat,
                                                          ParserConfig settings,
                                                          org.eclipse.rdf4j.model.Resource... contexts)
                                                   throws IOException,
                                                          RDFParseException,
                                                          UnsupportedRDFormatException
        Adds RDF data from an InputStream to a Model, optionally to one or more named contexts.
        Parameters:
        in - An InputStream from which RDF data can be read.
        baseURI - The base URI to resolve any relative URIs that are in the data against. May be null.
        dataFormat - The serialization format of the data.
        settings - The ParserConfig containing settings for configuring the parser.
        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.
        Returns:
        A Model containing the parsed statements.
        Throws:
        IOException - If an I/O error occurred while reading from the input stream.
        UnsupportedRDFormatException - If no RDFParser is available for the specified RDF format.
        RDFParseException - If an error was found while parsing the RDF data.
        Since:
        4.0.0
      • parse

        public static org.eclipse.rdf4j.model.Model parse​(InputStream in,
                                                          String baseURI,
                                                          RDFFormat dataFormat,
                                                          ParserConfig settings,
                                                          org.eclipse.rdf4j.model.ValueFactory valueFactory,
                                                          ParseErrorListener errors,
                                                          org.eclipse.rdf4j.model.Resource... contexts)
                                                   throws IOException,
                                                          RDFParseException,
                                                          UnsupportedRDFormatException
        Adds RDF data from an InputStream to a Model, optionally to one or more named contexts.
        Parameters:
        in - An InputStream from which RDF data can be read.
        baseURI - The base URI to resolve any relative URIs that are in the data against. May be null.
        dataFormat - The serialization format of the data.
        settings - The ParserConfig containing settings for configuring the parser.
        valueFactory - The ValueFactory used by the parser to create statements.
        errors - The ParseErrorListener used by the parser to signal errors, including errors that do not generate an RDFParseException.
        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.
        Returns:
        A Model containing the parsed statements.
        Throws:
        IOException - If an I/O error occurred while reading from the input stream.
        UnsupportedRDFormatException - If no RDFParser is available for the specified RDF format.
        RDFParseException - If an error was found while parsing the RDF data.
      • parse

        public static org.eclipse.rdf4j.model.Model parse​(InputStream in,
                                                          String baseURI,
                                                          RDFFormat dataFormat,
                                                          ParserConfig settings,
                                                          org.eclipse.rdf4j.model.ValueFactory valueFactory,
                                                          ParseErrorListener errors,
                                                          org.eclipse.rdf4j.model.ModelFactory modelFactory,
                                                          org.eclipse.rdf4j.model.Resource... contexts)
                                                   throws IOException,
                                                          RDFParseException,
                                                          UnsupportedRDFormatException
        Adds RDF data from an InputStream to a Model, optionally to one or more named contexts.
        Parameters:
        in - An InputStream from which RDF data can be read.
        baseURI - The base URI to resolve any relative URIs that are in the data against. May be null.
        dataFormat - The serialization format of the data.
        settings - The ParserConfig containing settings for configuring the parser.
        valueFactory - The ValueFactory used by the parser to create statements.
        errors - The ParseErrorListener used by the parser to signal errors, including errors that do not generate an RDFParseException.
        modelFactory - the ModelFactory used to instantiate the model that gets returned.
        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.
        Returns:
        A Model containing the parsed statements.
        Throws:
        IOException - If an I/O error occurred while reading from the input stream.
        UnsupportedRDFormatException - If no RDFParser is available for the specified RDF format.
        RDFParseException - If an error was found while parsing the RDF data.
      • parse

        public static org.eclipse.rdf4j.model.Model parse​(Reader reader,
                                                          RDFFormat dataFormat,
                                                          org.eclipse.rdf4j.model.Resource... contexts)
                                                   throws IOException,
                                                          RDFParseException,
                                                          UnsupportedRDFormatException
        Adds RDF data from a Reader to a Model, optionally to one or more named contexts. 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.
        Parameters:
        reader - A Reader from which RDF data can be read.
        dataFormat - The serialization format of the data.
        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.
        Returns:
        A Model containing the parsed statements.
        Throws:
        IOException - If an I/O error occurred while reading from the reader.
        UnsupportedRDFormatException - If no RDFParser is available for the specified RDF format.
        RDFParseException - If an error was found while parsing the RDF data.
        Since:
        3.5.0
      • parse

        public static org.eclipse.rdf4j.model.Model parse​(Reader reader,
                                                          RDFFormat dataFormat,
                                                          ParserConfig settings,
                                                          org.eclipse.rdf4j.model.Resource... contexts)
                                                   throws IOException,
                                                          RDFParseException,
                                                          UnsupportedRDFormatException
        Adds RDF data from a Reader to a Model, optionally to one or more named contexts. 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.
        Parameters:
        reader - A Reader from which RDF data can be read.
        dataFormat - The serialization format of the data.
        settings - The ParserConfig containing settings for configuring the parser.
        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.
        Returns:
        A Model containing the parsed statements.
        Throws:
        IOException - If an I/O error occurred while reading from the reader.
        UnsupportedRDFormatException - If no RDFParser is available for the specified RDF format.
        RDFParseException - If an error was found while parsing the RDF data.
        Since:
        4.0.0
      • parse

        public static org.eclipse.rdf4j.model.Model parse​(Reader reader,
                                                          String baseURI,
                                                          RDFFormat dataFormat,
                                                          org.eclipse.rdf4j.model.Resource... contexts)
                                                   throws IOException,
                                                          RDFParseException,
                                                          UnsupportedRDFormatException
        Adds RDF data from a Reader to a Model, optionally to one or more named contexts. 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.
        Parameters:
        reader - A Reader from which RDF data can be read.
        baseURI - The base URI to resolve any relative URIs that are in the data against. May be null.
        dataFormat - The serialization format of the data.
        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.
        Returns:
        A Model containing the parsed statements.
        Throws:
        IOException - If an I/O error occurred while reading from the reader.
        UnsupportedRDFormatException - If no RDFParser is available for the specified RDF format.
        RDFParseException - If an error was found while parsing the RDF data.
      • parse

        public static org.eclipse.rdf4j.model.Model parse​(Reader reader,
                                                          String baseURI,
                                                          RDFFormat dataFormat,
                                                          ParserConfig settings,
                                                          org.eclipse.rdf4j.model.ValueFactory valueFactory,
                                                          ParseErrorListener errors,
                                                          org.eclipse.rdf4j.model.Resource... contexts)
                                                   throws IOException,
                                                          RDFParseException,
                                                          UnsupportedRDFormatException
        Adds RDF data from a Reader to a Model, optionally to one or more named contexts. 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.
        Parameters:
        reader - A Reader from which RDF data can be read.
        baseURI - The base URI to resolve any relative URIs that are in the data against. May be null.
        dataFormat - The serialization format of the data.
        settings - The ParserConfig containing settings for configuring the parser.
        valueFactory - The ValueFactory used by the parser to create statements.
        errors - The ParseErrorListener used by the parser to signal errors, including errors that do not generate an RDFParseException.
        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.
        Returns:
        A Model containing the parsed statements.
        Throws:
        IOException - If an I/O error occurred while reading from the reader.
        UnsupportedRDFormatException - If no RDFParser is available for the specified RDF format.
        RDFParseException - If an error was found while parsing the RDF data.
      • parse

        public static org.eclipse.rdf4j.model.Model parse​(Reader reader,
                                                          String baseURI,
                                                          RDFFormat dataFormat,
                                                          ParserConfig settings,
                                                          org.eclipse.rdf4j.model.ValueFactory valueFactory,
                                                          ParseErrorListener errors,
                                                          org.eclipse.rdf4j.model.ModelFactory modelFactory,
                                                          org.eclipse.rdf4j.model.Resource... contexts)
                                                   throws IOException,
                                                          RDFParseException,
                                                          UnsupportedRDFormatException
        Adds RDF data from a Reader to a Model, optionally to one or more named contexts. 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.
        Parameters:
        reader - A Reader from which RDF data can be read.
        baseURI - The base URI to resolve any relative URIs that are in the data against. May be null.
        dataFormat - The serialization format of the data.
        settings - The ParserConfig containing settings for configuring the parser.
        valueFactory - The ValueFactory used by the parser to create statements.
        errors - The ParseErrorListener used by the parser to signal errors, including errors that do not generate an RDFParseException.
        modelFactory - the ModelFactory used to instantiate the model that gets returned.
        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.
        Returns:
        A Model containing the parsed statements.
        Throws:
        IOException - If an I/O error occurred while reading from the reader.
        UnsupportedRDFormatException - If no RDFParser is available for the specified RDF format.
        RDFParseException - If an error was found while parsing the RDF data.
      • write

        public static void write​(Iterable<org.eclipse.rdf4j.model.Statement> model,
                                 Writer output,
                                 RDFFormat dataFormat)
                          throws RDFHandlerException
        Writes the given statements to the given Writer in the given format.

        If the collection is a Model, its namespaces will also be written.

        Parameters:
        model - A collection of statements, such as a Model, to be written.
        output - The Writer to write the statements to.
        dataFormat - The RDFFormat to use when writing the statements.
        Throws:
        RDFHandlerException - Thrown if there is an error writing the statements.
        UnsupportedRDFormatException - If no RDFWriter is available for the specified RDF format.
      • write

        public static void write​(Iterable<org.eclipse.rdf4j.model.Statement> model,
                                 Writer output,
                                 RDFFormat dataFormat,
                                 WriterConfig settings)
                          throws RDFHandlerException
        Writes the given statements to the given Writer in the given format.

        If the collection is a Model, its namespaces will also be written.

        Parameters:
        model - A collection of statements, such as a Model, to be written.
        output - The Writer to write the statements to.
        dataFormat - The RDFFormat to use when writing the statements.
        settings - The WriterConfig containing settings for configuring the writer.
        Throws:
        RDFHandlerException - Thrown if there is an error writing the statements.
        UnsupportedRDFormatException - If no RDFWriter is available for the specified RDF format.
      • write

        public static void write​(Iterable<org.eclipse.rdf4j.model.Statement> model,
                                 RDFHandler writer)
                          throws RDFHandlerException
        Writes the given statements to the given RDFHandler.

        If the collection is a Model, its namespaces will also be written.

        Parameters:
        model - A collection of statements, such as a Model, to be written.
        writer -
        Throws:
        RDFHandlerException - Thrown if there is an error writing the statements.
      • write

        public static void write​(org.eclipse.rdf4j.model.Statement statement,
                                 Writer output,
                                 RDFFormat dataFormat)
                          throws RDFHandlerException
        Writes the given single statement to the given Writer in the given format.

        Parameters:
        statement - A statement to be written.
        output - The Writer to write the statement to.
        dataFormat - The RDFFormat to use when writing the statement.
        Throws:
        RDFHandlerException - Thrown if there is an error writing the statement.
        UnsupportedRDFormatException - If no RDFWriter is available for the specified RDF format.
      • write

        public static void write​(org.eclipse.rdf4j.model.Statement statement,
                                 RDFHandler writer)
                          throws RDFHandlerException
        Writes the given single statement to the given RDFHandler.

        Parameters:
        statement - A statement, to be written.
        writer -
        Throws:
        RDFHandlerException - Thrown if there is an error writing the statement.
      • unsupportedFormat

        public static Supplier<UnsupportedRDFormatException> unsupportedFormat​(RDFFormat unsupportedFormat)
        Helper method to use to create a lambda for Optional.orElseThrow(Supplier) to indicate a format is unsupported.
        Parameters:
        unsupportedFormat - The format that was not found.
        Returns:
        A lambda that can be used to generate an exception if the format is not found.
      • unsupportedFormat

        public static Supplier<UnsupportedRDFormatException> unsupportedFormat​(String unsupportedFormat)
        Helper method to use to create a lambda for Optional.orElseThrow(Supplier) to indicate a format is unsupported.
        Parameters:
        unsupportedFormat - The format that was not found.
        Returns:
        A lambda that can be used to generate an exception if the format is not found.