KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > i18n > ConditionalTagSupport


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

16
17 package org.apache.taglibs.i18n;
18
19 import java.io.IOException JavaDoc;
20
21 import java.util.ResourceBundle JavaDoc;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.JspTagException JavaDoc;
25 import javax.servlet.jsp.PageContext JavaDoc;
26 import javax.servlet.jsp.tagext.BodyTag JavaDoc;
27 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
28
29
30 /**
31  * This class provides the base implementation for the ifdef and ifndef tags.
32  * Subclasses must provide an implementation of the shouldEvaluate() method.
33  *
34  * @author <a HREF="mailto:tdawson@wamnet.com">Tim Dawson</a>
35  *
36  */

37 public abstract class ConditionalTagSupport extends BodyTagSupport JavaDoc
38 {
39
40     protected static String JavaDoc _tagname = "i18n:";
41
42     // instance variables used during processing
43
private String JavaDoc _key = null;
44     private ResourceBundle JavaDoc _bundle = null;
45     private String JavaDoc _bundleRef = null;
46
47     /**
48      * sets the key to use when looking up the value in the bundle
49      */

50     public void setKey(String JavaDoc key)
51     {
52         _key = key;
53     }
54
55     /**
56      * sets the bundle based on a variable defined in the page<br>
57      * if neither bundle or bundleRef are specified, uses the first bundle
58      * defined on the page by the i18n:bundle tag
59      */

60     public void setBundleRef(String JavaDoc varName)
61     {
62         _bundleRef = varName;
63     }
64
65     /**
66      * sets the bundle to an actual ResourceBundle object<br>
67      * if neither bundle or bundleRef are specified, uses the bundle most recently
68      * defined on the page by the i18n:bundle tag
69      */

70     public void setBundle(ResourceBundle JavaDoc aBundle)
71     {
72         _bundle = aBundle;
73     }
74
75     /**
76      * @return the bundle to use
77      */

78     public ResourceBundle JavaDoc getBundle()
79     {
80         if ( _bundleRef != null ) {
81             _bundle = (ResourceBundle JavaDoc)pageContext.getAttribute(_bundleRef);
82         }
83         if ( _bundle == null ) {
84             BundleTag bundleTag = (BundleTag)this.findAncestorWithClass(this,BundleTag.class);
85             if (bundleTag != null) {
86                 _bundle = bundleTag.getBundle();
87             }
88         }
89         if ( _bundle == null ) {
90             _bundle = ResourceHelper.getBundle(pageContext);
91         }
92         return _bundle;
93     }
94
95     /**
96      * clears out the key for the next usage
97      */

98     public void release()
99     {
100         super.release();
101         _key = null;
102         _bundle = null;
103     }
104
105     /**
106      * must be overridden by a subclass to return a boolean value
107      */

108     public abstract boolean shouldEvaluate() throws JspException JavaDoc;
109
110     /**
111      * returns (if any) the value specified for the key in the bundle.
112      *
113      * if no value is specified, traps the MissingResourceException and returns null
114      */

115     protected String JavaDoc getValue() throws JspException JavaDoc
116     {
117         // ensure we have a key
118
if ( _key == null) {
119             throw new JspTagException JavaDoc(
120                 this._tagname + " tag, requires a key attribute.");
121         }
122
123         // ensure a bundle was defined
124
ResourceBundle JavaDoc bundle = this.getBundle();
125         if ( this.getBundle() == null) {
126             throw new JspTagException JavaDoc(
127                 this._tagname + " tag, no bundle found for use with tag");
128         }
129
130         String JavaDoc value = null;
131         try {
132             value = bundle.getString(_key);
133         } catch (java.util.MissingResourceException JavaDoc e) {
134             value = null;
135         }
136         return value;
137     }
138
139     /**
140      * locates the bundle and tests whether the key has a value
141      */

142     public int doStartTag() throws JspException JavaDoc
143     {
144         int returnValue;
145
146         // evaluate the body if a value was found, otherwise skip it
147
if ( this.shouldEvaluate() ) {
148             returnValue = BodyTag.EVAL_BODY_TAG;
149         } else {
150             returnValue = BodyTag.SKIP_BODY;
151         }
152
153         return returnValue;
154     }
155
156     /**
157      * Prints the body of the tag.
158      * <P>
159      * Always return the flag for processing the rest of the JSP page.
160      *
161      * @return the flag for processing the rest of the JSP page.
162      * @exception JspException if the writing out of the body fails.
163      */

164     public int doEndTag() throws JspException JavaDoc
165     {
166         try {
167             if (bodyContent != null) {
168                 bodyContent.writeOut(bodyContent.getEnclosingWriter());
169             }
170         } catch (java.io.IOException JavaDoc ioe) {
171             throw new JspTagException JavaDoc(
172                 this._tagname + " tag, IO Error: " + ioe.getMessage());
173         }
174
175         // This is not a loop, so always process the rest of the JSP page
176
return BodyTag.EVAL_PAGE;
177     }
178
179 }
180
Popular Tags