Class DisplayTool
- java.lang.Object
-
- org.apache.velocity.tools.generic.SafeConfig
-
- org.apache.velocity.tools.generic.LocaleConfig
-
- org.apache.velocity.tools.generic.DisplayTool
-
- All Implemented Interfaces:
java.io.Serializable
@DefaultKey("display") @ValidScope("application") public class DisplayTool extends LocaleConfig implements java.io.Serializable
Provides general utility methods for controlling the display of references. Currently, this class contains methods for "pretty printing" an array orCollection, 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: --- Since:
- VelocityTools 2.0
- Version:
- $Id: DisplayTool.java 463298 2006-10-12 16:10:32Z henning $
- Author:
- Sean Legassick, Daniel Rall, Nathan Bubna
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDisplayTool.MeasurementsMeasures the dimensions of the string given to its constructor.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringALLOWED_TAGS_KEYstatic java.lang.StringCELL_LENGTH_KEYstatic java.lang.StringCELL_SUFFIX_KEYstatic java.lang.StringDEFAULT_ALTERNATE_KEYstatic java.lang.StringLIST_DELIM_KEYstatic java.lang.StringLIST_FINAL_DELIM_KEYstatic java.lang.StringTRUNCATE_AT_WORD_KEYstatic java.lang.StringTRUNCATE_LENGTH_KEYstatic java.lang.StringTRUNCATE_SUFFIX_KEY-
Fields inherited from class org.apache.velocity.tools.generic.LocaleConfig
DEFAULT_LOCALE
-
Fields inherited from class org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY
-
-
Constructor Summary
Constructors Constructor Description DisplayTool()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Objectalt(java.lang.Object checkMe)Returns a configured default value if specified value is null.java.lang.Objectalt(java.lang.Object checkMe, java.lang.Object alternate)Returns the second argument if first argument specified is null.java.lang.Stringbr(java.lang.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.java.lang.Stringcapitalize(java.lang.Object capitalizeMe)Changes the first character of the string value of the specified object to upper case and returns the resulting string.java.lang.Stringcell(java.lang.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.java.lang.Stringcell(java.lang.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.java.lang.Stringcell(java.lang.Object obj, int cellsize, java.lang.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.java.lang.Stringcell(java.lang.Object obj, java.lang.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 voidconfigure(ValueParser values)Does the actual configuration.protected java.lang.Stringformat(java.util.Collection list, java.lang.String delim, java.lang.String finaldelim, java.lang.String property)Does the actual formatting of the collection.java.lang.String[]getAllowedTags()intgetCellLength()java.lang.StringgetCellSuffix()java.lang.StringgetDefaultAlternate()java.lang.StringgetListDelimiter()java.lang.StringgetListFinalDelimiter()protected java.lang.ObjectgetProperty(java.lang.Object object, java.lang.String property)Safely retrieves the specified property from the specified object.booleangetTruncateAtWord()intgetTruncateLength()java.lang.StringgetTruncateSuffix()java.lang.Stringlist(java.lang.Object list)Formats a collection or array into the form "A, B and C".java.lang.Stringlist(java.lang.Object list, java.lang.String delim)Formats a collection or array into the form "A<delim>B<delim>C".java.lang.Stringlist(java.lang.Object list, java.lang.String delim, java.lang.String finaldelim)Formats a collection or array into the form "A<delim>B<finaldelim>C".java.lang.Stringlist(java.lang.Object list, java.lang.String delim, java.lang.String finaldelim, java.lang.String property)Formats a specified property of collection or array of objects into the form "A<delim>B<finaldelim>C".DisplayTool.Measurementsmeasure(java.lang.Object measureMe)Returns theDisplayTool.Measurementsof the string value of the specified object.java.lang.Stringmessage(java.lang.String format, java.lang.Object... args)UsesMessageFormatto format the specified String with the specified arguments.java.lang.Stringplural(int value, java.lang.String singular)Builds plural form of a passed word if 'value' is plural, otherwise returns 'singular'.java.lang.Stringplural(int value, java.lang.String singular, java.lang.String plural)Returns 'plural' parameter if passed 'value' is plural, otherwise 'singular' is returned.java.lang.Stringprintf(java.lang.String format, java.lang.Object... args)UsesString.format(Locale,String,Object...)to format the specified String with the specified arguments.protected voidsetAllowedTags(java.lang.String[] tags)protected voidsetCellLength(int maxlen)protected voidsetCellSuffix(java.lang.String suffix)protected voidsetDefaultAlternate(java.lang.String dflt)protected voidsetListDelimiter(java.lang.String delim)protected voidsetListFinalDelimiter(java.lang.String finalDelim)protected voidsetTruncateAtWord(boolean atWord)protected voidsetTruncateLength(int maxlen)protected voidsetTruncateSuffix(java.lang.String suffix)java.lang.Stringspace(int length)Returns a string of spaces of the specified length.java.lang.StringstripTags(java.lang.Object obj)Removes HTML tags from the string value of the specified object and returns the resulting string.java.lang.StringstripTags(java.lang.Object obj, java.lang.String... allowedTags)Removes all not allowed HTML tags from the string value of the specified object and returns the resulting string.java.lang.Stringtruncate(java.lang.Object truncateMe)Limits the string value of 'truncateMe' to the configured max length in characters (default is 30 characters).java.lang.Stringtruncate(java.lang.Object truncateMe, int maxLength)Limits the string value of 'truncateMe' to 'maxLength' characters.java.lang.Stringtruncate(java.lang.Object truncateMe, int maxLength, java.lang.String suffix)Limits the string value of 'truncateMe' to the specified max length in characters.java.lang.Stringtruncate(java.lang.Object truncateMe, int maxLength, java.lang.String suffix, boolean defaultTruncateAtWord)Limits the string value of 'truncateMe' to the latest complete word within the specified maxLength.java.lang.Stringtruncate(java.lang.Object truncateMe, java.lang.String suffix)Limits the string value of 'truncateMe' to the configured max length in characters (default is 30 characters).java.lang.Stringuncapitalize(java.lang.Object uncapitalizeMe)Changes the first character of the string value of the specified object to lower case and returns the resulting string.-
Methods inherited from class org.apache.velocity.tools.generic.LocaleConfig
getLocale, setLocale, toLocale
-
Methods inherited from class org.apache.velocity.tools.generic.SafeConfig
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
-
-
-
Field Detail
-
LIST_DELIM_KEY
public static final java.lang.String LIST_DELIM_KEY
- See Also:
- Constant Field Values
-
LIST_FINAL_DELIM_KEY
public static final java.lang.String LIST_FINAL_DELIM_KEY
- See Also:
- Constant Field Values
-
TRUNCATE_LENGTH_KEY
public static final java.lang.String TRUNCATE_LENGTH_KEY
- See Also:
- Constant Field Values
-
TRUNCATE_SUFFIX_KEY
public static final java.lang.String TRUNCATE_SUFFIX_KEY
- See Also:
- Constant Field Values
-
TRUNCATE_AT_WORD_KEY
public static final java.lang.String TRUNCATE_AT_WORD_KEY
- See Also:
- Constant Field Values
-
CELL_LENGTH_KEY
public static final java.lang.String CELL_LENGTH_KEY
- See Also:
- Constant Field Values
-
CELL_SUFFIX_KEY
public static final java.lang.String CELL_SUFFIX_KEY
- See Also:
- Constant Field Values
-
DEFAULT_ALTERNATE_KEY
public static final java.lang.String DEFAULT_ALTERNATE_KEY
- See Also:
- Constant Field Values
-
ALLOWED_TAGS_KEY
public static final java.lang.String ALLOWED_TAGS_KEY
- See Also:
- Constant Field Values
-
-
Method Detail
-
configure
protected void configure(ValueParser values)
Does the actual configuration. This is protected, so subclasses may share the same ValueParser and call configure at any time, while preventing templates from doing so when configure(Map) is locked.- Overrides:
configurein classLocaleConfig- Parameters:
values- configuration values
-
getListDelimiter
public java.lang.String getListDelimiter()
-
setListDelimiter
protected void setListDelimiter(java.lang.String delim)
-
getListFinalDelimiter
public java.lang.String getListFinalDelimiter()
-
setListFinalDelimiter
protected void setListFinalDelimiter(java.lang.String finalDelim)
-
getTruncateLength
public int getTruncateLength()
-
setTruncateLength
protected void setTruncateLength(int maxlen)
-
getTruncateSuffix
public java.lang.String getTruncateSuffix()
-
setTruncateSuffix
protected void setTruncateSuffix(java.lang.String suffix)
-
getTruncateAtWord
public boolean getTruncateAtWord()
-
setTruncateAtWord
protected void setTruncateAtWord(boolean atWord)
-
getCellSuffix
public java.lang.String getCellSuffix()
-
setCellSuffix
protected void setCellSuffix(java.lang.String suffix)
-
getCellLength
public int getCellLength()
-
setCellLength
protected void setCellLength(int maxlen)
-
getDefaultAlternate
public java.lang.String getDefaultAlternate()
-
setDefaultAlternate
protected void setDefaultAlternate(java.lang.String dflt)
-
getAllowedTags
public java.lang.String[] getAllowedTags()
-
setAllowedTags
protected void setAllowedTags(java.lang.String[] tags)
-
list
public java.lang.String list(java.lang.Object list)
Formats a collection or array into the form "A, B and C".- Parameters:
list- A collection or array.- Returns:
- A String.
-
list
public java.lang.String list(java.lang.Object list, java.lang.String delim)Formats a collection or array into the form "A<delim>B<delim>C".- Parameters:
list- A collection or array.delim- A String.- Returns:
- A String.
-
list
public java.lang.String list(java.lang.Object list, java.lang.String delim, java.lang.String finaldelim)Formats a collection or array into the form "A<delim>B<finaldelim>C".- Parameters:
list- A collection or array.delim- A String.finaldelim- A String.- Returns:
- A String.
-
list
public java.lang.String list(java.lang.Object list, java.lang.String delim, java.lang.String finaldelim, java.lang.String property)Formats a specified property of collection or array of objects into the form "A<delim>B<finaldelim>C".- Parameters:
list- A collection or array.delim- A String.finaldelim- A String.property- An object property to format.- Returns:
- A String.
-
format
protected java.lang.String format(java.util.Collection list, java.lang.String delim, java.lang.String finaldelim, java.lang.String property)Does the actual formatting of the collection.- Parameters:
list- collection of objects to formatdelim- delimiterfinaldelim- final delimiterproperty- objects property to use- Returns:
- formatted string
-
message
public java.lang.String message(java.lang.String format, java.lang.Object... args)UsesMessageFormatto 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 ofprintf(String,Object...).- Parameters:
format- format stringargs- arguments- Returns:
- formatted message
- Since:
- VelocityTools 2.0
-
printf
public java.lang.String printf(java.lang.String format, java.lang.Object... args)UsesString.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 ofmessage(String,Object...).- Parameters:
format- format stringargs- printf arguments- Returns:
- formatted string
- Since:
- VelocityTools 2.0
- See Also:
Formatter
-
truncate
public java.lang.String truncate(java.lang.Object truncateMe)
Limits the string value of 'truncateMe' to the configured max length in characters (default is 30 characters). If the string gets curtailed, the configured suffix (default is "...") is used as the ending of the truncated string.- Parameters:
truncateMe- The value to be truncated.- Returns:
- A String.
-
truncate
public java.lang.String truncate(java.lang.Object truncateMe, int maxLength)Limits the string value of 'truncateMe' to 'maxLength' characters. If the string gets curtailed, the configured suffix (default is "...") is used as the ending of the truncated string.- Parameters:
maxLength- An int with the maximum length.truncateMe- The value to be truncated.- Returns:
- A String.
-
truncate
public java.lang.String truncate(java.lang.Object truncateMe, java.lang.String suffix)Limits the string value of 'truncateMe' to the configured max length in characters (default is 30 characters). If the string gets curtailed, the specified suffix is used as the ending of the truncated string.- Parameters:
truncateMe- The value to be truncated.suffix- A String.- Returns:
- A String.
-
truncate
public java.lang.String truncate(java.lang.Object truncateMe, int maxLength, java.lang.String suffix)Limits the string value of 'truncateMe' to the specified max length in characters. If the string gets curtailed, the specified suffix is used as the ending of the truncated string.- Parameters:
truncateMe- The value to be truncated.maxLength- An int with the maximum length.suffix- A String.- Returns:
- A String.
-
truncate
public java.lang.String truncate(java.lang.Object truncateMe, int maxLength, java.lang.String suffix, boolean defaultTruncateAtWord)Limits the string value of 'truncateMe' to the latest complete word within the specified maxLength. If the string gets curtailed, the specified suffix is used as the ending of the truncated string.- Parameters:
truncateMe- The value to be truncated.maxLength- An int with the maximum length.suffix- A String.defaultTruncateAtWord- Truncate at a word boundary if true.- Returns:
- A String.
-
space
public java.lang.String space(int length)
Returns a string of spaces of the specified length.- Parameters:
length- the number of spaces to return- Returns:
- string of spaces
-
cell
public java.lang.String cell(java.lang.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.- Parameters:
obj- the value to be put in the 'cell'- Returns:
- truncated/padded string
-
cell
public java.lang.String cell(java.lang.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.- Parameters:
obj- the value to be put in the 'cell'cellsize- the size of the cell into which the object must be placed- Returns:
- truncated/padded string
-
cell
public java.lang.String cell(java.lang.Object obj, java.lang.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. If truncation is necessary, the specified suffix will replace the end of the string value to indicate that.- Parameters:
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 truncated- Returns:
- truncated/padded string
-
cell
public java.lang.String cell(java.lang.Object obj, int cellsize, java.lang.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.- Parameters:
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 truncated- Returns:
- truncated/padded string
-
capitalize
public java.lang.String capitalize(java.lang.Object capitalizeMe)
Changes the first character of the string value of the specified object to upper case and returns the resulting string.- Parameters:
capitalizeMe- The value to be capitalized.- Returns:
- capitalized string
-
uncapitalize
public java.lang.String uncapitalize(java.lang.Object uncapitalizeMe)
Changes the first character of the string value of the specified object to lower case and returns the resulting string.- Parameters:
uncapitalizeMe- The value to be uncapitalized.- Returns:
- uncapitalized string
-
alt
public java.lang.Object alt(java.lang.Object checkMe)
Returns a configured default value if specified value is null.- Parameters:
checkMe- object to check- Returns:
- a configured default value if the specified value is null.
-
alt
public java.lang.Object alt(java.lang.Object checkMe, java.lang.Object alternate)Returns the second argument if first argument specified is null.- Parameters:
checkMe- object to checkalternate- alternate value- Returns:
- the second argument if the first is null.
-
br
public java.lang.String br(java.lang.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.- Parameters:
obj- source object- Returns:
- string with added br tags
-
stripTags
public java.lang.String stripTags(java.lang.Object obj)
Removes HTML tags from the string value of the specified object and returns the resulting string.- Parameters:
obj- source object- Returns:
- text stripped from HTML tags
-
stripTags
public java.lang.String stripTags(java.lang.Object obj, java.lang.String... allowedTags)Removes all not allowed HTML tags from the string value of the specified object and returns the resulting string.- Parameters:
obj- source objectallowedTags- An array of allowed tag names (i.e. "h1","br","img")- Returns:
- text stripped from given HTML tags
-
plural
public java.lang.String plural(int value, java.lang.String singular)Builds plural form of a passed word if 'value' is plural, otherwise returns 'singular'. Plural form is built using some basic English language rules for nouns which does not guarantee correct syntax of a result in all cases.- Parameters:
value- countsingular- Singular form of a word.- Returns:
- guessed plural
-
plural
public java.lang.String plural(int value, java.lang.String singular, java.lang.String plural)Returns 'plural' parameter if passed 'value' is plural, otherwise 'singular' is returned.- Parameters:
value- countsingular- Singular form of a word.plural- Plural form of a word.- Returns:
- guessed plural
-
getProperty
protected java.lang.Object getProperty(java.lang.Object object, java.lang.String property)Safely retrieves the specified property from the specified object. Subclasses that wish to perform more advanced, efficient, or just different property retrieval methods should override this method to do so.- Parameters:
object- target objectproperty- target property- Returns:
- property value
- Throws:
java.lang.IllegalArgumentException- if it wasn't possible to get the object's property value
-
measure
public DisplayTool.Measurements measure(java.lang.Object measureMe)
Returns theDisplayTool.Measurementsof the string value of the specified object.- Parameters:
measureMe- object to measure- Returns:
DisplayTool.Measurementsobject
-
-