org.riotfamily.common.util
Class FormatUtils

java.lang.Object
  extended by org.riotfamily.common.util.FormatUtils

public final class FormatUtils
extends Object

Utility class that provides some simple text formatting methods.


Method Summary
static String camelToTitleCase(String s)
           camelCase -> Camel Case CamelCASE -> Camel CASE Cam31Case -> Cam 31 Case
static String camelToXmlCase(String s)
           CamelCase -> camel-case camelCASE -> camel-case
static String combine(String... s)
          "a", "b", "c" -> "a b c a-b a-b-c" "a", "b", null -> "a b a-b"
static String escapeChars(String s, String chars, char escape)
           
static int extractInt(String s, String regex)
          Extracts an integer from a String using the first capture group of the given regular expression.
static String fileNameToTitleCase(String s)
           foo.bar -> Foo Bar foo-foo_bar -> Foo Foo Bar foo.barBar -> Foo Bar Bar
static String formatByteSize(long bytes)
          Returns a formatted string using an appropriate unit (Bytes, KB or MB).
static String formatDate(Date date, String pattern, Locale locale)
           
static String formatIsoDate(Date date)
           
static String formatMediumDate(Date date, Locale locale)
           
static String formatMillis(long millis)
          Returns a formatted string using the pattern hh:mm:ss.
static String formatNumber(Number number, String pattern, Locale locale)
           
static String getDateDelimiter(String dateFormat)
          Returns the date delimiter for the given date format Examples: "MM/DD/YYYY" - "/" "DD.MM.YYYY" - ".
static String getDateFormat(Locale locale)
          Returns the date format for the given locale.
static String getExtension(String filename)
          Returns the extension of the given filename.
static Object[] htmlEscapeArgs(Object[] args)
          Escapes all XML special characters in the given array.
static String join(String delimiter, String... parts)
           
static Date parseDate(String s)
          Parses a formatted String and returns the date.
static long parseMillis(String s)
          Parses a formatted String and returns the value in milliseconds.
static String propertyToTitleCase(String s)
           foo.bar -> Foo Bar foo.barBar -> Foo Bar Bar
static String repeat(String s, int times)
          Repeats a String the given number of times.
static String sanitizePath(String s)
          Calls StringUtils.cleanPath(String) and removes all occurrences of "../".
static String stripExtension(String filename)
          Returns the the filename without it's extension.
static String stripLeadingSlash(String path)
          Returns the the path without a leading slash.
static String stripTags(String s)
          Removes all markup from the given String using the pattern "</?
static String stripTagsAndSpaces(String s)
          Convenience method that strips tags and whitespaces.
static String stripTrailingSlash(String path)
          Returns the the path without a trailing slash.
static String stripWhitespaces(String s)
          Strips whitespaces without preserving line breaks.
static String stripWhitespaces(String s, boolean preserveBreaks)
          Replaces consecutive whitespaces by a single space character.
static String toCssClass(String s)
          Converts the given String into a valid CSS class name.
static String toFilename(String s)
          Invokes toLowerCase(), converts all whitespaces to underscores and removes all characters other than a-z, 0-9, dot, underscore or minus.
static String truncate(String s, int length)
          Truncates the given String if its length exceeds the specified value.
static String uriEscape(String input)
          Translates the given string into application/x-www-form-urlencoded format using UTF-8 as encoding scheme.
static String uriEscapePath(String input)
          Translates the given path into application/x-www-form-urlencoded format using UTF-8 as encoding scheme.
static String uriUnescape(String input)
          Decodes the given application/x-www-form-urlencoded string using UTF-8 as encoding scheme.
static String xmlEscape(String input)
          Turn special characters into escaped characters conforming to XML.
static String xmlEscapeDanglingAmps(String s)
          XML-escapes dangling ampersands that are no entities.
static String xmlToCamelCase(String s)
           foo-bar -> fooBar Foo-bAR -> FooBAR
static String xmlToTitleCase(String s)
           foo-bar -> Foo Bar fooBar -> Foo Bar
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

formatNumber

public static String formatNumber(Number number,
                                  String pattern,
                                  Locale locale)

formatByteSize

public static String formatByteSize(long bytes)
Returns a formatted string using an appropriate unit (Bytes, KB or MB).


camelToTitleCase

public static String camelToTitleCase(String s)
 camelCase -> Camel Case
 CamelCASE -> Camel CASE
 Cam31Case -> Cam 31 Case
 


xmlToCamelCase

public static String xmlToCamelCase(String s)
 foo-bar -> fooBar
 Foo-bAR -> FooBAR
 


xmlToTitleCase

public static String xmlToTitleCase(String s)
 foo-bar -> Foo Bar
 fooBar  -> Foo Bar
 


propertyToTitleCase

public static String propertyToTitleCase(String s)
 foo.bar    -> Foo Bar
 foo.barBar -> Foo Bar Bar
 


fileNameToTitleCase

public static String fileNameToTitleCase(String s)
 foo.bar     -> Foo Bar
 foo-foo_bar -> Foo Foo Bar
 foo.barBar  -> Foo Bar Bar
 


