jstrachan 01/04/28 22:27:48
Modified: i18n/conf taglib.tld
i18n/examples/web format.jsp
i18n/src/org/apache/taglibs/i18n FormatTagSupport.java
Added: i18n/src/org/apache/taglibs/i18n FormatStringTag.java
Log:
Added <i18n:formatString> for completeness. This just outputs the String value if it
is not null or the defaultText value if the String is null. It avoids those messy 'if
( string == null ) string=defaultText;' branches we often write in JSP. This idea was
suggested by jonathan on the taglibs-dev list.
Revision Changes Path
1.3 +20 -0 jakarta-taglibs/i18n/conf/taglib.tld
Index: taglib.tld
===================================================================
RCS file: /home/cvs/jakarta-taglibs/i18n/conf/taglib.tld,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- taglib.tld 2001/04/28 15:05:42 1.2
+++ taglib.tld 2001/04/29 05:27:47 1.3
@@ -118,6 +118,26 @@
</tag>
<tag>
+ <name>formatString</name>
+ <tagclass>org.apache.taglibs.i18n.FormatStringTag</tagclass>
+ <bodycontent>JSP</bodycontent>
+ <info>
+ Outputs a String value or displays the defaultText property if the
+ String is null. The defaultText defaults to "".
+ </info>
+ <attribute>
+ <name>value</name>
+ <required>true</required>
+ <rtexprvalue>true</rtexprvalue>
+ </attribute>
+ <attribute>
+ <name>defaultText</name>
+ <required>false</required>
+ <rtexprvalue>true</rtexprvalue>
+ </attribute>
+ </tag>
+
+ <tag>
<name>formatNumber</name>
<tagclass>org.apache.taglibs.i18n.FormatNumberTag</tagclass>
<bodycontent>JSP</bodycontent>
1.2 +20 -0 jakarta-taglibs/i18n/examples/web/format.jsp
Index: format.jsp
===================================================================
RCS file: /home/cvs/jakarta-taglibs/i18n/examples/web/format.jsp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- format.jsp 2001/04/28 15:05:43 1.1
+++ format.jsp 2001/04/29 05:27:47 1.2
@@ -58,5 +58,25 @@
</i18n:locale>
+
+<h2><formatString> example</h2>
+
+<%
+ String s = "hello";
+ String s2 = null;
+%>
+
+<p>A non-null String is:
+ <b>
+ <i18n:formatString value="<%= s %>"/>
+ </b>
+</p>
+
+<p>A null String is:
+ <b>
+ <i18n:formatString value="<%= s2 %>" defaultText="the String was null"/>
+ </b>
+</p>
+
</body>
</html>
1.2 +6 -6
jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/FormatTagSupport.java
Index: FormatTagSupport.java
===================================================================
RCS file:
/home/cvs/jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/FormatTagSupport.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FormatTagSupport.java 2001/04/28 15:05:44 1.1
+++ FormatTagSupport.java 2001/04/29 05:27:48 1.2
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/FormatTagSupport.java,v 1.1
2001/04/28 15:05:44 jstrachan Exp $
- * $Revision: 1.1 $
- * $Date: 2001/04/28 15:05:44 $
+ * $Header:
/home/cvs/jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/FormatTagSupport.java,v 1.2
2001/04/29 05:27:48 jstrachan Exp $
+ * $Revision: 1.2 $
+ * $Date: 2001/04/29 05:27:48 $
*
* ====================================================================
*
@@ -78,15 +78,15 @@
* implementation inheritence.
*
* @author James Strachan
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public abstract class FormatTagSupport extends TagSupport {
/** the value to be formatted */
private Object value;
- /** Holds value of property locale. */
+ /** the locale used to format the value */
private Locale locale;
- /** Holds value of property defaultText. */
+ /** the text output if the value is null */
private String defaultText = "";
1.1
jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/FormatStringTag.java
Index: FormatStringTag.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/FormatStringTag.java,v 1.1
2001/04/29 05:27:48 jstrachan Exp $
* $Revision: 1.1 $
* $Date: 2001/04/29 05:27:48 $
*
* ====================================================================
*
* 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 [EMAIL PROTECTED]
*
* 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
* <http://www.apache.org/>.
*
*/
package org.apache.taglibs.i18n;
import java.io.IOException;
import java.text.Format;
import java.text.DateFormat;
import java.util.Enumeration;
import java.util.Locale;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
/** A simple tag that allows a String to be output with null handling.
*
* @author James Strachan
* @version $Revision: 1.1 $
*/
public class FormatStringTag extends TagSupport {
/** the value to be formatted */
private String value;
/** the text output if the value is null */
private String defaultText = "";
public FormatStringTag() {
}
// Tag interface
//-------------------------------------------------------------------------
public int doStartTag() throws JspException {
return EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException {
try {
JspWriter out = pageContext.getOut();
String text = getValue();
if ( text == null ) {
text = getDefaultText();
if ( text == null ) {
text = "";
}
}
out.print( text );
}
catch ( IOException e ) {
handleIOException( e );
}
return EVAL_PAGE;
}
public void release() {
super.release();
value = null;
defaultText = "";
}
// Properties
//-------------------------------------------------------------------------
public String getValue() {
return value;
}
public void setValue( String value ) {
this.value = value;
}
public String getDefaultText() {
return defaultText;
}
public void setDefaultText( String defaultText ) {
this.defaultText = defaultText;
}
// Implementation methods
//-------------------------------------------------------------------------
protected void handleIOException( IOException e ) throws JspException {
pageContext.getServletContext().log( "Caught: IOException: " + e );
throw new JspException( "IOException: " + e );
}
}