KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > template > Attribute


1 /*
2  * Copyright 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  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.template;
19
20 import javax.servlet.ServletRequest JavaDoc;
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.Writer JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 /**
28  * The Attribute tag defines an attribute within a template that may be set
29  * from a content page. For example, the page's title may be defined as an
30  * attribute in the template and then provided by each content page using the
31  * template. The attribute has a name and default value. If the content
32  * page specifies a value for the attribute it will be used, otherwise
33  * the default value is used.
34
35  * @jsptagref.tagdescription
36  *
37  * Defines a property placeholder within a template. The
38  * value of these placeholders may be set
39  * from a content page.
40  *
41  * <p>For example, a title placeholder may be defined
42  * in the template.
43  *
44  * <p><b>In the template JSP page...</b>
45  *
46  * <pre> &lt;head>
47  * &lt;title>
48  * &lt;netui-template:attribute name="title"/>
49  * &lt;/title>
50  * &lt;/head></pre>
51  *
52  * <p>Then content pages may set the value of this placeholder using the
53  * {@link SetAttribute} tag.
54  *
55  * <p><b>In a content JSP page...</b>
56  *
57  * <pre> &lt;netui-template:setAttribute name="title" value="myContentPage1.jsp"/></pre>
58  *
59  * <p>The HTML rendered in the browser appears as follows.
60  *
61  * <p><b>Rendered HTML in the browser...</b>
62  *
63  * <pre> &lt;head>
64  * &lt;title>
65  * myContentPage1.jsp
66  * &lt;/title>
67  * &lt;/head></pre>
68  *
69  * If the &lt;netui-template:setAttribute> tag specifies no value to be set in the
70  * placeholder, then the
71  * {@link Attribute} tag's <code>defaultValue</code> will be used.
72  *
73  * <pre> &lt;netui-template:attribute name="title" <b>defaultValue="My Page"</b>/></pre>
74  *
75  * The &lt;netui-template:attribute> tag may also be used to define placeholders within
76  * JSP and HTML tags.
77  *
78  * <p><b>In the template JSP page...</b>
79  *
80  * <pre> &lt;td colspan="3" bgcolor="<b>&lt;netui-template:attribute name="headerColor" defaultValue="#ffffff"/></b>"></pre>
81  *
82  * @example
83  * <p>In this sample, a &lt;netui-template:attribute&gt; tag defines a value placeholder
84  * within a &lt;td> tag</p>
85  *
86  * <pre> &lt;td colspan="3" bgcolor="<b>&lt;netui-template:attribute name="headerColor" defaultValue="#ffffff"/></b>"></pre>
87  *
88  * <p>Now a content JSP page can control the background color of the &lt;td>.
89  *
90  * <pre> &lt;netui-template:setAttribute name="headerColor" value="lightgreen"/></pre>
91  *
92  * The HTML rendered in the browser will appear as follows.
93  *
94  * <pre> &lt;td colspan="3" bgcolor="lightgreen"></pre>
95  *
96  * @netui:tag name="attribute" description="Place this tag in a template file, and then set its value with the netui-template:setAttribute tag."
97  */

98 public class
99         Attribute extends TagSupport JavaDoc
100         implements TemplateConstants
101 {
102     /**
103      * The name of the attribute.
104      */

105     private String JavaDoc _name;
106
107     /**
108      * Default value
109      */

110     private String JavaDoc _defaultValue;
111
112     /**
113      * Sets the <code>name</code> for the <code>Attribute</code>. An
114      * attribute may be used more than once in a template page.
115      * @param name The name of the attribute. The name does
116      * not need to be unique because it may be used more than once
117      * on the page.
118      *
119      * @jsptagref.attributedescription
120      * The <code>name</code> for the &lt;netui-template:attribute> placeholder. The <code>name</code>
121      * may be used more than once in a template page.
122      *
123      * @jsptagref.databindable false
124      *
125      * @jsptagref.attributesyntaxvalue <i>string_name</i>
126      *
127      * @netui:attribute required="true" rtexprvalue="true"
128      * description="The name for the &lt;netui-template:attribute> placeholder. The name
129      * may be used more than once in a template page."
130      */

131     public void setName(String JavaDoc name) {
132         _name = name;
133     }
134
135     /**
136      * Sets the <code>defaultValue</code> for the <code>Attribute</code>.
137      * If the content page does not define a value for this attribute
138      * through the <code>SetAttribute</code> tag, then the
139      * <code>defaultValue</code> will be used.
140      * If neither a value nor <code>defaultValue</code> is set, then the
141      * empty String "" will be output.
142      * @param defaultValue The value to set the defaultValue property.
143      *
144      * @jsptagref.attributedescription
145      * The default value for &lt;netui-template:attribute> placeholder.
146      * If a content page does not define a value for the placeholder
147      * through its &lt;netui-template:setAttribute> tag, then the
148      * <code>defaultValue</code> will be used.
149      * If neither a value nor <code>defaultValue</code> is set, then the
150      * empty String "" will be output.
151      *
152      * @jsptagref.databindable false
153      *
154      * @jsptagref.attributesyntaxvalue <i>string_defaultValue</i>
155      *
156      * @netui:attribute required="false" rtexprvalue="true"
157      * description="The default value for <netui-template:attribute> placeholder."
158      */

159     public void setDefaultValue(String JavaDoc defaultValue) {
160         _defaultValue = defaultValue;
161     }
162
163     /**
164      * Renders the content of the attribute.
165      * @return EVAL_PAGE to continue evaluation of the page.
166      * @throws JspException If there is any failure in the tag.
167      */

168     public int doStartTag()
169             throws JspException JavaDoc {
170         ServletRequest JavaDoc req = pageContext.getRequest();
171         HashMap JavaDoc atts = (HashMap JavaDoc) req.getAttribute(TEMPLATE_ATTRIBUTES);
172         try {
173             if (atts != null) {
174                 String JavaDoc val = (String JavaDoc) atts.get(_name);
175                 if (val != null) {
176                     Writer JavaDoc out = pageContext.getOut();
177                     out.write(val);
178                 }
179                 else {
180                     Writer JavaDoc out = pageContext.getOut();
181                     if (_defaultValue != null)
182                         out.write(_defaultValue);
183                     else
184                         out.write("");
185                 }
186             }
187             else {
188                 Writer JavaDoc out = pageContext.getOut();
189                 if (_defaultValue != null)
190                     out.write(_defaultValue);
191                 else
192                     out.write("");
193             }
194         }
195         catch (IOException JavaDoc e) {
196             localRelease();
197             throw new JspException JavaDoc("Caught IO Exception:" + e.getMessage(),e);
198         }
199         localRelease();
200         return EVAL_PAGE;
201     }
202
203     /**
204      * Resets all of the fields of the tag.
205      */

206     // this is the root because this tag doesn't extend AbstractBaseTag
207
protected void localRelease() {
208         _name = null;
209         _defaultValue = null;
210     }
211 }
212
Popular Tags