[prev in list] [next in list] [prev in thread] [next in thread]
List: taglibs-dev
Subject: cvs commit: jakarta-taglibs-sandbox/unstandard/src/org/apache/taglibs/unstandard SizeTag.java
From: bayard () apache ! org
Date: 2003-07-22 19:04:09
[Download RAW message or body]
bayard 2003/07/22 12:04:09
Added: unstandard/src/org/apache/taglibs/unstandard SizeTag.java
Log:
Tag for returning the length/size of an Object[], Map, Collection or String.
While the invoke tag can do this, this makes things a little simpler.
Revision Changes Path
1.1 jakarta-taglibs-sandbox/unstandard/src/org/apache/taglibs/unstandard/SizeTag.java
Index: SizeTag.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2002 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 acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Taglibs" 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",
* "Apache Taglibs", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* 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.unstandard;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.PageContext;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
import org.apache.taglibs.standard.tag.common.core.NullAttributeException;
import java.util.Map;
import java.util.Collection;
/**
* Works out the length/size of various types of Java objects.
* <un:size target="some_var" var="length"/>
* If var is not specified, the value is output.
* Handles Strings, Collections, Maps and arrays.
*
* @author <a href="bayard@apache.org">Henri Yandell</a>
*/
public class SizeTag extends BodyTagSupport {
public static String TAG_NAME = "length";
// The EL variables, these contain the value from
// the page, may contain unparsed EL content.
private String elVar;
private String elTarget;
// These are the true properties. Note that we've
// got a collection, a boolean, and a String.
// evaluateExpressions transforms the el strings
// into these values.
private String var = "size";
private Object target;
public SizeTag() {}
public int doStartTag() throws JspException {
// Evaluate the EL attributes. See the
// JSTL spec.
evaluateExpressions();
int result = -1;
if(target != null) {
if(target instanceof Collection) {
result = ( (Collection)target ).size();
} else
if(target instanceof Map) {
result = ( (Map)target ).size();
} else
if(target instanceof String) {
result = ( (String)target ).length();
} else
if(target instanceof Collection) {
result = ( (Object[])target ).length;
}
}
if(var == null && result != -1) {
try {
JspWriter writer = pageContext.getOut();
writer.print(Integer.toString(result));
} catch (IOException e) {
throw new JspTagException("IOException occured: " +
e.getMessage());
}
} else {
pageContext.setAttribute( var, new Integer(result));
}
return EVAL_PAGE;
}
// attribute methods
public String getVar() { return elVar; }
public void setVar(String pVar) { elVar = pVar; }
public String getTarget() { return elTarget; }
public void setTarget(String pTarget) { elTarget = pTarget; }
private void evaluateExpressions() throws JspException {
// Get the Variable name to set.
if( elVar != null ) {
Object r =
ExpressionEvaluatorManager.evaluate( "var", elVar,
String.class, this,
this.pageContext );
var = ((String) r);
}
// Get the object on which to find the length
if( elTarget != null ) {
Object r =
ExpressionEvaluatorManager.evaluate( "target", elTarget,
Object.class, this,
this.pageContext );
target = ((Object) r);
}
if( target == null ) {
throw new JspTagException( "Illegal for target to evaluate to null. ");
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-dev-help@jakarta.apache.org
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic