Class TextUtil
- java.lang.Object
-
- de.uplanet.lucy.server.auxiliaries.TextUtil
-
@VelocityCallable("singleton") public final class TextUtil extends ObjectText utilities for the velocity context. This class does not contain any data members and is thread-safe.
-
-
Constructor Summary
Constructors Constructor Description TextUtil()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description StringarrayToString(Collection<String> p_ar)Serializes an array list to a string, using default delimiter (|) and escape character (0).StringarrayToString(Collection<String> p_ar, String p_strDel, String p_strEsc)Serializes an array list to a string.intcalcValueByPercent(String p_iPercent, String p_iMaxValue)Calculates the percentage value of the given value.StringconvertIsoDateTimeToXml(String p_strValue)Deprecated.This method is flawed.StringcustomDefaultEncode(String p_strText)JavaScript-encode some text.StringdurationFormatter(RenderingContext p_ctx, IValueHolder<?> p_from, IValueHolder<?> p_dtto, String p_strOneDayFormat, String p_strMoreDayFormat)Formats a date time duration.voidescForXml(Writer p_out, CharSequence p_in)Replace <, >, &, ", and ' with their respective XML entities.StringBuilderescForXml(CharSequence p_in)Replace <, >, &, ", and ' with their respective XML entities.CharSequencefallbackIfNullOrEmpty(CharSequence p_chseq, String p_strFallback)Map strings or other character sequences that arenullor empty to a fallback value.voidfilterScript(Writer p_writer, String p_strText)Filter some text by script-tag and its content.intgetAverageColorInt(String p_strColorHex)Calculate the average color of the given HEX color.StringgetComplementaryColorHex(String p_strColorHex)Get the complementary color to the given HEX formatted color.StringgetDisplayCountry(String p_strLangCountry, String p_strMyLang)Returns the display name of a country in the given language.StringgetDisplayLanguage(String p_strLang, String p_strMyLang)Returns the display name of a language in the given language.StringgetDisplayLocale(String p_strLangTag, String p_strMyLang)Deprecated.UsegetDisplayLanguage(String, String)instead.Set<String>getGuidsFromText(CharSequence p_chseq)Get a distinct set of GUIDs from the given text.StringgetNumeral(int p_iNumber, String p_strLangId)StringgetTextFromFile(String p_strPath, String p_strEncoding)Get text from a file.StringhexToString(String p_str)StringhexToString(String p_str, String p_strCharset)StringhtmlEncode(String p_strText, boolean p_bInsertNbsp, boolean p_bInsertBr)HTML-encode some text.voidincludeTextFromFile(Writer p_writer, String p_strPath, String p_strEncoding)Include text from a file.StringjavaScriptEncode(String p_strText)The same asjavaScriptEncode(p_strText, false).StringjavaScriptEncode(String p_strText, boolean p_bEscapeLineBreaks)JavaScript-encode some text.StringjavaScriptEncode(String p_strText, String p_strPattern, String p_strReplacement)JavaScript-encode some text after replacing a given pattern.StringjavaScriptEncodeFromVTL(String p_strText)JavaScript-encode some text after replacing"by".Stringjoin(Collection<?> p_coll, String p_strDelim)Join a collection using the given delimiter.StringjsonEncode(Object p_value)Deprecated.UseJSONUtil.toJSONString(Object)instead.doubleparseDouble(String p_strIn)doubleparseDouble(String p_strIn, double p_dblDefault)intparseFloat(String p_strIn)Deprecated.Do not use this method.intparseInt(String p_strIn)Parse an integer number string.intparseInt(String p_strIn, int p_iDefault)Parse Integer.longparseLong(String p_strIn)Parse an integer number string.StringprepareForTooltip(String p_strText)StringprepareForTooltip(String p_strText, boolean p_bInsertBr)StringprepareForTooltipHtml(String p_strText)List<String>split(String p_str, String p_strDel)Split a string to a list at the given delimiter.List<String>splitCRLF(String p_str)Deserialize an array with a given Delimiter.List<String>stringToArray(String p_str)Deserialize an array that was previously serialized usingarrayToString(java.util.Collection<java.lang.String>)with default delimiter (|) and escape character (0).List<String>stringToArray(String p_str, String p_strDel, String p_strEsc)Deserialize an array that was previously serialized usingarrayToString(java.util.Collection<java.lang.String>).StringstringToHex(String p_str)Encode a string to it's hex representation, using the default character encoding, i.e,UTF-8.StringstringToHex(String p_str, String p_strCharset)voidundoEscForXml(Writer p_out, CharSequence p_in)StringBuilderundoEscForXml(CharSequence p_in)StringvhDateToXML(de.uplanet.lucy.server.dataobjects.IDateTimeValueHolder p_vh)Returns a xsd:date representation from a given DateTimeValueHolder.StringvhTimeToXML(de.uplanet.lucy.server.dataobjects.IDateTimeValueHolder p_vh)Returns a xsd:time representation from a given DateTimeValueHolder.voidwriteHtmlEncoded(Writer p_writer, String p_strText, boolean p_bInsertNbsp, boolean p_bInsertBr)Write text HTML-encoded to a stream.voidwriteJavaScriptEncoded(Writer p_writer, String p_strText)Deprecated.voidwriteJavaScriptEncoded(Writer p_writer, String p_strText, boolean p_bEscapeLineBreaks, boolean p_bDoubleEscapeQuote)Write JavaScript-encoded text to a stream.voidwriteJSONEncoded(Writer p_writer, Object p_value)Deprecated.UseJSONUtil.writeJSONString(Object, Writer)instead.
-
-
-
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.
-
stringToArray
public List<String> stringToArray(String p_str)
Deserialize an array that was previously serialized usingarrayToString(java.util.Collection<java.lang.String>)with default delimiter (|) and escape character (0).- Parameters:
p_str- The string containing the serialized array.- Returns:
- The deserialized array.
-
stringToArray
public List<String> stringToArray(String p_str, String p_strDel, String p_strEsc)
Deserialize an array that was previously serialized usingarrayToString(java.util.Collection<java.lang.String>).- Parameters:
p_str- The string containing the serialized array.p_strDel- The delimiter sequence to use.p_strEsc- The escape sequence to use.- Returns:
- The deserialized array.
-
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
joinandsplitare not inverse functions.- Parameters:
p_coll- The collection to be joined.p_strDelim- The delimiter to be used.- Returns:
- The joined connection, or
nullifp_collisnull.
-
split
public List<String> split(String p_str, String p_strDel)
Split a string to a list at the given delimiter.Note that
joinandsplitare not inverse functions.- Parameters:
p_str- The string containing the serialized array.p_strDel- The delimiter sequence to use.- Returns:
- The deserialized array.
-
stringToHex
public String stringToHex(String p_str) throws UnsupportedEncodingException
Encode a string to it's hex representation, using the default character encoding, i.e,UTF-8.- Parameters:
p_str- The string to encode.- Returns:
- The hex representation of the string.
- Throws:
UnsupportedEncodingException
-
stringToHex
public String stringToHex(String p_str, String p_strCharset) throws UnsupportedEncodingException
- Throws:
UnsupportedEncodingException- See Also:
Codec.hexEncodeString(String, String)
-
hexToString
public String hexToString(String p_str)
- See Also:
Codec.hexDecodeString(String)
-
hexToString
public String hexToString(String p_str, String p_strCharset)
- See Also:
Codec.hexDecodeString(String, String)
-
htmlEncode
public String htmlEncode(String p_strText, boolean p_bInsertNbsp, boolean p_bInsertBr)
HTML-encode some text.Use
writeHtmlEncoded(Writer, String, boolean, boolean)with$Responsefor large texts.- Parameters:
p_strText- The text to encode.p_bInsertNbsp- Flag that indicates if space characters are to be replaced with .p_bInsertBr- Flag that indicates ifCR/FLsequences 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 .p_bInsertBr- Flag that indicates ifCR/FLsequences 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)
The same asjavaScriptEncode(p_strText, false).- Parameters:
p_strText- The text to encode.- Returns:
- The encoded text.
- See Also:
writeJavaScriptEncoded(Writer, String)
-
javaScriptEncode
public String javaScriptEncode(String p_strText, boolean p_bEscapeLineBreaks)
JavaScript-encode some text.- Parameters:
p_strText- The text to encode.p_bEscapeLineBreaks- Iftrue,CRs are ignored.- Returns:
- The encoded text.
- See Also:
writeJavaScriptEncoded(Writer, String)
-
customDefaultEncode
public String customDefaultEncode(String p_strText)
JavaScript-encode some text.- Parameters:
p_strText- The text to encode.- 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"by".- Parameters:
p_strText- The text to encode.- Returns:
- The encoded text.
- See Also:
writeJavaScriptEncoded(Writer, String)
-
jsonEncode
@Deprecated public String jsonEncode(Object p_value)
Deprecated.UseJSONUtil.toJSONString(Object)instead.
-
writeJSONEncoded
@Deprecated public void writeJSONEncoded(Writer p_writer, Object p_value) throws IOException
Deprecated.UseJSONUtil.writeJSONString(Object, Writer)instead.- Throws:
IOException
-
escForXml
public StringBuilder escForXml(CharSequence p_in)
Replace <, >, &, ", and ' with their respective XML entities.- Parameters:
p_in- The input text.- Returns:
p_strwith 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
@Deprecated public void writeJavaScriptEncoded(Writer p_writer, String p_strText)
Deprecated.The same aswriteJavaScriptEncoded(p_writer, p_strText, false).- Parameters:
p_writer- The stream to write to.p_strText- The text to encode.- See Also:
writeJavaScriptEncoded(Writer, String, boolean, boolean)
-
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- IftrueCRs are ignored in the output andLFs are escaped as\n.
-
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, orzh).p_strMyLang- The target language (e.g.en, orzh-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, orde-CH).p_strMyLang- The target language (e.g.de, oren).- Returns:
- The display name of the language.
-
getDisplayLocale
@Deprecated public String getDisplayLocale(String p_strLangTag, String p_strMyLang)
Deprecated.UsegetDisplayLanguage(String, String)instead.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
0if 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 contextp_from- The from date time value holderp_dtto- The to date time value holderp_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 arenullor empty to a fallback value.- Parameters:
p_chseq- The character sequence to be checked.p_strFallback- The fallback value.- Returns:
p_chseqif this value is neithernullnor empty, or the fallback value otherwise.
-
-