Class TextUtil


  • @VelocityCallable("singleton")
    public final class TextUtil
    extends Object
    Text utilities for the velocity context. This class does not contain any data members and is thread-safe.
    • Constructor Detail

      • TextUtil

        public TextUtil()
    • Method Detail

      • arrayToString

        public String arrayToString​(Collection<String> p_ar)
        Serializes an array list to a string, using default delimiter (|) and escape character (0).
        Parameters:
        p_ar - The array list to serialize.
        Returns:
        The serialized array list.
      • arrayToString

        public String arrayToString​(Collection<String> p_ar,
                                    String p_strDel,
                                    String p_strEsc)
        Serializes an array list to a string.
        Parameters:
        p_ar - The array list to serialize.
        p_strDel - The delimiter sequence to use.
        p_strEsc - The escape sequence to use.
        Returns:
        The serialized array list.
      • splitCRLF

        public List<String> splitCRLF​(String p_str)
        Deserialize an array with a given Delimiter.
        Parameters:
        p_str - The string containing the serialized array.
        Returns:
        The deserialized array.
      • join

        public String join​(Collection<?> p_coll,
                           String p_strDelim)
        Join a collection using the given delimiter.

        Note that join and split are not inverse functions.

        Parameters:
        p_coll - The collection to be joined.
        p_strDelim - The delimiter to be used.
        Returns:
        The joined connection, or null if p_coll is null.
      • split

        public List<String> split​(String p_str,
                                  String p_strDel)
        Split a string to a list at the given delimiter.

        Note that join and split are not inverse functions.

        Parameters:
        p_str - The string containing the serialized array.
        p_strDel - The delimiter sequence to use.
        Returns:
        The deserialized array.
      • htmlEncode

        public String htmlEncode​(String p_strText,
                                 boolean p_bInsertNbsp,
                                 boolean p_bInsertBr)
        HTML-encode some text.

        Use writeHtmlEncoded(Writer, String, boolean, boolean) with $Response for large texts.

        Parameters:
        p_strText - The text to encode.
        p_bInsertNbsp - Flag that indicates if space characters are to be replaced with &nbsp;.
        p_bInsertBr - Flag that indicates if CR/FL sequences are to be replaced with <br />.
      • writeHtmlEncoded

        public void writeHtmlEncoded​(Writer p_writer,
                                     String p_strText,
                                     boolean p_bInsertNbsp,
                                     boolean p_bInsertBr)
        Write text HTML-encoded to a stream.
        Parameters:
        p_writer - The stream to write to.
        p_strText - The text to encode.
        p_bInsertNbsp - Flag that indicates if space characters are to be replaced with &nbsp;.
        p_bInsertBr - Flag that indicates if CR/FL sequences are to be replaced with <br />.
      • filterScript

        public void filterScript​(Writer p_writer,
                                 String p_strText)
        Filter some text by script-tag and its content.
        Parameters:
        p_writer - The stream to write to.
        p_strText - The text to filter.
      • javaScriptEncode

        public String javaScriptEncode​(String p_strText,
                                       boolean p_bEscapeLineBreaks)
        JavaScript-encode some text.
        Parameters:
        p_strText - The text to encode.
        p_bEscapeLineBreaks - If true, CRs are ignored.
        Returns:
        The encoded text.
        See Also:
        writeJavaScriptEncoded(Writer, String)
      • javaScriptEncode

        public String javaScriptEncode​(String p_strText,
                                       String p_strPattern,
                                       String p_strReplacement)
        JavaScript-encode some text after replacing a given pattern.
        Parameters:
        p_strText - The text to encode.
        p_strPattern - The pattern to search for.
        p_strReplacement - The replacement string, if the search pattern is found.
        Returns:
        The encoded text.
        See Also:
        writeJavaScriptEncoded(Writer, String)
      • javaScriptEncodeFromVTL

        public String javaScriptEncodeFromVTL​(String p_strText)
        JavaScript-encode some text after replacing &quot by ".
        Parameters:
        p_strText - The text to encode.
        Returns:
        The encoded text.
        See Also:
        writeJavaScriptEncoded(Writer, String)
      • prepareForTooltip

        public String prepareForTooltip​(String p_strText)
      • prepareForTooltip

        public String prepareForTooltip​(String p_strText,
                                        boolean p_bInsertBr)
      • prepareForTooltipHtml

        public String prepareForTooltipHtml​(String p_strText)
      • escForXml

        public StringBuilder escForXml​(CharSequence p_in)
        Replace <, >, &, ", and ' with their respective XML entities.
        Parameters:
        p_in - The input text.
        Returns:
        p_str with XML markup characters replaced with their entities.
        See Also:
        XmlMarkupEncoder
      • escForXml

        public void escForXml​(Writer p_out,
                              CharSequence p_in)
                       throws IOException
        Replace <, >, &, ", and ' with their respective XML entities.
        Parameters:
        p_out - The output stream to write the result to.
        p_in - The input text.
        Throws:
        IOException - - If an I/O error occurred.
        See Also:
        XmlMarkupEncoder
      • undoEscForXml

        public StringBuilder undoEscForXml​(CharSequence p_in)
        Parameters:
        p_in - The text to encode.
        Returns:
        The decoded text.
      • undoEscForXml

        public void undoEscForXml​(Writer p_out,
                                  CharSequence p_in)
                           throws IOException
        Parameters:
        p_out - The output stream to write the result to.
        p_in - The text to encode.
        Throws:
        IOException - - If an I/O error occurred.
      • writeJavaScriptEncoded

        public void writeJavaScriptEncoded​(Writer p_writer,
                                           String p_strText,
                                           boolean p_bEscapeLineBreaks,
                                           boolean p_bDoubleEscapeQuote)
        Write JavaScript-encoded text to a stream.

        Apostrophes and backslashes are escaped by prepending a backslash.

        Parameters:
        p_writer - The stream to write to.
        p_strText - The text to encode.
        p_bEscapeLineBreaks - If true CRs are ignored in the output and LFs are escaped as \n.
      • getNumeral

        public String getNumeral​(int p_iNumber,
                                 String p_strLangId)
      • getDisplayLanguage

        public String getDisplayLanguage​(String p_strLang,
                                         String p_strMyLang)
        Returns the display name of a language in the given language.
        Parameters:
        p_strLang - The language tag (e.g. de, or zh).
        p_strMyLang - The target language (e.g. en, or zh-CN).
        Returns:
        The display name of the language.
      • getDisplayCountry

        public String getDisplayCountry​(String p_strLangCountry,
                                        String p_strMyLang)
        Returns the display name of a country in the given language.
        Parameters:
        p_strLangCountry - The language tag (e.g. de-DE, or de-CH).
        p_strMyLang - The target language (e.g. de, or en).
        Returns:
        The display name of the language.
      • getDisplayLocale

        @Deprecated
        public String getDisplayLocale​(String p_strLangTag,
                                       String p_strMyLang)
        Deprecated.
        Returns the locale display name for the given language tag in the given language.
        Parameters:
        p_strLangTag - The locale language tag (e.g. de-DE).
        p_strMyLang - The target language (e.g. en).
        Returns:
        The locale display name.
      • parseInt

        public int parseInt​(String p_strIn,
                            int p_iDefault)
        Parse Integer.
        Parameters:
        p_strIn - The text to parse.
        p_iDefault - On error return this.
      • parseInt

        public int parseInt​(String p_strIn)
        Parse an integer number string.
        Parameters:
        p_strIn - The text to parse.
      • parseLong

        public long parseLong​(String p_strIn)
        Parse an integer number string.
        Parameters:
        p_strIn - The text to parse.
      • parseDouble

        public double parseDouble​(String p_strIn,
                                  double p_dblDefault)
        Parameters:
        p_strIn - The text to parse.
        p_dblDefault - On error return this.
      • parseDouble

        public double parseDouble​(String p_strIn)
        Parameters:
        p_strIn - The text to parse.
        Returns:
        The parsed value as a double, or 0 if the input could not be parsed.
      • parseFloat

        @Deprecated
        public int parseFloat​(String p_strIn)
        Deprecated.
        Do not use this method.
      • getGuidsFromText

        public Set<String> getGuidsFromText​(CharSequence p_chseq)
        Get a distinct set of GUIDs from the given text.

        Note that the order of GUIDs within the text is not preserved in the result.

        Parameters:
        p_chseq - The text to extract GUIDs from.
        Returns:
        The (possibly empty) set of GUIDs.
      • calcValueByPercent

        public int calcValueByPercent​(String p_iPercent,
                                      String p_iMaxValue)
        Calculates the percentage value of the given value.
        Parameters:
        p_iPercent - The percentage value.
        p_iMaxValue - The value to calculate the percentage for.
        Returns:
        The calculated percentage value.
      • getComplementaryColorHex

        public String getComplementaryColorHex​(String p_strColorHex)
        Get the complementary color to the given HEX formatted color.
        Parameters:
        p_strColorHex - The HEX formatted color.
        Returns:
        The complementary color.
      • getAverageColorInt

        public int getAverageColorInt​(String p_strColorHex)
        Calculate the average color of the given HEX color.
        Parameters:
        p_strColorHex - The HEX formatted color.
        Returns:
        The average color value.
      • convertIsoDateTimeToXml

        @Deprecated
        public String convertIsoDateTimeToXml​(String p_strValue)
        Deprecated.
        This method is flawed. Do not use it.
        Converts an ISO dateTime string (yyyy-MM-dd HH:mm:ss) to yyyy-MM-ddTHH:mm:ssZ using the current TimeZone for UTC transformation.
        Parameters:
        p_strValue - ISO datetime string.
        Returns:
        The XML conform datetime string in UTC format or an empty string if p_strValue is empty or not an valid ISO dateTime string.
      • vhDateToXML

        public String vhDateToXML​(de.uplanet.lucy.server.dataobjects.IDateTimeValueHolder p_vh)
        Returns a xsd:date representation from a given DateTimeValueHolder.
        Parameters:
        p_vh - DateTimeValueHolder (must not be null).
        Returns:
        A XML xs:date value from a DateTimeValueHolder.
      • vhTimeToXML

        public String vhTimeToXML​(de.uplanet.lucy.server.dataobjects.IDateTimeValueHolder p_vh)
        Returns a xsd:time representation from a given DateTimeValueHolder.
        Parameters:
        p_vh - DateTimeValueHolder (must not be null).
        Returns:
        A XML xs:time value from a DateTimeValueHolder.
      • getTextFromFile

        public String getTextFromFile​(String p_strPath,
                                      String p_strEncoding)
                               throws IOException
        Get text from a file.
        Parameters:
        p_strPath - The path to the file, relative to the portal directory.
        p_strEncoding - The file encoding.
        Throws:
        IOException - If an I/O error occurred.
      • includeTextFromFile

        public void includeTextFromFile​(Writer p_writer,
                                        String p_strPath,
                                        String p_strEncoding)
                                 throws IOException
        Include text from a file.
        Parameters:
        p_writer - The stream to write to.
        p_strPath - The path to the file, relative to the portal directory.
        p_strEncoding - The file encoding.
        Throws:
        IOException - If an I/O error occurred.
      • durationFormatter

        public String durationFormatter​(RenderingContext p_ctx,
                                        IValueHolder<?> p_from,
                                        IValueHolder<?> p_dtto,
                                        String p_strOneDayFormat,
                                        String p_strMoreDayFormat)
        Formats a date time duration.
        Parameters:
        p_ctx - The rendering context
        p_from - The from date time value holder
        p_dtto - The to date time value holder
        p_strOneDayFormat - The format, used when the from and to date is on the same day. ($from, $to will be replaced by the formatted datetime or time string).
        p_strMoreDayFormat - The format, used when the from and to date is not on the same day. ($from, $to will be replaced by the formatted datetime strings).
        Returns:
        The formatted duration string.
      • fallbackIfNullOrEmpty

        public CharSequence fallbackIfNullOrEmpty​(CharSequence p_chseq,
                                                  String p_strFallback)
        Map strings or other character sequences that are null or empty to a fallback value.
        Parameters:
        p_chseq - The character sequence to be checked.
        p_strFallback - The fallback value.
        Returns:
        p_chseq if this value is neither null nor empty, or the fallback value otherwise.