1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.components;
22
23 import java.io.Writer;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts2.views.annotations.StrutsTag;
31 import org.apache.struts2.views.annotations.StrutsTagAttribute;
32 import org.apache.struts2.components.Param.UnnamedParametric;
33 import org.apache.struts2.util.AppendIteratorFilter;
34 import org.apache.struts2.util.MakeIterator;
35
36 import com.opensymphony.xwork2.util.ValueStack;
37
38 /***
39 * <!-- START SNIPPET: javadoc -->
40 * <p>Component for AppendIteratorTag, which jobs is to append iterators to form an
41 * appended iterator whereby entries goes from one iterator to another after each
42 * respective iterator is exhausted of entries.</p>
43 *
44 * <p>For example, if there are 3 iterator appended (each iterator has 3 entries),
45 * the following will be how the appended iterator entries will be arranged:</p>
46 *
47 * <ol>
48 * <li>First Entry of the First Iterator</li>
49 * <li>Second Entry of the First Iterator</li>
50 * <li>Third Entry of the First Iterator</li>
51 * <li>First Entry of the Second Iterator</li>
52 * <li>Second Entry of the Second Iterator</li>
53 * <li>Third Entry of the Second Iterator</li>
54 * <li>First Entry of the Third Iterator</li>
55 * <li>Second Entry of the Third Iterator</li>
56 * <li>Third Entry of the Third ITerator</li>
57 * </ol>
58 * <!-- END SNIPPET: javadoc -->
59 *
60 * <!-- START SNIPPET: params -->
61 * <ul>
62 * <li>id (String) - the id of which if supplied will have the resultant
63 * appended iterator stored under in the stack's context</li>
64 * </ul>
65 * <!-- END SNIPPET: params -->
66 *
67 *
68 * <!-- START SNIPPET: code -->
69 * public class AppendIteratorTagAction extends ActionSupport {
70 *
71 * private List myList1;
72 * private List myList2;
73 * private List myList3;
74 *
75 *
76 * public String execute() throws Exception {
77 *
78 * myList1 = new ArrayList();
79 * myList1.add("1");
80 * myList1.add("2");
81 * myList1.add("3");
82 *
83 * myList2 = new ArrayList();
84 * myList2.add("a");
85 * myList2.add("b");
86 * myList2.add("c");
87 *
88 * myList3 = new ArrayList();
89 * myList3.add("A");
90 * myList3.add("B");
91 * myList3.add("C");
92 *
93 * return "done";
94 * }
95 *
96 * public List getMyList1() { return myList1; }
97 * public List getMyList2() { return myList2; }
98 * public List getMyList3() { return myList3; }
99 *}
100 * <!-- END SNIPPET: code -->
101 *
102 * <!-- START SNIPPET: example -->
103 * <s:append id="myAppendIterator">
104 * <s:param value="%{myList1}" />
105 * <s:param value="%{myList2}" />
106 * <s:param value="%{myList3}" />
107 * </s:append>
108 * <s:iterator value="%{#myAppendIterator}">
109 * <s:property />
110 * </s:iterator>
111 * <!-- END SNIPPET: example -->
112 *
113 *
114 * @see org.apache.struts2.util.AppendIteratorFilter
115 * @see org.apache.struts2.views.jsp.iterator.AppendIteratorTag
116 *
117 */
118 @StrutsTag(name="append", tldTagClass="org.apache.struts2.views.jsp.iterator.AppendIteratorTag", description="Append the values of a list of iterators to one iterator")
119 public class AppendIterator extends Component implements UnnamedParametric {
120
121 private static final Log _log = LogFactory.getLog(AppendIterator.class);
122
123 private AppendIteratorFilter appendIteratorFilter= null;
124 private List _parameters;
125
126 public AppendIterator(ValueStack stack) {
127 super(stack);
128 }
129
130 public boolean start(Writer writer) {
131 _parameters = new ArrayList();
132 appendIteratorFilter = new AppendIteratorFilter();
133
134 return super.start(writer);
135 }
136
137 public boolean end(Writer writer, String body) {
138
139 for (Iterator paramEntries = _parameters.iterator(); paramEntries.hasNext(); ) {
140
141 Object iteratorEntryObj = paramEntries.next();
142 if (! MakeIterator.isIterable(iteratorEntryObj)) {
143 _log.warn("param with value resolved as "+iteratorEntryObj+" cannot be make as iterator, it will be ignored and hence will not appear in the merged iterator");
144 continue;
145 }
146 appendIteratorFilter.setSource(MakeIterator.convert(iteratorEntryObj));
147 }
148
149 appendIteratorFilter.execute();
150
151 if (getId() != null && getId().length() > 0) {
152 getStack().getContext().put(getId(), appendIteratorFilter);
153 }
154
155 appendIteratorFilter = null;
156
157 return super.end(writer, body);
158 }
159
160
161 public void addParameter(Object value) {
162 _parameters.add(value);
163 }
164
165 @StrutsTagAttribute(description="The id of which if supplied will have the resultant appended iterator stored under in the stack's context")
166 public void setId(String id) {
167 super.setId(id);
168 }
169 }
170
171