001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.components;
006:
007: import java.io.Writer;
008: import java.util.ArrayList;
009: import java.util.Iterator;
010: import java.util.List;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014:
015: import com.opensymphony.webwork.components.Param.UnnamedParametric;
016: import com.opensymphony.webwork.util.AppendIteratorFilter;
017: import com.opensymphony.webwork.util.MakeIterator;
018: import com.opensymphony.xwork.util.OgnlValueStack;
019:
020: /**
021: * <!-- START SNIPPET: javadoc -->
022: * <p>Component for AppendIteratorTag, which jobs is to append iterators to form an
023: * appended iterator whereby entries goes from one iterator to another after each
024: * respective iterator is exhausted of entries.</p>
025: *
026: * <p>For example, if there are 3 iterator appended (each iterator has 3 entries),
027: * the following will be how the appended iterator entries will be arranged:</p>
028: *
029: * <ol>
030: * <li>First Entry of the First Iterator</li>
031: * <li>Second Entry of the First Iterator</li>
032: * <li>Third Entry of the First Iterator</li>
033: * <li>First Entry of the Second Iterator</li>
034: * <li>Second Entry of the Second Iterator</li>
035: * <li>Third Entry of the Second Iterator</li>
036: * <li>First Entry of the Third Iterator</li>
037: * <li>Second Entry of the Third Iterator</li>
038: * <li>Third Entry of the Third ITerator</li>
039: * </ol>
040: * <!-- END SNIPPET: javadoc -->
041: *
042: * <!-- START SNIPPET: params -->
043: * <ul>
044: * <li>id (String) - the id of which if supplied will have the resultant
045: * appended iterator stored under in the stack's context</li>
046: * </ul>
047: * <!-- END SNIPPET: params -->
048: *
049: *
050: * <!-- START SNIPPET: code -->
051: * public class AppendIteratorTagAction extends ActionSupport {
052: *
053: * private List myList1;
054: * private List myList2;
055: * private List myList3;
056: *
057: *
058: * public String execute() throws Exception {
059: *
060: * myList1 = new ArrayList();
061: * myList1.add("1");
062: * myList1.add("2");
063: * myList1.add("3");
064: *
065: * myList2 = new ArrayList();
066: * myList2.add("a");
067: * myList2.add("b");
068: * myList2.add("c");
069: *
070: * myList3 = new ArrayList();
071: * myList3.add("A");
072: * myList3.add("B");
073: * myList3.add("C");
074: *
075: * return "done";
076: * }
077: *
078: * public List getMyList1() { return myList1; }
079: * public List getMyList2() { return myList2; }
080: * public List getMyList3() { return myList3; }
081: *}
082: * <!-- END SNIPPET: code -->
083: *
084: * <!-- START SNIPPET: example -->
085: * <ww:append id="myAppendIterator">
086: * <ww:param value="%{myList1}" />
087: * <ww:param value="%{myList2}" />
088: * <ww:param value="%{myList3}" />
089: * </ww:append>
090: * <ww:iterator value="%{#myAppendIterator}">
091: * <ww:property />
092: * </ww:iterator>
093: * <!-- END SNIPPET: example -->
094: *
095: *
096: * @author tm_jee ( tm_jee (at) yahoo.co.uk )
097: * @version $Date: 2006-03-18 17:28:55 +0100 (Sat, 18 Mar 2006) $ $Id: AppendIterator.java 2468 2006-03-18 16:28:55Z rgielen $
098: * @see com.opensymphony.webwork.util.AppendIteratorFilter
099: * @see com.opensymphony.webwork.views.jsp.iterator.AppendIteratorTag
100: *
101: * @ww.tag name="append" tld-body-content="JSP" tld-tag-class="com.opensymphony.webwork.views.jsp.iterator.AppendIteratorTag"
102: * description="Append the values of a list of iterators to one iterator"
103: */
104: public class AppendIterator extends Component implements
105: UnnamedParametric {
106:
107: private static final Log _log = LogFactory
108: .getLog(AppendIterator.class);
109:
110: private AppendIteratorFilter appendIteratorFilter = null;
111: private List _parameters;
112:
113: public AppendIterator(OgnlValueStack stack) {
114: super (stack);
115: }
116:
117: public boolean start(Writer writer) {
118: _parameters = new ArrayList();
119: appendIteratorFilter = new AppendIteratorFilter();
120:
121: return super .start(writer);
122: }
123:
124: public boolean end(Writer writer, String body) {
125:
126: for (Iterator paramEntries = _parameters.iterator(); paramEntries
127: .hasNext();) {
128:
129: Object iteratorEntryObj = paramEntries.next();
130: if (!MakeIterator.isIterable(iteratorEntryObj)) {
131: _log
132: .warn("param with value resolved as "
133: + iteratorEntryObj
134: + " cannot be make as iterator, it will be ignored and hence will not appear in the merged iterator");
135: continue;
136: }
137: appendIteratorFilter.setSource(MakeIterator
138: .convert(iteratorEntryObj));
139: }
140:
141: appendIteratorFilter.execute();
142:
143: if (getId() != null && getId().length() > 0) {
144: getStack().getContext().put(getId(), appendIteratorFilter);
145: }
146:
147: appendIteratorFilter = null;
148:
149: return super .end(writer, body);
150: }
151:
152: // UnnamedParametric implementation --------------------------------------
153: public void addParameter(Object value) {
154: _parameters.add(value);
155: }
156:
157: /**
158: * the id of which if supplied will have the resultant appended iterator stored under in the stack's context
159: * @ww.tagattribute required="false"
160: */
161: public void setId(String id) {
162: super.setId(id);
163: }
164: }
|