Class Text


  • public class Text
    extends java.lang.Object
    This Class provides some text related utilities
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static char[] hexTable
      used for the md5
      static java.util.BitSet URISave
      The list of characters that are not encoded by the escape() and unescape() METHODS.
      static java.util.BitSet URISaveEx
      Same as URISave but also contains the '/'
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String digest​(java.lang.String algorithm, byte[] data)
      Digest the plain string using the given algorithm.
      static java.lang.String digest​(java.lang.String algorithm, java.lang.String data, java.lang.String enc)
      Digest the plain string using the given algorithm.
      static java.lang.String encodeIllegalXMLCharacters​(java.lang.String text)
      Replaces illegal XML characters in the given string by their corresponding predefined entity references.
      static java.lang.String escape​(java.lang.String string)
      Does a URL encoding of the string.
      static java.lang.String escape​(java.lang.String string, char escape)
      Does an URL encoding of the string using the escape character.
      static java.lang.String escape​(java.lang.String string, char escape, boolean isPath)
      Does an URL encoding of the string using the escape character.
      static java.lang.String escapeIllegalJcrChars​(java.lang.String name)
      Escapes all illegal JCR name characters of a string.
      static java.lang.String escapeIllegalXpathSearchChars​(java.lang.String s)
      Escapes illegal XPath search characters at the end of a string.
      static java.lang.String escapePath​(java.lang.String path)
      Does a URL encoding of the path.
      static java.lang.String[] explode​(java.lang.String str, int ch)
      returns an array of strings decomposed of the original string, split at every occurrence of 'ch'.
      static java.lang.String[] explode​(java.lang.String str, int ch, boolean respectEmpty)
      returns an array of strings decomposed of the original string, split at every occurance of 'ch'.
      static java.lang.String getAbsoluteParent​(java.lang.String path, int level)
      Returns the nth absolute parent of the path, where n=level.
      static java.lang.String getLocalName​(java.lang.String qname)
      Returns the local name of the given qname.
      static java.lang.String getName​(java.lang.String path)
      Returns the name part of the path.
      static java.lang.String getName​(java.lang.String path, boolean ignoreTrailingSlash)
      Same as getName(String) but adding the possibility to pass paths that end with a trailing '/'
      static java.lang.String getName​(java.lang.String path, char delim)
      Returns the name part of the path, delimited by the given delim.
      static java.lang.String getNamespacePrefix​(java.lang.String qname)
      Returns the namespace prefix of the given qname.
      static java.lang.String getRelativeParent​(java.lang.String path, int level)
      Returns the nth relative parent of the path, where n=level.
      static java.lang.String getRelativeParent​(java.lang.String path, int level, boolean ignoreTrailingSlash)
      Same as getRelativeParent(String, int) but adding the possibility to pass paths that end with a trailing '/'
      static java.lang.String implode​(java.lang.String[] arr, java.lang.String delim)
      Concatenates all strings in the string array using the specified delimiter.
      static boolean isDescendant​(java.lang.String path, java.lang.String descendant)
      Determines if the descendant path is hierarchical a descendant of path.
      static boolean isDescendantOrEqual​(java.lang.String path, java.lang.String descendant)
      Determines if the descendant path is hierarchical a descendant of path or equal to it.
      static boolean isSibling​(java.lang.String p1, java.lang.String p2)
      Determines, if two paths denote hierarchical siblins.
      static java.lang.String md5​(java.lang.String data)
      Calculate an MD5 hash of the string given using 'utf-8' encoding.
      static java.lang.String md5​(java.lang.String data, java.lang.String enc)
      Calculate an MD5 hash of the string given.
      static java.lang.String replace​(java.lang.String text, java.lang.String oldString, java.lang.String newString)
      Replaces all occurrences of oldString in text with newString.
      static java.lang.String replaceVariables​(java.util.Properties variables, java.lang.String value, boolean ignoreMissing)
      Performs variable replacement on the given string value.
      static java.lang.String unescape​(java.lang.String string)
      Does a URL decoding of the string.
      static java.lang.String unescape​(java.lang.String string, char escape)
      Does a URL decoding of the string using the escape character.
      static java.lang.String unescapeIllegalJcrChars​(java.lang.String name)
      Unescapes previously escaped jcr chars.
      • Methods inherited from class java.lang.Object

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

      • hexTable

        public static final char[] hexTable
        used for the md5
      • URISave

        public static java.util.BitSet URISave
        The list of characters that are not encoded by the escape() and unescape() METHODS. They contains the characters as defined 'unreserved' in section 2.3 of the RFC 2396 'URI generic syntax':
         unreserved  = alphanum | mark
         mark        = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
         
      • URISaveEx

        public static java.util.BitSet URISaveEx
        Same as URISave but also contains the '/'
    • Method Detail

      • md5

        public static java.lang.String md5​(java.lang.String data,
                                           java.lang.String enc)
                                    throws java.io.UnsupportedEncodingException
        Calculate an MD5 hash of the string given.
        Parameters:
        data - the data to encode
        enc - the character encoding to use
        Returns:
        a hex encoded string of the md5 digested input
        Throws:
        java.io.UnsupportedEncodingException - if the encoding is not supported
      • md5

        public static java.lang.String md5​(java.lang.String data)
        Calculate an MD5 hash of the string given using 'utf-8' encoding.
        Parameters:
        data - the data to encode
        Returns:
        a hex encoded string of the md5 digested input
      • digest

        public static java.lang.String digest​(java.lang.String algorithm,
                                              java.lang.String data,
                                              java.lang.String enc)
                                       throws java.security.NoSuchAlgorithmException,
                                              java.io.UnsupportedEncodingException
        Digest the plain string using the given algorithm.
        Parameters:
        algorithm - The alogrithm for the digest. This algorithm must be supported by the MessageDigest class.
        data - The plain text String to be digested.
        enc - The character encoding to use
        Returns:
        The digested plain text String represented as Hex digits.
        Throws:
        java.security.NoSuchAlgorithmException - if the desired algorithm is not supported by the MessageDigest class.
        java.io.UnsupportedEncodingException - if the encoding is not supported
      • digest

        public static java.lang.String digest​(java.lang.String algorithm,
                                              byte[] data)
                                       throws java.security.NoSuchAlgorithmException
        Digest the plain string using the given algorithm.
        Parameters:
        algorithm - The algorithm for the digest. This algorithm must be supported by the MessageDigest class.
        data - the data to digest with the given algorithm
        Returns:
        The digested plain text String represented as Hex digits.
        Throws:
        java.security.NoSuchAlgorithmException - if the desired algorithm is not supported by the MessageDigest class.
      • explode

        public static java.lang.String[] explode​(java.lang.String str,
                                                 int ch)
        returns an array of strings decomposed of the original string, split at every occurrence of 'ch'. if 2 'ch' follow each other with no intermediate characters, empty "" entries are avoided.
        Parameters:
        str - the string to decompose
        ch - the character to use a split pattern
        Returns:
        an array of strings
      • explode

        public static java.lang.String[] explode​(java.lang.String str,
                                                 int ch,
                                                 boolean respectEmpty)
        returns an array of strings decomposed of the original string, split at every occurance of 'ch'.
        Parameters:
        str - the string to decompose
        ch - the character to use a split pattern
        respectEmpty - if true, empty elements are generated
        Returns:
        an array of strings
      • implode

        public static java.lang.String implode​(java.lang.String[] arr,
                                               java.lang.String delim)
        Concatenates all strings in the string array using the specified delimiter.
        Parameters:
        arr - The String array
        delim - The delimiter
        Returns:
        the concatenated string
      • replace

        public static java.lang.String replace​(java.lang.String text,
                                               java.lang.String oldString,
                                               java.lang.String newString)
        Replaces all occurrences of oldString in text with newString.
        Parameters:
        text - The test to replace
        oldString - old substring to be replaced with newString
        newString - new substring to replace occurrences of oldString
        Returns:
        a string
      • encodeIllegalXMLCharacters

        public static java.lang.String encodeIllegalXMLCharacters​(java.lang.String text)
        Replaces illegal XML characters in the given string by their corresponding predefined entity references.
        Parameters:
        text - text to be escaped
        Returns:
        a string
      • escape

        public static java.lang.String escape​(java.lang.String string,
                                              char escape)
        Does an URL encoding of the string using the escape character. The characters that don't need encoding are those defined 'unreserved' in section 2.3 of the 'URI generic syntax' RFC 2396, but without the escape character.
        Parameters:
        string - the string to encode.
        escape - the escape character.
        Returns:
        the escaped string
        Throws:
        java.lang.NullPointerException - if string is null.
      • escape

        public static java.lang.String escape​(java.lang.String string,
                                              char escape,
                                              boolean isPath)
        Does an URL encoding of the string using the escape character. The characters that don't need encoding are those defined 'unreserved' in section 2.3 of the 'URI generic syntax' RFC 2396, but without the escape character. If isPath is true, additionally the slash '/' is ignored, too.
        Parameters:
        string - the string to encode.
        escape - the escape character.
        isPath - if true, the string is treated as path
        Returns:
        the escaped string
        Throws:
        java.lang.NullPointerException - if string is null.
      • escape

        public static java.lang.String escape​(java.lang.String string)
        Does a URL encoding of the string. The characters that don't need encoding are those defined 'unreserved' in section 2.3 of the 'URI generic syntax' RFC 2396.
        Parameters:
        string - the string to encode
        Returns:
        the escaped string
        Throws:
        java.lang.NullPointerException - if string is null.
      • escapePath

        public static java.lang.String escapePath​(java.lang.String path)
        Does a URL encoding of the path. The characters that don't need encoding are those defined 'unreserved' in section 2.3 of the 'URI generic syntax' RFC 2396. In contrast to the escape(String) method, not the entire path string is escaped, but every individual part (i.e. the slashes are not escaped).
        Parameters:
        path - the path to encode
        Returns:
        the escaped path
        Throws:
        java.lang.NullPointerException - if path is null.
      • unescape

        public static java.lang.String unescape​(java.lang.String string,
                                                char escape)
        Does a URL decoding of the string using the escape character. Please note that in opposite to the URLDecoder it does not transform the + into spaces.
        Parameters:
        string - the string to decode
        escape - the escape character
        Returns:
        the decoded string
        Throws:
        java.lang.NullPointerException - if string is null.
        java.lang.IllegalArgumentException - if the 2 characters following the escape character do not represent a hex-number or if not enough characters follow an escape character
      • unescape

        public static java.lang.String unescape​(java.lang.String string)
        Does a URL decoding of the string. Please note that in opposite to the URLDecoder it does not transform the + into spaces.
        Parameters:
        string - the string to decode
        Returns:
        the decoded string
        Throws:
        java.lang.NullPointerException - if string is null.
        java.lang.ArrayIndexOutOfBoundsException - if not enough character follow an escape character
        java.lang.IllegalArgumentException - if the 2 characters following the escape character do not represent a hex-number.
      • escapeIllegalJcrChars

        public static java.lang.String escapeIllegalJcrChars​(java.lang.String name)
        Escapes all illegal JCR name characters of a string. The encoding is loosely modeled after URI encoding, but only encodes the characters it absolutely needs to in order to make the resulting string a valid JCR name. Use unescapeIllegalJcrChars(String) for decoding.

        QName EBNF:

         simplename ::= onecharsimplename | twocharsimplename | threeormorecharname
         onecharsimplename ::= (* Any Unicode character except: '.', '/', ':', '[', ']', '*', '|' or any whitespace character *)
         twocharsimplename ::= '.' onecharsimplename | onecharsimplename '.' | onecharsimplename onecharsimplename
         threeormorecharname ::= nonspace string nonspace
         string ::= char | string char
         char ::= nonspace | ' '
         nonspace ::= (* Any Unicode character except: '/', ':', '[', ']', '*', '|' or any whitespace character *)
         
        Parameters:
        name - the name to escape
        Returns:
        the escaped name
      • escapeIllegalXpathSearchChars

        public static java.lang.String escapeIllegalXpathSearchChars​(java.lang.String s)
        Escapes illegal XPath search characters at the end of a string.

        Example:
        A search string like 'test?' will run into a ParseException documented in http://issues.apache.org/jira/browse/JCR-1248

        Parameters:
        s - the string to encode
        Returns:
        the escaped string
      • unescapeIllegalJcrChars

        public static java.lang.String unescapeIllegalJcrChars​(java.lang.String name)
        Unescapes previously escaped jcr chars.

        Please note, that this does not exactly the same as the url related unescape(String), since it handles the byte-encoding differently.

        Parameters:
        name - the name to unescape
        Returns:
        the unescaped name
      • getName

        public static java.lang.String getName​(java.lang.String path)
        Returns the name part of the path. If the given path is already a name (i.e. contains no slashes) it is returned.
        Parameters:
        path - the path
        Returns:
        the name part or null if path is null.
      • getName

        public static java.lang.String getName​(java.lang.String path,
                                               char delim)
        Returns the name part of the path, delimited by the given delim. If the given path is already a name (i.e. contains no delim characters) it is returned.
        Parameters:
        path - the path
        delim - the delimiter
        Returns:
        the name part or null if path is null.
      • getName

        public static java.lang.String getName​(java.lang.String path,
                                               boolean ignoreTrailingSlash)
        Same as getName(String) but adding the possibility to pass paths that end with a trailing '/'
        Parameters:
        path - the path to get the name from
        ignoreTrailingSlash - true to ignore the trailing slash
        Returns:
        the name
        See Also:
        getName(String)
      • getNamespacePrefix

        public static java.lang.String getNamespacePrefix​(java.lang.String qname)
        Returns the namespace prefix of the given qname. If the prefix is missing, an empty string is returned. Please note, that this method does not validate the name or prefix.

        the qname has the format: qname := [prefix ':'] local;

        Parameters:
        qname - a qualified name
        Returns:
        the prefix of the name or "".
        Throws:
        java.lang.NullPointerException - if qname is null
        See Also:
        getLocalName(String)
      • getLocalName

        public static java.lang.String getLocalName​(java.lang.String qname)
        Returns the local name of the given qname. Please note, that this method does not validate the name.

        the qname has the format: qname := [prefix ':'] local;

        Parameters:
        qname - a qualified name
        Returns:
        the localname
        Throws:
        java.lang.NullPointerException - if qname is null
        See Also:
        getNamespacePrefix(String)
      • isSibling

        public static boolean isSibling​(java.lang.String p1,
                                        java.lang.String p2)
        Determines, if two paths denote hierarchical siblins.
        Parameters:
        p1 - first path
        p2 - second path
        Returns:
        true if on same level, false otherwise
      • isDescendant

        public static boolean isDescendant​(java.lang.String path,
                                           java.lang.String descendant)
        Determines if the descendant path is hierarchical a descendant of path.
        Parameters:
        path - the current path
        descendant - the potential descendant
        Returns:
        true if the descendant is a descendant; false otherwise.
      • isDescendantOrEqual

        public static boolean isDescendantOrEqual​(java.lang.String path,
                                                  java.lang.String descendant)
        Determines if the descendant path is hierarchical a descendant of path or equal to it.
        Parameters:
        path - the path to check
        descendant - the potential descendant
        Returns:
        true if the descendant is a descendant or equal; false otherwise.
      • getRelativeParent

        public static java.lang.String getRelativeParent​(java.lang.String path,
                                                         int level)
        Returns the nth relative parent of the path, where n=level.

        Example:
        Text.getRelativeParent("/foo/bar/test", 1) == "/foo/bar"

        Parameters:
        path - the path of the page
        level - the level of the parent
        Returns:
        the path of the relative parent
      • getRelativeParent

        public static java.lang.String getRelativeParent​(java.lang.String path,
                                                         int level,
                                                         boolean ignoreTrailingSlash)
        Same as getRelativeParent(String, int) but adding the possibility to pass paths that end with a trailing '/'
        Parameters:
        path - The path.
        level - The level if the parent.
        ignoreTrailingSlash - true to ignore the trailing slash
        Returns:
        the path of hte paerent
        See Also:
        getRelativeParent(String, int)
      • getAbsoluteParent

        public static java.lang.String getAbsoluteParent​(java.lang.String path,
                                                         int level)
        Returns the nth absolute parent of the path, where n=level.

        Example:
        Text.getAbsoluteParent("/foo/bar/test", 1) == "/foo/bar"

        Parameters:
        path - the path of the page
        level - the level of the parent
        Returns:
        the absolute parent path
      • replaceVariables

        public static java.lang.String replaceVariables​(java.util.Properties variables,
                                                        java.lang.String value,
                                                        boolean ignoreMissing)
                                                 throws java.lang.IllegalArgumentException
        Performs variable replacement on the given string value. Each ${...} sequence within the given value is replaced with the value of the named parser variable. If a variable is not found in the properties an IllegalArgumentException is thrown unless ignoreMissing is true. In the later case, the missing variable is replaced by the empty string.
        Parameters:
        variables - the variables to replace
        value - the original value
        ignoreMissing - if true, missing variables are replaced by the empty string.
        Returns:
        value after variable replacements
        Throws:
        java.lang.IllegalArgumentException - if the replacement of a referenced variable is not found