KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > Label


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.html;
19
20 import org.apache.beehive.netui.tags.HtmlUtils;
21 import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
22 import org.apache.beehive.netui.tags.rendering.LabelTag;
23 import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
24 import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
25 import org.apache.beehive.netui.util.Bundle;
26 import org.apache.beehive.netui.util.logging.Logger;
27
28 import javax.servlet.ServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.jsp.JspException JavaDoc;
31
32 /**
33  * @jsptagref.tagdescription Associates text with an input element in a {@link Form}.
34  * @netui:tag name="label" description="a <label> element which may point to a form control."
35  */

36 public class Label extends LabelBase
37         implements IFormattable
38 {
39     private static final Logger logger = Logger.getInstance(Label.class);
40
41     private LabelTag.State _state = new LabelTag.State();
42
43     /**
44      * Return the name of the Tag.
45      */

46     public String JavaDoc getTagName()
47     {
48         return "Label";
49     }
50
51     /**
52      * This method will return the state associated with the tag. This is used by this
53      * base class to access the individual state objects created by the tags.
54      * @return a subclass of the <code>AbstractHtmlState</code> class.
55      */

56     protected AbstractHtmlState getState()
57     {
58         return _state;
59     }
60
61     /**
62      * Set the <code>for</code> attribute.
63      * @param forAttr the for attribute.
64      * @jsptagref.attributedescription The value of this attribute matches a tagId on an form input and links the value to that control.
65      * @jsptagref.databindable false
66      * @jsptagref.attributesyntaxvalue <i>string_forAttr</i>
67      * @netui:attribute required="false" rtexprvalue="true"
68      * description="The value of this attribute matches a tagId on an form input and links the value to that control."
69      */

70     public void setFor(String JavaDoc forAttr)
71     {
72         _state.forAttr = forAttr;
73     }
74
75     /**
76      * Prepare the label formatters.
77      * @throws JspException if a JSP exception has occurred
78      */

79     public int doStartTag() throws JspException JavaDoc
80     {
81         return EVAL_BODY_BUFFERED;
82     }
83
84
85     /**
86      * Render the label.
87      * @throws JspException if a JSP exception has occurred
88      */

89     public int doEndTag() throws JspException JavaDoc
90     {
91         boolean usingDefault = false;
92         boolean bypassEscape = false;
93
94         String JavaDoc scriptId = null;
95         ServletRequest JavaDoc req = pageContext.getRequest();
96         Object JavaDoc labelObject = null;
97
98         String JavaDoc labelValue = null;
99
100         // if this is not client side binding, evalute the value
101
if (_value != null)
102             labelObject = _value;
103         else {
104             if (_defaultValue != null) {
105                 labelObject = _defaultValue;
106                 bypassEscape = HtmlUtils.containsEntity(_defaultValue.toString());
107             }
108             else {
109                 logger.warn(Bundle.getString("Tags_LabelExpressionNull", _value));
110                 labelObject = DEFAULT_NULL_TEXT;
111             }
112             usingDefault = true;
113         }
114
115         // we assume that tagId will over have override id if both
116
// are defined.
117
if (_state.id != null) {
118             scriptId = renderNameAndId((HttpServletRequest JavaDoc) req, _state, null);
119         }
120
121         // push the evaluated expression when we are not client side bound...
122
labelValue = (usingDefault && !_formatDefaultValue) ?
123                 labelObject.toString() : formatText(labelObject);
124
125         if (hasErrors())
126             return reportAndExit(EVAL_PAGE);
127
128         // fully qualify the for attribute if it exists
129
if (_state.forAttr != null) {
130             _state.forAttr = getIdForTagId(_state.forAttr);
131
132         }
133
134         WriteRenderAppender writer = new WriteRenderAppender(pageContext);
135         TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.LABEL_TAG, req);
136         br.doStartTag(writer, _state);
137
138         // push the evaluated expression when we are not client side bound...
139
//if (!usingDefault)
140
// labelValue = formatText(labelValue);
141

142         if (!bypassEscape)
143             filter(labelValue, writer, _escapeWhiteSpace);
144         else
145             write(labelValue);
146         br.doEndTag(writer);
147
148         if (scriptId != null)
149             write(scriptId);
150
151         localRelease();
152         return EVAL_PAGE;
153     }
154
155     /**
156      * Release any acquired resources.
157      */

158     protected void localRelease()
159     {
160         super.localRelease();
161
162         _state.clear();
163     }
164 }
165
Popular Tags