View Javadoc

1   /*
2    * $Id: AppendIterator.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * &lt;s:append id="myAppendIterator"&gt;
99   *		&lt;s:param value="%{myList1}" /&gt;
100  *		&lt;s:param value="%{myList2}" /&gt;
101  *		&lt;s:param value="%{myList3}" /&gt;
102  * &lt;/s:append&gt;
103  * &lt;s:iterator value="%{#myAppendIterator}"&gt;
104  *		&lt;s:property /&gt;
105  * &lt;/s:iterator&gt;
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 	// UnnamedParametric implementation --------------------------------------
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