jstrachan    2002/09/30 10:40:16

  Modified:    jelly/src/java/org/apache/commons/jelly/tags/core
                        CoreTagLibrary.java UseBeanTag.java
  Added:       jelly/src/java/org/apache/commons/jelly/tags/core
                        UseListTag.java
  Log:
  Improved the reusability of the <useBean> tag so that it can be subclassed and 
default to a regular implementation class. This makes derivations of UseBeanTag easier 
to write.
  
  Also added a simple <useList> tag which simplifies the creation and use of Lists. 
For example to create a new List from some arbitarary 
collection/enumeration/array/iterator you can do things like
  
  <useList var="mylist" values="${foo.iterator()}"/>
  ${mylist[2]}
  
  etc.
  
  Revision  Changes    Path
  1.17      +6 -5      
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/CoreTagLibrary.java
  
  Index: CoreTagLibrary.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/CoreTagLibrary.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- CoreTagLibrary.java       24 Sep 2002 11:38:53 -0000      1.16
  +++ CoreTagLibrary.java       30 Sep 2002 17:40:16 -0000      1.17
  @@ -101,6 +101,7 @@
           registerTag("new", NewTag.class);
           registerTag("setProperties", SetPropertiesTag.class);
           registerTag("useBean", UseBeanTag.class);
  +        registerTag("useList", UseListTag.class);
           registerTag("whitespace", WhitespaceTag.class);
           registerTag("thread", ThreadTag.class);
           registerTag("file", FileTag.class);
  
  
  
  1.3       +13 -2     
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/UseBeanTag.java
  
  Index: UseBeanTag.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/UseBeanTag.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- UseBeanTag.java   24 Sep 2002 16:48:54 -0000      1.2
  +++ UseBeanTag.java   30 Sep 2002 17:40:16 -0000      1.3
  @@ -90,7 +90,7 @@
       public UseBeanTag(){
       }
   
  -    //BeanSource interface
  +    // BeanSource interface
       //-------------------------------------------------------------------------     
               
   
       /**
  @@ -141,7 +141,11 @@
               return (Class) classObject;
           }
           else if ( classObject == null ) {
  -            throw new MissingAttributeException("class");
  +            Class theClass = getDefaultClass();
  +            if (theClass == null) {
  +                throw new MissingAttributeException("class");
  +            }
  +            return theClass;
           }
           else {
               String className = classObject.toString();
  @@ -189,5 +193,12 @@
           if (var != null) {
               context.setVariable(var, bean);
           }
  +    }
  +    
  +    /**
  +     * Allows derived classes to provide a default bean implementation class
  +     */
  +    protected Class getDefaultClass() {
  +        return null;
       }
   }
  
  
  
  1.1                  
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/UseListTag.java
  
  Index: UseListTag.java
  ===================================================================
  /*
   * 
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-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 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", "Commons", 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.commons.jelly.tags.core;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  
  import org.apache.commons.jelly.expression.Expression;
  
  /** 
   * A tag which creates a List implementation and optionally 
   * adds all of the elements identified by the items attribute.
   * The exact implementation of List can be specified via the 
   * class attribute
   * </pre>
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
   * @version $Revision: 1.3 $
   */
  public class UseListTag extends UseBeanTag {
  
      private Expression items;
      
      public UseListTag(){
      }
  
      public List getList() {
          return (List) getBean();
      }
      
  
      // Implementation methods
      //-------------------------------------------------------------------------      
              
      protected void setBeanProperties(Object bean, Map attributes) throws Exception {
          Object value = attributes.remove("items");
          System.out.println( "value: " + value );
          super.setBeanProperties(bean, attributes);
          
          // #### @todo use same algorithm as evaluateAsIterator()
          List list = getList();
          if (value instanceof Iterator) {
              Iterator iter = (Iterator) value;
              while (iter.hasNext()) {
                  list.add( iter.next() );
              }
          }
      }
      
      protected void processBean(String var, Object bean) throws Exception {
          super.processBean(var, bean);
          
          List list = getList();
  
          System.out.println( "Created list: " + list + " with items: " + items );
          
          
          // if the items variable is specified lets append all the items
          if (items != null) {
              Iterator iter = items.evaluateAsIterator(context);
              while (iter.hasNext()) {
                  list.add( iter.next() );
              }
          }
      }
      
      protected Class getDefaultClass() {
          return ArrayList.class;
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to