KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.taglibs;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.ContentHandler;
17 import info.magnolia.cms.core.ItemType;
18 import info.magnolia.cms.util.Resource;
19
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import javax.jcr.RepositoryException;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.jsp.PageContext JavaDoc;
26 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
27
28 import org.apache.log4j.Logger;
29
30
31 /**
32  * @author Sameer Charles
33  * @author Fabrizio Giustina
34  * @version $Revision $ ($Author $)
35  */

36 public class ContentNodeIterator extends TagSupport JavaDoc {
37
38     public static final String JavaDoc CONTENT_NODE_COLLECTION_NAME = "contentNodeCollectionName"; //$NON-NLS-1$
39

40     protected static final String JavaDoc CURRENT_INDEX = "currentIndex"; //$NON-NLS-1$
41

42     protected static final String JavaDoc SIZE = "size"; //$NON-NLS-1$
43

44     /**
45      * Stable serialVersionUID.
46      */

47     private static final long serialVersionUID = 222L;
48
49     /**
50      * Logger.
51      */

52     private static Logger log = Logger.getLogger(ContentNodeIterator.class);
53
54     private String JavaDoc contentNodeCollectionName;
55
56     private int beginIndex;
57
58     private int endIndex;
59
60     private int step = 1;
61
62     private int size;
63
64     private int currentIndex;
65
66     private Iterator JavaDoc contentNodeIterator;
67
68     /**
69      * @param name container list name on which this tag will iterate
70      * @deprecated
71      */

72     public void setContainerListName(String JavaDoc name) {
73         this.setContentNodeCollectionName(name);
74     }
75
76     /**
77      * @param name content node name on which this tag will iterate
78      */

79     public void setContentNodeCollectionName(String JavaDoc name) {
80         this.contentNodeCollectionName = name;
81     }
82
83     /**
84      * @param index index to begin with
85      */

86     public void setBegin(String JavaDoc index) {
87         this.beginIndex = (new Integer JavaDoc(index)).intValue();
88     }
89
90     /**
91      * @param index index to end at
92      */

93     public void setEnd(String JavaDoc index) {
94         this.endIndex = (new Integer JavaDoc(index)).intValue();
95     }
96
97     /**
98      * @param step to jump to
99      */

100     public void setStep(String JavaDoc step) {
101         this.step = (new Integer JavaDoc(step)).intValue();
102     }
103
104     /**
105      * @return end index
106      */

107     private int getEnd() {
108         if (this.endIndex == 0) {
109             return this.size;
110         }
111         return this.endIndex;
112     }
113
114     /**
115      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
116      */

117     public int doStartTag() {
118         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
119         Content page = Resource.getCurrentActivePage(request);
120         try {
121             Collection JavaDoc children = page.getContent(this.contentNodeCollectionName).getChildren(
122                 ItemType.CONTENTNODE,
123                 ContentHandler.SORT_BY_SEQUENCE);
124             this.size = children.size();
125             if (this.size == 0) {
126                 return SKIP_BODY;
127             }
128             pageContext.setAttribute(ContentNodeIterator.SIZE, new Integer JavaDoc(this.getEnd()), PageContext.REQUEST_SCOPE);
129             pageContext.setAttribute(
130                 ContentNodeIterator.CURRENT_INDEX,
131                 new Integer JavaDoc(this.currentIndex),
132                 PageContext.REQUEST_SCOPE);
133             pageContext.setAttribute(
134                 ContentNodeIterator.CONTENT_NODE_COLLECTION_NAME,
135                 this.contentNodeCollectionName,
136                 PageContext.REQUEST_SCOPE);
137             this.contentNodeIterator = children.iterator();
138             Resource.setLocalContentNodeCollectionName(request, this.contentNodeCollectionName);
139             for (; this.beginIndex > -1; --this.beginIndex) {
140                 Resource.setLocalContentNode(request, (Content) this.contentNodeIterator.next());
141             }
142         }
143         catch (RepositoryException re) {
144             log.debug(re.getMessage());
145             return SKIP_BODY;
146         }
147         return EVAL_BODY_INCLUDE;
148     }
149
150     /**
151      * @return int
152      */

153     public int doAfterBody() {
154         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
155         if (this.contentNodeIterator.hasNext() && (this.currentIndex < this.getEnd())) {
156             this.currentIndex++;
157             pageContext.setAttribute(
158                 ContentNodeIterator.CURRENT_INDEX,
159                 new Integer JavaDoc(this.currentIndex),
160                 PageContext.REQUEST_SCOPE);
161             for (int i = 0; i < this.step; i++) {
162                 Resource.setLocalContentNode(request, (Content) this.contentNodeIterator.next());
163             }
164             return EVAL_BODY_AGAIN;
165         }
166         return SKIP_BODY;
167     }
168
169     /**
170      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
171      */

172     public int doEndTag() {
173         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
174         Resource.removeLocalContentNode(request);
175         Resource.removeLocalContentNodeCollectionName(request);
176         pageContext.removeAttribute(ContentNodeIterator.CURRENT_INDEX);
177         pageContext.removeAttribute(ContentNodeIterator.SIZE);
178         pageContext.removeAttribute(ContentNodeIterator.CONTENT_NODE_COLLECTION_NAME);
179         this.beginIndex = 0;
180         this.endIndex = 0;
181         this.step = 1;
182         this.size = 0;
183         this.currentIndex = 0;
184         return EVAL_PAGE;
185     }
186
187     /**
188      * @see javax.servlet.jsp.tagext.TagSupport#release()
189      */

190     public void release() {
191         this.contentNodeCollectionName = null;
192         this.contentNodeIterator = null;
193         this.beginIndex = 0;
194         this.endIndex = 0;
195         this.step = 1;
196         this.size = 0;
197         this.currentIndex = 0;
198         super.release();
199     }
200
201 }
202
Popular Tags