KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > struts > taglib > logic > ExIterateTag


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.ext.struts.taglib.logic;
66
67 import com.jcorporate.expresso.core.controller.Block;
68 import com.jcorporate.expresso.core.controller.ControllerElement;
69 import com.jcorporate.expresso.core.misc.StringUtil;
70 import com.jcorporate.expresso.ext.struts.taglib.ControllerUtils;
71 import org.apache.log4j.Logger;
72 import org.apache.struts.taglib.logic.IterateTag;
73 import org.apache.struts.util.RequestUtils;
74
75 import javax.servlet.jsp.JspException JavaDoc;
76 import java.util.ArrayList JavaDoc;
77 import java.util.Vector JavaDoc;
78
79
80 /**
81  * Custom tag that iterates the elements of a collection, which can be
82  * either an attribute or the property of an attribute. The collection
83  * can be any of the following: An Input, Output, Block, or Transition with
84  * nested elements.
85  * <p/>
86  * <p/>
87  * tag atteribute <b>property</b> should be set to
88  * <p/>
89  * <ul>
90  * <li>"input" - iterates through all <code>Input</code> controller elements.</li>
91  * <li>"output" - iterates through all <code>Output</code> controller elements.</li>
92  * <li>"transition" - iterates through all <code>Transition</code> controller elements.</li>
93  * <li>"block" - iterates through all <code>Block</code> controller elements.</li>
94  * <li>"nested" - iterates through <em>all</em> controller elements generically.</li>
95  * </ul>
96  * </p>
97  * <p/>
98  * <p> Documentation Peter Pilgrim Wed Dec 04 14:45:44 GMT 2002
99  * </p>
100  * <p/>
101  * </p>
102  *
103  * @author Craig R. McClanahan
104  */

