tdawson 01/10/15 21:02:08
Modified: i18n/src/org/apache/taglibs/i18n MessageTag.java
Log:
fixed NPE pointed out by Thomas Bruckner
added debug logging provided by Stephen Drye
added ability for message tag's body to be processed for arguments
Submitted by: Stephen Drye, Tim Dawson
Revision Changes Path
1.4 +64 -16 jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/MessageTag.java
Index: MessageTag.java
===================================================================
RCS file:
/home/cvs/jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/MessageTag.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- MessageTag.java 2001/10/14 22:09:23 1.3
+++ MessageTag.java 2001/10/16 04:02:08 1.4
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/MessageTag.java,v 1.3
2001/10/14 22:09:23 tdawson Exp $
- * $Revision: 1.3 $
- * $Date: 2001/10/14 22:09:23 $
+ * $Header:
/home/cvs/jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/MessageTag.java,v 1.4
2001/10/16 04:02:08 tdawson Exp $
+ * $Revision: 1.4 $
+ * $Date: 2001/10/16 04:02:08 $
*
* ====================================================================
*
@@ -106,7 +106,8 @@
private String _key = null;
private String _value = null;
private ResourceBundle _bundle = null;
-
+ private boolean _debug = false;
+
// these are reused for each message tag; luckily tags are thread-safe
private final MessageFormat _messageFormat = new MessageFormat("");
private final List _arguments = new ArrayList();
@@ -155,7 +156,10 @@
if ( _bundle == null )
{
BundleTag bundleTag =
(BundleTag)this.findAncestorWithClass(this,BundleTag.class);
- _bundle = bundleTag.getBundle();
+ if (bundleTag != null)
+ {
+ _bundle = bundleTag.getBundle();
+ }
}
if ( _bundle == null )
{
@@ -165,11 +169,30 @@
}
/**
+ * Turn debugging log messages on or off
+ */
+ public void setDebug(boolean value)
+ {
+ _debug = value;
+ }
+
+ public boolean getDebug()
+ {
+ return _debug;
+ }
+
+
+ /**
* adds to the list of arguments used when formatting the message
*/
public void addArg(Object arg)
{
_arguments.add(arg);
+ if (_debug)
+ {
+ ServletContext sc = pageContext.getServletContext();
+ sc.log("i18n:message added arg: " + arg.toString());
+ }
}
/**
@@ -219,6 +242,11 @@
try
{
_value = bundle.getString(key);
+ if (_debug)
+ {
+ ServletContext sc = pageContext.getServletContext();
+ sc.log("i18n: message: template for " + key + " is: " + _value);
+ }
}
catch (java.util.MissingResourceException e)
{
@@ -230,28 +258,48 @@
}
/**
- * Performs the proper runtime substitution.
+ * Performs the proper runtime substitution. If an id attribute was
+ * specified, then it is assumed that this tag is merely defining a
+ * string variable; otherwise output is provided.
*/
public int doEndTag()
throws JspException
{
try
{
- // If there is a body content to print out, do so
- if ( _value != null )
+ // if the value is null, use the body content
+ if ( _value == null )
{
- if ( _arguments != null )
+ _value = bodyContent.getString();
+ }
+
+ // perform parameter substitutions
+ if ( _value != null && _arguments != null )
+ {
+ // reformat the value as specified
+ _messageFormat.setLocale(_bundle.getLocale());
+ _messageFormat.applyPattern(_value);
+ _value = _messageFormat.format(_arguments.toArray());
+ }
+
+ if ( _value == null)
+ {
+ if (_debug)
{
- // reformat the value as specified
- _messageFormat.setLocale(_bundle.getLocale());
- _messageFormat.applyPattern(_value);
- _value = _messageFormat.format(_arguments.toArray());
+ ServletContext sc = pageContext.getServletContext();
+ String key = this.getKey();
+ sc.log("i18n: message: skipping null value for " + key);
}
- this.pageContext.getOut().print(_value);
}
- else if (bodyContent != null)
+ else if (id != null)
{
- bodyContent.writeOut(bodyContent.getEnclosingWriter());
+ // define the variable in the page context
+ pageContext.setAttribute(id,_value);
+ }
+ else
+ {
+ // print the value to the JspWriter
+ this.pageContext.getOut().print(_value);
}
}
catch (java.io.IOException e)