@DefaultKey(value="display") @ValidScope(value="application") public class DisplayTool extends LocaleConfig implements Serializable
Collection
, methods for truncating the string value of a reference
at a configured or specified length, methods for displaying an alternate
value when a specified value is null, a method for generating whitespace,
a "printf" type of method for formatting messages, and
methods for forcing values into "cells" of equal size (via truncation or
padding with whitespace).
Example Use:
tools.xml... <tools> <toolbox scope="application"> <tool class="org.apache.velocity.tools.generic.DisplayTool"/> </toolbox> </tools> template... #set( $list = [1..5] ) $display.list($list) $display.truncate("This is a long string.", 10) Not Null: $display.alt("not null", "--") Null: $display.alt($null, "--") output... 1, 2, 3, 4 and 5 This is... Not Null: not null Null: --
Modifier and Type | Class and Description |
---|---|
static class |
DisplayTool.Measurements
Measures the dimensions of the string given to its constructor.
|
Modifier and Type | Field and Description |
---|---|
static String |
ALLOWED_TAGS_KEY |
static String |
CELL_LENGTH_KEY |
static String |
CELL_SUFFIX_KEY |
static String |
DEFAULT_ALTERNATE_KEY |
static String |
LIST_DELIM_KEY |
static String |
LIST_FINAL_DELIM_KEY |
static String |
TRUNCATE_AT_WORD_KEY |
static String |
TRUNCATE_LENGTH_KEY |
static String |
TRUNCATE_SUFFIX_KEY |
DEFAULT_LOCALE
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY
Constructor and Description |
---|
DisplayTool() |
Modifier and Type | Method and Description |
---|---|
Object |
alt(Object checkMe)
Returns a configured default value if specified value is null.
|
Object |
alt(Object checkMe,
Object alternate)
Returns the second argument if first argument specified is null.
|
String |
br(Object obj)
Inserts HTML line break tag (<br />) in front of all newline
characters of the string value of the specified object and returns the
resulting string.
|
String |
capitalize(Object capitalizeMe)
Changes the first character of the string value of the specified object
to upper case and returns the resulting string.
|
String |
cell(Object obj)
Truncates or pads the string value of the specified object as necessary
to ensure that the returned string's length equals the default cell size.
|
String |
cell(Object obj,
int cellsize)
Truncates or pads the string value of the specified object as necessary
to ensure that the returned string's length equals the specified cell size.
|
String |
cell(Object obj,
int cellsize,
String suffix)
Truncates or pads the string value of the specified object as necessary
to ensure that the returned string's length equals the specified cell size.
|
String |
cell(Object obj,
String suffix)
Truncates or pads the string value of the specified object as necessary
to ensure that the returned string's length equals the default cell size.
|
protected void |
configure(ValueParser values)
Does the actual configuration.
|
protected String |
format(Collection list,
String delim,
String finaldelim,
String property)
Does the actual formatting of the collection.
|
String[] |
getAllowedTags() |
int |
getCellLength() |
String |
getCellSuffix() |
String |
getDefaultAlternate() |
String |
getListDelimiter() |
String |
getListFinalDelimiter() |
protected Object |
getProperty(Object object,
String property)
Safely retrieves the specified property from the specified object.
|
boolean |
getTruncateAtWord() |
int |
getTruncateLength() |
String |
getTruncateSuffix() |
String |
list(Object list)
Formats a collection or array into the form "A, B and C".
|
String |
list(Object list,
String delim)
Formats a collection or array into the form
"A<delim>B<delim>C".
|
String |
list(Object list,
String delim,
String finaldelim)
Formats a collection or array into the form
"A<delim>B<finaldelim>C".
|
String |
list(Object list,
String delim,
String finaldelim,
String property)
Formats a specified property of collection or array of objects into the
form "A<delim>B<finaldelim>C".
|
DisplayTool.Measurements |
measure(Object measureMe)
Returns the
DisplayTool.Measurements of the string value of the specified object. |
String |
message(String format,
Object... args)
Uses
MessageFormat to format the specified String with
the specified arguments. |
String |
plural(int value,
String singular)
Builds plural form of a passed word if 'value' is plural, otherwise
returns 'singular'.
|
String |
plural(int value,
String singular,
String plural)
Returns 'plural' parameter if passed 'value' is plural, otherwise
'singular' is returned.
|
String |
printf(String format,
Object... args)
Uses
String.format(Locale,String,Object...) to format the specified String
with the specified arguments. |
protected void |
setAllowedTags(String[] tags) |
protected void |
setCellLength(int maxlen) |
protected void |
setCellSuffix(String suffix) |
protected void |
setDefaultAlternate(String dflt) |
protected void |
setListDelimiter(String delim) |
protected void |
setListFinalDelimiter(String finalDelim) |
protected void |
setTruncateAtWord(boolean atWord) |
protected void |
setTruncateLength(int maxlen) |
protected void |
setTruncateSuffix(String suffix) |
String |
space(int length)
Returns a string of spaces of the specified length.
|
String |
stripTags(Object obj)
Removes HTML tags from the string value of the specified object and
returns the resulting string.
|
String |
stripTags(Object obj,
String... allowedTags)
Removes all not allowed HTML tags from the string value of the specified
object and returns the resulting string.
|
String |
truncate(Object truncateMe)
Limits the string value of 'truncateMe' to the configured max length
in characters (default is 30 characters).
|
String |
truncate(Object truncateMe,
int maxLength)
Limits the string value of 'truncateMe' to 'maxLength' characters.
|
String |
truncate(Object truncateMe,
int maxLength,
String suffix)
Limits the string value of 'truncateMe' to the specified max length in
characters.
|
String |
truncate(Object truncateMe,
int maxLength,
String suffix,
boolean defaultTruncateAtWord)
Limits the string value of 'truncateMe' to the latest complete word
within the specified maxLength.
|
String |
truncate(Object truncateMe,
String suffix)
Limits the string value of 'truncateMe' to the configured max length
in characters (default is 30 characters).
|
String |
uncapitalize(Object uncapitalizeMe)
Changes the first character of the string value of the specified object
to lower case and returns the resulting string.
|
getLocale, setLocale, toLocale
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
public static final String LIST_DELIM_KEY
public static final String LIST_FINAL_DELIM_KEY
public static final String TRUNCATE_LENGTH_KEY
public static final String TRUNCATE_SUFFIX_KEY
public static final String TRUNCATE_AT_WORD_KEY
public static final String CELL_LENGTH_KEY
public static final String CELL_SUFFIX_KEY
public static final String DEFAULT_ALTERNATE_KEY
public static final String ALLOWED_TAGS_KEY
protected void configure(ValueParser values)
configure
in class LocaleConfig
values
- configuration valuespublic String getListDelimiter()
protected void setListDelimiter(String delim)
public String getListFinalDelimiter()
protected void setListFinalDelimiter(String finalDelim)
public int getTruncateLength()
protected void setTruncateLength(int maxlen)
public String getTruncateSuffix()
protected void setTruncateSuffix(String suffix)
public boolean getTruncateAtWord()
protected void setTruncateAtWord(boolean atWord)
public String getCellSuffix()
protected void setCellSuffix(String suffix)
public int getCellLength()
protected void setCellLength(int maxlen)
public String getDefaultAlternate()
protected void setDefaultAlternate(String dflt)
public String[] getAllowedTags()
protected void setAllowedTags(String[] tags)
public String list(Object list)
list
- A collection or array.public String list(Object list, String delim)
list
- A collection or array.delim
- A String.public String list(Object list, String delim, String finaldelim)
list
- A collection or array.delim
- A String.finaldelim
- A String.public String list(Object list, String delim, String finaldelim, String property)
list
- A collection or array.delim
- A String.finaldelim
- A String.property
- An object property to format.protected String format(Collection list, String delim, String finaldelim, String property)
list
- collection of objects to formatdelim
- delimiterfinaldelim
- final delimiterproperty
- objects property to usepublic String message(String format, Object... args)
MessageFormat
to format the specified String with
the specified arguments. If there are no arguments, then the String
is returned directly. Please note that the format
required here is quite different from that of
printf(String,Object...)
.format
- format stringargs
- argumentspublic String printf(String format, Object... args)
String.format(Locale,String,Object...)
to format the specified String
with the specified arguments. Please note that the format
required here is quite different from that of
message(String,Object...)
.format
- format stringargs
- printf argumentsFormatter
public String truncate(Object truncateMe)
truncateMe
- The value to be truncated.public String truncate(Object truncateMe, int maxLength)
maxLength
- An int with the maximum length.truncateMe
- The value to be truncated.public String truncate(Object truncateMe, String suffix)
truncateMe
- The value to be truncated.suffix
- A String.public String truncate(Object truncateMe, int maxLength, String suffix)
truncateMe
- The value to be truncated.maxLength
- An int with the maximum length.suffix
- A String.public String truncate(Object truncateMe, int maxLength, String suffix, boolean defaultTruncateAtWord)
truncateMe
- The value to be truncated.maxLength
- An int with the maximum length.suffix
- A String.defaultTruncateAtWord
- Truncate at a word boundary if true.public String space(int length)
length
- the number of spaces to returnpublic String cell(Object obj)
obj
- the value to be put in the 'cell'public String cell(Object obj, int cellsize)
obj
- the value to be put in the 'cell'cellsize
- the size of the cell into which the object must be placedpublic String cell(Object obj, String suffix)
obj
- the value to be put in the 'cell'suffix
- the suffix to put at the end of any values that need truncating
to indicate that they've been truncatedpublic String cell(Object obj, int cellsize, String suffix)
obj
- the value to be put in the 'cell'cellsize
- the size of the cell into which the object must be placedsuffix
- the suffix to put at the end of any values that need truncating
to indicate that they've been truncatedpublic String capitalize(Object capitalizeMe)
capitalizeMe
- The value to be capitalized.public String uncapitalize(Object uncapitalizeMe)
uncapitalizeMe
- The value to be uncapitalized.public Object alt(Object checkMe)
checkMe
- object to checkpublic Object alt(Object checkMe, Object alternate)
checkMe
- object to checkalternate
- alternate valuepublic String br(Object obj)
obj
- source objectpublic String stripTags(Object obj)
obj
- source objectpublic String stripTags(Object obj, String... allowedTags)
obj
- source objectallowedTags
- An array of allowed tag names (i.e. "h1","br","img")public String plural(int value, String singular)
value
- countsingular
- Singular form of a word.public String plural(int value, String singular, String plural)
value
- countsingular
- Singular form of a word.plural
- Plural form of a word.protected Object getProperty(Object object, String property)
object
- target objectproperty
- target propertyIllegalArgumentException
- if it wasn't possible to get the object's property valuepublic DisplayTool.Measurements measure(Object measureMe)
DisplayTool.Measurements
of the string value of the specified object.measureMe
- object to measureDisplayTool.Measurements
objectCopyright © 2002–2021 The Apache Software Foundation. All rights reserved.