com.flexive.shared
Class FxFormatUtils

java.lang.Object
  extended by com.flexive.shared.FxFormatUtils

public final class FxFormatUtils
extends java.lang.Object

Miscellaneous formatting utility functions.

Author:
Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at), Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)

Field Summary
static java.lang.String CONTRAST_BACKGROUND_COLOR
           
static java.lang.String DATE_STD
           
static java.lang.String DATETIME_STD
           
static char DECIMAL_SEP_STD
           
static java.lang.String DEFAULT_COLOR
           
static char GROUPING_SEP_STD
           
static java.lang.String TIME_STD
           
static java.lang.String UNIVERSAL_TIMEFORMAT
           
static boolean USE_GROUPING_STD
           
 
Method Summary
static java.lang.String checkEmail(java.lang.String email)
          Checks the email.
static java.lang.String colorCodeToStyle(java.lang.String code)
          Converts "#FF0000" or "FF0000" rgb values to style="color:#FF0000" and all other style values to class="XXX".
static java.lang.String convertToJavaUnicode(java.lang.String input, java.lang.Character start1, java.lang.Character end1, java.lang.Character start2, java.lang.Character end2)
          Convert international characters to hex-encoded unicode format (\\u....) internat.
static java.lang.String encodeBase64(byte[] data)
          Encodes a byte array into a base64 String.
static java.lang.String encodeBase64(byte[] data, int len)
          Encodes a byte array into a base64 String.
static java.lang.String encodeHex(byte[] bytes)
          Turns an array of bytes into a String representing each byte as an unsigned hex number.
static java.lang.String encodePassword(long accountId, java.lang.String username, java.lang.String password)
          Checks the password and encodes it.
static java.lang.String escapeForAttribute(java.lang.Object value)
          Escape the given (string) value for rendering in a HTML or XML attribute (enclosed by double quotes).
static java.lang.String escapeForCsv(java.lang.Object value)
          Escape the given value for CSV.
static java.lang.String escapeForJavaScript(java.lang.String value)
          Escape text content for output in a javascript function call parameter.
static java.lang.String escapeForJavaScript(java.lang.String value, boolean replaceNewlines)
          Escape text content for output in a javascript function call parameter.
static java.lang.String escapeForJavaScript(java.lang.String value, boolean replaceNewlines, boolean filterHtml)
          Escape text content for output in a javascript function call parameter, based on the Struts TagUtils#filter function.
static java.lang.String escapeForSql(java.lang.Object value)
          Generic SQL escape method.
static java.lang.String escapeTreePath(java.lang.String path)
          Escape a path to allow only a-zA-Z0-9/ and replace all other letters with underscores (_)
static java.lang.String formatResource(java.lang.String resource, long languageId, java.lang.Object... values)
          Format a resource, replacing all {x} by the appropriate value in values.
static java.lang.String formatTimeSpan(long time)
          Format a timespan given in ms as human readable output (using hours as the maximum time unit)
static java.lang.String formatTimeSpanForScheduler(long time)
          Format a timespan given in ms as human readable output (using weeks as the maximum time unit and seconds as the smallest)
static java.text.SimpleDateFormat getDateFormat()
          Returns a basic date format that is readable but not localized.
static java.text.SimpleDateFormat getDateTimeFormat()
          Returns a basic date/time format that is readable but not localized.
static java.lang.String getPaddedIntString3(int value)
          Get an int as String with at least 3 digits, padded by "0" if needed
static java.text.SimpleDateFormat getUniversalDateTimeFormat()
          Returns a formatter for the universal date/time format
static boolean isQuoted(java.lang.String value)
          Is the value quoted by ' or " ?
static boolean isRGBCode(java.lang.String value)
          Checks if a value is a RGB color code.
static boolean lackOfContrast(java.lang.String color)
          Is there a lack of contrast in the given color string?
static java.lang.String processColorString(java.lang.String paramName, java.lang.String value)
          Check if a color value is valid.
static java.lang.String removeCommandChars(java.lang.String text)
          Remove special command characters (such as newlines) from the given string.
static java.lang.Boolean toBoolean(java.lang.String value)
          Convert a String to Boolean
static java.util.Date toDate(java.lang.String value)
          Convert a String to Date
static java.util.Date toDateTime(java.lang.String value)
          Convert a String to DateTime
static java.lang.Double toDouble(java.lang.String value)
          Convert a String to Double
static java.lang.Float toFloat(java.lang.String value)
          Convert a String to Float
static java.lang.Integer toInteger(java.lang.String value)
          Convert a String to Integer
static java.lang.Long toLong(java.lang.String value)
          Convert a String to Long
static java.lang.String toString(java.util.Date date)
           
static java.lang.String unquote(java.lang.String value)
          Unquote a quoted value
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_COLOR

