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