View Javadoc

1   /*
2    * $Id: AppendIteratorFilter.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.util;
23  
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import com.opensymphony.xwork2.Action;
29  
30  
31  /***
32   * A bean that takes several iterators and outputs them in sequence
33   *
34   * @see org.apache.struts2.components.AppendIterator
35   * @see org.apache.struts2.views.jsp.iterator.AppendIteratorTag
36   */
37  public class AppendIteratorFilter extends IteratorFilterSupport implements Iterator, Action {
38  
39      List iterators = new ArrayList();
40  
41      // Attributes ----------------------------------------------------
42      List sources = new ArrayList();
43  
44  
45      // Public --------------------------------------------------------
46      public void setSource(Object anIterator) {
47          sources.add(anIterator);
48      }
49  
50      // Action implementation -----------------------------------------
51      public String execute() {
52          // Make source transformations
53          for (int i = 0; i < sources.size(); i++) {
54              Object source = sources.get(i);
55              iterators.add(getIterator(source));
56          }
57  
58          return SUCCESS;
59      }
60  
61      // Iterator implementation ---------------------------------------
62      public boolean hasNext() {
63          if (iterators.size() > 0) {
64              return (((Iterator) iterators.get(0)).hasNext());
65          } else {
66              return false;
67          }
68      }
69  
70      public Object next() {
71          try {
72              return ((Iterator) iterators.get(0)).next();
73          } finally {
74              if (iterators.size() > 0) {
75                  if (!((Iterator) iterators.get(0)).hasNext()) {
76                      iterators.remove(0);
77                  }
78              }
79          }
80      }
81  
82      public void remove() {
83          throw new UnsupportedOperationException();
84      }
85  }