org.davesag.generica.util
Class StringUtils

java.lang.Object
  extended byorg.davesag.generica.util.StringUtils

public class StringUtils
extends Object

StringUtils is a collection of handy static utility methods that help us do repetative things with Strings.

Since:
Generica 1.0
Version:
CVS Revision $Id: StringUtils.java,v 1.25 2004/04/03 09:40:38 davesag Exp $
Author:
Dave Sag

Field Summary
static String ISO_8601_DATE_FORMAT
          ISO 8601 style date format string.
static String RFC_822_DATE_FORMAT
          RFC 822 style date format string as per http://www.w3.org/Protocols/rfc822/.
static String UTC_DATE_FORMAT
          UTC style date format string.
static String UTF8
          UTF-8 Encoding format string.
 
Constructor Summary
StringUtils()
           
 
Method Summary
static List actorControllers(Map params)
          Most generica actions can accept multiple actor controllers.
static String className(Object o)
          Returns the short class name for the given object.
static String escapeMetaAndLink(String input)
          Return an escaped string where <meta, <link tags are escaped.
static String escapeString(String input)
          Return an escaped string where &, <, and > are converted to their HTML equivalents.
static List filterParamMap(String filter, Map params)
          Filters a parameter map for values whose key starts with the supplied filter.
static String fixSorts(String instancename, String sort)
          tidies up an incoming sort string for use with various hibernate queries.
static String formatError(Throwable error)
          format a throwable into a nice string for reporting to the user.
static String getDateKey(Date date)
          Return a string of "YYYYMMDD"
static String getFileExtension(String filename)
          Return the file extension for a given filename or null if no file extension is present
static String getFilename(String filename)
          Return the filename without extension for a given filename
static String getFirstLine(String input, int length)
          Extracts the first line in a given string, otherwise returns the first n bytes
static String getFormattedDate(Date date, String format)
          Return a formatted date string based on the supplied format and date.
static String getISO8601Date(Date date)
          Return a date in ISO 8601 style as per http://www.w3.org/TR/NOTE-datetime
static String getRFC822Date(Date date)
          Return a date in RFC 822 style as per http://www.w3.org/Protocols/rfc822/
static String getUTCDate(Date date)
          Return a date in UTC style as per http://www.ghcc.msfc.nasa.gov/utc.html
static String hostIp(String host)
          get the IP number for the host name provided
static String nullToBlank(String input)
          Check to see if the given input string is null and if so, return a blank string instead.
static Map paramToMap(String param)
          The parameter supplied in an ActionMapping should either just be a Controller class name in the case of simple Actions, or a ';' delimited list of name=value pairs.
static Map parseActorParams(Map params)
          Parse a Map of Action parameters and extract any actor-param s.
static String[] parseCommaList(String list)
          Parse a comma-separated list of values, consuming internal trailing white space.
static String[] parseDelimitedList(String list, String delimiter)
          Parse a delimited list of values, consuming trailing white space.
static Map parseListParams(Map params)
          Parse a Map of Action parameters and extract any list-param s.
static Map parsePresentationParams(Map params)
          Parse a Map of Action parameters and extract any presentation-param s.
static List presentationControllers(Map params)
          Most generica actions can accept multiple presentation controllers.
static String replace(String str, String pattern, String replace)
          Replace any occurances of a string pattern within a string with a different string, or Regex made easy.
static String stripNonNumerics(String input)
          strip out anything that is not a digit
static String urlDecode(String input)
          Return a URL decoded string
static String urlEncode(String input)
          Return an input string URL encoded
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UTF8

public static final String UTF8
UTF-8 Encoding format string.

See Also:
Constant Field Values

RFC_822_DATE_FORMAT

public static final String RFC_822_DATE_FORMAT
RFC 822 style date format string as per http://www.w3.org/Protocols/rfc822/.

See Also:
Constant Field Values

ISO_8601_DATE_FORMAT

