Source code: com/obinary/cms/taglibs/ContainerLoop.java
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-2003 obinary Ltd. (http://www.obinary.com) All rights reserved.
11 *
12 * */
13
14
15
16
17
18 package com.obinary.cms.taglibs;
19
20
21 import com.obinary.cms.core.Content;
22 import com.obinary.cms.core.Container;
23 import com.obinary.cms.util.Resource;
24
25 import javax.servlet.jsp.tagext.TagSupport;
26 import javax.servlet.jsp.PageContext;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.jcr.RepositoryException;
29 import java.util.Iterator;
30 import java.util.Collection;
31
32
33 /**
34 * Date: Apr 28, 2003
35 * Time: 11:20:59 AM
36 * @author Sameer Charles
37 * @version 1.0
38 */
39
40
41 public class ContainerLoop extends TagSupport {
42
43
44 protected static final String CURRENT_INDEX = "currentIndex";
45 protected static final String SIZE = "size";
46
47 public static final String CONTAINER_LIST_NAME = "containerListName";
48
49
50
51
52
53
54 private String containerListName;
55 private Iterator containerListIterator;
56 private Content page;
57 private HttpServletRequest request;
58 private int beginIndex = 0;
59 private int endIndex = 0;
60 private int step = 1;
61 private int size = 0;
62 private int currentIndex = 0;
63
64
65
66
67
68
69
70 /**
71 * <p>starts loop tag</p>
72 *
73 * @return int
74 */
75 public int doStartTag() {
76 this.request = (HttpServletRequest)pageContext.getRequest();
77 this.page = Resource.getCurrentActivePage(this.request);
78 try {
79 Collection children = this.page.getContainerList(this.containerListName).getChildren();
80 this.size = children.size();
81 if (this.size == 0) return SKIP_BODY;
82 pageContext.setAttribute(ContainerLoop.SIZE,new Integer(this.getEnd()),PageContext.REQUEST_SCOPE);
83 pageContext.setAttribute(ContainerLoop.CURRENT_INDEX,new Integer(this.currentIndex),PageContext.REQUEST_SCOPE);
84 pageContext.setAttribute(ContainerLoop.CONTAINER_LIST_NAME,this.containerListName,PageContext.REQUEST_SCOPE);
85 this.containerListIterator = children.iterator();
86 Resource.setLocalContainerListName(this.request,this.containerListName);
87 for (;this.beginIndex > -1;--this.beginIndex)
88 Resource.setLocalContainer(this.request,(Container)this.containerListIterator.next());
89 } catch (RepositoryException re) {re.printStackTrace(); return SKIP_BODY;}
90 this.page = null;
91 return EVAL_BODY_INCLUDE;
92 }
93
94
95
96 /**
97 * @return int
98 */
99 public int doAfterBody() {
100 if (this.containerListIterator.hasNext() && (this.currentIndex < this.getEnd())) {
101 this.currentIndex++;
102 pageContext.setAttribute(ContainerLoop.CURRENT_INDEX,new Integer(this.currentIndex),PageContext.REQUEST_SCOPE);
103 for (int i=0;i<this.step; i++)
104 Resource.setLocalContainer(this.request,(Container)this.containerListIterator.next());
105 return EVAL_BODY_AGAIN;
106 }
107 return SKIP_BODY;
108 }
109
110
111
112 /**
113 * @param name , container list name on which this tag will iterate
114 */
115 public void setContainerListName(String name) {
116 this.containerListName = name;
117 }
118
119
120 public String getContainerListName() {
121 return this.containerListName;
122 }
123
124
125
126 /**
127 * @param index , to begin with
128 */
129 public void setBegin(String index) {
130 this.beginIndex = (new Integer(index)).intValue();
131 }
132
133
134
135 /**
136 * @param index , to end at
137 */
138 public void setEnd(String index) {
139 this.endIndex = (new Integer(index)).intValue();
140 }
141
142
143
144 /**
145 * @return end index
146 */
147 private int getEnd() {
148 if (this.endIndex == 0)
149 return this.size;
150 return this.endIndex;
151 }
152
153
154 /**
155 * @param step , to jump to
156 */
157 public void setStep(String step) {
158 this.step = (new Integer(step)).intValue();
159 }
160
161
162
163 /**
164 * <p>end loop</p>
165 *
166 * @return int
167 */
168 public int doEndTag() {
169 Resource.removeLocalContainer(this.request);
170 Resource.removeLocalContainerListName(this.request);
171 pageContext.removeAttribute(ContainerLoop.CURRENT_INDEX);
172 pageContext.removeAttribute(ContainerLoop.SIZE);
173 pageContext.removeAttribute(ContainerLoop.CONTAINER_LIST_NAME);
174 this.beginIndex=0;
175 this.endIndex = 0;
176 this.step = 1;
177 this.size = 0;
178 this.currentIndex = 0;
179 return EVAL_PAGE;
180 }
181
182
183
184 }