Class TextUtil


  • public class TextUtil
    extends java.lang.Object
    • Field Detail

      • SYSTEM_CHARSET

        public static final java.lang.String SYSTEM_CHARSET
      • CR

        private static final char[] CR
        The predefined line delimiters
      • LF

        private static final char[] LF
      • CRLF

        private static final char[] CRLF
      • EMPTY

        private static final char[] EMPTY
      • DEFAULT_CHARACTERS_REQUIRED_IN_PATH

        public static final java.lang.String DEFAULT_CHARACTERS_REQUIRED_IN_PATH
        See Also:
        Constant Field Values
      • DEFAULT_CHARACTERS_DISALLOWED_IN_PATH

        public static final java.lang.String DEFAULT_CHARACTERS_DISALLOWED_IN_PATH
      • DEFAULT_LINE_SEPARATOR_REGEX

        public static final java.lang.String DEFAULT_LINE_SEPARATOR_REGEX
        See Also:
        Constant Field Values
      • INVALID_PATH_ENDS_CHARACTERS

        private static final java.lang.String INVALID_PATH_ENDS_CHARACTERS
        See Also:
        Constant Field Values
      • DEFAULT_UNICODIFY_ALL

        public static final boolean DEFAULT_UNICODIFY_ALL
        See Also:
        Constant Field Values
      • DEFAULT_BASE64_LINE_LENGTH

        public static final int DEFAULT_BASE64_LINE_LENGTH
        See Also:
        Constant Field Values
      • instance

        private static TextUtil instance
      • useRequiredInPathChars

        public boolean useRequiredInPathChars
      • charsDisallowedInPath

        private java.lang.String charsDisallowedInPath
      • charsRequiredInPath

        private java.lang.String charsRequiredInPath
      • lineSeparatorRegex

        private java.lang.String lineSeparatorRegex
      • base64LineLength

        private int base64LineLength
      • unicodifyAll

        private boolean unicodifyAll
      • WHITE_SPACE_PATTERN

        private static final java.util.regex.Pattern WHITE_SPACE_PATTERN
      • UNICODE_PATTERN

        private static final java.util.regex.Pattern UNICODE_PATTERN
      • VARIABLE_PATTERN

        private static final java.util.regex.Pattern VARIABLE_PATTERN
        $HOME (group "one") or ( $(HOME) or ${HOME} ) (group "two")
      • HTML_ESCAPE_CHARS

        private static final java.lang.String[][] HTML_ESCAPE_CHARS
        Characters used for escape operations
    • Constructor Detail

      • TextUtil

        private TextUtil()
    • Method Detail

      • getInstance

        private static TextUtil getInstance()
      • fromCamelToUnderscore

        public static java.lang.String fromCamelToUnderscore​(java.lang.String string)
        Parameters:
        string - in the "camel" notation like "beMyCamel"
        Returns:
        the resulting string in usual notation like "be_my_camel"
      • fromUnderscoreToCamel

        public static java.lang.String fromUnderscoreToCamel​(java.lang.String string)
        Parameters:
        string - in the "underscore" notation like "be_my_camel"
        Returns:
        the resulting string in "camel" notation like "beMyCamel"
      • fromCamelCaseToPascalCaseBidirectional

        public static java.lang.String fromCamelCaseToPascalCaseBidirectional​(java.lang.String string,
                                                                              boolean toCamel)
        Parameters:
        string - in the "camel" notation like "beMyCamel"
        toCamel - determines the direction of conversion, true-to Camel, false-to Pascal
        Returns:
        the resulting string in Pascal notation like "BeMyCamel"
      • isPath

        public boolean isPath​(java.lang.String path)
        Check String to match real path name
        Returns:
        false if this path is may be not a File/Dir path, i.e. contains not alloved characters etc.
      • isFilePath

        public boolean isFilePath​(java.lang.String path)
        Check if given string can contain real file path name
        Returns:
        false if this path is may be not a file path, i.e. contains not alloved characters etc.
      • isJavaType

        public boolean isJavaType​(java.lang.String type)
        Check if given string can contain real Java type name
        Returns:
        false if this type is may be not a Java type name, i.e. contains not alloved characters etc.
      • trimPath

        public java.lang.String trimPath​(java.lang.String path)
        Parameters:
        path - string to check
        Returns:
        a copy of the string, with leading and trailing whitespace and not in path allowed characters (leading and trailing) omitted.
      • getEnv

        private static java.lang.String getEnv​(java.lang.String var)
      • trimJavaType

        public java.lang.String trimJavaType​(java.lang.String type)
      • findLineReferenceRegex

        public int findLineReferenceRegex​(java.lang.String line,
                                          int startOffset)
        Search for occurencies of line references in text, like
         foo/Foo.java RegexSeparator 156
         
        If the regular expression separator can't be compiled it just uses the separator as string.
        Parameters:
        line -
        startOffset -
        Returns:
        integer value guessed as line reference in text (this is not a offset in given line!!!)
      • findLineReference

        private int findLineReference​(java.lang.String line,
                                      int startOffset)
        Search for occurencies of line references in text, like
         foo/Foo.java:156
         
        Returns:
        integer value guessed as line reference in text (this is not a offset in given line!!!)
      • findJavaType

        public java.lang.String findJavaType​(java.lang.String line,
                                             int caretOffset)
      • indexOf

        public static int indexOf​(java.lang.String line,
                                  char c,
                                  int startOffset,
                                  int stopOffset,
                                  boolean forward)
      • getCharsDisallowedInPath

        public java.lang.String getCharsDisallowedInPath()
      • getCharsRequiredInPath

        public java.lang.String getCharsRequiredInPath()
      • getLineSeparatorRegex

        public java.lang.String getLineSeparatorRegex()
      • isUseRequiredInPathChars

        public boolean isUseRequiredInPathChars()
      • setCharsDisallowedInPath

        public void setCharsDisallowedInPath​(java.lang.String string)
      • setCharsRequiredInPath

        public void setCharsRequiredInPath​(java.lang.String string)
      • setLineSeparatorRegex

        public void setLineSeparatorRegex​(java.lang.String string)
      • setUseRequiredInPathChars

        public void setUseRequiredInPathChars​(boolean b)
      • base64decode

        public java.lang.String base64decode​(java.lang.String base64,
                                             java.lang.String charset)
      • base64encode

        public java.lang.String base64encode​(java.lang.String plainText,
                                             java.lang.String charset)
      • base64trim

        public java.lang.String base64trim​(java.lang.String text,
                                           java.lang.String lineDelim)
      • toUnicode

        public java.lang.String toUnicode​(java.lang.String input)
      • leading4Zeros

        private char[] leading4Zeros​(java.lang.String hexString)
        Parameters:
        hexString - max 4 characters length
        Returns:
        same string with leading zeros
      • fromUnicode

        public java.lang.String fromUnicode​(java.lang.String charset,
                                            java.lang.String input)
                                     throws java.lang.UnsupportedOperationException
        Parameters:
        charset - may be null. If null, no checks for the supported encoding would be performed
        input - non null
        Throws:
        java.lang.UnsupportedOperationException - if given charset does not support characters from given text
      • quoteReplacement

        public static java.lang.String quoteReplacement​(java.lang.String s)
      • canEncode

        public static java.lang.String canEncode​(java.lang.String charset,
                                                 java.lang.CharSequence text,
                                                 java.lang.String unicodeValue)
        Parameters:
        charset - non null
        text - non null
        unicodeValue -
        Returns:
        null if text could be encoded, error message otherwise
      • isValidLineSeparatorRegex

        public static boolean isValidLineSeparatorRegex​(java.lang.String regex)
      • updateTextUtils

        public static void updateTextUtils()
      • getDefaultTextUtilities

        public static TextUtil getDefaultTextUtilities()
      • convertTabsToSpaces

        public static boolean convertTabsToSpaces​(java.lang.StringBuffer line,
                                                  int tabWidth,
                                                  boolean removeTrailing,
                                                  boolean ignoreBlankLines,
                                                  boolean replaceAllTabs,
                                                  boolean useModulo4Tabs)
      • calculateSpaces4Tabs

        private static int calculateSpaces4Tabs​(int spacesCount,
                                                int tabsCount,
                                                int tabWidth,
                                                boolean useModulo4Tabs)
      • calculateTabs4Spaces

        private static int calculateTabs4Spaces​(int spacesCount,
                                                int tabWidth)
      • replaceAllTabs

        private static boolean replaceAllTabs​(java.lang.StringBuffer line,
                                              int start,
                                              int tabWidth)
      • replaceAllSpaces

        private static boolean replaceAllSpaces​(java.lang.StringBuffer line,
                                                int start,
                                                int tabWidth)
      • removeTrailingSpace

        public static boolean removeTrailingSpace​(java.lang.StringBuffer line,
                                                  boolean ignoreBlankLine)
      • convertSpacesToTabs

        public static boolean convertSpacesToTabs​(java.lang.StringBuffer line,
                                                  int tabWidth,
                                                  boolean removeTrailing,
                                                  boolean ignoreBlankLines,
                                                  boolean replaceAllSpaces)
      • getLineEnd

        private static char[] getLineEnd​(java.lang.StringBuffer line)
      • count

        public static int count​(java.lang.String s,
                                char c)
        Returns:
        number of occurencies of c in s
      • fillWith

        private static char[] fillWith​(int length,
                                       char c)
        Returns:
        char array with specified amount of given characters.
      • getEntityForChar

        public static java.lang.String getEntityForChar​(char ch)
        Get html entity for escape character
        Returns:
        null, if no entity found for given character
      • escapeText

        public static java.lang.String escapeText​(java.lang.String s)
        change escape characters to html entities (from http://www.rgagnon.com/howto.html)
        Parameters:
        s - string to be modified
        Returns:
        string with escape characters, changed to html entities
      • unescapeText

        public static java.lang.String unescapeText​(java.lang.String s)
        change html entities to escape characters (from http://www.rgagnon.com/howto.html)
        Parameters:
        s - string to unescape
        Returns:
        new string with html entities changed to escape characters
      • indexOfNextWord

        private static int indexOfNextWord​(java.lang.String line,
                                           int startOffset,
                                           int lastIdx)
        get index of first non-whitespace letter (one of " \t\r\n")
        Returns:
        -1 if no such (non-whitespace) character found from given startOffset (inclusive)
      • capitalize

        public static java.lang.String capitalize​(java.lang.String line)
      • invertCase

        public static java.lang.String invertCase​(java.lang.String line)