You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by ma...@apache.org on 2003/10/19 00:57:24 UTC
cvs commit: jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts MessageTool.java
marino 2003/10/18 15:57:24
Modified: src/java/org/apache/velocity/tools/struts MessageTool.java
Log:
Added overloaded get() methods that include a bundle name for a given resource.
Revision Changes Path
1.5 +122 -19 jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts/MessageTool.java
Index: MessageTool.java
===================================================================
RCS file: /home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts/MessageTool.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- MessageTool.java 25 Jul 2003 16:54:40 -0000 1.4
+++ MessageTool.java 18 Oct 2003 22:57:24 -0000 1.5
@@ -69,12 +69,12 @@
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
+ * 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
@@ -90,6 +90,14 @@
// --------------------------------------------- Properties -------
+ /** A reference to the ServletContext */
+ protected ServletContext app;
+
+
+ /** A reference to the HttpServletRequest. */
+ protected HttpServletRequest request;
+
+
/**
* A reference to the Struts message resources.
*/
@@ -102,7 +110,6 @@
protected Locale locale;
-
// --------------------------------------------- Constructors -------------
/**
@@ -111,8 +118,8 @@
public MessageTool()
{
}
-
-
+
+
/**
* Initializes this tool.
*
@@ -127,11 +134,11 @@
}
ViewContext context = (ViewContext)obj;
- HttpServletRequest request = context.getRequest();
+ this.request = context.getRequest();
HttpSession session = request.getSession(false);
- ServletContext application = context.getServletContext();
+ this.app = context.getServletContext();
- this.resources = StrutsUtils.getMessageResources(request, application);
+ this.resources = StrutsUtils.getMessageResources(request, app);
this.locale = StrutsUtils.getLocale(request, session);
}
@@ -141,12 +148,12 @@
/**
* Looks up and returns the localized message for the specified key.
- * The user's locale is consulted to determine the language of the
+ * 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
+ * @return the localized message for the specified key or
* <code>null</code> if no such message exists
*/
public String get(String key)
@@ -162,14 +169,37 @@
/**
* 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
+ * The user's locale is consulted to determine the language of the
+ * message.
+ *
+ * @param key message key
+ * @param bundle The bundle name to look for.
+ *
+ * @return the localized message for the specified key or
+ * <code>null</code> if no such message exists
+ */
+ public String get(String key, String bundle)
+ {
+ MessageResources res = StrutsUtils.getMessageResources(this.request, app, bundle);
+ if (res == null)
+ {
+ Velocity.error("Message resources are not available.");
+ return null;
+ }
+ return res.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
+ * @return the localized message for the specified key or
* <code>null</code> if no such message exists
*/
public String get(String key, Object args[])
@@ -179,7 +209,7 @@
Velocity.error("Message resources are not available.");
return null;
}
-
+
// return the requested message
if (args == null)
{
@@ -191,6 +221,39 @@
}
}
+ /**
+ * 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 bundle The bundle name to look for.
+ * @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, String bundle, Object args[])
+ {
+ MessageResources res = StrutsUtils.getMessageResources(this.request, app, bundle);
+ if (res == null)
+ {
+ Velocity.error("Message resources are not available.");
+ return null;
+ }
+
+ // return the requested message
+ if (args == null)
+ {
+ return res.getMessage(locale, key);
+ }
+ else
+ {
+ return res.getMessage(locale, key, args);
+ }
+ }
/**
* Same as {@link #get(String key, Object[] args)}, but takes a
@@ -205,7 +268,47 @@
*/
public String get(String key, List args)
{
- return get(key, args.toArray());
+ return get(key, args.toArray());
+ }
+
+ /**
+ * 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 bundle The bundle name to look for.
+ * @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, String bundle, List args)
+ {
+ return get(key, bundle, args.toArray());
+ }
+
+ /**
+ * Checks if a message string for a specified message key exists
+ * for the user's locale.
+ *
+ * @param key message key
+ * @param bundle The bundle name to look for.
+ *
+ * @return <code>true</code> if a message strings exists,
+ * <code>false</code> otherwise
+ */
+ public boolean exists(String key, String bundle)
+ {
+ MessageResources res = StrutsUtils.getMessageResources(this.request, app, bundle);
+ if (res == null)
+ {
+ Velocity.error("Message resources are not available.");
+ return false;
+ }
+
+ // Return the requested message presence indicator
+ return (res.isPresent(locale, key));
}
@@ -215,7 +318,7 @@
*
* @param key message key
*
- * @return <code>true</code> if a message strings exists,
+ * @return <code>true</code> if a message strings exists,
* <code>false</code> otherwise
*/
public boolean exists(String key)
@@ -232,12 +335,12 @@
/**
- * Returns the user's locale. If a locale is not found, the default
+ * 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: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org