KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > taglibrary > handlers > SumBodyTagHandler


1 /*
2  * SumBodyTagHandler.java
3  *
4  * Created on 07 January 2005, 15:00
5  */

6
7 package org.netbeans.test.taglibrary.handlers;
8
9 import java.io.IOException JavaDoc;
10 import javax.servlet.jsp.JspException JavaDoc;
11 import javax.servlet.jsp.JspWriter JavaDoc;
12 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
13 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
14
15 /**
16  * Generated tag handler class.
17  * @author Administrator
18  * @version
19  */

20
21 public class SumBodyTagHandler extends BodyTagSupport JavaDoc {
22
23     /**
24      * Initialization of x property.
25      */

26     private int x = 0;
27
28     /**
29      * Initialization of y property.
30      */

31     private int y = 0;
32     
33     /** Creates new instance of tag handler */
34     public SumBodyTagHandler() {
35         super();
36     }
37     
38     ////////////////////////////////////////////////////////////////
39
/// ///
40
/// User methods. ///
41
/// ///
42
/// Modify these methods to customize your tag handler. ///
43
/// ///
44
////////////////////////////////////////////////////////////////
45

46     /**
47      * Method called from doStartTag().
48      * Fill in this method to perform other operations from doStartTag().
49      *
50      */

51     private void otherDoStartTagOperations() {
52         //
53
// TODO: code that performs other operations in doStartTag
54
// should be placed here.
55
// It will be called after initializing variables,
56
// finding the parent, setting IDREFs, etc, and
57
// before calling theBodyShouldBeEvaluated().
58
//
59
// For example, to print something out to the JSP, use the following:
60
//
61
// try {
62
// JspWriter out = pageContext.getOut();
63
// out.println("something");
64
// } catch (IOException ex) {
65
// // do something
66
// }
67
//
68
//
69
}
70     
71     /**
72      * Method called from doEndTag()
73      * Fill in this method to perform other operations from doEndTag().
74      *
75      */

76     private void otherDoEndTagOperations() {
77         //
78
// TODO: code that performs other operations in doEndTag
79
// should be placed here.
80
// It will be called after initializing variables,
81
// finding the parent, setting IDREFs, etc, and
82
// before calling shouldEvaluateRestOfPageAfterEndTag().
83
//
84
}
85     
86     /**
87      * Fill in this method to process the body content of the tag.
88      * You only need to do this if the tag's BodyContent property
89      * is set to "JSP" or "tagdependent."
90      * If the tag's bodyContent is set to "empty," then this method
91      * will not be called.
92      */

93     private void writeTagBodyContent(JspWriter JavaDoc out, BodyContent JavaDoc bodyContent) throws IOException JavaDoc {
94         //
95
// TODO: insert code to write html before writing the body content.
96
// e.g.:
97
//
98
out.println("<p>");
99         out.println("Sum of " + x + " and " + y + " is " + (x+y));
100         out.println("<br/>");
101         
102         //
103
// write the body content (after processing by the JSP engine) on the output Writer
104
//
105
bodyContent.writeOut(out);
106         
107         out.println("<br/>");
108         out.println("END of sum");
109         out.println("</p>");
110         
111         
112         // clear the body content for the next time through.
113
bodyContent.clearBody();
114     }
115     
116     ////////////////////////////////////////////////////////////////
117
/// ///
118
/// Tag Handler interface methods. ///
119
/// ///
120
/// Do not modify these methods; instead, modify the ///
121
/// methods that they call. ///
122
/// ///
123
////////////////////////////////////////////////////////////////
124

125     /**
126      * This method is called when the JSP engine encounters the start tag,
127      * after the attributes are processed.
128      * Scripting variables (if any) have their values set here.
129      * @return EVAL_BODY_BUFFERED if the JSP engine should evaluate the tag body, otherwise return SKIP_BODY.
130      * This method is automatically generated. Do not modify this method.
131      * Instead, modify the methods that this method calls.
132      */

133     
134     public int doStartTag() throws JspException JavaDoc, JspException JavaDoc {
135         otherDoStartTagOperations();
136         
137         if (theBodyShouldBeEvaluated()) {
138             return EVAL_BODY_BUFFERED;
139         } else {
140             return SKIP_BODY;
141         }
142     }
143     
144     /**
145      * This method is called after the JSP engine finished processing the tag.
146      * @return EVAL_PAGE if the JSP engine should continue evaluating the JSP page, otherwise return SKIP_PAGE.
147      * This method is automatically generated. Do not modify this method.
148      * Instead, modify the methods that this method calls.
149      */

150     public int doEndTag() throws JspException JavaDoc, JspException JavaDoc {
151         otherDoEndTagOperations();
152         
153         if (shouldEvaluateRestOfPageAfterEndTag()) {
154             return EVAL_PAGE;
155         } else {
156             return SKIP_PAGE;
157         }
158     }
159     
160     /**
161      * This method is called after the JSP engine processes the body content of the tag.
162      * @return EVAL_BODY_AGAIN if the JSP engine should evaluate the tag body again, otherwise return SKIP_BODY.
163      * This method is automatically generated. Do not modify this method.
164      * Instead, modify the methods that this method calls.
165      */

166     public int doAfterBody() throws JspException JavaDoc {
167         try {
168             //
169
// This code is generated for tags whose bodyContent is "JSP"
170
//
171
BodyContent JavaDoc bodyContent = getBodyContent();
172             JspWriter JavaDoc out = bodyContent.getEnclosingWriter();
173             
174             writeTagBodyContent(out, bodyContent);
175         } catch (Exception JavaDoc ex) {
176             handleBodyContentException(ex);
177         }
178         
179         if (theBodyShouldBeEvaluatedAgain()) {
180             return EVAL_BODY_AGAIN;
181         } else {
182             return SKIP_BODY;
183         }
184     }
185     
186     /**
187      * Handles exception from processing the body content.
188      */

189     private void handleBodyContentException(Exception JavaDoc ex) throws JspException JavaDoc {
190         // Since the doAfterBody method is guarded, place exception handing code here.
191
throw new JspException JavaDoc("error in NewTag: " + ex);
192     }
193     
194     /**
195      * Fill in this method to determine if the rest of the JSP page
196      * should be generated after this tag is finished.
197      * Called from doEndTag().
198      */

199     private boolean shouldEvaluateRestOfPageAfterEndTag() {
200         //
201
// TODO: code that determines whether the rest of the page
202
// should be evaluated after the tag is processed
203
// should be placed here.
204
// Called from the doEndTag() method.
205
//
206
return true;
207     }
208     
209     /**
210      * Fill in this method to determine if the tag body should be evaluated
211      * again after evaluating the body.
212      * Use this method to create an iterating tag.
213      * Called from doAfterBody().
214      */

215     private boolean theBodyShouldBeEvaluatedAgain() {
216         //
217
// TODO: code that determines whether the tag body should be
218
// evaluated again after processing the tag
219
// should be placed here.
220
// You can use this method to create iterating tags.
221
// Called from the doAfterBody() method.
222
//
223
return false;
224     }
225
226     /**
227      * Fill in this method to determine if the tag body should be evaluated.
228      * Called from doStartTag().
229      */

230     private boolean theBodyShouldBeEvaluated() {
231
232         //
233
// TODO: code that determines whether the body should be
234
// evaluated should be placed here.
235
// Called from the doStartTag() method.
236
//
237
return true;
238     }
239
240     /**
241      * Setter for the x attribute.
242      */

243     public void setX(int value) {
244         this.x = value;
245     }
246
247     /**
248      * Setter for the y attribute.
249      */

250     public void setY(int value) {
251         this.y = value;
252     }
253     
254 }
255
Popular Tags