Bug 31529 - allow string arg to given in body, not just in attribute, of messageArg tag
Summary: allow string arg to given in body, not just in attribute, of messageArg tag
Status: RESOLVED WONTFIX
Alias: None
Product: Taglibs
Classification: Unclassified
Component: I18N Taglib (show other bugs)
Version: 1.1
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-10-04 17:54 UTC by Ronald
Modified: 2009-07-10 00:36 UTC (History)
0 users



Attachments
Proposed patch, as an attachment (3.25 KB, patch)
2004-10-04 17:58 UTC, Ronald
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ronald 2004-10-04 17:54:49 UTC
It is often useful to be able pass the output of some other tag as an argument
to the message-formatter. An example is a tag that generates an appropriate url
for a link. In order to do this, I've modified the MessageArg tag to accept
either a 'value' attribute (current behaviour) _or_ a body. Here is the patch:

------------------------------------------------------------------------
--- MessageArgumentTag.java.orig        2004-10-04 10:19:11.995213714 -0700
+++ MessageArgumentTag.java     2004-10-04 10:41:08.033316344 -0700
@@ -16,23 +16,25 @@
 
 package org.apache.taglibs.i18n;
 
-import java.text.MessageFormat;
-
 import javax.servlet.jsp.JspException;
 import javax.servlet.jsp.JspTagException;
-import javax.servlet.jsp.tagext.Tag;
+import javax.servlet.jsp.tagext.BodyContent;
+import javax.servlet.jsp.tagext.BodyTag;
 import javax.servlet.jsp.tagext.TagSupport;
 
 /**
  *  This class implements is used inside a MessageTag to create an ordered
- *  list of arguments to use with java.text.MessageFormat.
+ *  list of arguments to use with java.text.MessageFormat. It must contain
+ *  either a 'value' attribute or a non-empty body.
  *  <P>
  *  <H2>Examples</H2>
  *  <PRE>
- *  &lt;i18n:getMessage key="test"/&gt;
- *  &lt;i18n:msgArg value="&lt;%= test %&gt;"/&gt;
- *  &lt;i18n:msgArg value="&lt;%= test %&gt;"/&gt;
- *  &lt;/i18n:getMessage&gt;
+ *  &lt;i18n:message key="test"&gt;
+ *    &lt;i18n:messagegArg value="&lt;%= test %&gt;"/&gt;
+ *    &lt;i18n:messagegArg&gt;
+ *      &lt;my:someTag/&gt;
+ *    &lt;/i18n:messagegArg&gt;
+ *  &lt;/i18n:message&gt;
  *  etc...
  *  </PRE>
  *  <P>
@@ -42,13 +44,34 @@
  *  @author <a href="mailto:tdawson@wamnet.com">Tim Dawson</a>
  *
  */
-public class MessageArgumentTag extends TagSupport
+public class MessageArgumentTag extends TagSupport implements BodyTag
 {
-  
+    private Object      attr;
+    private BodyContent cont;
+
+    public void setValue(Object argumentValue)
+    {
+        attr = argumentValue;
+    }
+
+    public int doStartTag()
+    {
+        return EVAL_BODY_BUFFERED;
+    }
+
+    public void setBodyContent(BodyContent b)
+    {
+        cont = b;
+    }
+
+    public void doInitBody()
+    {
+    }
+
     /**
      *  locate the parent tag and add the argument to the Message's arg list
      */
-    public void setValue(Object argumentValue) throws JspException
+    public int doEndTag() throws JspException
     {
         // Get the parent MessageTag
         MessageTag messageTag = null;
@@ -63,8 +86,21 @@
                 "i18n:msgArg tag must be nested inside a message tag.");
         }
 
+       // Check the value attribute and body
+        if (attr == null && cont == null)
+            throw new JspTagException(
+                   "i18n:msgArg tag must have either a 'value' attribute or a
body.");
+        if (attr != null && cont != null)
+            throw new JspTagException(
+                   "i18n:msgArg tag may not have both a 'value' attribute and a
body.");
+
         // now we know we're safe to add the argument
-        messageTag.addArg(argumentValue);
+        messageTag.addArg((attr != null) ? attr : cont.getString());
+
+        attr = null;
+        cont = null;
+
+        return EVAL_PAGE;
     }
 
 }
--- taglibs-i18n.tld.orig       2004-10-04 10:18:10.977434463 -0700
+++ taglibs-i18n.tld    2004-10-04 10:18:38.868419367 -0700
@@ -106,10 +106,10 @@
   <tag>
     <name>messageArg</name>
     <tagclass>org.apache.taglibs.i18n.MessageArgumentTag</tagclass>
-    <bodycontent>empty</bodycontent>
+    <bodycontent>JSP</bodycontent>
     <attribute>
       <name>value</name>
-      <required>true</required>
+      <required>false</required>
       <rtexprvalue>true</rtexprvalue>
     </attribute>
   </tag>
------------------------------------------------------------------------
Comment 1 Ronald 2004-10-04 17:58:29 UTC
Created attachment 12933 [details]
Proposed patch, as an attachment
Comment 2 Henri Yandell 2009-07-10 00:36:21 UTC
JSTL replaced the i18n taglib, so this won't be worked on.