View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: ForEachTag.java,v $
31   * Revision 1.2  2005/04/09 18:04:20  colinmacleod
32   * Changed copyright text to GPL v2 explicitly.
33   *
34   * Revision 1.1  2005/01/19 12:58:20  colinmacleod
35   * Moved from ivata groupware.
36   *
37   * Revision 1.2  2004/11/03 16:10:12  colinmacleod
38   * Changed todo comments to TODO: all caps.
39   *
40   * Revision 1.1  2004/09/30 15:16:03  colinmacleod
41   * Split off addressbook elements into security subproject.
42   *
43   * Revision 1.2  2004/03/21 21:16:10  colinmacleod
44   * Shortened name to ivata op.
45   *
46   * Revision 1.1.1.1  2004/01/27 20:57:58  colinmacleod
47   * Moved ivata openportal to SourceForge..
48   *
49   * Revision 1.1.1.1  2003/10/13 20:50:11  colin
50   * Restructured portal into subprojects
51   *
52   * Revision 1.3  2003/05/08 20:25:24  colin
53   * added clear buffer
54   *
55   * Revision 1.2  2003/05/08 17:58:52  colin
56   * removed illegal flush
57   *
58   * Revision 1.1  2003/02/25 08:15:50  colin
59   * moved to jsp category
60   *
61   * Revision 1.2  2002/10/16 14:34:14  colin
62   * bug fixes - setBegin no longer sets the counter value directly, as this
63   * causes probs with hard-coded attribute values
64   *
65   * Revision 1.1  2002/09/26 12:33:58  colin
66   * resurrected forEach tag because of probs in JSTL
67   *
68   * Revision 1.1  2002/01/20 12:32:40  colin
69   * created generic looping tag with to and test parameters
70   * -----------------------------------------------------------------------------
71   */
72  package com.ivata.mask.web.tag.util;
73  
74  import javax.servlet.jsp.JspException;
75  import javax.servlet.jsp.JspWriter;
76  import javax.servlet.jsp.tagext.BodyContent;
77  import javax.servlet.jsp.tagext.BodyTagSupport;
78  
79  /***
80   * <p>Loop increasing a counter nCounter until it reaches to (inclusive).</p>
81   *
82   * @since ivata masks 0.5 (2001-12-12)
83   * @author Colin MacLeod
84   * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
85   * @version $Revision: 1.2 $
86   * TODO: This class is a temporary workaround because we had problems with the
87   * JSTL class.
88   */
89  public class ForEachTag extends BodyTagSupport {
90  
91      /***
92       * Refer to {@link #getBegin}.
93       */
94      private int begin = 0;
95  
96      /***
97       * Refer to {@link #getEnd}.
98       */
99      private int end;
100 
101     /***
102      * Refer to {@link #getStep}.
103      */
104     private int step = 1;
105 
106     /***
107      * Refer to {@link #getVar}.
108      */
109     private int var = 0;
110 
111     /***
112      * Name of an attribute to set the var value to.
113      */
114     private String varAttribute = "forEachTagVarVariable";
115 
116     /***
117      * Refer to {@link IterationTag#doAfterBody}.
118      *
119      * @return Refer to {@link IterationTag#doAfterBody}.
120      * @throws JspException Refer to {@link IterationTag#doAfterBody}.
121      */
122     public int doAfterBody() throws JspException {
123         try {
124             JspWriter out = getPreviousOut();
125             BodyContent bodyContent = getBodyContent();
126 
127             bodyContent.writeOut(out);
128             // clear the body content for the next time through.
129             bodyContent.clearBody();
130             bodyContent.clearBuffer();
131         } catch (Exception ex) {
132             throw new JspException("error in ForEachTag: " + ex.getClass()
133                 + ": "  + ex);
134         }
135 
136         var += step;
137         pageContext.setAttribute(varAttribute, new Integer(var));
138         if (var <= end) {
139             return EVAL_BODY_BUFFERED;
140         } else {
141             return SKIP_BODY;
142         }
143     }
144 
145 
146     /***
147      * Refer to {@link Tag#doStartTag}.
148      * @return Refer to {@link Tag#doStartTag}.
149      * @throws JspException Refer to {@link Tag#doStartTag}.
150      */
151     public int doStartTag() throws JspException {
152         pageContext.setAttribute(varAttribute, new Integer(var));
153         if (var <= end) {
154             return EVAL_BODY_BUFFERED;
155         } else {
156             return SKIP_BODY;
157         }
158     }
159 
160     /***
161      * Get the value of the counter.
162      *
163      * @return current counter value.
164      */
165     public final String getVar() {
166         return varAttribute;
167     }
168 
169     /***
170      * Sets the first value of the loop counter.
171      *
172      * @param value the value the counter should start at.
173      */
174     public final void setBegin(final int value) {
175         begin = value;
176     }
177 
178     /***
179      * Sets the final value of the loop counter, inclusive.
180      *
181      * @param value final value of the loop counter, inclusive.
182      */
183     public final void setEnd(final int value) {
184         end = value;
185     }
186 
187     /***
188      * Sets the increment by which the counter is increased after each loop.
189      *
190      * @param value increment by which the counter is increased after each loop.
191      */
192     public final void setStep(final int value) {
193         step = value;
194     }
195 
196     /***
197      * Set the current value of the counter.
198      *
199      * @param value new value of counter.
200      */
201     public final void setVar(final String value) {
202         varAttribute = value;
203     }
204 }