KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > sample > servlet > SampleTag


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.sample.servlet;
21
22 import java.io.IOException JavaDoc;
23
24 import java.util.Enumeration JavaDoc;
25
26 import javax.servlet.jsp.JspTagException JavaDoc;
27 import javax.servlet.jsp.JspWriter JavaDoc;
28 import javax.servlet.jsp.PageContext JavaDoc;
29 import javax.servlet.jsp.tagext.Tag JavaDoc;
30 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
31
32 /**
33  * Sample tag that implements simple tag logic.
34  *
35  * @version $Id: SampleTag.java,v 1.3 2004/02/29 16:36:45 vmassol Exp $
36  */

37 public class SampleTag extends TagSupport JavaDoc
38 {
39     /**
40      * Determines whether the tag's body should be shown.
41      */

42     private boolean showBody;
43
44     /**
45      * Determines whether page should continue after the tag.
46      */

47     private boolean stopPage;
48
49     /**
50      * Determines whether the tag's body should be shown.
51      *
52      * @param isBodyShown a String equaling 'true' will be taken as
53      * <code>true</code>. Anything else will be
54      * taken as <code>false</code>.
55      */

56     public void setShowBody(String JavaDoc isBodyShown)
57     {
58         this.showBody = "true".equals(isBodyShown.toLowerCase());
59     }
60
61     /**
62      * Determines whether page should stop after the tag.
63      *
64      * @param isPageStopped a String equaling 'true' will be taken as
65      * <code>true</code>. Anything else will be
66      * taken as <code>false</code>.
67      */

68     public void setStopPage(String JavaDoc isPageStopped)
69     {
70         this.stopPage = "true".equals(isPageStopped);
71     }
72
73     /**
74      * Prints the names and values of everything in page scope to the response,
75      * along with the body (if showBody is set to <code>true</code>).
76      *
77      * @return the return code
78      * @exception JspTagException on failure
79      */

80     public int doStartTag() throws JspTagException JavaDoc
81     {
82         Enumeration JavaDoc names = pageContext.getAttributeNamesInScope(
83             PageContext.PAGE_SCOPE);
84
85         JspWriter JavaDoc out = pageContext.getOut();
86
87         try
88         {
89             out.println("The following attributes exist in page scope: <BR>");
90
91             while (names.hasMoreElements())
92             {
93                 String JavaDoc name = (String JavaDoc) names.nextElement();
94                 Object JavaDoc attribute = pageContext.getAttribute(name);
95
96                 out.println(name + " = " + attribute + " <BR>");
97             }
98
99             if (this.showBody)
100             {
101                 out.println("Body Content Follows: <BR>");
102
103                 return EVAL_BODY_INCLUDE;
104             }
105         }
106         catch (IOException JavaDoc e)
107         {
108             throw new JspTagException JavaDoc(e.getMessage());
109         }
110
111         return SKIP_BODY;
112     }
113
114     /**
115      * Does two things:
116      * <ul>
117      * <li>Stops the page if the corresponding attribute has been set</li>
118      * <li>Prints a message another tag encloses this one.</li>
119      * </ul>
120      *
121      * @return the return code
122      * @exception JspTagException on failure
123      */

124     public int doEndTag() throws JspTagException JavaDoc
125     {
126         //get the parent if any
127
Tag JavaDoc parent = this.getParent();
128
129         if (parent != null)
130         {
131             try
132             {
133                 JspWriter JavaDoc out = this.pageContext.getOut();
134
135                 out.println("This tag has a parent. <BR>");
136             }
137             catch (IOException JavaDoc e)
138             {
139                 throw new JspTagException JavaDoc(e.getMessage());
140             }
141         }
142
143         if (this.stopPage)
144         {
145             return Tag.SKIP_PAGE;
146         }
147
148         return Tag.EVAL_PAGE;
149     }
150 }
151
Popular Tags