Class TextUtil


  • public final class TextUtil
    extends java.lang.Object
    Contains a number of static utility methods.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String LEGACY_CHARS_ALLOWED
      Lists all punctuation characters allowed in WikiMarkup.
      static int PASSWORD_LENGTH
      Length of password.
      static java.lang.String PUNCTUATION_CHARS_ALLOWED
      Lists all punctuation characters allowed in page names.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String beautifyString​(java.lang.String s)
      Adds spaces in suitable locations of the input string.
      static java.lang.String beautifyString​(java.lang.String s, java.lang.String space)
      Adds spaces in suitable locations of the input string.
      static java.lang.String cleanString​(java.lang.String text, java.lang.String allowedChars)
      Cleans a Wiki name based on a list of characters.
      static int countSections​(java.lang.String pagedata)
      Counts the number of sections (separated with "----") from the page.
      static java.util.Properties createProperties​(java.lang.String[] values)
      Creates a Properties object based on an array which contains alternatively a key and a value.
      static java.lang.String generateRandomPassword()
      Generate a random String suitable for use as a temporary password.
      static boolean getBooleanProperty​(java.util.Properties props, java.lang.String key, boolean defval)
      Gets a boolean property from a standard Properties list.
      static java.lang.String getCanonicalFilePathProperty​(java.util.Properties props, java.lang.String key, java.lang.String defval)
      Fetches a file path property from the set of Properties.
      static int getIntegerProperty​(java.util.Properties props, java.lang.String key, int defVal)
      Gets an integer-valued property from a standard Properties list.
      static java.lang.String getRequiredProperty​(java.util.Properties props, java.lang.String key)
      Throws an exception if a property is not found.
      static java.lang.String getRequiredProperty​(java.util.Properties props, java.lang.String key, java.lang.String deprecatedKey)
      getRequiredProperty(Properties, String) overload that handles deprecated keys, so that a key and its deprecated counterpart can coexist in a given version of JSPWiki.
      static java.lang.String getSection​(java.lang.String pagedata, int section)
      Gets the given section (separated with "----") from the page text.
      static java.lang.String getStringProperty​(java.util.Properties props, java.lang.String key, java.lang.String defval)
      Fetches a String property from the set of Properties.
      static java.lang.String getStringProperty​(java.util.Properties props, java.lang.String key, java.lang.String deprecatedKey, java.lang.String defval)
      getStringProperty(Properties, String, String) overload that handles deprecated keys, so that a key and its deprecated counterpart can coexist in a given version of JSPWiki.
      static boolean isNumber​(java.lang.String s)
      Returns true, if the argument contains a number, otherwise false.
      static boolean isPositive​(java.lang.String val)
      Returns true, if the string "val" denotes a positive string.
      static java.lang.String native2Ascii​(java.lang.String s)
      Converts a string from the Unicode representation into something that can be embedded in a java properties file.
      static java.lang.String normalizePostData​(java.lang.String postData)
      Makes sure that the POSTed data is conforms to certain rules.
      static int parseIntParameter​(java.lang.String value, int defvalue)
      Parses an integer parameter, returning a default value if the value is null or a non-number.
      static java.lang.String repeatString​(java.lang.String what, int times)
      A simple routine which just repeates the arguments.
      static java.lang.String replaceEntities​(java.lang.String src)
      Replaces the relevant entities inside the String.
      static java.lang.String replaceString​(java.lang.String orig, int start, int end, java.lang.String text)
      Replaces a part of a string with a new String.
      static java.lang.String replaceString​(java.lang.String orig, java.lang.String src, java.lang.String dest)
      Replaces a string with another string.
      static java.lang.String replaceStringCaseUnsensitive​(java.lang.String orig, java.lang.String src, java.lang.String dest)
      Replaces a string with another string.
      static java.lang.String toHexString​(byte[] bytes)
      Generates a hexadecimal string from an array of bytes.
      static java.lang.String urlDecode​(java.lang.String data, java.lang.String encoding)
      Provides decoded version of string depending on encoding.
      static java.lang.String urlDecodeUTF8​(java.lang.String utf8)
      As java.net.URLDecoder class, but for UTF-8 strings.
      static java.lang.String urlEncode​(java.lang.String data, java.lang.String encoding)
      Provides encoded version of string depending on encoding.
      static java.lang.String urlEncodeUTF8​(java.lang.String text)
      As java.net.URLEncoder class, but this does it in UTF8 character set.
      • Methods inherited from class java.lang.Object

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

      • urlEncodeUTF8

        public static java.lang.String urlEncodeUTF8​(java.lang.String text)
        As java.net.URLEncoder class, but this does it in UTF8 character set.
        Parameters:
        text - The text to decode
        Returns:
        An URLEncoded string.
      • urlDecodeUTF8

        public static java.lang.String urlDecodeUTF8​(java.lang.String utf8)
        As java.net.URLDecoder class, but for UTF-8 strings. null is a safe value and returns null.
        Parameters:
        utf8 - The UTF-8 encoded string
        Returns:
        A plain, normal string.
      • urlEncode

        public static java.lang.String urlEncode​(java.lang.String data,
                                                 java.lang.String encoding)
        Provides encoded version of string depending on encoding. Encoding may be UTF-8 or ISO-8859-1 (default).

        This implementation is the same as in FileSystemProvider.mangleName().

        Parameters:
        data - A string to encode
        encoding - The encoding in which to encode
        Returns:
        A URL encoded string.
      • urlDecode

        public static java.lang.String urlDecode​(java.lang.String data,
                                                 java.lang.String encoding)
                                          throws java.lang.IllegalArgumentException
        Provides decoded version of string depending on encoding. Encoding may be UTF-8 or ISO-8859-1 (default).

        This implementation is the same as in FileSystemProvider.unmangleName().

        Parameters:
        data - The URL-encoded string to decode
        encoding - The encoding to use
        Returns:
        A decoded string.
        Throws:
        java.lang.IllegalArgumentException - If the data cannot be decoded.
      • replaceEntities

        public static java.lang.String replaceEntities​(java.lang.String src)
        Replaces the relevant entities inside the String. All & >, <, and " are replaced by their respective names.
        Parameters:
        src - The source string.
        Returns:
        The encoded string.
        Since:
        1.6.1
      • replaceString

        public static java.lang.String replaceString​(java.lang.String orig,
                                                     java.lang.String src,
                                                     java.lang.String dest)
        Replaces a string with another string.
        Parameters:
        orig - Original string. Null is safe.
        src - The string to find.
        dest - The string to replace src with.
        Returns:
        A string with the replacement done.
      • replaceString

        public static java.lang.String replaceString​(java.lang.String orig,
                                                     int start,
                                                     int end,
                                                     java.lang.String text)
        Replaces a part of a string with a new String.
        Parameters:
        start - Where in the original string the replacing should start.
        end - Where the replacing should end.
        orig - Original string. Null is safe.
        text - The new text to insert into the string.
        Returns:
        The string with the orig replaced with text.
      • replaceStringCaseUnsensitive

        public static java.lang.String replaceStringCaseUnsensitive​(java.lang.String orig,
                                                                    java.lang.String src,
                                                                    java.lang.String dest)
        Replaces a string with another string. Case-insensitive matching is used
        Parameters:
        orig - Original string. Null is safe.
        src - The string to find.
        dest - The string to replace src with.
        Returns:
        A string with all instances of src replaced with dest.
      • parseIntParameter

        public static int parseIntParameter​(java.lang.String value,
                                            int defvalue)
        Parses an integer parameter, returning a default value if the value is null or a non-number.
        Parameters:
        value - The value to parse
        defvalue - A default value in case the value is not a number
        Returns:
        The parsed value (or defvalue).
      • getIntegerProperty

        public static int getIntegerProperty​(java.util.Properties props,
                                             java.lang.String key,
                                             int defVal)
        Gets an integer-valued property from a standard Properties list. Before inspecting the props, we first check if there is a Java System Property with the same name, if it exists we use that value, if not we check an environment variable with that (almost) same name, almost meaning we replace dots with underscores. If the value does not exist, or is a non-integer, returns defVal.
        Parameters:
        props - The property set to look through
        key - The key to look for
        defVal - If the property is not found or is a non-integer, returns this value.
        Returns:
        The property value as an integer (or defVal).
        Since:
        2.1.48.
      • getBooleanProperty

        public static boolean getBooleanProperty​(java.util.Properties props,
                                                 java.lang.String key,
                                                 boolean defval)
        Gets a boolean property from a standard Properties list. Returns the default value, in case the key has not been set. Before inspecting the props, we first check if there is a Java System Property with the same name, if it exists we use that value, if not we check an environment variable with that (almost) same name, almost meaning we replace dots with underscores.

        The possible values for the property are "true"/"false", "yes"/"no", or "on"/"off". Any value not recognized is always defined as "false".

        Parameters:
        props - A list of properties to search.
        key - The property key.
        defval - The default value to return.
        Returns:
        True, if the property "key" was set to "true", "on", or "yes".
        Since:
        2.0.11
      • getStringProperty

        public static java.lang.String getStringProperty​(java.util.Properties props,
                                                         java.lang.String key,
                                                         java.lang.String defval)
        Fetches a String property from the set of Properties. This differs from Properties.getProperty() in a couple of key respects: First, property value is trim()med (so no extra whitespace back and front). Before inspecting the props, we first check if there is a Java System Property with the same name, if it exists we use that value, if not we check an environment variable with that (almost) same name, almost meaning we replace dots with underscores.
        Parameters:
        props - The Properties to search through
        key - The property key
        defval - A default value to return, if the property does not exist.
        Returns:
        The property value.
        Since:
        2.1.151
      • getStringProperty

        public static java.lang.String getStringProperty​(java.util.Properties props,
                                                         java.lang.String key,
                                                         java.lang.String deprecatedKey,
                                                         java.lang.String defval)
        getStringProperty(Properties, String, String) overload that handles deprecated keys, so that a key and its deprecated counterpart can coexist in a given version of JSPWiki.
        Parameters:
        props - The Properties to search through
        key - The property key
        deprecatedKey - the property key being superseeded by key
        defval - A default value to return, if the property does not exist.
        Returns:
        The property value.
      • getRequiredProperty

        public static java.lang.String getRequiredProperty​(java.util.Properties props,
                                                           java.lang.String key)
                                                    throws java.util.NoSuchElementException
        Throws an exception if a property is not found.
        Parameters:
        props - A set of properties to search the key in.
        key - The key to look for.
        Returns:
        The required property
        Throws:
        java.util.NoSuchElementException - If the search key is not in the property set.
        Since:
        2.0.26 (on TextUtils, moved To WikiEngine on 2.11.0-M1 and back to TextUtils on 2.11.0-M6)
      • getRequiredProperty

        public static java.lang.String getRequiredProperty​(java.util.Properties props,
                                                           java.lang.String key,
                                                           java.lang.String deprecatedKey)
                                                    throws java.util.NoSuchElementException
        getRequiredProperty(Properties, String) overload that handles deprecated keys, so that a key and its deprecated counterpart can coexist in a given version of JSPWiki.
        Parameters:
        props - The Properties to search through
        key - The property key
        deprecatedKey - the property key being superseeded by key
        Returns:
        The property value.
        Throws:
        java.util.NoSuchElementException
      • getCanonicalFilePathProperty

        public static java.lang.String getCanonicalFilePathProperty​(java.util.Properties props,
                                                                    java.lang.String key,
                                                                    java.lang.String defval)
        Fetches a file path property from the set of Properties. Before inspecting the props, we first check if there is a Java System Property with the same name, if it exists we use that value, if not we check an environment variable with that (almost) same name, almost meaning we replace dots with underscores. If the implementation fails to create the canonical path it just returns the original value of the property which is a bit doggy.
        Parameters:
        props - The Properties to search through
        key - The property key
        defval - A default value to return, if the property does not exist.
        Returns:
        the canonical path of the file or directory being referenced
        Since:
        2.10.1
      • isPositive

        public static boolean isPositive​(java.lang.String val)
        Returns true, if the string "val" denotes a positive string. Allowed values are "yes", "on", and "true". Comparison is case-insignificant. Null values are safe.
        Parameters:
        val - Value to check.
        Returns:
        True, if val is "true", "on", or "yes"; otherwise false.
        Since:
        2.0.26
      • normalizePostData

        public static java.lang.String normalizePostData​(java.lang.String postData)
        Makes sure that the POSTed data is conforms to certain rules. These rules are:
        • The data always ends with a newline (some browsers, such as NS4.x series, does not send a newline at the end, which makes the diffs a bit strange sometimes.
        • The CR/LF/CRLF mess is normalized to plain CRLF.
        The reason why we're using CRLF is that most browser already return CRLF since that is the closest thing to an HTTP standard.
        Parameters:
        postData - The data to normalize
        Returns:
        Normalized data
      • beautifyString

        public static java.lang.String beautifyString​(java.lang.String s)
        Adds spaces in suitable locations of the input string. This is used to transform a WikiName into a more readable format.
        Parameters:
        s - String to be beautified.
        Returns:
        A beautified string.
      • beautifyString

        public static java.lang.String beautifyString​(java.lang.String s,
                                                      java.lang.String space)
        Adds spaces in suitable locations of the input string. This is used to transform a WikiName into a more readable format.
        Parameters:
        s - String to be beautified.
        space - Use this string for the space character.
        Returns:
        A beautified string.
        Since:
        2.1.127
      • cleanString

        public static java.lang.String cleanString​(java.lang.String text,
                                                   java.lang.String allowedChars)
        Cleans a Wiki name based on a list of characters. Also, any multiple whitespace is collapsed into a single space, and any leading or trailing space is removed.
        Parameters:
        text - text to be cleared. Null is safe, and causes this to return null.
        allowedChars - Characters which are allowed in the string.
        Returns:
        A cleaned text.
        Since:
        2.6
      • createProperties

        public static java.util.Properties createProperties​(java.lang.String[] values)
                                                     throws java.lang.IllegalArgumentException
        Creates a Properties object based on an array which contains alternatively a key and a value. It is useful for generating default mappings. For example:
             String[] properties = { "jspwiki.property1", "value1", "jspwiki.property2", "value2 };
             Properties props = TextUtil.createPropertes( values );
             System.out.println( props.getProperty("jspwiki.property1") );
          
        would output "value1".
        Parameters:
        values - Alternating key and value pairs.
        Returns:
        Property object
        Throws:
        java.lang.IllegalArgumentException - if the property array is missing a value for a key.
        Since:
        2.2.
        See Also:
        Properties
      • countSections

        public static int countSections​(java.lang.String pagedata)
        Counts the number of sections (separated with "----") from the page.
        Parameters:
        pagedata - The WikiText to parse.
        Returns:
        int Number of counted sections.
        Since:
        2.1.86.
      • getSection

        public static java.lang.String getSection​(java.lang.String pagedata,
                                                  int section)
                                           throws java.lang.IllegalArgumentException
        Gets the given section (separated with "----") from the page text. Note that the first section is always #1. If a page has no section markers, then there is only a single section, #1.
        Parameters:
        pagedata - WikiText to parse.
        section - Which section to get.
        Returns:
        String The section.
        Throws:
        java.lang.IllegalArgumentException - If the page does not contain this many sections.
        Since:
        2.1.86.
      • repeatString

        public static java.lang.String repeatString​(java.lang.String what,
                                                    int times)
        A simple routine which just repeates the arguments. This is useful for creating something like a line or something.
        Parameters:
        what - String to repeat
        times - How many times to repeat the string.
        Returns:
        Guess what?
        Since:
        2.1.98.
      • native2Ascii

        public static java.lang.String native2Ascii​(java.lang.String s)
        Converts a string from the Unicode representation into something that can be embedded in a java properties file. All references outside the ASCII range are replaced with \\uXXXX.
        Parameters:
        s - The string to convert
        Returns:
        the ASCII string
      • toHexString

        public static java.lang.String toHexString​(byte[] bytes)
        Generates a hexadecimal string from an array of bytes. For example, if the array contains { 0x01, 0x02, 0x3E }, the resulting string will be "01023E".
        Parameters:
        bytes - A Byte array
        Returns:
        A String representation
        Since:
        2.3.87
      • isNumber

        public static boolean isNumber​(java.lang.String s)
        Returns true, if the argument contains a number, otherwise false. In a quick test this is roughly the same speed as Integer.parseInt() if the argument is a number, and roughly ten times the speed, if the argument is NOT a number.
        Parameters:
        s - String to check
        Returns:
        True, if s represents a number. False otherwise.
        Since:
        2.4
      • generateRandomPassword

        public static java.lang.String generateRandomPassword()
        Generate a random String suitable for use as a temporary password.
        Returns:
        String suitable for use as a temporary password
        Since:
        2.4