001: /*
002: * $Id: AppendIterator.java 497654 2007-01-19 00:21:57Z rgielen $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components;
022:
023: import java.io.Writer;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.struts2.views.annotations.StrutsTag;
031: import org.apache.struts2.views.annotations.StrutsTagAttribute;
032: import org.apache.struts2.components.Param.UnnamedParametric;
033: import org.apache.struts2.util.AppendIteratorFilter;
034: import org.apache.struts2.util.MakeIterator;
035:
036: import com.opensymphony.xwork2.util.ValueStack;
037:
038: /**
039: * <!-- START SNIPPET: javadoc -->
040: * <p>Component for AppendIteratorTag, which jobs is to append iterators to form an
041: * appended iterator whereby entries goes from one iterator to another after each
042: * respective iterator is exhausted of entries.</p>
043: *
044: * <p>For example, if there are 3 iterator appended (each iterator has 3 entries),
045: * the following will be how the appended iterator entries will be arranged:</p>
046: *
047: * <ol>
048: * <li>First Entry of the First Iterator</li>
049: * <li>Second Entry of the First Iterator</li>
050: * <li>Third Entry of the First Iterator</li>
051: * <li>First Entry of the Second Iterator</li>
052: * <li>Second Entry of the Second Iterator</li>
053: * <li>Third Entry of the Second Iterator</li>
054: * <li>First Entry of the Third Iterator</li>
055: * <li>Second Entry of the Third Iterator</li>
056: * <li>Third Entry of the Third ITerator</li>
057: * </ol>
058: * <!-- END SNIPPET: javadoc -->
059: *
060: * <!-- START SNIPPET: params -->
061: * <ul>
062: * <li>id (String) - the id of which if supplied will have the resultant
063: * appended iterator stored under in the stack's context</li>
064: * </ul>
065: * <!-- END SNIPPET: params -->
066: *
067: *
068: * <!-- START SNIPPET: code -->
069: * public class AppendIteratorTagAction extends ActionSupport {
070: *
071: * private List myList1;
072: * private List myList2;
073: * private List myList3;
074: *
075: *
076: * public String execute() throws Exception {
077: *
078: * myList1 = new ArrayList();
079: * myList1.add("1");
080: * myList1.add("2");
081: * myList1.add("3");
082: *
083: * myList2 = new ArrayList();
084: * myList2.add("a");
085: * myList2.add("b");
086: * myList2.add("c");
087: *
088: * myList3 = new ArrayList();
089: * myList3.add("A");
090: * myList3.add("B");
091: * myList3.add("C");
092: *
093: * return "done";
094: * }
095: *
096: * public List getMyList1() { return myList1; }
097: * public List getMyList2() { return myList2; }
098: * public List getMyList3() { return myList3; }
099: *}
100: * <!-- END SNIPPET: code -->
101: *
102: * <!-- START SNIPPET: example -->
103: * <s:append id="myAppendIterator">
104: * <s:param value="%{myList1}" />
105: * <s:param value="%{myList2}" />
106: * <s:param value="%{myList3}" />
107: * </s:append>
108: * <s:iterator value="%{#myAppendIterator}">
109: * <s:property />
110: * </s:iterator>
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
120: UnnamedParametric {
121:
122: private static final Log _log = LogFactory
123: .getLog(AppendIterator.class);
124:
125: private AppendIteratorFilter appendIteratorFilter = null;
126: private List _parameters;
127:
128: public AppendIterator(ValueStack stack) {
129: super (stack);
130: }
131:
132: public boolean start(Writer writer) {
133: _parameters = new ArrayList();
134: appendIteratorFilter = new AppendIteratorFilter();
135:
136: return super .start(writer);
137: }
138:
139: public boolean end(Writer writer, String body) {
140:
141: for (Iterator paramEntries = _parameters.iterator(); paramEntries
142: .hasNext();) {
143:
144: Object iteratorEntryObj = paramEntries.next();
145: if (!MakeIterator.isIterable(iteratorEntryObj)) {
146: _log
147: .warn("param with value resolved as "
148: + iteratorEntryObj
149: + " cannot be make as iterator, it will be ignored and hence will not appear in the merged iterator");
150: continue;
151: }
152: appendIteratorFilter.setSource(MakeIterator
153: .convert(iteratorEntryObj));
154: }
155:
156: appendIteratorFilter.execute();
157:
158: if (getId() != null && getId().length() > 0) {
159: getStack().getContext().put(getId(), appendIteratorFilter);
160: }
161:
162: appendIteratorFilter = null;
163:
164: return super .end(writer, body);
165: }
166:
167: // UnnamedParametric implementation --------------------------------------
168: public void addParameter(Object value) {
169: _parameters.add(value);
170: }
171:
172: @StrutsTagAttribute(description="The id of which if supplied will have the resultant appended iterator stored under in the stack's context")
173: public void setId(String id) {
174: super.setId(id);
175: }
176: }
|