org.apache.velocity.app.tools

Class VelocityFormatter


public class VelocityFormatter
extends java.lang.Object

Formatting tool for inserting into the Velocity WebContext. Can format dates or lists of objects.

Here's an example of some uses:

 $formatter.formatShortDate($object.Date)
 $formatter.formatLongDate($db.getRecord(232).getDate())
 $formatter.formatArray($array)
 $formatter.limitLen(30, $object.Description)
 

Version:
$Id: VelocityFormatter.java,v 1.7 2001/10/16 23:14:29 dlr Exp $

Authors:
Sean Legassick
Daniel Rall

Field Summary

(package private) Context
context
(package private) NumberFormat
nf

Constructor Summary

VelocityFormatter(Context context)
Constructor needs a backpointer to the context.

Method Summary

String
formatArray(Object array)
Formats an array into the form "A, B and C".
String
formatArray(Object array, String delim)
Formats an array into the form "A<delim>B<delim>C".
String
formatArray(Object array, String delim, String finaldelim)
Formats an array into the form "A<delim>B<finaldelim>C".
String
formatLongDate(Date date)
Formats a date in 'long' style.
String
formatLongDateTime(Date date)
Formats a date/time in 'long' style.
String
formatShortDate(Date date)
Formats a date in 'short' style.
String
formatShortDateTime(Date date)
Formats a date/time in 'short' style.
String
formatVector(Vector vector)
Formats a vector into the form "A, B and C".
String
formatVector(Vector vector, String delim)
Formats a vector into the form "A<delim>B<delim>C".
String
formatVector(Vector vector, String delim, String finaldelim)
Formats a vector into the form "Adelim>B<finaldelim>C".
Object
isNull(Object o, Object dflt)
Returns a default value if the object passed is null.
String
limitLen(int maxlen, String string)
Limits 'string' to 'maxlen' characters.
String
limitLen(int maxlen, String string, String suffix)
Limits 'string' to 'maxlen' character.
String
makeAlternator(String name, String alt1, String alt2)
Makes an alternator object that alternates between two values.
String
makeAlternator(String name, String alt1, String alt2, String alt3)
Makes an alternator object that alternates between three values.
String
makeAlternator(String name, String alt1, String alt2, String alt3, String alt4)
Makes an alternator object that alternates between four values.
String
makeAutoAlternator(String name, String alt1, String alt2)
Makes an alternator object that alternates between two values automatically.

Field Details

context

(package private)  Context context


nf

(package private)  NumberFormat nf

Constructor Details

VelocityFormatter

public VelocityFormatter(Context context)
Constructor needs a backpointer to the context.

Parameters:
context - A Context.

Method Details

formatArray

public String formatArray(Object array)
Formats an array into the form "A, B and C".

Parameters:
array - An Object.

Returns:
A String.


formatArray

public String formatArray(Object array,
                          String delim)
Formats an array into the form "A<delim>B<delim>C".

Parameters:
array - An Object.
delim - A String.

Returns:
A String.


formatArray

public String formatArray(Object array,
                          String delim,
                          String finaldelim)
Formats an array into the form "A<delim>B<finaldelim>C".

Parameters:
array - An Object.
delim - A String.

Returns:
A String.


formatLongDate

public String formatLongDate(Date date)
Formats a date in 'long' style.

Parameters:
date - A Date.

Returns:
A String.


formatLongDateTime

public String formatLongDateTime(Date date)
Formats a date/time in 'long' style.

Parameters:
date - A Date.

Returns:
A String.


formatShortDate

public String formatShortDate(Date date)
Formats a date in 'short' style.

Parameters:
date - A Date.

Returns:
A String.


formatShortDateTime

public String formatShortDateTime(Date date)
Formats a date/time in 'short' style.

Parameters:
date - A Date.

Returns:
A String.


formatVector

public String formatVector(Vector vector)
Formats a vector into the form "A, B and C".

Parameters:
vector - A Vector.

Returns:
A String.


formatVector

public String formatVector(Vector vector,
                           String delim)
Formats a vector into the form "A<delim>B<delim>C".

Parameters:
vector - A Vector.
delim - A String.

Returns:
A String.


formatVector

public String formatVector(Vector vector,
                           String delim,
                           String finaldelim)
Formats a vector into the form "Adelim>B<finaldelim>C".

Parameters:
vector - A Vector.
delim - A String.

Returns:
A String.


isNull

public Object isNull(Object o,
                     Object dflt)
Returns a default value if the object passed is null.


limitLen

public String limitLen(int maxlen,
                       String string)
Limits 'string' to 'maxlen' characters. If the string gets curtailed, "..." is appended to it.

Parameters:
maxlen - An int with the maximum length.
string - A String.

Returns:
A String.


limitLen

public String limitLen(int maxlen,
                       String string,
                       String suffix)
Limits 'string' to 'maxlen' character. If the string gets curtailed, 'suffix' is appended to it.

Parameters:
maxlen - An int with the maximum length.
string - A String.
suffix - A String.

Returns:
A String.


makeAlternator

public String makeAlternator(String name,
                             String alt1,
                             String alt2)
Makes an alternator object that alternates between two values.

Example usage in a Velocity template:

 <table>
 $formatter.makeAlternator("rowcolor", "#c0c0c0", "#e0e0e0")
 #foreach $item in $items
 #begin
 <tr><td bgcolor="$rowcolor">$item.Name</td></tr>
 $rowcolor.alternate()
 #end
 </table>
 

Parameters:
name - The name for the alternator int the context.
alt1 - The first alternate.
alt2 - The second alternate.

Returns:
The newly created instance.


makeAlternator

public String makeAlternator(String name,
                             String alt1,
                             String alt2,
                             String alt3)
Makes an alternator object that alternates between three values.

See Also:
makeAlternator(String name, String alt1, String alt2)


makeAlternator

public String makeAlternator(String name,
                             String alt1,
                             String alt2,
                             String alt3,
                             String alt4)
Makes an alternator object that alternates between four values.

See Also:
makeAlternator(String name, String alt1, String alt2)


makeAutoAlternator

public String makeAutoAlternator(String name,
                                 String alt1,
                                 String alt2)
Makes an alternator object that alternates between two values automatically.

See Also:
makeAlternator(String name, String alt1, String alt2)


Copyright © 2003 Apache Software Foundation. All Rights Reserved.