1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
50
51 public void addItem(Object value) {
52 getList().add(value);
53 }
54
55
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
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
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 }