Class RDFUtils


  • public class RDFUtils
    extends Object
    Basic class providing a set of utility methods when dealing with RDF.
    Author:
    Michele Mostarda (mostarda@fbk.eu), Davide Palmisano (dpalmisano@gmail.com), Jacek Grzebyta (jgrzebyta@apache.org)
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static org.eclipse.rdf4j.model.BNode bnode()  
      static org.eclipse.rdf4j.model.BNode bnode​(String id)
      Creates a BNode.
      static String fixAbsoluteIRI​(String uri)
      Fixes typical errors in an absolute org.eclipse.rdf4j.model.IRI, such as unescaped spaces.
      static String fixIRIWithException​(String unescapedIRI)
      Tries to fix a potentially broken relative or absolute URI.
      static org.eclipse.rdf4j.model.BNode getBNode​(String id)
      Creates a BNode.
      static Optional<org.eclipse.rdf4j.rio.RDFFormat> getFormatByExtension​(String ext)
      Returns a parser type from the given extension.
      static Collection<org.eclipse.rdf4j.rio.RDFFormat> getFormats()
      Returns all the available RDFFormats.
      static org.eclipse.rdf4j.rio.RDFParser getParser​(org.eclipse.rdf4j.rio.RDFFormat format)
      Creates a new RDFParser instance.
      static org.eclipse.rdf4j.rio.RDFWriter getWriter​(org.eclipse.rdf4j.rio.RDFFormat format, OutputStream os)
      Creates a new RDFWriter instance.
      static org.eclipse.rdf4j.rio.RDFWriter getWriter​(org.eclipse.rdf4j.rio.RDFFormat format, Writer writer)
      Creates a new RDFWriter instance.
      static String getXSDDate​(String dateToBeParsed, String format)
      This method allows to obtain an XML Schema compliant date providing a textual representation of a date and textual a pattern for parsing it.
      static org.eclipse.rdf4j.model.IRI iri​(String iri)
      Creates a IRI.
      static org.eclipse.rdf4j.model.IRI iri​(String namespace, String localName)
      Creates a IRI.
      static boolean isAbsoluteIRI​(String href)
      Checks if href is absolute or not.
      static org.eclipse.rdf4j.model.Literal literal​(boolean b)
      Creates a Literal.
      static org.eclipse.rdf4j.model.Literal literal​(byte b)
      Creates a Literal.
      static org.eclipse.rdf4j.model.Literal literal​(double d)
      Creates a Literal.
      static org.eclipse.rdf4j.model.Literal literal​(float f)
      Creates a Literal.
      static org.eclipse.rdf4j.model.Literal literal​(int i)
      Creates a Literal.
      static org.eclipse.rdf4j.model.Literal literal​(long l)
      Creates a Literal.
      static org.eclipse.rdf4j.model.Literal literal​(short s)
      Creates a Literal.
      static org.eclipse.rdf4j.model.Literal literal​(String s)
      Creates a Literal.
      static org.eclipse.rdf4j.model.Literal literal​(String s, String l)
      Creates a Literal.
      static org.eclipse.rdf4j.model.Literal literal​(String s, org.eclipse.rdf4j.model.IRI datatype)
      Creates a Literal.
      static org.eclipse.rdf4j.model.Value makeIRI()  
      static org.eclipse.rdf4j.model.Value makeIRI​(String inString)
      Convert string to either IRI or Literal.
      static org.eclipse.rdf4j.model.Resource makeIRI​(String type, org.eclipse.rdf4j.model.IRI docIRI)
      static org.eclipse.rdf4j.model.Resource makeIRI​(String type, org.eclipse.rdf4j.model.IRI docIRI, boolean addId)
      Creates implementation of Resource from given arguments: type and docIRI.
      static org.eclipse.rdf4j.model.Resource makeIRI​(org.eclipse.rdf4j.model.IRI docUri)
      static org.eclipse.rdf4j.model.Statement[] parseRDF​(String resource)
      Parses the content of the resource file guessing the content format from the extension.
      static org.eclipse.rdf4j.model.Statement[] parseRDF​(org.eclipse.rdf4j.rio.RDFFormat format, InputStream is)
      Parses the content of is input stream with the specified parser p using '' as base org.eclipse.rdf4j.model.IRI.
      static org.eclipse.rdf4j.model.Statement[] parseRDF​(org.eclipse.rdf4j.rio.RDFFormat format, InputStream is, String baseIRI)
      Parses the content of is input stream with the specified parser p using baseIRI.
      static org.eclipse.rdf4j.model.Statement[] parseRDF​(org.eclipse.rdf4j.rio.RDFFormat format, String in)
      Parses the content of in string with the specified parser p using '' as base org.eclipse.rdf4j.model.IRI.
      static org.eclipse.rdf4j.model.Statement quad​(String s, String p, String o, String g)
      Creates a statement of type: toValue(s), toValue(p), toValue(o), toValue(g)
      static org.eclipse.rdf4j.model.Statement quad​(org.eclipse.rdf4j.model.Resource s, org.eclipse.rdf4j.model.IRI p, org.eclipse.rdf4j.model.Value o, org.eclipse.rdf4j.model.Resource g)
      Creates a Statement.
      static org.eclipse.rdf4j.model.Value toValue​(String s)
      Creates a Value.
      static String toXSDDateTime​(Date date)
      Prints a date to the XSD datetime format.
      static org.eclipse.rdf4j.model.Statement triple​(String s, String p, String o)
      Creates a statement of type: toValue(s), toValue(p), toValue(o)
      static org.eclipse.rdf4j.model.Statement triple​(org.eclipse.rdf4j.model.Resource s, org.eclipse.rdf4j.model.IRI p, org.eclipse.rdf4j.model.Value o)
      Creates a Statement.
    • Method Detail

      • fixAbsoluteIRI

        public static String fixAbsoluteIRI​(String uri)
        Fixes typical errors in an absolute org.eclipse.rdf4j.model.IRI, such as unescaped spaces.
        Parameters:
        uri - An absolute org.eclipse.rdf4j.model.IRI, can have typical syntax errors
        Returns:
        An absolute org.eclipse.rdf4j.model.IRI that is valid against the org.eclipse.rdf4j.model.IRI syntax
        Throws:
        IllegalArgumentException - if org.eclipse.rdf4j.model.IRI is not fixable
      • toXSDDateTime

        public static String toXSDDateTime​(Date date)
        Prints a date to the XSD datetime format.
        Parameters:
        date - date to be printed.
        Returns:
        the string representation of the input date.
      • fixIRIWithException

        public static String fixIRIWithException​(String unescapedIRI)

        Tries to fix a potentially broken relative or absolute URI.

        These appear to be good rules: Remove whitespace or '\' or '"' in beginning and end Replace space with %20 Drop the triple if it matches this regex (only protocol): ^[a-zA-Z0-9]+:(//)?$ Drop the triple if it matches this regex: ^javascript: Truncate ">.*$ from end of lines (Neko didn't quite manage to fix broken markup) Drop the triple if any of these appear in the URL: <>[]|*{}"<>\
        Parameters:
        unescapedIRI - uri string to be unescaped.
        Returns:
        the unescaped string.
      • iri

        public static org.eclipse.rdf4j.model.IRI iri​(String iri)
        Creates a IRI.
        Parameters:
        iri - a base string for the IRI
        Returns:
        a valid IRI
      • iri

        public static org.eclipse.rdf4j.model.IRI iri​(String namespace,
                                                      String localName)
        Creates a IRI.
        Parameters:
        namespace - a base namespace for the IRI
        localName - a local name to associate with the namespace
        Returns:
        a valid IRI
      • literal

        public static org.eclipse.rdf4j.model.Literal literal​(String s)
        Creates a Literal.
        Parameters:
        s - string representation of the Literal
        Returns:
        valid Literal
      • literal

        public static org.eclipse.rdf4j.model.Literal literal​(boolean b)
        Creates a Literal.
        Parameters:
        b - boolean representation of the Literal
        Returns:
        valid Literal
      • literal

        public static org.eclipse.rdf4j.model.Literal literal​(byte b)
        Creates a Literal.
        Parameters:
        b - byte representation of the Literal
        Returns:
        valid Literal
      • literal

        public static org.eclipse.rdf4j.model.Literal literal​(short s)
        Creates a Literal.
        Parameters:
        s - short representation of the Literal
        Returns:
        valid Literal
      • literal

        public static org.eclipse.rdf4j.model.Literal literal​(int i)
        Creates a Literal.
        Parameters:
        i - int representation of the Literal
        Returns:
        valid Literal
      • literal

        public static org.eclipse.rdf4j.model.Literal literal​(long l)
        Creates a Literal.
        Parameters:
        l - long representation of the Literal
        Returns:
        valid Literal
      • literal

        public static org.eclipse.rdf4j.model.Literal literal​(float f)
        Creates a Literal.
        Parameters:
        f - float representation of the Literal
        Returns:
        valid Literal
      • literal

        public static org.eclipse.rdf4j.model.Literal literal​(double d)
        Creates a Literal.
        Parameters:
        d - double representation of the Literal
        Returns:
        valid Literal
      • literal

        public static org.eclipse.rdf4j.model.Literal literal​(String s,
                                                              String l)
        Creates a Literal.
        Parameters:
        s - the literal's label
        l - the literal's language
        Returns:
        valid Literal
      • literal

        public static org.eclipse.rdf4j.model.Literal literal​(String s,
                                                              org.eclipse.rdf4j.model.IRI datatype)
        Creates a Literal.
        Parameters:
        s - the literal's label
        datatype - the literal's datatype
        Returns:
        valid Literal
      • bnode

        public static org.eclipse.rdf4j.model.BNode bnode​(String id)
        Creates a BNode.
        Parameters:
        id - string representation of the BNode
        Returns:
        the valid BNode
      • bnode

        public static org.eclipse.rdf4j.model.BNode bnode()
        Returns:
        a bnode with unique id.
      • getBNode

        public static org.eclipse.rdf4j.model.BNode getBNode​(String id)
        Creates a BNode.
        Parameters:
        id - string representation of the BNode name for which we will create a md5 hash.
        Returns:
        the valid BNode
      • triple

        public static org.eclipse.rdf4j.model.Statement triple​(org.eclipse.rdf4j.model.Resource s,
                                                               org.eclipse.rdf4j.model.IRI p,
                                                               org.eclipse.rdf4j.model.Value o)
        Creates a Statement.
        Parameters:
        s - subject Resource
        p - predicate URI
        o - object Value
        Returns:
        valid Statement
      • triple

        public static org.eclipse.rdf4j.model.Statement triple​(String s,
                                                               String p,
                                                               String o)
        Creates a statement of type: toValue(s), toValue(p), toValue(o)
        Parameters:
        s - subject.
        p - predicate.
        o - object.
        Returns:
        a statement instance.
      • quad

        public static org.eclipse.rdf4j.model.Statement quad​(org.eclipse.rdf4j.model.Resource s,
                                                             org.eclipse.rdf4j.model.IRI p,
                                                             org.eclipse.rdf4j.model.Value o,
                                                             org.eclipse.rdf4j.model.Resource g)
        Creates a Statement.
        Parameters:
        s - subject.
        p - predicate.
        o - object.
        g - quad resource
        Returns:
        a statement instance.
      • quad

        public static org.eclipse.rdf4j.model.Statement quad​(String s,
                                                             String p,
                                                             String o,
                                                             String g)
        Creates a statement of type: toValue(s), toValue(p), toValue(o), toValue(g)
        Parameters:
        s - subject.
        p - predicate.
        o - object.
        g - quad resource
        Returns:
        a statement instance.
      • toValue

        public static org.eclipse.rdf4j.model.Value toValue​(String s)
        Creates a Value. If s == 'a' returns an RDF.TYPE. If s.matches('[a-z0-9]+:.*') expands the corresponding prefix using PopularPrefixes.
        Parameters:
        s - string representation of value.
        Returns:
        a value instance.
      • getFormats

        public static Collection<org.eclipse.rdf4j.rio.RDFFormat> getFormats()
        Returns all the available RDFFormats.
        Returns:
        an unmodifiable collection of formats.
      • getParser

        public static org.eclipse.rdf4j.rio.RDFParser getParser​(org.eclipse.rdf4j.rio.RDFFormat format)
        Creates a new RDFParser instance.
        Parameters:
        format - parser format.
        Returns:
        parser instance.
        Throws:
        IllegalArgumentException - if format is not supported.
      • getWriter

        public static org.eclipse.rdf4j.rio.RDFWriter getWriter​(org.eclipse.rdf4j.rio.RDFFormat format,
                                                                Writer writer)
        Creates a new RDFWriter instance.
        Parameters:
        format - output format.
        writer - data output writer.
        Returns:
        writer instance.
        Throws:
        IllegalArgumentException - if format is not supported.
      • getWriter

        public static org.eclipse.rdf4j.rio.RDFWriter getWriter​(org.eclipse.rdf4j.rio.RDFFormat format,
                                                                OutputStream os)
        Creates a new RDFWriter instance.
        Parameters:
        format - output format.
        os - output stream.
        Returns:
        writer instance.
        Throws:
        IllegalArgumentException - if format is not supported.
      • getFormatByExtension

        public static Optional<org.eclipse.rdf4j.rio.RDFFormat> getFormatByExtension​(String ext)
        Returns a parser type from the given extension.
        Parameters:
        ext - input extension.
        Returns:
        parser matching the extension.
        Throws:
        IllegalArgumentException - if no extension matches.
      • parseRDF

        public static org.eclipse.rdf4j.model.Statement[] parseRDF​(org.eclipse.rdf4j.rio.RDFFormat format,
                                                                   InputStream is,
                                                                   String baseIRI)
                                                            throws IOException
        Parses the content of is input stream with the specified parser p using baseIRI.
        Parameters:
        format - input format type.
        is - input stream containing RDF.
        baseIRI - base uri.
        Returns:
        list of statements detected within the input stream.
        Throws:
        IOException - if there is an error reading the InputStream
      • parseRDF

        public static org.eclipse.rdf4j.model.Statement[] parseRDF​(org.eclipse.rdf4j.rio.RDFFormat format,
                                                                   InputStream is)
                                                            throws IOException
        Parses the content of is input stream with the specified parser p using '' as base org.eclipse.rdf4j.model.IRI.
        Parameters:
        format - input format type.
        is - input stream containing RDF.
        Returns:
        list of statements detected within the input stream.
        Throws:
        IOException - if there is an error reading the InputStream
      • parseRDF

        public static org.eclipse.rdf4j.model.Statement[] parseRDF​(org.eclipse.rdf4j.rio.RDFFormat format,
                                                                   String in)
                                                            throws IOException
        Parses the content of in string with the specified parser p using '' as base org.eclipse.rdf4j.model.IRI.
        Parameters:
        format - input format type.
        in - input string containing RDF.
        Returns:
        list of statements detected within the input string.
        Throws:
        IOException - if there is an error reading the InputStream
      • parseRDF

        public static org.eclipse.rdf4j.model.Statement[] parseRDF​(String resource)
                                                            throws IOException
        Parses the content of the resource file guessing the content format from the extension.
        Parameters:
        resource - resource name.
        Returns:
        the statements declared within the resource file.
        Throws:
        IOException - if an error occurs while reading file.
      • isAbsoluteIRI

        public static boolean isAbsoluteIRI​(String href)
        Checks if href is absolute or not.
        Parameters:
        href - candidate org.eclipse.rdf4j.model.IRI.
        Returns:
        true if href is absolute, false otherwise.
      • makeIRI

        public static org.eclipse.rdf4j.model.Resource makeIRI​(org.eclipse.rdf4j.model.IRI docUri)
        Parameters:
        docUri - It is a namespace. If it ends with '/' character than stays unchanged otherwise the hash character '#' is added to the end.
        Returns:
        instance of Resource.
      • makeIRI

        public static org.eclipse.rdf4j.model.Resource makeIRI​(String type,
                                                               org.eclipse.rdf4j.model.IRI docIRI,
                                                               boolean addId)
        Creates implementation of Resource from given arguments: type and docIRI. NB: The Java Naming Conventions is described by GeeksForGeeks.
        Parameters:
        type - This argument is converted following Java naming conventions with StringUtils.implementJavaNaming(java.lang.String).
        docIRI - It is a namespace. If it ends with '/' character than stays unchanged otherwise the hash character '#' is added to the end.
        addId - If argument is TRUE than the node identifier is added to the end formated '_{int}'.
        Returns:
        instance of Resource.
      • makeIRI

        public static org.eclipse.rdf4j.model.Value makeIRI​(String inString)
        Convert string to either IRI or Literal. If string value expresses valid IRI than IRI is created. Otherwise method creates simple Literal xsd:string.
        Parameters:
        inString - an input string to manifest as Value
        Returns:
        either IRI or Literal.
      • makeIRI

        public static org.eclipse.rdf4j.model.Value makeIRI()