public static final java.lang.String DEFAULT_COLOR
See Also:
Constant Field Values

UNIVERSAL_TIMEFORMAT

public static final java.lang.String UNIVERSAL_TIMEFORMAT
See Also:
Constant Field Values

CONTRAST_BACKGROUND_COLOR

public static final java.lang.String CONTRAST_BACKGROUND_COLOR
See Also:
Constant Field Values

DATE_STD

public static final java.lang.String DATE_STD
See Also:
Constant Field Values

DATETIME_STD

public static final java.lang.String DATETIME_STD
See Also:
Constant Field Values

TIME_STD

public static final java.lang.String TIME_STD
See Also:
Constant Field Values

DECIMAL_SEP_STD

public static final char DECIMAL_SEP_STD
See Also:
Constant Field Values

GROUPING_SEP_STD

public static final char GROUPING_SEP_STD
See Also:
Constant Field Values

USE_GROUPING_STD

public static final boolean USE_GROUPING_STD
See Also:
Constant Field Values
Method Detail

isRGBCode

public static boolean isRGBCode(java.lang.String value)
Checks if a value is a RGB color code.

The RGB color code my start with a '#', but is also recognized without it.

Parameters:
value - the string to check
Returns:
true if the value is a valid RGB color code

colorCodeToStyle

public static java.lang.String colorCodeToStyle(java.lang.String code)
Converts "#FF0000" or "FF0000" rgb values to style="color:#FF0000" and all other style values to class="XXX".

Parameters:
code - the color code
Returns:
the style or class value

lackOfContrast

public static boolean lackOfContrast(java.lang.String color)
Is there a lack of contrast in the given color string?

Parameters:
color - color to check
Returns:
contrast lacking compared to white

checkEmail

public static java.lang.String checkEmail(java.lang.String email)
                                   throws FxInvalidParameterException
Checks the email. Also allows local email addresses without a domain (e.g. 'root@localhost').

Parameters:
email - the email to check
Returns:
the email without whitespaces
Throws:
FxInvalidParameterException - if the email is invalid

encodePassword

public static java.lang.String encodePassword(long accountId,
                                              java.lang.String username,
                                              java.lang.String password)
                                       throws FxInvalidParameterException
Checks the password and encodes it.

Parameters:
accountId - the account ID (needed for computing the hash)
password - unencoded the password
Returns:
the encoded password
Throws:
FxInvalidParameterException - if the password is invalid (too short, too simple, ..)
Since:
3.1.6

processColorString

public static java.lang.String processColorString(java.lang.String paramName,
                                                  java.lang.String value)
                                           throws FxInvalidParameterException
Check if a color value is valid.

The color may be a RGB value (recognized by a starting '#') or a css class name.
The function returns the default color if value is empty.
If the value defines an invalid RGB value a FxInvalidParameterException is thrown.

Parameters:
paramName - the name of the parameter that is used when a FxInvalidParameterException is thrown
value - the color alue
Returns:
the (corrected) color
Throws:
FxInvalidParameterException - if the color is invalid

escapeTreePath

public static java.lang.String escapeTreePath(java.lang.String path)
Escape a path to allow only a-zA-Z0-9/ and replace all other letters with underscores (_)

Parameters:
path - the path to escape
Returns:
escaped path

toString

public static java.lang.String toString(java.util.Date date)

isQuoted

public static boolean isQuoted(java.lang.String value)
Is the value quoted by ' or " ?

Parameters:
value - value to check
Returns:
if quoted by ' or " ?

unquote

public static java.lang.String unquote(java.lang.String value)
Unquote a quoted value

Parameters:
value - value to unquote
Returns:
unquoted calue

encodeHex

public static java.lang.String encodeHex(byte[] bytes)
Turns an array of bytes into a String representing each byte as an unsigned hex number.

Parameters:
bytes - an array of bytes to convert to a hex-string
Returns:
generated hex string

encodeBase64

public static java.lang.String encodeBase64(byte[] data)
Encodes a byte array into a base64 String.

Parameters:
data - a byte array to encode.
Returns:
a base64 encode String.

encodeBase64

public static java.lang.String encodeBase64(byte[] data,
                                            int len)
Encodes a byte array into a base64 String.

Parameters:
data - a byte array to encode.
len - length of the byte array to use
Returns:
a base64 encode String.

formatResource

public static java.lang.String formatResource(java.lang.String resource,
                                              long languageId,
                                              java.lang.Object... values)
Format a resource, replacing all {x} by the appropriate value in values. If values is an instance of FxString the given localeId is used

Parameters:
resource - String containing {0}..{n} placeholders
languageId - used locale if values contain FxString instances
values - either FxString, FxException or any Object using toString()
Returns:
formatted resource

escapeForJavaScript