105 public class ExIterateTag
106         extends IterateTag {
107     private static Logger log = Logger.getLogger("expresso.ext.struts.taglib.logic.ExIterateTag");
108
109     public ExIterateTag() {
110         super();
111     }
112
113     /**
114      * Construct an iterator for the specified collection, and begin
115      * looping through the body once per element.
116      *
117      * @return integer as specified by the JSP Taglib spec
118      * @throws JspException if a JSP exception has occurred
119      */

120     public int doStartTag()
121             throws JspException JavaDoc {
122         Vector JavaDoc v = null;
123
124         if (property == null) {
125             if (name == null) {
126                 throw new JspException JavaDoc("Property and name may not both be null");
127             }
128
129             ControllerElement ce = ControllerUtils.findElement(pageContext,
130                     name, null);
131             collection = ce;
132
133             if (ce != null) {
134                 iterator = ce.getNestedIterator();
135             } else {
136                 throw new JspException JavaDoc("No such ControllerElement '" +
137                         name + "' in session");
138             }
139         } else if (property.equalsIgnoreCase("blocks")) {
140             if (name != null) {
141                 ControllerElement ce = ControllerUtils.findElement(pageContext, name, null);
142
143                 if (ce instanceof Block) {
144                     Block b = (Block) ce;
145                     collection = ce;
146                     v = b.getBlocks();
147
148                     if (v != null) {
149                         iterator = new ArrayList JavaDoc(v).iterator();
150                     }
151                 } else {
152                     throw new JspException JavaDoc("You cannot request nested blocks from '" +
153                             name + "', it is not a block");
154                 }
155             } else {
156                 v = ControllerUtils.getResponse(pageContext).getBlocks();
157
158                 if (v != null) {
159                     collection = v;
160                     iterator = new ArrayList JavaDoc(v).iterator();
161                 }
162             }
163         } else if (property.equalsIgnoreCase("transitions")) {
164             if (name != null) {
165                 ControllerElement ce = ControllerUtils.findElement(pageContext, name, null);
166
167                 if (ce instanceof Block) {
168                     Block b = (Block) ce;
169                     collection = ce;
170                     v = b.getTransitions();
171
172                     if (v != null) {
173                         iterator = new ArrayList JavaDoc(v).iterator();
174                     }
175                 } else {
176                     throw new JspException JavaDoc("You cannot request nested blocks from '" +
177                             name + "', it is not a block");
178                 }
179             } else {
180                 v = ControllerUtils.getResponse(pageContext).getTransitions();
181
182                 if (v != null) {
183                     collection = v;
184                     iterator = new ArrayList JavaDoc(v).iterator();
185                 }
186             }
187         } else if (property.equalsIgnoreCase("inputs")) {
188             if (name != null) {
189                 ControllerElement ce = ControllerUtils.findElement(pageContext, name, null);
190
191                 if (ce instanceof Block) {
192                     Block b = (Block) ce;
193                     collection = ce;
194                     v = b.getInputs();
195
196                     if (v != null) {
197                         iterator = new ArrayList JavaDoc(v).iterator();
198                     }
199                 } else {
200                     throw new JspException JavaDoc("You cannot request nested blocks from '" +
201                             name + "', it is not a block");
202                 }
203             } else {
204                 v = ControllerUtils.getResponse(pageContext).getInputs();
205
206                 if (v != null) {
207                     collection = v;
208                     iterator = new ArrayList JavaDoc(v).iterator();
209                 }
210             }
211
212         } else if (property.equalsIgnoreCase("outputs")) {
213             if (log.isDebugEnabled()) {
214                 log.debug("Iterating over all outputs");
215             }
216
217             if (name != null) {
218                 ControllerElement ce = ControllerUtils.findElement(pageContext, name, null);
219
220                 if (ce instanceof Block) {
221                     Block b = (Block) ce;
222                     collection = ce;
223                     v = b.getOutputs();
224
225                     if (v != null) {
226                         iterator = new ArrayList JavaDoc(v).iterator();
227                     }
228                 } else {
229                     throw new JspException JavaDoc("You cannot request nested blocks from '" +
230                             name + "', it is not a block");
231                 }
232             } else {
233                 v = ControllerUtils.getResponse(pageContext).getOutputs();
234
235                 if (v != null) {
236                     collection = v;
237                     iterator = new ArrayList JavaDoc(v).iterator();
238                 } else {
239                     if (log.isDebugEnabled()) {
240                         log.debug("Got no outputs!");
241                     }
242                 }
243             }
244
245
246         } else if (property.equalsIgnoreCase("nested")) {
247             if (log.isDebugEnabled()) {
248                 log.debug("Iterating over all nested controller elements generically");
249             }
250
251             if (name != null) {
252                 ControllerElement ce = ControllerUtils.findElement(pageContext, name, null);
253
254                 // Get all nested controller elements
255
// Peter Pilgrim Wed Dec 04 15:14:31 GMT 2002
256
v = ce.getNested();
257                 if (v != null) {
258                     iterator = new ArrayList JavaDoc(v).iterator();
259                 } else {
260                     throw new JspException JavaDoc("The controller element '" +
261                             name + "', does not contain any nested elements");
262                 }
263             } else {
264                 // Get all nested elements from the controller generically
265
// Unfortunate we must retrieve every single response
266
// generated by the controller.
267
// Peter Pilgrim Wed Dec 04 15:14:31 GMT 2002
268

269                 v = new Vector JavaDoc();
270                 Vector JavaDoc tmp = ControllerUtils.getResponse(pageContext).getInputs();
271                 if (tmp != null) {
272                     v.addAll(tmp);
273                 }
274                 tmp = ControllerUtils.getResponse(pageContext).getOutputs();
275                 if (tmp != null) {
276                     v.addAll(tmp);
277                 }
278                 tmp = ControllerUtils.getResponse(pageContext).getTransitions();
279                 if (tmp != null) {
280                     v.addAll(tmp);
281                 }
282                 tmp = ControllerUtils.getResponse(pageContext).getBlocks();
283                 if (tmp != null) {
284                     v.addAll(tmp);
285                 }
286
287                 collection = v;
288                 iterator = new ArrayList JavaDoc(v).iterator();
289                 if (v.size() < 0 && log.isDebugEnabled()) {
290                     log.debug("MVC Controller did not generate any controller elements!");
291                 }
292             }
293         } else {
294             ControllerElement ce = ControllerUtils.findElement(pageContext,
295                     name,
296                     property);
297             collection = ce;
298
299             if (ce != null) {
300                 iterator = ce.getNestedIterator();
301             }
302         }
303         if (collection == null) {
304             JspException JavaDoc e = new JspException JavaDoc(messages.getMessage("iterate.collection"));
305             RequestUtils.saveException(pageContext, e);
306             throw e;
307         }
308         // Calculate the starting offset
309
if (offset == null) {
310             offsetValue = 0;
311         } else {
312             try {
313                 offsetValue = Integer.parseInt(offset);
314             } catch (NumberFormatException JavaDoc e) {
315                 Integer JavaDoc offsetObject = (Integer JavaDoc) pageContext.findAttribute(offset);
316
317                 if (offsetObject == null) {
318                     offsetValue = 0;
319                 } else {
320                     offsetValue = offsetObject.intValue();
321                 }
322             }
323         }
324         if (offsetValue < 0) {
325             offsetValue = 0;
326         }
327         // Calculate the rendering length
328
if (length == null) {
329             lengthValue = 0;
330         } else {
331             try {
332                 lengthValue = Integer.parseInt(length);
333             } catch (NumberFormatException JavaDoc e) {
334                 Integer JavaDoc lengthObject = (Integer JavaDoc) pageContext.findAttribute(length);
335
336                 if (lengthObject == null) {
337                     lengthValue = 0;
338                 } else {
339                     lengthValue = lengthObject.intValue();
340                 }
341             }
342         }
343         if (lengthValue < 0) {
344             lengthValue = 0;
345         }
346
347         lengthCount = 0;
348
349         // Skip the leading elements up to the starting offset
350
for (int i = 0; i < offsetValue; i++) {
351             if (iterator.hasNext()) {
352                 iterator.next();
353             }
354         }
355         if (iterator == null) {
356             throw new JspException JavaDoc("No nested elements for bean '" +
357                     StringUtil.notNull(name) + "', property '" +
358                     StringUtil.notNull(property) + "'");
359         }
360         // Store the first value and evaluate, or skip the body if none
361
if (iterator.hasNext()) {
362             Object JavaDoc element = iterator.next();
363
364             if (element == null) {
365                 pageContext.removeAttribute(id);
366             } else {
367                 pageContext.setAttribute(id, element);
368             }
369
370             lengthCount++;
371             started = true;
372
373             if (indexId != null) {
374                 pageContext.setAttribute(indexId, new Integer JavaDoc(getIndex()));
375             }
376
377             //return (EVAL_BODY_TAG);
378
return (EVAL_BODY_BUFFERED);
379         } else {
380             return (SKIP_BODY);
381         }
382     }
383 }
384
Popular Tags