01: /*
02: * $Id: AppendIteratorFilter.java 471756 2006-11-06 15:01:43Z husted $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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: * A bean that takes several iterators and outputs them in sequence
31: *
32: * @see org.apache.struts2.components.AppendIterator
33: * @see org.apache.struts2.views.jsp.iterator.AppendIteratorTag
34: */
35: public class AppendIteratorFilter extends IteratorFilterSupport
36: implements Iterator, Action {
37:
38: List iterators = new ArrayList();
39:
40: // Attributes ----------------------------------------------------
41: List sources = new ArrayList();
42:
43: // Public --------------------------------------------------------
44: public void setSource(Object anIterator) {
45: sources.add(anIterator);
46: }
47:
48: // Action implementation -----------------------------------------
49: public String execute() {
50: // Make source transformations
51: for (int i = 0; i < sources.size(); i++) {
52: Object source = sources.get(i);
53: iterators.add(getIterator(source));
54: }
55:
56: return SUCCESS;
57: }
58:
59: // Iterator implementation ---------------------------------------
60: public boolean hasNext() {
61: if (iterators.size() > 0) {
62: return (((Iterator) iterators.get(0)).hasNext());
63: } else {
64: return false;
65: }
66: }
67:
68: public Object next() {
69: try {
70: return ((Iterator) iterators.get(0)).next();
71: } finally {
72: if (iterators.size() > 0) {
73: if (!((Iterator) iterators.get(0)).hasNext()) {
74: iterators.remove(0);
75: }
76: }
77: }
78: }
79:
80: public void remove() {
81: throw new UnsupportedOperationException();
82: }
83: }
|