org.apache.velocity.tools.struts
Class MessageTool

java.lang.Object
  |
  +--org.apache.velocity.tools.struts.MessageTool
All Implemented Interfaces:
ViewTool

public class MessageTool
extends java.lang.Object
implements ViewTool

View tool that provides methods to render Struts message resources.

This class is equipped to be used with a toolbox manager, for example the ServletToolboxManager included with VelServlet. This class implements interface ViewTool, which allows a toolbox manager to pass the required context information.

This class is not thread-safe by design. A new instance is needed for the processing of every template request. This means this tool should only be used in the request scope, not application or session scopes.

Version:
$Id: MessageTool.java,v 1.3 2003/05/28 00:17:15 nbubna Exp $
Author:
Gabe Sidler

Field Summary
protected  java.util.Locale locale
          A reference to the user's locale.
protected  org.apache.struts.util.MessageResources resources
          A reference to the Struts message resources.
 
Constructor Summary
MessageTool()
          Default constructor.
 
Method Summary
 boolean exists(java.lang.String key)
          Checks if a message string for a specified message key exists for the user's locale.
 java.lang.String get(java.lang.String key)
          Looks up and returns the localized message for the specified key.
 java.lang.String get(java.lang.String key, java.util.List args)
          Same as get(String key, Object[] args), but takes a java.util.List instead of an array.
 java.lang.String get(java.lang.String key, java.lang.Object[] args)
          Looks up and returns the localized message for the specified key.
 java.util.Locale getLocale()
          Returns the user's locale.
 void init(java.lang.Object obj)
          Initializes this tool.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

resources

protected org.apache.struts.util.MessageResources resources
A reference to the Struts message resources.


locale

protected java.util.Locale locale
A reference to the user's locale.

Constructor Detail

MessageTool

public MessageTool()
Default constructor. Tool must be initialized before use.

Method Detail

init

public void init(java.lang.Object obj)
Initializes this tool.

Specified by:
init in interface ViewTool
Parameters:
obj - the current ViewContext
Throws:
java.lang.IllegalArgumentException - if the param is not a ViewContext

get

public java.lang.String get(java.lang.String key)
Looks up and returns the localized message for the specified key. The user's locale is consulted to determine the language of the message.

Parameters:
key - message key
Returns:
the localized message for the specified key or null if no such message exists

get

public java.lang.String get(java.lang.String key,
                            java.lang.Object[] args)
Looks up and returns the localized message for the specified key. Replacement parameters passed with args are inserted into the message. The user's locale is consulted to determine the language of the message.

Parameters:
key - message key
args - replacement parameters for this message
Returns:
the localized message for the specified key or null if no such message exists

get

public java.lang.String get(java.lang.String key,
                            java.util.List args)
Same as get(String key, Object[] args), but takes a java.util.List instead of an array. This is more Velocity friendly.

Parameters:
key - message key
args - replacement parameters for this message
Returns:
the localized message for the specified key or null if no such message exists

exists

public boolean exists(java.lang.String key)
Checks if a message string for a specified message key exists for the user's locale.

Parameters:
key - message key
Returns:
true if a message strings exists, false otherwise

getLocale

public java.util.Locale getLocale()
Returns the user's locale. If a locale is not found, the default locale is returned.



Copyright (c) 2003 Apache Software Foundation