/*
* $Header: /home/cvs/jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/MessageTag.java,v 1.5 2002/12/17 04:18:59 glenn Exp $
* $Revision: 1.5 $
* $Date: 2002/12/17 04:18:59 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
*
* <i18n:bundle baseName="test"/> * <i18n:getMessage key="test"/> * <i18n:getMessage key="test" args="1,2,3"/> * etc... **
*
* @author Tim Dawson
*
*/
public class MessageTag extends BodyTagSupport
{
// instance variables used during processing
private String _key = null;
private String _value = null;
private ResourceBundle _bundle = null;
private String _bundleRef = null;
private Object [] _args = null;
private boolean _debug = false;
// these are reused for each message tag; luckily tags are thread-safe
private MessageFormat _messageFormat = new MessageFormat("");
private List _arguments = new ArrayList();
/**
* sets the key to use when looking up the value in the bundle
*/
public final void setKey(String key)
{
_key = key;
}
/**
* sets the bundle based on a variable defined in the page
* if neither bundle or bundleRef are specified, uses the first bundle
* defined on the page by the i18n:bundle tag
*/
public final void setBundleRef(String varName)
{
_bundleRef = varName;
}
/**
* sets the bundle to an actual ResourceBundle object
* if neither bundle or bundleRef are specified, uses the bundle most recently
* defined on the page by the i18n:bundle tag
*/
public final void setBundle(ResourceBundle aBundle)
{
_bundle = aBundle;
}
/**
* @return the bundle to use
*/
private ResourceBundle getBundle() throws JspException
{
ResourceBundle bundle = _bundle;
if ( bundle == null ) {
if ( _bundleRef != null ) {
bundle = (ResourceBundle)pageContext.getAttribute(_bundleRef);
if ( bundle == null ) {
throw new JspTagException(
"i18n:message tag, could not find bundleRef=" + _bundleRef);
}
} else {
BundleTag bundleTag = (BundleTag)this.findAncestorWithClass(this,BundleTag.class);
if (bundleTag != null) {
return bundleTag.getBundle();
}
bundle = ResourceHelper.getBundle(pageContext);
}
}
return bundle;
}
/**
* Turn debugging log messages on or off
*/
public final void setDebug(boolean value)
{
_debug = value;
}
/**
* adds to the list of arguments used when formatting the message
*/
protected final void addArg(Object arg)
{
_arguments.add(arg);
if (_debug) {
ServletContext sc = pageContext.getServletContext();
sc.log("i18n:message added arg: " + arg.toString());
}
}
/**
* allows setting all the arguments at once
*/
public final void setArgs(Object[] args)
{
_args = args;
}
/**
* clears the argument list so that sub tags can call addArgument
* clears out any previous key and value in case key is not provided,
* or the value is null
*/
public final void release()
{
super.release();
_arguments.clear();
_key = null;
_value = null;
_bundle = null;
}
/**
* locates the bundle and gets the value
*/
public final int doStartTag() throws JspException
{
// Reset value for resource bundle key
_value = null;
// ensure we have a key
if ( _key == null ) {
throw new JspTagException("i18n:message tag requires a key attribute.");
}
ResourceBundle bundle = this.getBundle();
if ( bundle == null ) {
throw new JspTagException(
"i18n:message tag, no bundle available for use.");
}
// Reset the arguments
_arguments.clear();
if ( _args != null ) {
_arguments.addAll(Arrays.asList(_args));
}
// see if the bundle has a value, if not, we default to the tag contents
try {
_value = bundle.getString(_key);
if (_debug) {
ServletContext sc = pageContext.getServletContext();
sc.log("i18n:message tag: template for " + _key + " is: " + _value);
}
} catch (java.util.MissingResourceException e) {
ServletContext sc = pageContext.getServletContext();
sc.log("i18n:message tag, value not found for key:" + _key);
}
return EVAL_BODY_TAG;
}
/**
* 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 final int doEndTag() throws JspException
{
try {
// if the value is null, use the body content
if ( _value == null ) {
_value = bodyContent.getString();
bodyContent.clear();
}
// perform parameter substitutions
if ( _value != null && _arguments != null && _arguments.size() > 0) {
// reformat the value as specified
_messageFormat.setLocale(this.getBundle().getLocale());
_messageFormat.applyPattern(_value);
_value = _messageFormat.format(_arguments.toArray());
}
if ( _value == null) {
if (_debug) {
ServletContext sc = pageContext.getServletContext();
sc.log("i18n: message: skipping null value for " + _key);
}
} else if (id != null) {
// 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) {
throw new JspTagException("i18n:message tag IO Error: " + e.getMessage());
}
// only process the body once
return EVAL_PAGE;
}
}