View Javadoc

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