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> - * <i18n:getMessage key="test"/> - * <i18n:msgArg value="<%= test %>"/> - * <i18n:msgArg value="<%= test %>"/> - * </i18n:getMessage> + * <i18n:message key="test"> + * <i18n:messagegArg value="<%= test %>"/> + * <i18n:messagegArg> + * <my:someTag/> + * </i18n:messagegArg> + * </i18n:message> * 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> ------------------------------------------------------------------------
Created attachment 12933 [details] Proposed patch, as an attachment
JSTL replaced the i18n taglib, so this won't be worked on.