public static final String ISO_8601_DATE_FORMAT
ISO 8601 style date format string. ISO 8601 [W3CDTF] date format (used in rdf)

See Also:
Constant Field Values

UTC_DATE_FORMAT

public static final String UTC_DATE_FORMAT
UTC style date format string.

See Also:
Constant Field Values
Constructor Detail

StringUtils

public StringUtils()
Method Detail

getRFC822Date

public static String getRFC822Date(Date date)
Return a date in RFC 822 style as per http://www.w3.org/Protocols/rfc822/

Parameters:
date - The Date
Returns:
String representing the Date formatted as RFC 822.

getFormattedDate

public static String getFormattedDate(Date date,
                                      String format)
Return a formatted date string based on the supplied format and date.

Parameters:
date - The Date.
format - The Date Format String.
Returns:
A formatted date string.

getISO8601Date

public static String getISO8601Date(Date date)
Return a date in ISO 8601 style as per http://www.w3.org/TR/NOTE-datetime

Parameters:
date - The Date
Returns:
String representing the Date formatted as ISO 8601.

getUTCDate

public static String getUTCDate(Date date)
Return a date in UTC style as per http://www.ghcc.msfc.nasa.gov/utc.html

Parameters:
date - Date
Returns:
String representing the Date formatted as UTC.

parseCommaList

public static String[] parseCommaList(String list)
Parse a comma-separated list of values, consuming internal trailing white space.

Parameters:
list - A list of comma-separated items.
Returns:
An Array of individual strings from the comma-separated list

parseDelimitedList

public static String[] parseDelimitedList(String list,
                                          String delimiter)
Parse a delimited list of values, consuming trailing white space.

Parameters:
list - The delimited list
delimiter - The delimiter
Returns:
An Array of andividual strings from the delimited list.

paramToMap

public static Map paramToMap(String param)
The parameter supplied in an ActionMapping should either just be a Controller class name in the case of simple Actions, or a ';' delimited list of name=value pairs. The params may be separated by any amount of white space

If the param string is broken or incomplete in any way this method will load as many params as it can make sense of and then stop. It will write to the log file for any errors.

Parameters:
param - The parameter string.
Returns:
A Map of parameters.

presentationControllers

public static List presentationControllers(Map params)
Most generica actions can accept multiple presentation controllers. This method extracts a list of presentation controller class names from the action's parameter.

you must name the controllers presentation-controller 0 ... presentation-controller n

Parameters:
params - The map of params supplied to the action.
Returns:
A List of controller names.

actorControllers

public static List actorControllers(Map params)
Most generica actions can accept multiple actor controllers. This method extracts a list of actor controller class names from the action's parameter.

you must name the controllers actor-controller 0 ... actor-controller n

Parameters:
params - The map of params supplied to the action.
Returns:
A List of controller names.

filterParamMap

public static List filterParamMap(String filter,
                                  Map params)
Filters a parameter map for values whose key starts with the supplied filter.

Parameters:
filter - The key name you are looking for.
params - The map of params supplied to the action.
Returns:
A List of param values whose key matched the filter.

parsePresentationParams

public static Map parsePresentationParams(Map params)
Parse a Map of Action parameters and extract any presentation-param s. presentation-param s can be of the form
              presentation-param = someparam;
 
or
              presentation-param0 = data:somedata;
              presentation-param1 = fish:somefish;
              ...
              presentation-paramn = whatever:somewhatever;
 
White space is ignored and the params do not need to be sequential.

Parameters:
params - The Map of Action configuration parameters.
Returns:
A Map of parameters for the Presenter s to use. Never null.

parseActorParams

public static Map parseActorParams(Map params)
Parse a Map of Action parameters and extract any actor-param s. actor-param s can be of the form
              actor-param = someparam;
 
or
              actor-param0 = data:somedata;
              actor-param1 = fish:somefish;
              ...
              actor-paramn = whatever:somewhatever;
 
