KickJava   Java API By Example, From Geeks To Geeks.

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


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

100 public class SetAttribute extends AbstractClassicTag
101         implements TemplateConstants
102 {
103     /**
104      * The name of the attribute.
105      */

106     private String JavaDoc _name;
107
108     /**
109      * The value of the attribute.
110      */

111     private String JavaDoc _value;
112
113     /**
114      * Return the name of the tag. This is used by error reporting
115      * in the base class <code>AbstractBaseTag</code>.
116      */

117     public String JavaDoc getTagName() {
118         return "SetAttribute";
119     }
120
121     /**
122      * Set the <code>name</code> of the attribute.
123      * @param name The name of the <code>Attribute</code> in the
124      * template for which this tags sets the value.
125      *
126      * @jsptagref.attributedescription
127      * The name of the attribute to set.
128      *
129      * @jsptagref.databindable false
130      *
131      * @jsptagref.attributesyntaxvalue <i>string_name</i>
132      *
133      * @netui:attribute required="true" rtexprvalue="true"
134      * description="The name of the attribute to set."
135      */

136     public void setName(String JavaDoc name) {
137         _name = name;
138     }
139
140     /**
141      * Set the value of the <code>Attribute</code>. This attribute
142      * may be assigned a read only expression.
143      * @param value The value to use for the <code>Attribute</code>
144      * in the template.
145      *
146      * @jsptagref.attributedescription
147      * Sets the value of the attribute.
148      *
149      * @jsptagref.databindable Read Only
150      *
151      * @jsptagref.attributesyntaxvalue <i>string_or_expression_value</i>
152      *
153      * @netui:attribute required="true" rtexprvalue="true"
154      * description="Sets the value of the attribute."
155      */

156     public void setValue(String JavaDoc value)
157         throws JspException JavaDoc {
158         _value = value;
159     }
160
161     /**
162      * Tag Lifecycle method called when the tag is first seen. This method
163      * will add the <code>Attribute</code> value to a <code>HashMap</code>
164      * stored in the request allowing the template to access the value. If
165      * there are errors, the error text will be placed into the attribute
166      * value. Nothing is written into the <code>ServletResponse</code>
167      * @return EVAL_PAGE to continue processing the page.
168      * @throws JspException on error
169      */

170     public int doStartTag()
171             throws JspException JavaDoc {
172         ServletRequest JavaDoc req = pageContext.getRequest();
173         HashMap JavaDoc atts = (HashMap JavaDoc) req.getAttribute(TEMPLATE_ATTRIBUTES);
174         if (atts == null) {
175             atts = new HashMap JavaDoc();
176             req.setAttribute(TEMPLATE_ATTRIBUTES,atts);
177         }
178         if (hasErrors()) {
179             String JavaDoc s = getErrorsReport();
180             atts.put(_name,s);
181             localRelease();
182             return EVAL_PAGE;
183         }
184
185         atts.put(_name,_value);
186         localRelease();
187         return EVAL_PAGE;
188     }
189
190     /**
191      * Reset all of the fields of the tag.
192      */

193     protected void localRelease() {
194         super.localRelease();
195         _name = null;
196         _value = null;
197     }
198 }
199
Popular Tags