View Javadoc

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