View Javadoc

1   /*
2    * $Id: AppendIterator.java 497654 2007-01-19 00:21:57Z rgielen $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  * &lt;s:append id="myAppendIterator"&gt;
104  *      &lt;s:param value="%{myList1}" /&gt;
105  *      &lt;s:param value="%{myList2}" /&gt;
106  *      &lt;s:param value="%{myList3}" /&gt;
107  * &lt;/s:append&gt;
108  * &lt;s:iterator value="%{#myAppendIterator}"&gt;
109  *      &lt;s:property /&gt;
110  * &lt;/s:iterator&gt;
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     // UnnamedParametric implementation --------------------------------------
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