White space is ignored and the params do not need to be sequential.

Parameters:
params - The Map of Action configuration parameters.
Returns:
A Map of parameters for the TargetActor s to use. Never null.

parseListParams

public static Map parseListParams(Map params)
Parse a Map of Action parameters and extract any list-param s. list-param s can be of the form
              list-param = someparam;
 
or
              list-param0 = data:somedata;
              list-param1 = fish:somefish;
              ...
              list-paramn = whatever:somewhatever;
 
White space is ignored and the params do not need to be sequential.

Parameters:
params - The Map of Action configuration parameters.
Returns:
A Map of parameters for the Lister s to use. Never null.

fixSorts

public static String fixSorts(String instancename,
                              String sort)
tidies up an incoming sort string for use with various hibernate queries.

Parameters:
instancename - the name of the instance used in the hibernate query
sort - the sort string
Returns:
a sort String with the instance name properly prepended to each part if needs be.

className

public static String className(Object o)
Returns the short class name for the given object.

Parameters:
o - The object whose class we want the name of.
Returns:
The short name.

escapeString

public static String escapeString(String input)
Return an escaped string where &, <, and > are converted to their HTML equivalents.

Parameters:
input - The unescaped string.
Returns:
An escaped version of the input string containing HTML equivalents for &, <, and >.

escapeMetaAndLink

public static String escapeMetaAndLink(String input)
Return an escaped string where <meta, <link tags are escaped.

Parameters:
input - The unescaped string.
Returns:
A version of the input string where <meta, <link tags are escaped.

replace

public static String replace(String str,
                             String pattern,
                             String replace)
Replace any occurances of a string pattern within a string with a different string, or Regex made easy.

Parameters:
str - The source string. This is the string that will be searched and have the replacements
pattern - The pattern to look for in str
replace - The string to insert in the place of pattern
Returns:
String with replace occurences

getFileExtension

public static String getFileExtension(String filename)
Return the file extension for a given filename or null if no file extension is present

Parameters:
filename - Filename
Returns:
File extension without the . or null if no file extension is present

getFilename

public static String getFilename(String filename)
Return the filename without extension for a given filename

Parameters:
filename - Filename
Returns:
Filename up to the '.', including any path data.

getDateKey

public static String getDateKey(Date date)
Return a string of "YYYYMMDD"

Parameters:
date - Date from which to extract "key"
Returns:
String of "YYYYMMDD"

getFirstLine

public static String getFirstLine(String input,
                                  int length)
Extracts the first line in a given string, otherwise returns the first n bytes

Parameters:
input - String from which to extract the first line
length - Number of bytes to return if line seperator isnot found
Returns:
the first line of the string

urlEncode

public static final String urlEncode(String input)
Return an input string URL encoded

Parameters:
input - Input string
Returns:
URL encoded string or null if either the input was null. Returns the unencoded input string if there is a encoding exception.

urlDecode

public static final String urlDecode(String input)
Return a URL decoded string

Parameters:
input - Input string
Returns:
URL decoded string or null if either the input was null or there is a decoding exception

nullToBlank

public static String nullToBlank(String input)
Check to see if the given input string is null and if so, return a blank string instead.

Parameters:
input - Input string
Returns:
Blank string if the input string is null, otherwise just return the input string

stripNonNumerics

public static String stripNonNumerics(String input)
strip out anything that is not a digit

Parameters:
input - The text to strip.
Returns:
The stripped text.

hostIp

public static String hostIp(String host)
get the IP number for the host name provided

Parameters:
host - The hostname.
Returns:
The IP number, or the hostname if the number could not be determined.

formatError

public static String formatError(Throwable error)
format a throwable into a nice string for reporting to the user.

Parameters:
error - The throwable error or exception.
Returns:
a nice, nested message with root causes.


Dave Sag Generica API version 0.5.9 - (prerelease) Copyright 2003 - 2004 Dave Sag.