craigmcc    01/02/12 17:56:08

  Modified:    src/doc  struts-bean.xml
               src/share/org/apache/struts/taglib/bean
                        LocalStrings.properties
               web/test index.jsp
  Added:       src/share/org/apache/struts/taglib/bean SizeTag.java
                        SizeTei.java
               web/test bean-size.jsp
  Log:
  Add a new <bean:size> tag that will store the size of an array,
  Collection, or Map into a page-scope bean with type java.lang.Integer.
  
  Submitted by: Hal Deadman <[EMAIL PROTECTED]>
  
  Revision  Changes    Path
  1.19      +82 -0     jakarta-struts/src/doc/struts-bean.xml
  
  Index: struts-bean.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/doc/struts-bean.xml,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- struts-bean.xml   2001/02/03 03:23:24     1.18
  +++ struts-bean.xml   2001/02/13 01:56:05     1.19
  @@ -662,6 +662,88 @@
   
     <tag>
   
  +    <name>size</name>
  +    <summary>
  +    Define a bean containing the number of elements in a Collection or Map.
  +    </summary>
  +    <tagclass>org.apache.struts.taglib.bean.SizeTag</tagclass>
  +    <teiclass>org.apache.struts.taglib.bean.SizeTei</teiclass>
  +    <bodycontent>empty</bodycontent>
  +    <info>
  +    <p>Given a reference to an array, Collection or Map, creates a new bean, of
  +    type <code>java.lang.Integer</code>, whose value is the number of elements
  +    in that collection.  You can specify the collection to be counted in any
  +    one of the following ways:</p>
  +    <ul>
  +    <li>As a runtime expression specified as the value of the
  +        <code>collection</code> attribute.</li>
  +    <li>As a JSP bean specified by the <code>name</code> attribute.</li>
  +    <li>As the property, specified by the <code>property</code> attribute,
  +        of the JSP bean specified by the <code>bean</code> attribute.</li>
  +    </ul>
  +    </info>
  +
  +    <attribute>
  +      <name>collection</name>
  +      <required>false</required>
  +      <rtexprvalue>true</rtexprvalue>
  +      <info>
  +      <p>A runtime expression that evaluates to an array, a Collection, or
  +      a Map.</p>
  +      </info>
  +    </attribute>     
  +
  +    <attribute>
  +      <name>id</name>
  +      <required>true</required>
  +      <rtexprvalue>true</rtexprvalue>
  +      <info>
  +      <p>The name of a page scope JSP bean, of type
  +      <code>java.lang.Integer</code>, that will be created to contain the
  +      size of the underlying collection being counted.</p>
  +      </info>
  +    </attribute>
  +
  +    <attribute>
  +      <name>name</name>
  +      <required>false</required>
  +      <rtexprvalue>true</rtexprvalue>
  +      <info>
  +      <p>The name of the JSP bean (optionally constrained to the scope
  +      specified by the <code>scope</code> attribute) that contains the
  +      collection to be counted (if <code>property</code> is not specified),
  +      or whose property getter is called to return the collection to be
  +      counted (if <code>property</code> is specified.</p>
  +      </info>
  +    </attribute>
  +
  +    <attribute>
  +      <name>property</name>
  +      <required>false</required>
  +      <rtexprvalue>true</rtexprvalue>
  +      <info>
  +      <p>The name of the property, of the bean specified by the
  +      <code>name</code> attribute, whose getter method will return the
  +      collection to be counted.</p>
  +      </info>
  +    </attribute>
  +
  +    <attribute>
  +      <name>scope</name>
  +      <required>false</required>
  +      <rtexprvalue>true</rtexprvalue>
  +      <info>
  +      <p>The bean scope within which to search for the JSP bean specified
  +      by the <code>name</code> attribute.  If not specified, the available
  +      scopes are searched in ascending sequence.</p>
  +      </info>
  +    </attribute>
  +
  +  </tag>
  +
  +
  +  <tag>
  +
       <name>struts</name>
       <summary>
       Expose a named Struts internal configuration object as a bean.
  
  
  
  1.10      +1 -0      
jakarta-struts/src/share/org/apache/struts/taglib/bean/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  RCS file: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/bean/LocalStrings.properties,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- LocalStrings.properties   2001/02/12 01:26:57     1.9
  +++ LocalStrings.properties   2001/02/13 01:56:06     1.10
  @@ -11,5 +11,6 @@
   page.selector=Invalid page context selector {0}
   parameter.get=No parameter {0} was included in this request
   resource.get=No resource {0} available in this application
  +size.collection=No valid collection specified for size tag
   struts.missing=No Struts internal object named {0} is available
   struts.selector=You must specify exactly one of formBean, forward, or mapping
  
  
  
  1.1                  
jakarta-struts/src/share/org/apache/struts/taglib/bean/SizeTag.java
  
  Index: SizeTag.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/bean/SizeTag.java,v 1.1 
2001/02/13 01:56:07 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2001/02/13 01:56:07 $
   *
   * ====================================================================
   *
   * 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.bean;
  
  
  import java.io.IOException;
  import java.lang.reflect.Array;
  import java.util.Collection;
  import java.util.List;
  import java.util.Map;
  import javax.servlet.jsp.JspException;
  import javax.servlet.jsp.PageContext;
  import javax.servlet.jsp.tagext.TagSupport;
  import org.apache.struts.util.MessageResources;
  import org.apache.struts.util.PropertyUtils;
  import org.apache.struts.util.RequestUtils;
  
  
  /**
   * Define a scripting variable that will contain the number of elements
   * found in a specified array, Collection, or Map.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2001/02/13 01:56:07 $
   */
  
  public class SizeTag extends TagSupport {
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * The actual collection to be counted.
       */
      protected Object collection = null;
  
      public Object getCollection() {
          return (this.collection);
      }
  
      public void setCollection(Object collection) {
          this.collection = collection;
      }
  
  
      /**
       * The name of the scripting variable that will be exposed as a page
       * scope attribute.
       */
      protected String id = null;
  
      public String getId() {
          return (this.id);
      }
  
      public void setId(String id) {
          this.id = id;
      }
  
  
      /**
       * The message resources for this package.
       */
      protected static MessageResources messages =
        MessageResources.getMessageResources
        ("org.apache.struts.taglib.logic.LocalStrings");
  
  
  
      /**
       * The name of the bean owning the property to be counted.
       */
      protected String name = null;
  
      public String getName() {
          return (this.name);
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  
      /**
       * The name of the property to be retrieved.
       */
      protected String property = null;
  
      public String getProperty() {
          return (this.property);
      }
  
      public void setProperty(String property) {
          this.property = property;
      }
  
  
      /**
       * The scope within which to search for the specified bean.
       */
      protected String scope = null;
  
      public String getScope() {
          return (this.scope);
      }
  
      public void setScope(String scope) {
          this.scope = scope;
      }
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Retrieve the required property and expose it as a scripting variable.
       *
       * @exception JspException if a JSP exception has occurred
       */
      public int doStartTag() throws JspException {
  
          // Retrieve the required property value
          Object value = this.collection;
          if (value == null)
              value = RequestUtils.lookup(pageContext, name, property, scope);
  
          // Identify the number of elements, based on the collection type
          int size = 0;
          if (value == null) {
              JspException e = new JspException
                  (messages.getMessage("size.collection"));
              RequestUtils.saveException(pageContext, e);
              throw e;
          } else if (value.getClass().isArray()) {
              size = Array.getLength(value);
          } else if (value instanceof Collection) {
              size = ((Collection) value).size();
          } else if (value instanceof Map) {
              size = ((Map) value).size();
          } else {
              JspException e = new JspException
                  (messages.getMessage("size.collection"));
              RequestUtils.saveException(pageContext, e);
              throw e;
          }
  
          // Expose this size as a scripting variable
          pageContext.setAttribute(this.id, new Integer(size),
                                   PageContext.PAGE_SCOPE);
          return (SKIP_BODY);
  
      }
  
  
      /**
       * Release all allocated resources.
       */
      public void release() {
  
          super.release();
          collection = null;
          id = null;
          name = null;
          property = null;
          scope = null;
  
      }
  
  
  }
  
  
  
  1.1                  
jakarta-struts/src/share/org/apache/struts/taglib/bean/SizeTei.java
  
  Index: SizeTei.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/bean/SizeTei.java,v 1.1 
2001/02/13 01:56:07 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2001/02/13 01:56:07 $
   *
   * ====================================================================
   *
   * 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.bean;
  
  
  import javax.servlet.jsp.tagext.TagData;
  import javax.servlet.jsp.tagext.TagExtraInfo;
  import javax.servlet.jsp.tagext.VariableInfo;
  
  
  /**
   * Implementation of <code>TagExtraInfo</code> for the <b>size</b>
   * tag, identifying the scripting object(s) to be made visible.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2001/02/13 01:56:07 $
   */
  
  public class SizeTei extends TagExtraInfo {
  
  
      /**
       * Return information about the scripting variables to be created.
       */
      public VariableInfo[] getVariableInfo(TagData data) {
  
        return new VariableInfo[] {
          new VariableInfo(data.getAttributeString("id"),
                           "java.lang.Integer",
                           true,
                           VariableInfo.AT_BEGIN)
        };
  
      }
  
  
  }
  
  
  
  1.5       +1 -0      jakarta-struts/web/test/index.jsp
  
  Index: index.jsp
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/web/test/index.jsp,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- index.jsp 2001/01/08 22:20:52     1.4
  +++ index.jsp 2001/02/13 01:56:08     1.5
  @@ -16,6 +16,7 @@
   <li><a href="bean-include.jsp">&lt;bean:include&gt;</a></li>
   <li><a 
href="bean-parameter.jsp?param1=value1&param2=value2">&lt;bean:parameter&gt;</a></li>
   <li><a href="bean-resource.jsp">&lt;bean:resource&gt;</a></li>
  +<li><a href="bean-size.jsp">&lt;bean:size&gt;</a></li>
   <li><a href="bean-write.jsp">&lt;bean:write&gt;</a></li>
   </ul>
   
  
  
  
  1.1                  jakarta-struts/web/test/bean-size.jsp
  
  Index: bean-size.jsp
  ===================================================================
  <%@ page language="java" %>
  <%@ taglib uri="/WEB-INF/struts-bean.tld"  prefix="bean" %>
  <html>
  <head>
  <title>Test struts-bean Size Tag</title>
  </head>
  <body bgcolor="white">
  
  <%
      java.util.ArrayList data = new java.util.ArrayList();
      data.add("First");
      data.add("Second");
      data.add("Third");
      data.add("Fourth");
      data.add("Fifth");
      pageContext.setAttribute("list", data, PageContext.PAGE_SCOPE);
      java.util.HashMap temp = new java.util.HashMap();
      temp.put("First", "0");
      temp.put("Second", "1");
      temp.put("Third", "2");
      temp.put("Fourth", "3");
      temp.put("Fifth", "4");
      pageContext.setAttribute("map", temp, PageContext.PAGE_SCOPE);
  %>
  <bean:size id="dataSize" collection="<%= data %>"/>
  <bean:size id="tempSize" collection="<%= temp %>"/>
  
  <div align="center">
  <h1>Test struts-bean Size Tag</h1>
  </div>
  
  <jsp:useBean id="bean" scope="page" class="org.apache.struts.test.TestBean"/>
  <bean:size id="stringSize" name="bean" property="stringArray"/>
  <bean:size id="intSize" name="bean" property="intArray"/>
  <jsp:useBean id="list" scope="page" class="java.util.ArrayList"/>
  <bean:size id="listSize" name="list"/>
  <jsp:useBean id="map" scope="page" class="java.util.HashMap"/>
  <bean:size id="mapSize" name="map"/>
  
  <table border="1">
    <tr>
      <th>Collection Type</th>
      <th>Correct Value</th>
      <th>Test Result</th>
    </tr>
    <tr>
      <td>Bean (List)</td>
      <td align="center"><%= list.size() %></td>
      <td align="center"><bean:write name="listSize"/></td>
    </tr>
    <tr>
      <td>Bean (Map)</td>
      <td align="center"><%= map.size() %></td>
      <td align="center"><bean:write name="mapSize"/></td>
    </tr>
    <tr>
      <td>Collection (List)</td>
      <td align="center"><%= data.size() %></td>
      <td align="center"><bean:write name="dataSize"/></td>
    </tr>
    <tr>
      <td>Collection (Map)</td>
      <td align="center"><%= temp.size() %></td>
      <td align="center"><bean:write name="tempSize"/></td>
    </tr>
    <tr>
      <td>Property (int[])</td>
      <td align="center"><% int intValues[] = bean.getIntArray(); 
out.print(intValues.length); %></td>
      <td align="center"><bean:write name="intSize"/></td>
    </tr>
    <tr>
      <td>Property (String[])</td>
      <td align="center"><% String stringValues[] = bean.getStringArray(); 
out.print(stringValues.length); %></td>
      <td align="center"><bean:write name="stringSize"/></td>
    </tr>
  </table>
  
  </html>
  </body>
  
  
  

Reply via email to