camelToXmlCase

public static String camelToXmlCase(String s)
 CamelCase -> camel-case
 camelCASE -> camel-case
 


join

public static String join(String delimiter,
                          String... parts)

combine

public static String combine(String... s)
"a", "b", "c" -> "a b c a-b a-b-c" "a", "b", null -> "a b a-b"


truncate

public static String truncate(String s,
                              int length)
Truncates the given String if its length exceeds the specified value.

Since:
6.4

toCssClass

public static String toCssClass(String s)
Converts the given String into a valid CSS class name.


parseMillis

public static long parseMillis(String s)
Parses a formatted String and returns the value in milliseconds. You can use one of the following suffixes:
     s - seconds
     m - minutes
     h - hours
     D - days
     W - weeks
     M - months
     Y - years
 


formatMillis

public static String formatMillis(long millis)
Returns a formatted string using the pattern hh:mm:ss. The hours are omitted if they are zero, the minutes are padded with a '0' character if they are less than 10.


getExtension

public static String getExtension(String filename)
Returns the extension of the given filename. Examples:
   "foo.bar" - "bar"
   "/some/file.name.foo" - "foo"
 
The following examples will return an empty String:
   "foo"
   "foo."
   "/dir.with.dots/file"
   ".bar"
   "/foo/.bar"
 


stripExtension

public static String stripExtension(String filename)
Returns the the filename without it's extension.


stripLeadingSlash

public static String stripLeadingSlash(String path)
Returns the the path without a leading slash.

Since:
7.0

stripTrailingSlash

public static String stripTrailingSlash(String path)
Returns the the path without a trailing slash.

Since:
7.0

parseDate

public static Date parseDate(String s)
Parses a formatted String and returns the date. The date to parse starts with today. You can use one of the following sufixes:

     D - days
     W - weeks
     M - months
     Y - years
 
Days is option, any number without a suffix is treated as a number of days


formatDate

public static String formatDate(Date date,
                                String pattern,
                                Locale locale)

formatMediumDate

public static String formatMediumDate(Date date,
                                      Locale locale)

formatIsoDate

public static String formatIsoDate(Date date)

getDateFormat

public static String getDateFormat(Locale locale)
Returns the date format for the given locale. Examples:
  Locale.ENGLISH - "MM/DD/YYYY"
  Locale.GERMAN - "DD.MM.YYYY"
 


getDateDelimiter

public static String getDateDelimiter(String dateFormat)
Returns the date delimiter for the given date format Examples:
   "MM/DD/YYYY" - "/"
   "DD.MM.YYYY" - "."
 


toFilename

public static String toFilename(String s)
Invokes toLowerCase(), converts all whitespaces to underscores and removes all characters other than a-z, 0-9, dot, underscore or minus.


xmlEscape

public static String xmlEscape(String input)
Turn special characters into escaped characters conforming to XML.

Parameters:
input - the input string
Returns:
the escaped string

escapeChars

public static String escapeChars(String s,
                                 String chars,
                                 char escape)

uriEscape

public static String uriEscape(String input)
Translates the given string into application/x-www-form-urlencoded format using UTF-8 as encoding scheme.


uriEscapePath

public static String uriEscapePath(String input)
Translates the given path into application/x-www-form-urlencoded format using UTF-8 as encoding scheme. This method differs from uriEscape(String)} that path component separators (/) are not encoded.

See Also:
uriEscape(String)

uriUnescape

public static String uriUnescape(String input)
Decodes the given application/x-www-form-urlencoded string using UTF-8 as encoding scheme.


htmlEscapeArgs

public static Object[] htmlEscapeArgs(Object[] args)
Escapes all XML special characters in the given array. Dates and primitive-wrappers are left as-is, all other objects are converted to their String representation and escaped using xmlEscape(String).

Since:
6.4

extractInt

public static int extractInt(String s,
                             String regex)
Extracts an integer from a String using the first capture group of the given regular expression.

Since:
6.4

stripWhitespaces

public static String stripWhitespaces(String s)
Strips whitespaces without preserving line breaks.

See Also:
stripWhitespaces(String, boolean)

stripWhitespaces

public static String stripWhitespaces(String s,
                                      boolean preserveBreaks)
Replaces consecutive whitespaces by a single space character.

Parameters:
s - The String to tidy up
preserveBreaks - Whether line breaks should be preserved

xmlEscapeDanglingAmps

public static String xmlEscapeDanglingAmps(String s)
XML-escapes dangling ampersands that are no entities.

Since:
8.0

stripTags

public static String stripTags(String s)
Removes all markup from the given String using the pattern "</?[^>]+>".

Since:
7.0

stripTagsAndSpaces

public static String stripTagsAndSpaces(String s)
Convenience method that strips tags and whitespaces.

See Also:
stripTags(String), stripWhitespaces(String)

sanitizePath

public static String sanitizePath(String s)
Calls StringUtils.cleanPath(String) and removes all occurrences of "../".

Since:
7.0

repeat

public static String repeat(String s,
                            int times)
Repeats a String the given number of times.

Since:
8.0