KickJava   Java API By Example, From Geeks To Geeks.

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


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.SpanTag;
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  * Generates styled text span based on an expression or literal value.
34  *
35  * <p>If the resulting value to be output is the <nobr>" "</nobr> String, it will output as
36  * the value <nobr><code>"&amp;nbsp;"</code></nobr>.</p>
37  * @jsptagref.tagdescription <p>Generates styled text based on a String literal or data binding expression.
38  *
39  * <p>The &lt;netui:span> tag is similar to the {@link Content} tag,
40  * except with respect to the way that it treats
41  * characters sensitive to HTML browsers.
42  * The &lt;netui:span> tag filters the input string for browser-sensitive characters
43  * and replaces these characters
44  * with the corresponding entity strings. For example, if you pass the
45  * string '&amp;amp;' to a &lt;netui:span> tag, the string '&amp;amp;amp;' will be written to
46  * the HTML source file, and the following will be displayed
47  * in the browser: '&amp;amp;'.
48  *
49  * <p>The following table shows how the &lt;netui:span> and &lt;netui:content> tags treat HTML-sensitive characters.
50  * <blockquote>
51  * <table border="1">
52  * <tr>
53  * <td width="30%"><b>tag</b></td>
54  * <td width="30%"><b>generated HTML source</b></td>
55  * <td width="30%"><b>displayed in browser</b></td>
56  * </tr>
57  * <tr>
58  * <td>&lt;netui:content value="&amp;amp;"/></td>
59  * <td>&amp;amp;</td>
60  * <td>&</td>
61  * </tr>
62  * <tr>
63  * <td>&lt;netui:span value="&amp;amp;"/></td>
64  * <td>&amp;amp;amp;</td>
65  * <td>&amp;amp;</td>
66  * </tr>
67  * </table>
68  * </blockquote>
69  *
70  * <p><b>Note:</b> escaping is <i>not</i> applied to browser-sensitive characters in
71  * the <code>defaultValue</code> attribute.
72  * @example In this first sample, a &lt;netui:span> tag displays the Form Bean's firstName property.
73  * The &lt;netui:span> tag will resolve this data binding expression, and display its value.
74  *
75  * <pre> &lt;netui:span value="${actionForm.firstName}" /></pre>
76  *
77  * <p>In this next sample, the <code>value</code> attribute will resolve to null.
78  * This causes the <code>defaultValue</code> to be displayed. The user will see '&nbsp;'.</p>
79  * <pre> &lt;netui:span value="${pageFlow.somethingNull}" defaultValue="&amp;nbsp;"/></pre>
80  *
81  * <p>In this next sample, the HTML will contain the text "&amp;amp;nbsp;" and the user will
82  * see '&amp;nbsp;'</p>
83  * <pre> &lt;netui:span value="${pageFlow.somethingNull}" defaultValue="&amp;amp;nbsp;"/></pre>
84  * @netui:tag name="span" description="Places formatted or dynamically generated text on the page inside an HTML span."
85  */

86 public class Span extends LabelBase
87         implements IFormattable
88 {
89     private static final Logger logger = Logger.getInstance(Span.class);
90
91     private SpanTag.State _state = new SpanTag.State();
92
93     /**
94      * Return the name of the Tag.
95      */

96     public String JavaDoc getTagName()
97     {
98         return "Label";
99     }
100
101     /**
102      * This method will return the state associated with the tag. This is used by this
103      * base class to access the individual state objects created by the tags.
104      * @return a subclass of the <code>AbstractHtmlState</code> class.
105      */

106     protected AbstractHtmlState getState()
107     {
108         return _state;
109     }
110
111     /**
112      * Prepare the label formatters.
113      * @throws JspException if a JSP exception has occurred
114      */

115     public int doStartTag() throws JspException JavaDoc
116     {
117         return EVAL_BODY_BUFFERED;
118     }
119
120     /**
121      * Render the label.
122      * @throws JspException if a JSP exception has occurred
123      */

124     public int doEndTag() throws JspException JavaDoc
125     {
126         boolean usingDefault = false;
127         boolean bypassEscape = false;
128
129         String JavaDoc scriptId = null;
130         ServletRequest JavaDoc req = pageContext.getRequest();
131
132         Object JavaDoc labelObject = null;
133
134         // if this is not client side binding, evalute the value
135
if (_value != null)
136             labelObject = _value;
137         else {
138             if (_defaultValue != null) {
139                 labelObject = _defaultValue;
140                 bypassEscape = HtmlUtils.containsEntity(_defaultValue.toString());
141             }
142             else {
143                 logger.warn(Bundle.getString("Tags_LabelExpressionNull", _value));
144                 labelObject = DEFAULT_NULL_TEXT;
145             }
146             usingDefault = true;
147         }
148
149         // we assume that tagId will over have override id if both
150
// are defined.
151
if (_state.id != null) {
152             scriptId = renderNameAndId((HttpServletRequest JavaDoc) req, _state, null);
153         }
154
155         // push the evaluated expression when we are not client side bound...
156
String JavaDoc labelValue = (usingDefault && !_formatDefaultValue) ?
157                 labelObject.toString() : formatText(labelObject);
158
159         // if there were errors in the formatters, report them.
160
if (_formatterErrors) {
161             if (bodyContent != null) {
162                 String JavaDoc value = bodyContent.getString().trim();
163                 bodyContent.clearBody();
164                 write(value);
165             }
166         }
167
168         if (hasErrors())
169             return reportAndExit(EVAL_PAGE);
170
171         WriteRenderAppender writer = new WriteRenderAppender(pageContext);
172         TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.SPAN_TAG, req);
173         br.doStartTag(writer, _state);
174
175         if (!bypassEscape)
176             filter(labelValue, writer, _escapeWhiteSpace);
177         else
178             write(labelValue);
179
180         br.doEndTag(writer);
181
182         if (scriptId != null)
183             write(scriptId);
184
185         localRelease();
186         return EVAL_PAGE;
187     }
188
189     /**
190      * Release any acquired resources.
191      */

192     protected void localRelease()
193     {
194         super.localRelease();
195         _state.clear();
196     }
197 }
198
Popular Tags