KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > taglibs > PageIterator


1 package info.magnolia.cms.taglibs;
2
3 import info.magnolia.cms.core.Content;
4 import info.magnolia.cms.core.ItemType;
5 import info.magnolia.cms.util.Resource;
6
7 import java.util.Collection JavaDoc;
8 import java.util.Iterator JavaDoc;
9
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.jsp.JspException JavaDoc;
12 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
13
14
15 /**
16  * @author Andreas Brenk
17  * @since 18.01.2006
18  */

19 public class PageIterator extends TagSupport JavaDoc {
20
21     /**
22      * Stable serialVersionUID.
23      */

24     private static final long serialVersionUID = 222L;
25
26     private Iterator JavaDoc contentIterator;
27
28     private String JavaDoc hiddenAttribute = "hidden";
29
30     public int doAfterBody() throws JspException JavaDoc {
31         boolean hasNext = nextContent();
32
33         if (hasNext) {
34             return EVAL_BODY_AGAIN;
35         }
36
37         return SKIP_BODY;
38     }
39
40     public int doEndTag() throws JspException JavaDoc {
41         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) this.pageContext.getRequest();
42         Resource.restoreCurrentActivePage(request);
43
44         return EVAL_PAGE;
45     }
46
47     public int doStartTag() throws JspException JavaDoc {
48         initContentIterator();
49
50         boolean hasNext = nextContent();
51         if (hasNext) {
52             return EVAL_BODY_INCLUDE;
53         }
54
55         return SKIP_BODY;
56     }
57
58     public void release() {
59         this.contentIterator = null;
60         super.release();
61     }
62
63     public void setHiddenAttribute(String JavaDoc hiddenAttribute) {
64         this.hiddenAttribute = hiddenAttribute;
65     }
66
67     private void initContentIterator() {
68         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) this.pageContext.getRequest();
69         Content activePage = Resource.getCurrentActivePage(req);
70         Collection JavaDoc children = activePage.getChildren(ItemType.CONTENT);
71
72         for (Iterator JavaDoc i = children.iterator(); i.hasNext();) {
73             Content c = (Content) i.next();
74             boolean hidden = c.getNodeData(this.hiddenAttribute).getBoolean();
75             if (hidden) {
76                 i.remove();
77             }
78         }
79
80         this.contentIterator = children.iterator();
81     }
82
83     /**
84      * @return <code>true</code> if the next page was activated, <code>false</code> if there are no pages left
85      */

86     private boolean nextContent() {
87         if (this.contentIterator.hasNext()) {
88             HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) this.pageContext.getRequest();
89             Content page = (Content) this.contentIterator.next();
90
91             Resource.setCurrentActivePage(request, page);
92
93             return true;
94         }
95
96         return false;
97     }
98 }
99
Popular Tags