Class Text


  • public class Text
    extends java.lang.Object
    Constants and utilities for text.
    Author:
    Garret Wilson
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String CARRIAGE_RETURN_STRING
      The string representing the CR EOL character sequence.
      static java.lang.String CRLF_STRING
      The string representing the CRLF EOL sequence.
      static java.util.regex.Pattern LINE_FEED_PATTERN
      The pattern that can split a line based upon linefeeds.
      static java.lang.String LINE_FEED_STRING
      The string representing the LF EOL character sequence.
      static ContentType PLAIN_CONTENT_TYPE
      The content type for plain text: text/plain.
      static java.lang.String PLAIN_SUBTYPE
      The MIME subtype of text/plain.
      static java.lang.String TXT_NAME_EXTENSION
      The name extension for text files.
    • Constructor Summary

      Constructors 
      Constructor Description
      Text()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int compare​(java.lang.String string1, java.lang.String string2, java.text.Collator collator)
      Compares two strings for order in ascending order using the specified collator.
      static int compare​(java.lang.String string1, java.lang.String string2, java.text.Collator collator, SortOrder sortOrder)
      Compares two strings for order using the specified collator with the specified sort order.
      static java.lang.String createControlString​(java.lang.String string)
      Creates a control string according to ECMA-48, "Control Functions for Coded Character Sets", Section 5.6, "Control strings".
      static java.lang.String escape​(java.lang.CharSequence charSequence, Characters restricted, char escape)
      Escapes a given string by inserting an escape character before every restricted character, including any occurrence of the given escape character.
      static java.lang.StringBuilder escape​(java.lang.StringBuilder stringBuilder, Characters restricted, char escape)
      Escapes a given string builder by inserting an escape character before every restricted character, including any occurrence of the given escape character.
      static java.lang.StringBuilder escape​(java.lang.StringBuilder stringBuilder, Characters restricted, char escape, boolean escapeEscape)
      Escapes a given string builder by inserting an escape character before every restricted character, optionally including any occurrence of the given escape character.
      static boolean isText​(ContentType contentType)
      Determines if the given content type is one representing text in some form.
      static java.lang.CharSequence normalizeEOL​(java.lang.CharSequence charSequence, java.lang.CharSequence eol)
      Normalizes end-of-line sequences in the character sequence to the given .
      static java.lang.String recode​(java.lang.String string, java.lang.String oldEncoding, java.lang.String newEncoding)
      Re-encodes the given string to the new encoding (such as UTF-8), assuming the string was encoded from an array of bytes using the old encoding (e.g.
      • Methods inherited from class java.lang.Object

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

      • PLAIN_SUBTYPE

        public static final java.lang.String PLAIN_SUBTYPE
        The MIME subtype of text/plain.
        See Also:
        Constant Field Values
      • PLAIN_CONTENT_TYPE

        public static final ContentType PLAIN_CONTENT_TYPE
        The content type for plain text: text/plain.
      • TXT_NAME_EXTENSION

        public static final java.lang.String TXT_NAME_EXTENSION
        The name extension for text files.
        See Also:
        Constant Field Values
      • CARRIAGE_RETURN_STRING

        public static final java.lang.String CARRIAGE_RETURN_STRING
        The string representing the CR EOL character sequence.
        See Also:
        Characters.CARRIAGE_RETURN_CHAR
      • LINE_FEED_STRING

        public static final java.lang.String LINE_FEED_STRING
        The string representing the LF EOL character sequence.
        See Also:
        Characters.LINE_FEED_CHAR
      • LINE_FEED_PATTERN

        public static final java.util.regex.Pattern LINE_FEED_PATTERN
        The pattern that can split a line based upon linefeeds.
        See Also:
        Characters.LINE_FEED_CHAR
    • Constructor Detail

      • Text

        public Text()
    • Method Detail

      • compare

        public static int compare​(java.lang.String string1,
                                  java.lang.String string2,
                                  java.text.Collator collator)
        Compares two strings for order in ascending order using the specified collator. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. Identical strings are always considered equal. This method functions exactly as if the two stringss were compared using Collator.compare(String, String), except:
        • Identical strings are recognized as such without delegating to the actual Collator.compare(String, String) method.
        • This method allows null arguments, considering a null string to be lower than a non-null string.
        This method matches the semantics of Collator.compare(String, String), except that this method allows null arguments.
        Parameters:
        string1 - The first string to be compared, or null if the string is not available.
        string2 - The second string to be compared, or null if the string is not available.
        collator - The collator used to perform comparisons.
        Returns:
        A negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
        Throws:
        java.lang.NullPointerException - if the given collator is null.
        java.lang.ClassCastException - if the arguments' types prevent them from being compared.
        See Also:
        Collator.compare(String, String)
      • compare

        public static int compare​(java.lang.String string1,
                                  java.lang.String string2,
                                  java.text.Collator collator,
                                  SortOrder sortOrder)
        Compares two strings for order using the specified collator with the specified sort order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. Identical strings are always considered equal. This method functions exactly as if the two strings were compared using Collator.compare(String, String), except:
        • Identical strings are recognized as such without delegating to the actual Collator.compare(String, String) method.
        • This method allows null arguments, considering a null string to be lower than a non-null string.
        Parameters:
        string1 - The first string to be compared, or null if the string is not available.
        string2 - The second string to be compared, or null if the string is not available.
        collator - The collator used to perform comparisons.
        sortOrder - The order in which to perform comparisons.
        Returns:
        A negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
        Throws:
        java.lang.NullPointerException - if the given collator and/or sort order is null.
        java.lang.ClassCastException - if the arguments' types prevent them from being compared.
        See Also:
        Collator.compare(String, String)
      • createControlString

        public static final java.lang.String createControlString​(java.lang.String string)
        Creates a control string according to ECMA-48, "Control Functions for Coded Character Sets", Section 5.6, "Control strings". A control string begins with the Start of String control character (U+0098) and ends with a String Terminator control character (U+009C). ECMA-48 publication is also approved as ISO/IEC 6429.
        Parameters:
        string - The string from which a control string will be created.
        Returns:
        An ECMA-48 control string with the given string as its content.
        Throws:
        java.lang.NullPointerException - if the given string is null.
        See Also:
        ECMA-48: Control Functions for Coded Character Sets
      • isText

        public static boolean isText​(ContentType contentType)
        Determines if the given content type is one representing text in some form.

        Text media types include:

        • text/*
        • application/xml
        • application/*+xml
        • application/xml-external-parsed-entity
        • application/*+xml-external-parsed-entity (not formally defined)
        Parameters:
        contentType - The content type of a resource, or null for no content type.
        Returns:
        true if the given content type is one of several text media types.
      • recode

        public static java.lang.String recode​(java.lang.String string,
                                              java.lang.String oldEncoding,
                                              java.lang.String newEncoding)
                                       throws java.io.UnsupportedEncodingException
        Re-encodes the given string to the new encoding (such as UTF-8), assuming the string was encoded from an array of bytes using the old encoding (e.g. ISO-8859-1).
        Parameters:
        string - The string to recode.
        oldEncoding - The encoding used to create the string originally.
        newEncoding - The new encoding to use when creating the string.
        Returns:
        The a string created from encoding the characters in the specified new encoding.
        Throws:
        java.io.UnsupportedEncodingException - Thrown if either the old encoding or the new encoding is not supported.
      • escape

        public static java.lang.String escape​(java.lang.CharSequence charSequence,
                                              Characters restricted,
                                              char escape)
        Escapes a given string by inserting an escape character before every restricted character, including any occurrence of the given escape character.
        Parameters:
        charSequence - The data to escape.
        restricted - The characters to be escaped; should not include the escape character.
        escape - The character used to escape the restricted characters.
        Returns:
        A string containing the escaped data.
        Throws:
        java.lang.NullPointerException - if the given character sequence is null.
      • escape

        public static java.lang.StringBuilder escape​(java.lang.StringBuilder stringBuilder,
                                                     Characters restricted,
                                                     char escape)
        Escapes a given string builder by inserting an escape character before every restricted character, including any occurrence of the given escape character.
        Parameters:
        stringBuilder - The data to escape.
        restricted - The characters to be escaped; should not include the escape character.
        escape - The character used to escape the restricted characters.
        Returns:
        A string containing the escaped data.
        Throws:
        java.lang.NullPointerException - if the given string builder is null.
      • escape

        public static java.lang.StringBuilder escape​(java.lang.StringBuilder stringBuilder,
                                                     Characters restricted,
                                                     char escape,
                                                     boolean escapeEscape)
        Escapes a given string builder by inserting an escape character before every restricted character, optionally including any occurrence of the given escape character.
        Parameters:
        stringBuilder - The data to escape.
        restricted - The characters to be escaped; should not include the escape character.
        escape - The character used to escape the restricted characters.
        escapeEscape - true if the escape character should also be escaped.
        Returns:
        A string containing the escaped data.
        Throws:
        java.lang.NullPointerException - if the given string builder is null.
      • normalizeEOL

        public static java.lang.CharSequence normalizeEOL​(java.lang.CharSequence charSequence,
                                                          java.lang.CharSequence eol)
        Normalizes end-of-line sequences in the character sequence to the given . The following sequences are normalized to the provided EOL:
        • CR
        • LF
        • CRLF
        Parameters:
        charSequence - The character sequence to normalize.
        eol - The end of line characters to which to normalize the ends of lines.
        Returns:
        A character sequence with the ends of lines normalized to the given end of line characters.
        Throws:
        java.lang.NullPointerException - if the given character sequence and/or EOL characters is null.