martinc 01/07/07 21:15:15 Modified: doc struts-logic.xml src/exercise-taglib/org/apache/struts/webapp/exercise TestBean.java web/exercise-taglib index.jsp Added: src/share/org/apache/struts/taglib/logic EmptyTag.java NotEmptyTag.java web/exercise-taglib logic-empty.jsp Log: Added two new logic tags, empty and notEmpty, which are similar to present and notPresent, except for their handling of empty strings. Also added new tests to make sure they do what they are supposed to do. Revision Changes Path 1.8 +102 -0 jakarta-struts/doc/struts-logic.xml Index: struts-logic.xml =================================================================== RCS file: /home/cvs/jakarta-struts/doc/struts-logic.xml,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- struts-logic.xml 2001/06/14 04:25:31 1.7 +++ struts-logic.xml 2001/07/08 04:15:11 1.8 @@ -69,6 +69,57 @@ <tag> + <name>empty</name> + <summary> + Evaluate the nested body content of this tag if the requested variable is + either <code>null</code> or an empty string. + </summary> + <tagclass>org.apache.struts.taglib.logic.EmptyTag</tagclass> + <bodycontent>JSP</bodycontent> + <info> + <p>This tag evaluates its nested body content only if the specified value + is either absent (i.e. <code>null</code>) or an empty string (i.e. a + <code>java.lang.String</code> with a length of zero).</p> + </info> + + <attribute> + <name>name</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <info> + <p>The variable to be compared is the JSP bean specified by this + attribute, if <code>property</code> is not specified, or the value + of the specified property of this bean, if <code>property</code> + is specified.</p> + </info> + </attribute> + + <attribute> + <name>property</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <info> + <p>The variable to be compared is the property (of the bean specified + by the <code>name</code> attribute) specified by this attribute. + The property reference can be simple, nested, and/or indexed.</p> + </info> + </attribute> + + <attribute> + <name>scope</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <info> + <p>The bean scope within which to search for the bean named by the + <code>name</code> property, or "any scope" if not specified.</p> + </info> + </attribute> + + </tag> + + + <tag> + <name>equal</name> <summary> Evaluate the nested body content of this tag if the requested @@ -834,6 +885,57 @@ <info> <p>The constant value which is checked for existence as a substring of the specified variable.</p> + </info> + </attribute> + + </tag> + + + <tag> + + <name>notEmpty</name> + <summary> + Evaluate the nested body content of this tag if the requested variable is + neither <code>null</code> nor an empty string. + </summary> + <tagclass>org.apache.struts.taglib.logic.NotEmptyTag</tagclass> + <bodycontent>JSP</bodycontent> + <info> + <p>This tag evaluates its nested body content only if the specified value + is present (i.e. not <code>null</code>) and is not an empty string (i.e. a + <code>java.lang.String</code> with a length of zero).</p> + </info> + + <attribute> + <name>name</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <info> + <p>The variable to be compared is the JSP bean specified by this + attribute, if <code>property</code> is not specified, or the value + of the specified property of this bean, if <code>property</code> + is specified.</p> + </info> + </attribute> + + <attribute> + <name>property</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <info> + <p>The variable to be compared is the property (of the bean specified + by the <code>name</code> attribute) specified by this attribute. + The property reference can be simple, nested, and/or indexed.</p> + </info> + </attribute> + + <attribute> + <name>scope</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <info> + <p>The bean scope within which to search for the bean named by the + <code>name</code> property, or "any scope" if not specified.</p> </info> </attribute> 1.3 +17 -4 jakarta-struts/src/exercise-taglib/org/apache/struts/webapp/exercise/TestBean.java Index: TestBean.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/exercise-taglib/org/apache/struts/webapp/exercise/TestBean.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- TestBean.java 2001/05/04 21:49:27 1.2 +++ TestBean.java 2001/07/08 04:15:12 1.3 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-struts/src/exercise-taglib/org/apache/struts/webapp/exercise/TestBean.java,v 1.2 2001/05/04 21:49:27 craigmcc Exp $ - * $Revision: 1.2 $ - * $Date: 2001/05/04 21:49:27 $ + * $Header: /home/cvs/jakarta-struts/src/exercise-taglib/org/apache/struts/webapp/exercise/TestBean.java,v 1.3 2001/07/08 04:15:12 martinc Exp $ + * $Revision: 1.3 $ + * $Date: 2001/07/08 04:15:12 $ * * ==================================================================== * @@ -72,7 +72,7 @@ * General purpose test bean for Struts custom tag tests. * * @author Craig R. McClanahan - * @version $Revision: 1.2 $ $Date: 2001/05/04 21:49:27 $ + * @version $Revision: 1.3 $ $Date: 2001/07/08 04:15:12 $ */ public class TestBean extends ActionForm { @@ -315,6 +315,19 @@ public void setStringProperty(String stringProperty) { this.stringProperty = stringProperty; + } + + /** + * An empty String property. + */ + private String emptyStringProperty = ""; + + public String getEmptyStringProperty() { + return (this.emptyStringProperty); + } + + public void setEmptyStringProperty(String emptyStringProperty) { + this.emptyStringProperty = emptyStringProperty; } 1.1 jakarta-struts/src/share/org/apache/struts/taglib/logic/EmptyTag.java Index: EmptyTag.java =================================================================== /* * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/logic/EmptyTag.java,v 1.1 2001/07/08 04:15:13 martinc Exp $ * $Revision: 1.1 $ * $Date: 2001/07/08 04:15:13 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2001 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", "Struts", 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.struts.taglib.logic; import javax.servlet.jsp.JspException; import org.apache.struts.util.RequestUtils; /** * Evalute the nested body content of this tag if the specified value * is empty for this request. * * @author Martin Cooper * @version $Revision: 1.1 $ $Date: 2001/07/08 04:15:13 $ */ public class EmptyTag extends ConditionalTagBase { // ------------------------------------------------------ Protected Methods /** * Evaluate the condition that is being tested by this particular tag, * and return <code>true</code> if the nested body content of this tag * should be evaluated, or <code>false</code> if it should be skipped. * This method must be implemented by concrete subclasses. * * @exception JspException if a JSP exception occurs */ protected boolean condition() throws JspException { return (condition(true)); } /** * Evaluate the condition that is being tested by this particular tag, * and return <code>true</code> if the nested body content of this tag * should be evaluated, or <code>false</code> if it should be skipped. * This method must be implemented by concrete subclasses. * * @param desired Desired outcome for a true result * * @exception JspException if a JSP exception occurs */ protected boolean condition(boolean desired) throws JspException { // Evaluate the presence of the specified value boolean empty = true; if (name != null) { Object value = RequestUtils.lookup(pageContext, name, property, scope); if (value != null) { if (value instanceof String) { String strValue = (String)value; empty = (strValue.length() < 1); } else { empty = false; } } } else { JspException e = new JspException (messages.getMessage("logic.selector")); RequestUtils.saveException(pageContext, e); throw e; } return (empty == desired); } } 1.1 jakarta-struts/src/share/org/apache/struts/taglib/logic/NotEmptyTag.java Index: NotEmptyTag.java =================================================================== /* * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/logic/NotEmptyTag.java,v 1.1 2001/07/08 04:15:13 martinc Exp $ * $Revision: 1.1 $ * $Date: 2001/07/08 04:15:13 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2001 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", "Struts", 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.struts.taglib.logic; import javax.servlet.jsp.JspException; /** * Evalute the nested body content of this tag if the specified value * is not empty for this request. * * @author Martin Cooper * @version $Revision: 1.1 $ $Date: 2001/07/08 04:15:13 $ */ public class NotEmptyTag extends EmptyTag { // ------------------------------------------------------ Protected Methods /** * Evaluate the condition that is being tested by this particular tag, * and return <code>true</code> if the nested body content of this tag * should be evaluated, or <code>false</code> if it should be skipped. * This method must be implemented by concrete subclasses. * * @exception JspException if a JSP exception occurs */ protected boolean condition() throws JspException { return (condition(false)); } } 1.3 +1 -0 jakarta-struts/web/exercise-taglib/index.jsp Index: index.jsp =================================================================== RCS file: /home/cvs/jakarta-struts/web/exercise-taglib/index.jsp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- index.jsp 2001/06/01 18:58:44 1.2 +++ index.jsp 2001/07/08 04:15:14 1.3 @@ -34,6 +34,7 @@ <li><a href="logic-iterate.jsp">Iterate Tag</a></li> <li><a href="logic-match.jsp?param1=value1">Match Tags</a></li> <li><a href="logic-present.jsp?param1=value1">Presence Tags</a></li> +<li><a href="logic-empty.jsp">Emptiness Tags</a></li> </ul> </body> 1.1 jakarta-struts/web/exercise-taglib/logic-empty.jsp Index: logic-empty.jsp =================================================================== <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <html> <head> <title>Test struts-logic Emptiness Tags</title> </head> <body bgcolor="white"> <div align="center"> <h1>Test struts-logic Emptiness Tags</h1> </div> <jsp:useBean id="bean" scope="page" class="org.apache.struts.webapp.exercise.TestBean"/> <table border="1"> <tr> <th>Test Type</th> <th>Correct Value</th> <th>Test Result</th> </tr> <tr> <td>null</td> <td>empty</td> <td> <logic:empty name="bean" property="nullProperty"> empty </logic:empty> <logic:notEmpty name="bean" property="nullProperty"> notEmpty </logic:notEmpty> </td> </tr> <tr> <td>empty string</td> <td>empty</td> <td> <logic:empty name="bean" property="emptyStringProperty"> empty </logic:empty> <logic:notEmpty name="bean" property="emptyStringProperty"> notEmpty </logic:notEmpty> </td> </tr> <tr> <td>non-empty string</td> <td>notEmpty</td> <td> <logic:empty name="bean" property="stringProperty"> empty </logic:empty> <logic:notEmpty name="bean" property="stringProperty"> notEmpty </logic:notEmpty> </td> </tr> <tr> <td>non-string object</td> <td>notEmpty</td> <td> <logic:empty name="bean" property="intProperty"> empty </logic:empty> <logic:notEmpty name="bean" property="intProperty"> notEmpty </logic:notEmpty> </td> </tr> </table> </body> </html>