/* * based off of Jakarta's Struts' bean:write tag. */ package net.toodarkpark.taglib.html; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.TagSupport; import org.apache.struts.util.PropertyUtils; import org.apache.struts.util.RequestUtils; import org.apache.struts.util.ResponseUtils; import java.text.SimpleDateFormat; import java.text.DecimalFormat; import java.util.Date; import java.util.Calendar; /** * Tag that retrieves the specified property of the specified bean, converts * it to a String representation (if necessary), and writes it to the current * output stream, optionally filtering characters that are sensitive in HTML. * * @author Craig R. McClanahan * @version $Revision: 1.1 $ $Date: 2002/09/30 20:45:03 $ */ /* * Additions: * (1) if the property's value is a Calendar, use a user-supplied dateFormat * (2) if the property's value is a Number, use a user-supplied numberFormat * (3) if the property's value is null, use a user-supplied value instead * (4) trim a string based on a length, optionally adding ellipses (user-supplied string) */ public class HSString extends TagSupport { // Filter the rendered output for characters that are sensitive in HTML? protected boolean escapeHTML = false; protected String name = null; protected String property = null; protected String scope = null; protected String format = null; protected String valueWhenEmpty = null; protected String length = null; protected String ellipses = null; public boolean getEscapeHTML() { return escapeHTML; } public void setEscapeHTML( boolean value ) { escapeHTML = value; } public String getName() { return name; } public void setName( String value ) { name = value; } public String getProperty() { return property; } public void setProperty( String value ) { property = value; } public String getScope() { return scope; } public void setScope( String value ) { scope = value; } public String getFormat() { return format; } public void setFormat( String value ) { format = value; } public String getValueWhenEmpty() { return valueWhenEmpty; } public void setValueWhenEmpty( String value ) { valueWhenEmpty = value; } public String getLength() { return length; } public void setLength( String value ) { length = value; } public String getEllipses() { return ellipses; } public void setEllipses( String value ) { ellipses = value; } public int doStartTag() throws JspException { // Look up the requested bean (if necessary) Object value = null; String output = null; // Look up the requested item if( property == null ) { value = RequestUtils.lookup( pageContext, name, scope ); } else { value = RequestUtils.lookup( pageContext, name, property, scope ); } if( value == null ) { if( valueWhenEmpty != null ) { output = valueWhenEmpty; } } else { if( value instanceof Calendar || value instanceof Date ) { // do we have a dateFormat to use? if( format != null ) { SimpleDateFormat sdf = new SimpleDateFormat( format ); Date dateValue = ( value instanceof Calendar ? ((Calendar)value).getTime() : (Date)value ); output = sdf.format( dateValue ); } else { output = value.toString(); } } else if( value instanceof Number ) { Number numberValue = (Number)value; // use DecimalFormat to format the number based on format if( format != null ) { DecimalFormat df = new DecimalFormat( format ); if( numberValue instanceof Long || numberValue instanceof Integer || numberValue instanceof Short ) { long v = numberValue.longValue(); output = df.format( v ); } else if( numberValue instanceof Double || numberValue instanceof Float ) { float v = numberValue.floatValue(); output = df.format( v ); } } else { output = value.toString(); } } else if( value instanceof String ) { int l = -1; // do we need to 'trim' the string ? if( length != null ) { try { l = Integer.parseInt( this.getLength() ); } catch( NumberFormatException nfe ) { // length is a bean Integer li = (Integer)pageContext.findAttribute( length ); if( li != null ) { l = li.intValue(); } } } // now make sure its a given length if( l != -1 ) { // only trim the string if its length is greater than the max length that we want to show if( l < ((String)value).length() ) { // java needs a wrapper around this even though ive just made sure it can never be thrown. try { output = ((String)value).substring( 0, l ); if( ellipses != null ) { output += ellipses; } } catch( Exception sve ) { } } } } } // if output is still null, just convert it to a string via .toString(). if( output == null ) { output = value.toString(); } if( this.getEscapeHTML() == true ) { ResponseUtils.write(pageContext, ResponseUtils.filter( output ) ); } else { ResponseUtils.write( pageContext, output ); } // Continue processing this page return (SKIP_BODY); } /** * Release all allocated resources. */ public void release() { super.release(); escapeHTML = false; name = null; property = null; scope = null; format = null; valueWhenEmpty = null; length = null; } }