Class NTriplesUtil

  • Direct Known Subclasses:
    NTriplesUtil

    public class NTriplesUtil
    extends java.lang.Object
    Utility methods for N-Triples encoding/decoding.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  NTriplesUtil.TripleMatch  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String BNODE  
      private static java.lang.String BNODE_ID  
      private static java.util.regex.Pattern BNODE_ID_PATTERN  
      private static java.util.regex.Pattern BNODE_PATTERN  
      private static java.lang.String ECHAR  
      private static java.lang.String HEX  
      private static java.lang.String IRI  
      private static java.util.regex.Pattern IRI_PATTERN  
      private static java.lang.String LANGTAG  
      private static java.lang.String LITERAL  
      private static java.util.regex.Pattern LITERAL_PATTERN  
      private static java.lang.String PN_CHARS  
      private static java.lang.String PN_CHARS_BASE  
      private static java.lang.String PN_CHARS_U  
      private static java.lang.String STRING_LITERAL_QUOTE  
      private static java.lang.String UCHAR  
    • Constructor Summary

      Constructors 
      Constructor Description
      NTriplesUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static void append​(BNode bNode, java.lang.Appendable appendable)
      Appends the N-Triples representation of the given BNode to the given Appendable.
      static void append​(IRI uri, java.lang.Appendable appendable)
      Appends the N-Triples representation of the given IRI to the given Appendable.
      static void append​(IRI uri, java.lang.Appendable appendable, boolean escapeUnicode)
      Appends the N-Triples representation of the given IRI to the given Appendable.
      static void append​(Literal lit, java.lang.Appendable appendable)
      Appends the N-Triples representation of the given Literal to the given Appendable.
      static void append​(Literal lit, java.lang.Appendable appendable, boolean xsdStringToPlainLiteral, boolean escapeUnicode)
      Appends the N-Triples representation of the given Literal to the given Appendable, optionally ignoring the xsd:string datatype as it is implied for RDF-1.1.
      static void append​(Resource resource, java.lang.Appendable appendable)
      Appends the N-Triples representation of the given Resource to the given Appendable.
      static void append​(Triple triple, java.lang.Appendable appendable)
      Appends the N-Triples (non-standard) representation of the given Triple to the given Appendable.
      static void append​(Value value, java.lang.Appendable appendable)
      Appends the N-Triples representation of the given Value to the given Appendable.
      static void append​(Value value, java.lang.Appendable appendable, boolean xsdStringToPlainLiteral, boolean escapeUnicode)
      Appends the N-Triples representation of the given Value to the given Appendable, optionally not serializing the datatype a Literal with the xsd:string datatype as it is implied for RDF-1.1.
      static java.lang.String escapeString​(java.lang.String label)
      Escapes a Unicode string to an all-ASCII character sequence.Any special characters are escaped using backslashes ( " becomes \", etc.), and non-ascii/non-printable characters are escaped using Unicode escapes ( \uxxxx and \Uxxxxxxxx).
      static void escapeString​(java.lang.String label, java.lang.Appendable appendable)
      Escapes a Unicode string to an all-ASCII character sequence.
      static void escapeString​(java.lang.String label, java.lang.Appendable appendable, boolean escapeUnicode)
      Escapes a Unicode string to an N-Triples compatible character sequence.Any special characters are escaped using backslashes (" becomes \", etc.), and non-ascii/non-printable characters are escaped using Unicode escapes (\uxxxx and \Uxxxxxxxx) if the option is selected.
      private static int findEndOfLabel​(java.lang.String nTriplesLiteral)
      Finds the end of the label in a literal string.
      static boolean isDot​(int c)
      Checks whether the supplied character is Dot '.'.
      static boolean isLetter​(int c)
      Deprecated.
      static boolean isLetterOrNumber​(int c)
      static boolean isLiberalCharactersButNotDot​(int c)
      Checks whether the supplied character is in list of liberal characters according to the N-Triples specification except Dot.
      static boolean isNumber​(int c)
      Deprecated.
      static boolean isUnderscore​(int c)
      Checks whether the supplied character is Underscore.
      static boolean isValidCharacterForBNodeLabel​(int c)
      Checks whether the supplied character is valid character as per N-Triples specification.
      static BNode parseBNode​(java.lang.String nTriplesBNode, ValueFactory valueFactory)
      Parses an N-Triples bNode, creates an object for it using the supplied ValueFactory and returns this object.
      static Literal parseLiteral​(java.lang.String nTriplesLiteral, ValueFactory valueFactory)
      Parses an N-Triples literal, creates an object for it using the supplied ValueFactory and returns this object.
      static Resource parseResource​(java.lang.String nTriplesResource, ValueFactory valueFactory)
      Parses an N-Triples resource, creates an object for it using the supplied ValueFactory and returns this object.
      static Triple parseTriple​(java.lang.String nTriplesTriple, ValueFactory valueFactory)
      Parses an RDF-star triple (non-standard N-Triples), creates an object for it using the supplied ValueFactory and returns this object.
      private static NTriplesUtil.TripleMatch parseTripleInternal​(java.lang.String nTriplesTriple, ValueFactory valueFactory)
      Parses an RDF-star triple (non-standard N-Triples), creates an object for it using the supplied ValueFactory and returns an object that contains the parsed triple and the length of the parsed text.
      static IRI parseURI​(java.lang.String nTriplesURI, ValueFactory valueFactory)
      Parses an N-Triples URI, creates an object for it using the supplied ValueFactory and returns this object.
      static Value parseValue​(java.lang.String nTriplesValue, ValueFactory valueFactory)
      Parses an N-Triples value, creates an object for it using the supplied ValueFactory and returns this object.
      static java.lang.String toHexString​(int decimal, int stringLength)
      Converts a decimal value to a hexadecimal string representation of the specified length.
      static java.lang.String toNTriplesString​(BNode bNode)
      Creates an N-Triples string for the supplied blank node.
      static java.lang.String toNTriplesString​(IRI uri)
      Creates an N-Triples string for the supplied URI.
      static java.lang.String toNTriplesString​(Literal lit)
      Creates an N-Triples string for the supplied literal.
      static java.lang.String toNTriplesString​(Literal lit, boolean xsdStringToPlainLiteral)
      Creates an N-Triples string for the supplied literal, optionally ignoring the xsd:string datatype as it is implied for RDF-1.1.
      static java.lang.String toNTriplesString​(Resource resource)
      Creates an N-Triples string for the supplied resource.
      static java.lang.String toNTriplesString​(Triple triple)
      Creates an N-Triples (non-standard) string for the supplied RDF-star triple.
      static java.lang.String toNTriplesString​(Value value)
      Creates an N-Triples string for the supplied value.
      static java.lang.String toNTriplesString​(Value value, boolean xsdStringToPlainLiteral)
      Creates an N-Triples string for the supplied value.If the supplied value is a Literal, it optionally ignores the xsd:string datatype, since this datatype is implicit in RDF-1.1.
      static java.lang.String unescapeString​(java.lang.String s)
      Unescapes an escaped Unicode string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • STRING_LITERAL_QUOTE

        private static final java.lang.String STRING_LITERAL_QUOTE
        See Also:
        Constant Field Values
      • BNODE_ID_PATTERN

        private static final java.util.regex.Pattern BNODE_ID_PATTERN
      • BNODE_PATTERN

        private static final java.util.regex.Pattern BNODE_PATTERN
      • IRI_PATTERN

        private static final java.util.regex.Pattern IRI_PATTERN
      • LITERAL_PATTERN

        private static final java.util.regex.Pattern LITERAL_PATTERN
    • Constructor Detail

      • NTriplesUtil

        public NTriplesUtil()
    • Method Detail

      • parseValue

        public static Value parseValue​(java.lang.String nTriplesValue,
                                       ValueFactory valueFactory)
                                throws java.lang.IllegalArgumentException
        Parses an N-Triples value, creates an object for it using the supplied ValueFactory and returns this object.
        Parameters:
        nTriplesValue - The N-Triples value to parse.
        valueFactory - The ValueFactory to use for creating the object.
        Returns:
        An object representing the parsed value.
        Throws:
        java.lang.IllegalArgumentException - If the supplied value could not be parsed correctly.
      • parseResource

        public static Resource parseResource​(java.lang.String nTriplesResource,
                                             ValueFactory valueFactory)
                                      throws java.lang.IllegalArgumentException
        Parses an N-Triples resource, creates an object for it using the supplied ValueFactory and returns this object.
        Parameters:
        nTriplesResource - The N-Triples resource to parse.
        valueFactory - The ValueFactory to use for creating the object.
        Returns:
        An object representing the parsed resource.
        Throws:
        java.lang.IllegalArgumentException - If the supplied resource could not be parsed correctly.
      • parseURI

        public static IRI parseURI​(java.lang.String nTriplesURI,
                                   ValueFactory valueFactory)
                            throws java.lang.IllegalArgumentException
        Parses an N-Triples URI, creates an object for it using the supplied ValueFactory and returns this object.
        Parameters:
        nTriplesURI - The N-Triples URI to parse.
        valueFactory - The ValueFactory to use for creating the object.
        Returns:
        An object representing the parsed URI.
        Throws:
        java.lang.IllegalArgumentException - If the supplied URI could not be parsed correctly.
      • parseBNode

        public static BNode parseBNode​(java.lang.String nTriplesBNode,
                                       ValueFactory valueFactory)
                                throws java.lang.IllegalArgumentException
        Parses an N-Triples bNode, creates an object for it using the supplied ValueFactory and returns this object.
        Parameters:
        nTriplesBNode - The N-Triples bNode to parse.
        valueFactory - The ValueFactory to use for creating the object.
        Returns:
        An object representing the parsed bNode.
        Throws:
        java.lang.IllegalArgumentException - If the supplied bNode could not be parsed correctly.
      • parseLiteral

        public static Literal parseLiteral​(java.lang.String nTriplesLiteral,
                                           ValueFactory valueFactory)
                                    throws java.lang.IllegalArgumentException
        Parses an N-Triples literal, creates an object for it using the supplied ValueFactory and returns this object.
        Parameters:
        nTriplesLiteral - The N-Triples literal to parse.
        valueFactory - The ValueFactory to use for creating the object.
        Returns:
        An object representing the parsed literal.
        Throws:
        java.lang.IllegalArgumentException - If the supplied literal could not be parsed correctly.
      • parseTriple

        public static Triple parseTriple​(java.lang.String nTriplesTriple,
                                         ValueFactory valueFactory)
        Parses an RDF-star triple (non-standard N-Triples), creates an object for it using the supplied ValueFactory and returns this object.
        Parameters:
        nTriplesTriple - The RDF-star triple to parse.
        valueFactory - The ValueFactory to use for creating the object.
        Returns:
        An object representing the parsed triple.
        Throws:
        java.lang.IllegalArgumentException - If the supplied triple could not be parsed correctly.
      • parseTripleInternal

        private static NTriplesUtil.TripleMatch parseTripleInternal​(java.lang.String nTriplesTriple,
                                                                    ValueFactory valueFactory)
        Parses an RDF-star triple (non-standard N-Triples), creates an object for it using the supplied ValueFactory and returns an object that contains the parsed triple and the length of the parsed text.
        Parameters:
        nTriplesTriple - The RDF-star triple to parse.
        valueFactory - The ValueFactory to use for creating the object.
        Returns:
        An object representing the parsed triple and the length of the matching text.
        Throws:
        java.lang.IllegalArgumentException - If the supplied triple could not be parsed correctly.
      • findEndOfLabel

        private static int findEndOfLabel​(java.lang.String nTriplesLiteral)
        Finds the end of the label in a literal string. This method takes into account that characters can be escaped using backslashes.
        Returns:
        The index of the double quote ending the label, or -1 if it could not be found.
      • toNTriplesString

        public static java.lang.String toNTriplesString​(Value value)
        Creates an N-Triples string for the supplied value.
        Parameters:
        value -
        Returns:
        string
      • toNTriplesString

        public static java.lang.String toNTriplesString​(Value value,
                                                        boolean xsdStringToPlainLiteral)
        Creates an N-Triples string for the supplied value.If the supplied value is a Literal, it optionally ignores the xsd:string datatype, since this datatype is implicit in RDF-1.1.
        Parameters:
        value - The value to write.
        xsdStringToPlainLiteral - True to omit serialising the xsd:string datatype and false to always serialise the datatype for literals.
        Returns:
        string
      • append

        public static void append​(Value value,
                                  java.lang.Appendable appendable)
                           throws java.io.IOException
        Appends the N-Triples representation of the given Value to the given Appendable.
        Parameters:
        value - The value to write.
        appendable - The object to append to.
        Throws:
        java.io.IOException
      • append

        public static void append​(Value value,
                                  java.lang.Appendable appendable,
                                  boolean xsdStringToPlainLiteral,
                                  boolean escapeUnicode)
                           throws java.io.IOException
        Appends the N-Triples representation of the given Value to the given Appendable, optionally not serializing the datatype a Literal with the xsd:string datatype as it is implied for RDF-1.1.
        Parameters:
        value - The value to write.
        appendable - The object to append to.
        xsdStringToPlainLiteral - True to omit serializing the xsd:string datatype and false to always serialize the datatype for literals.
        escapeUnicode -
        Throws:
        java.io.IOException
      • toNTriplesString

        public static java.lang.String toNTriplesString​(Resource resource)
        Creates an N-Triples string for the supplied resource.
        Parameters:
        resource -
        Returns:
        string
      • append

        public static void append​(Resource resource,
                                  java.lang.Appendable appendable)
                           throws java.io.IOException
        Appends the N-Triples representation of the given Resource to the given Appendable.
        Parameters:
        resource - The resource to write.
        appendable - The object to append to.
        Throws:
        java.io.IOException
      • toNTriplesString

        public static java.lang.String toNTriplesString​(IRI uri)
        Creates an N-Triples string for the supplied URI.
        Parameters:
        uri -
        Returns:
        string
      • append

        public static void append​(IRI uri,
                                  java.lang.Appendable appendable)
                           throws java.io.IOException
        Appends the N-Triples representation of the given IRI to the given Appendable.
        Parameters:
        uri - The IRI to write.
        appendable - The object to append to.
        Throws:
        java.io.IOException
      • append

        public static void append​(IRI uri,
                                  java.lang.Appendable appendable,
                                  boolean escapeUnicode)
                           throws java.io.IOException
        Appends the N-Triples representation of the given IRI to the given Appendable.
        Parameters:
        uri -
        appendable -
        escapeUnicode -
        Throws:
        java.io.IOException
      • toNTriplesString

        public static java.lang.String toNTriplesString​(BNode bNode)
        Creates an N-Triples string for the supplied blank node.
        Parameters:
        bNode -
        Returns:
        string
      • append

        public static void append​(BNode bNode,
                                  java.lang.Appendable appendable)
                           throws java.io.IOException
        Appends the N-Triples representation of the given BNode to the given Appendable.
        Parameters:
        bNode -
        appendable -
        Throws:
        java.io.IOException
      • toNTriplesString

        public static java.lang.String toNTriplesString​(Literal lit)
        Creates an N-Triples string for the supplied literal.
        Parameters:
        lit -
        Returns:
        string
      • toNTriplesString

        public static java.lang.String toNTriplesString​(Literal lit,
                                                        boolean xsdStringToPlainLiteral)
        Creates an N-Triples string for the supplied literal, optionally ignoring the xsd:string datatype as it is implied for RDF-1.1.
        Parameters:
        lit - The literal to write.
        xsdStringToPlainLiteral - True to omit serializing the xsd:string datatype and false to always serialize the datatype for literals.
        Returns:
        String
      • append

        public static void append​(Literal lit,
                                  java.lang.Appendable appendable)
                           throws java.io.IOException
        Appends the N-Triples representation of the given Literal to the given Appendable.
        Parameters:
        lit -
        appendable -
        Throws:
        java.io.IOException
      • append

        public static void append​(Literal lit,
                                  java.lang.Appendable appendable,
                                  boolean xsdStringToPlainLiteral,
                                  boolean escapeUnicode)
                           throws java.io.IOException
        Appends the N-Triples representation of the given Literal to the given Appendable, optionally ignoring the xsd:string datatype as it is implied for RDF-1.1.
        Parameters:
        lit - The literal to write.
        appendable - The object to append to.
        xsdStringToPlainLiteral - True to omit serializing the xsd:string datatype and false to always serialize the datatype for literals.
        escapeUnicode - True to escape non-ascii/non-printable characters using Unicode escapes (\uxxxx and \Uxxxxxxxx), false to print without escaping.
        Throws:
        java.io.IOException
      • toNTriplesString

        public static java.lang.String toNTriplesString​(Triple triple)
        Creates an N-Triples (non-standard) string for the supplied RDF-star triple.
        Parameters:
        triple -
        Returns:
        string
      • append

        public static void append​(Triple triple,
                                  java.lang.Appendable appendable)
                           throws java.io.IOException
        Appends the N-Triples (non-standard) representation of the given Triple to the given Appendable.
        Parameters:
        triple -
        appendable -
        Throws:
        java.io.IOException
      • isLetterOrNumber

        @Deprecated
        public static boolean isLetterOrNumber​(int c)
        Checks whether the supplied character is a letter or number according to the N-Triples specification.
        Parameters:
        c -
        Returns:
        true if it is a letter or a number
        See Also:
        isLetter(int), isNumber(int)
      • isLetter

        @Deprecated
        public static boolean isLetter​(int c)
        Deprecated.
        Checks whether the supplied character is a letter according to the N-Triples specification.N-Triples letters are A - Z and a - z.
        Parameters:
        c -
        Returns:
        true if c is an ascii leter
      • isNumber

        @Deprecated
        public static boolean isNumber​(int c)
        Deprecated.
        Checks whether the supplied character is a number according to the N-Triples specification.N-Triples numbers are 0 - 9.
        Parameters:
        c -
        Returns:
        true if the character is a number
      • isValidCharacterForBNodeLabel

        public static boolean isValidCharacterForBNodeLabel​(int c)
        Checks whether the supplied character is valid character as per N-Triples specification.
        Parameters:
        c -
        Returns:
        true if valid
        See Also:
        https://www.w3.org/TR/n-triples/#BNodes
      • isLiberalCharactersButNotDot

        public static boolean isLiberalCharactersButNotDot​(int c)
        Checks whether the supplied character is in list of liberal characters according to the N-Triples specification except Dot.
        Parameters:
        c -
        Returns:
        true if valid
      • isUnderscore

        public static boolean isUnderscore​(int c)
        Checks whether the supplied character is Underscore.
        Parameters:
        c -
        Returns:
        true if it is an underscore
      • isDot

        public static boolean isDot​(int c)
        Checks whether the supplied character is Dot '.'.
        Parameters:
        c -
        Returns:
        true if it is a dot
      • escapeString

        public static java.lang.String escapeString​(java.lang.String label)
        Escapes a Unicode string to an all-ASCII character sequence.Any special characters are escaped using backslashes ( " becomes \", etc.), and non-ascii/non-printable characters are escaped using Unicode escapes ( \uxxxx and \Uxxxxxxxx).
        Parameters:
        label -
        Returns:
        an escaped string (unicode to ascii plus codepoints).
      • escapeString

        public static void escapeString​(java.lang.String label,
                                        java.lang.Appendable appendable)
                                 throws java.io.IOException
        Escapes a Unicode string to an all-ASCII character sequence. Any special characters are escaped using backslashes ( " becomes \", etc.), and non-ascii/non-printable characters are escaped using Unicode escapes ( \uxxxx and \Uxxxxxxxx).
        Parameters:
        label -
        appendable -
        Throws:
        java.io.IOException
      • escapeString

        public static void escapeString​(java.lang.String label,
                                        java.lang.Appendable appendable,
                                        boolean escapeUnicode)
                                 throws java.io.IOException
        Escapes a Unicode string to an N-Triples compatible character sequence.Any special characters are escaped using backslashes (" becomes \", etc.), and non-ascii/non-printable characters are escaped using Unicode escapes (\uxxxx and \Uxxxxxxxx) if the option is selected.
        Parameters:
        label -
        appendable -
        escapeUnicode -
        Throws:
        java.io.IOException
      • unescapeString

        public static java.lang.String unescapeString​(java.lang.String s)
        Unescapes an escaped Unicode string. Any Unicode sequences ( \uxxxx and \Uxxxxxxxx) are restored to the value indicated by the hexadecimal argument and any backslash-escapes ( \", \\, etc.) are decoded to their original form.
        Parameters:
        s - An escaped Unicode string.
        Returns:
        The unescaped string.
        Throws:
        java.lang.IllegalArgumentException - If the supplied string is not a correctly escaped N-Triples string.
      • toHexString

        public static java.lang.String toHexString​(int decimal,
                                                   int stringLength)
        Converts a decimal value to a hexadecimal string representation of the specified length.
        Parameters:
        decimal - A decimal value.
        stringLength - The length of the resulting string.
        Returns:
        padded string