You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Apache Wiki <wi...@apache.org> on 2006/02/16 23:53:09 UTC

[Struts Wiki] Update of "MessageTool" by MichaelJouravlev

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Struts Wiki" for change notification.

The following page has been changed by MichaelJouravlev:
http://wiki.apache.org/struts/MessageTool

The comment on the change is:
Orphaned; no docs, only the code.

------------------------------------------------------------------------------
- July 24, 2003
+ deleted
  
- Here is an updated version of the MessageTool, compatible with Struts 1.1 ... it now calls the new .getMessageResources() in StrutsUtils.
- 
- Any comments are welcome on the Velocity Developer's List <ve...@jakarta.apache.org>.
- 
- Marinó A. Jónsson
- 
- ----
- {{{ 
- { { { 
- 
- package org.apache.velocity.tools.struts;
- 
- import java.util.List;
- import java.util.Locale;
- 
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- import javax.servlet.ServletContext;
- 
- import org.apache.struts.util.MessageResources;
- import org.apache.struts.action.*;
- 
- import org.apache.velocity.app.Velocity;
- import org.apache.velocity.tools.view.context.ViewContext;
- import org.apache.velocity.tools.view.tools.ViewTool;
- 
- 
- /**
- {{{ * <p>View tool that provides methods to render Struts message resources.</p> 
-  * 
-  * <p>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.</p> 
-  * 
-  * <p>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.</p> 
-  * 
-  * @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a> 
-  * 
-  * @version $Id: MessageTool.java,v 1.3 2003/05/28 00:17:15 nbubna Exp $ 
-  */ }}}
- public class MessageTool implements ViewTool
- {
- 
- {{{    // --------------------------------------------- Properties ------- 
- 
-     /** 
-      * A reference to the Struts message resources. 
-      */ 
-     protected MessageResources resources; 
- 
- 
-     /** 
-      * A reference to the user's locale. 
-      */ 
-     protected Locale locale; 
- 
- 
- 
-     // --------------------------------------------- Constructors ------------- 
- 
-     /** 
-      * Default constructor. Tool must be initialized before use. 
-      */ 
-     public MessageTool() 
-     { 
-     } 
- 
- 
-     /** 
-      * Initializes this tool. 
-      * 
-      * @param obj the current ViewContext 
-      * @throws IllegalArgumentException if the param is not a ViewContext 
-      */ 
-     public void init(Object obj) 
-     { 
-         if (!(obj instanceof ViewContext)) 
-         { 
-             throw new IllegalArgumentException("Tool can only be initialized with a ViewContext"); 
-         } 
- 
-         ViewContext context = (ViewContext)obj; 
-         HttpServletRequest request = context.getRequest(); 
-         HttpSession session = request.getSession(false); 
-         ServletContext application = context.getServletContext(); 
- 
-         this.resources = StrutsUtils.getMessageResources(request, application); 
-         this.locale = StrutsUtils.getLocale(request, session); 
-     } 
- 
- 
- 
-     // --------------------------------------------- View Helpers ------------- 
- 
-     /** 
-      * Looks up and returns the localized message for the specified key. 
-      * The user's locale is consulted to determine the language of the 
-      * message. 
-      * 
-      * @param key message key 
-      * 
-      * @return the localized message for the specified key or 
-      * <code>null</code> if no such message exists 
-      */ 
-     public String get(String key) 
-     { 
-         if (resources == null) 
-         { 
-             Velocity.error("Message resources are not available."); 
-             return null; 
-         } 
-         return resources.getMessage(locale, key); 
-     } 
- 
- 
-     /** 
-      * Looks up and returns the localized message for the specified key. 
-      * Replacement parameters passed with <code>args</code> are 
-      * inserted into the message. The user's locale is consulted to 
-      * determine the language of the message. 
-      * 
-      * @param key message key 
-      * @param args replacement parameters for this message 
-      * 
-      * @return the localized message for the specified key or 
-      * <code>null</code> if no such message exists 
-      */ 
-     public String get(String key, Object args[]) 
-     { 
-         if (resources == null) 
-         { 
-             Velocity.error("Message resources are not available."); 
-             return null; 
-         } 
- 
-         // return the requested message 
-         if (args == null) 
-         { 
-             return resources.getMessage(locale, key); 
-         } 
-         else 
-         { 
-             return resources.getMessage(locale, key, args); 
-         } 
-     } 
- 
- 
-     /** 
-      * Same as {@link #get(String key, Object[] args)}, but takes a 
-      * <code>java.util.List</code> instead of an array. This is more 
-      * Velocity friendly. 
-      * 
-      * @param key message key 
-      * @param args replacement parameters for this message 
-      * 
-      * @return the localized message for the specified key or 
-      * <code>null</code> if no such message exists 
-      */ 
-     public String get(String key, List args) 
-     { 
-         return get(key, args.toArray()); 
-     } 
- 
- 
-     /** 
-      * Checks if a message string for a specified message key exists 
-      * for the user's locale. 
-      * 
-      * @param key message key 
-      * 
-      * @return <code>true</code> if a message strings exists, 
-      * <code>false</code> otherwise 
-      */ 
-     public boolean exists(String key) 
-     { 
-         if (resources == null) 
-         { 
-             Velocity.error("Message resources are not available."); 
-             return false; 
-         } 
- 
-         // Return the requested message presence indicator 
-         return (resources.isPresent(locale, key)); 
-     } 
- 
- 
-     /** 
-      * Returns the user's locale. If a locale is not found, the default 
-      * locale is returned. 
-      */ 
-     public Locale getLocale() 
-     { 
-         return locale; 
-     } }}}
- }
- 
- {{{ } } } 
-  }}}
- 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org