public static java.lang.String escapeForJavaScript(java.lang.String value,
                                                   boolean replaceNewlines,
                                                   boolean filterHtml)
Escape text content for output in a javascript function call parameter, based on the Struts TagUtils#filter function.

Parameters:
value - the text content
replaceNewlines - replaces linebreaks with html
tags if set to true, or simply removes them (replaces them with spaces) if set to false.
filterHtml - filter html?
Returns:
the escaped text

escapeForJavaScript

public static java.lang.String escapeForJavaScript(java.lang.String value,
                                                   boolean replaceNewlines)
Escape text content for output in a javascript function call parameter.

Parameters:
value - the text content
replaceNewlines - replaces linebreaks with html
tags if set to true, or simply removes them (replaces them with spaces) if set to false.
Returns:
the escaped text

escapeForJavaScript

public static java.lang.String escapeForJavaScript(java.lang.String value)
Escape text content for output in a javascript function call parameter.

Parameters:
value - the text content
Returns:
the escaped text

escapeForSql

public static java.lang.String escapeForSql(java.lang.Object value)
Generic SQL escape method.

Parameters:
value - the value to be formatted
Returns:
the formatted value

escapeForAttribute

public static java.lang.String escapeForAttribute(java.lang.Object value)
Escape the given (string) value for rendering in a HTML or XML attribute (enclosed by double quotes).

Parameters:
value - the value to be escaped
Returns:
the escaped value
Since:
3.1.3

escapeForCsv

public static java.lang.String escapeForCsv(java.lang.Object value)
Escape the given value for CSV.

Parameters:
value - the value to be formatted
Returns:
the escaped value.
Since:
3.1

getDateTimeFormat

public static java.text.SimpleDateFormat getDateTimeFormat()
Returns a basic date/time format that is readable but not localized.

Returns:
a basic date/time format that is readable but not localized.

getUniversalDateTimeFormat

public static java.text.SimpleDateFormat getUniversalDateTimeFormat()
Returns a formatter for the universal date/time format

Returns:
formatter for the universal date/time format

getDateFormat

public static java.text.SimpleDateFormat getDateFormat()
Returns a basic date format that is readable but not localized.

Returns:
a basic date format that is readable but not localized.

toBoolean

public static java.lang.Boolean toBoolean(java.lang.String value)
Convert a String to Boolean

Parameters:
value - value to convert
Returns:
Boolean

toInteger

public static java.lang.Integer toInteger(java.lang.String value)
Convert a String to Integer

Parameters:
value - value to convert
Returns:
Integer

toLong

public static java.lang.Long toLong(java.lang.String value)
Convert a String to Long

Parameters:
value - value to convert
Returns:
Long

toDouble

public static java.lang.Double toDouble(java.lang.String value)
Convert a String to Double

Parameters:
value - value to convert
Returns:
Double

toFloat

public static java.lang.Float toFloat(java.lang.String value)
Convert a String to Float

Parameters:
value - value to convert
Returns:
Float

toDate

public static java.util.Date toDate(java.lang.String value)
Convert a String to Date

Parameters:
value - value to convert
Returns:
Date

toDateTime

public static java.util.Date toDateTime(java.lang.String value)
Convert a String to DateTime

Parameters:
value - value to convert
Returns:
Date

removeCommandChars

public static java.lang.String removeCommandChars(java.lang.String text)
Remove special command characters (such as newlines) from the given string.

Parameters:
text - the string to be filtered
Returns:
the string without command characters
Since:
3.1

getPaddedIntString3

public static java.lang.String getPaddedIntString3(int value)
Get an int as String with at least 3 digits, padded by "0" if needed

Parameters:
value - int value
Returns:
padded String

formatTimeSpan

public static java.lang.String formatTimeSpan(long time)
Format a timespan given in ms as human readable output (using hours as the maximum time unit)

Parameters:
time - time in ms
Returns:
human readable time
Since:
3.1.2

formatTimeSpanForScheduler

public static java.lang.String formatTimeSpanForScheduler(long time)
Format a timespan given in ms as human readable output (using weeks as the maximum time unit and seconds as the smallest)

Parameters:
time - time in ms
Returns:
human readable time

convertToJavaUnicode

public static java.lang.String convertToJavaUnicode(java.lang.String input,
                                                    java.lang.Character start1,
                                                    java.lang.Character end1,
                                                    java.lang.Character start2,
                                                    java.lang.Character end2)
Convert international characters to hex-encoded unicode format (\\u....) internat. chars: e.g. ~ 0x007E - 0x00FF Takes one or two character ranges as parameters (second range == null --> converted to "first" range) Ranges are inclusive!

Parameters:
input - the input String
start1 - start of range 1
end1 - end of range 2
start2 - start of range 2
end2 - end of range 2
Returns:
the String containing unicode characters