View Javadoc

    1   /*
    2    * Copyright 2002,2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.jelly.tags.core;
    17
    18  import java.util.ArrayList;
    19  import java.util.Iterator;
    20  import java.util.List;
    21  import java.util.Map;
    22
    23  import org.apache.commons.jelly.JellyTagException;
    24  import org.apache.commons.jelly.expression.Expression;
    25  import org.apache.commons.jelly.impl.CollectionTag;
    26
    27  /***
    28   * A tag which creates a List implementation and optionally
    29   * adds all of the elements identified by the items attribute.
    30   * The exact implementation of List can be specified via the
    31   * class attribute
    32   * </pre>
    33   *
    34   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    35   * @version $Revision: 155420 $
    36   */
    37  public class UseListTag extends UseBeanTag implements CollectionTag {
    38
    39      private Expression items;
    40
    41      public UseListTag(){
    42      }
    43
    44      public List getList() {
    45          return (List) getBean();
    46      }
    47
    48
    49      // CollectionTag interface
    50      //-------------------------------------------------------------------------
    51      public void addItem(Object value) {
    52          getList().add(value);
    53      }
    54
    55      // DynaTag interface
    56      //-------------------------------------------------------------------------
    57      public Class getAttributeType(String name) throws JellyTagException {
    58          if (name.equals("items")) {
    59              return Expression.class;
    60          }
    61          return super.getAttributeType(name);
    62      }
    63
    64
    65      // Implementation methods
    66      //-------------------------------------------------------------------------
    67
    68      protected void setBeanProperties(Object bean, Map attributes) throws JellyTagException {
    69          items = (Expression) attributes.remove("items");
    70          super.setBeanProperties(bean, attributes);
    71      }
    72
    73      protected void processBean(String var, Object bean) throws JellyTagException {
    74          super.processBean(var, bean);
    75
    76          List list = getList();
    77
    78          // if the items variable is specified lets append all the items
    79          if (items != null) {
    80              Iterator iter = items.evaluateAsIterator(context);
    81              while (iter.hasNext()) {
    82                  list.add( iter.next() );
    83              }
    84          }
    85      }
    86
    87      protected Class getDefaultClass() {
    88          return ArrayList.class;
    89      }
    90  }