KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > servlet > jsp > taglib > IterateTag


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.servlet.jsp.taglib;
10
11 import java.util.Iterator JavaDoc;
12 import java.util.LinkedList JavaDoc;
13
14 import javax.servlet.jsp.JspException JavaDoc;
15 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
16
17 import org.jboss.portal.common.context.Context;
18 import org.jboss.portal.common.context.NamedContext;
19 import org.jboss.portal.core.servlet.jsp.PortalJsp;
20
21 /**
22  * Iterate tag to define section that can be iterated on
23  * @author <a HREF="theute@jboss.org">Thomas Heute</a>
24  * $Revision: 1.1 $
25  */

26 public class IterateTag
27       extends TagSupport JavaDoc
28 {
29    /**
30     * Context of the section
31     */

32    private String JavaDoc ctx;
33
34    private Iterator JavaDoc children;
35    
36    private LinkedList JavaDoc contextStack;
37    
38    private boolean include;
39    
40    /**
41     * @param ctx The context to set.
42     */

43    public void setCtx(String JavaDoc ctx)
44    {
45       this.ctx = ctx;
46    }
47
48    
49    public int doStartTag() throws JspException JavaDoc {
50       contextStack = (LinkedList JavaDoc)PortalJsp.contextStack.get();
51       if (contextStack.isEmpty())
52       {
53          include = false;
54          PortalJsp.log.debug("No context has been found");
55          return SKIP_BODY;
56       }
57       
58       Context currentContext = ((NamedContext)contextStack.getLast()).getContext();
59       children = currentContext.childIterator(ctx);
60       
61       if (children.hasNext()) {
62          include = true;
63          contextStack.addLast(new NamedContext(ctx, (Context)children.next()));
64          return EVAL_PAGE;
65       } else {
66          include = false;
67          return SKIP_BODY;
68       }
69    }
70    
71    public int doAfterBody() {
72       if (children.hasNext()) {
73          Context context = (Context)children.next();
74          contextStack.removeLast();
75          contextStack.addLast(new NamedContext(ctx, context));
76          return EVAL_BODY_AGAIN;
77       } else {
78          return SKIP_BODY;
79       }
80    }
81    
82    public int doEndTag() {
83       if (include) {
84        contextStack.removeLast();
85       }
86       return SKIP_BODY;
87    }
88 }
89
Popular Tags