KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > taglib > CounterTag


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.taglib;
19
20
21
22 import com.finalist.jag.*;
23 import com.finalist.jag.taglib.util.RequestUtil;
24
25 import java.util.*;
26
27
28 /**
29  * Class CounterTag
30  *
31  *
32  * @author Wendel D. de Witte
33  * @version %I%, %G%
34  */

35 public class CounterTag extends TagBodySupport {
36
37     /** Field name */
38     private String JavaDoc name = null;
39
40     /** Field sensitive */
41     private String JavaDoc odd = "false";
42
43     /** Field output */
44     private String JavaDoc output = null;
45
46     /** Field equal */
47     protected boolean body = false;
48
49     /** Field counter */
50     protected int counter = 0;
51
52     /////////////////////////////////////
53

54     /**
55      * Method getName
56      *
57      *
58      * @return
59      *
60      */

61     public String JavaDoc getName() {
62         return (this.name);
63     }
64
65     /**
66      * Method setName
67      *
68      *
69      * @param name
70      *
71      */

72     public void setName(String JavaDoc name) {
73         this.name = name;
74     }
75
76     /**
77      * Method getOdd
78      *
79      *
80      * @return
81      *
82      */

83     public String JavaDoc getOdd() {
84         return (this.odd);
85     }
86
87     /**
88      * Method setOdd
89      *
90      *
91      * @param odd
92      *
93      */

94     public void setOdd(String JavaDoc odd) {
95         this.odd = odd;
96     }
97
98     /**
99      * Method getOutput
100      *
101      *
102      * @return
103      *
104      */

105     public String JavaDoc getOutput() {
106         return (this.output);
107     }
108
109     /**
110      * Method setOutput
111      *
112      *
113      * @param name
114      *
115      */

116     public void setOutput(String JavaDoc output) {
117         this.output = output;
118     }
119     /**
120      * Method doStartTag
121      *
122      *
123      * @return
124      *
125      * @throws JagException
126      *
127      */

128     public int doStartTag() throws JagException {
129
130         SessionContext session = getPageContext().getSessionContext();
131
132         try {
133             String JavaDoc s = (String JavaDoc) getPageContext().getAttribute(name);
134             int n = new Integer JavaDoc(s).intValue();
135
136             body = n % 2 == 1;
137
138             if (odd != null && odd.equalsIgnoreCase("false")) {
139                 body = !body;
140             }
141
142             if (output != null && output.equalsIgnoreCase("true")) {
143                 getWriter().print(s);
144             }
145         } catch (Exception JavaDoc e) {
146             new JagException(e.getMessage());
147         }
148
149         return (EVAL_PAGE);
150     }
151
152     /**
153      * Method doAfterBodyTag
154      *
155      *
156      * @return
157      *
158      * @throws JagException
159      *
160      */

161     public int doAfterBodyTag() throws JagException {
162
163         return (body && (counter++ < 1))
164                ? (EVAL_BODY_TAG)
165                : (SKIP_BODY);
166     }
167 }
168
Popular Tags