KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.internal.InternalStringBuilder;
21
22 import org.apache.beehive.netui.tags.HtmlUtils;
23 import org.apache.beehive.netui.tags.rendering.AbstractRenderAppender;
24
25 import javax.servlet.jsp.JspException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27
28 /**
29  * This is a base class providing implementation for both the Label and FormLabel.
30  */

31 public abstract class LabelBase extends HtmlBaseTag
32         implements IFormattable
33 {
34     protected static final String JavaDoc DEFAULT_NULL_TEXT = "";
35
36     protected Object JavaDoc _defaultValue; // The attribute value of the defaultValue attribute
37
protected Object JavaDoc _value; // The text of the Label.
38
protected boolean _escapeWhiteSpace = true; // escape white space flag
39
protected boolean _formatterErrors = false; // The formatter has errors.
40
protected boolean _formatDefaultValue = false; // Format the defaultValue
41
private ArrayList JavaDoc _formatters; // The formatters
42

43     /**
44      * Set the default value of this Label.
45      * This can be an expression. If the default value is an expression
46      * all formatters will be applied, otherwise the default value will be output
47      * without being formatted.
48      * @param defaultValue the default value
49      * @jsptagref.attributedescription The String literal or expression to be used as the
50      * default output. If the default value is an expression all formatters will be applied,
51      * otherwise the default value will be output without being formatted.
52      * @jsptagref.databindable false
53      * @jsptagref.attributesyntaxvalue <i>string_or_expression_defaultValue</i>
54      * @netui:attribute required="false" rtexprvalue="true" type="java.lang.Object"
55      * description="Set the default value of this Label or Span."
56      */

57     public void setDefaultValue(Object JavaDoc defaultValue)
58     {
59         _defaultValue = defaultValue;
60     }
61
62     /**
63      * Boolean indicating whether the formatter should be applied to the defaultValue.
64      * The default is "false" meaning formatters will not be applied.
65      * @param formatDisplay Apply formatting to the default value.
66      * @jsptagref.attributedescription Boolean indicating whether the formatter should be applied
67      * to the defaultValue. The default is "false" meaning formatters will not be applied.
68      * @jsptagref.databindable false
69      * @jsptagref.attributesyntaxvalue boolean_formatDisplay
70      * @netui:attribute required="false" rtexprvalue="true"
71      * description="Apply formatting to the default value of this Label or Span."
72      */

73     public void setFormatDefaultValue(boolean formatDisplay)
74     {
75         _formatDefaultValue = formatDisplay;
76     }
77
78     /**
79      * Sets the text of the Label.
80      * @param value the Label value or expression.
81      * @jsptagref.attributedescription The String literal or expression used as the text of the Label.
82      * @jsptagref.databindable false
83      * @jsptagref.attributesyntaxvalue <i>string_or_expression_output</i>
84      * @netui:attribute required="true" rtexprvalue="true" type="java.lang.Object"
85      * description="Sets the text of the Label or Span."
86      */

87     public void setValue(Object JavaDoc value)
88     {
89         _value = value;
90     }
91
92     /**
93      * Sets a <code>boolean</code> flag indicating if we will escape
94      * white space for HTML. If this is <code>true</code> the white space
95      * charcters ' ' will be converted into "&nbsp;" and '\n' converted into
96      * "<br />". The result is that in HTML white space will be represented
97      * correctly. If this is <code>false</code> then white space will be
98      * output as it is found in the value.
99      * @param escapeWhiteSpace boolean indicating if we are escaping for white space.
100      * @jsptagref.attributedescription Sets a boolean flag indicating if we will escape
101      * white space for HTML.
102      * @jsptagref.attributesyntaxvalue <i>boolean_escapeWhiteSpace</i>
103      * @netui:attribute required="false" rtexprvalue="true" type="boolean"
104      * description="Sets a boolean flag indicating if we will escape
105      * white space for HTML."
106      */

107     public void setEscapeWhiteSpaceForHtml(boolean escapeWhiteSpace)
108     {
109         _escapeWhiteSpace = escapeWhiteSpace;
110
111     }
112
113     /**
114      * Adds a FormatTag.Formatter to the Label's set of formatters
115      * @param formatter a FormatTag.Formatter added by a child FormatTag.
116      */

117     public void addFormatter(FormatTag.Formatter formatter)
118     {
119         if (_formatters == null)
120             _formatters = new ArrayList JavaDoc();
121         _formatters.add(formatter);
122     }
123
124     /**
125      * Indicate that a formatter has reported an error so the formatter should output it's
126      * body text.
127      */

128     public void formatterHasError()
129     {
130         _formatterErrors = true;
131     }
132
133     /**
134      * Filter the specified string for characters that are senstive to
135      * HTML interpreters, returning the string with these characters replaced
136      * by the corresponding character entities.
137      * @param value The string to be filtered and returned
138      * @param markupHTMLSpaceReturn convert space characters and return characters
139      * to &amp;nbsp; and &lt;br /&gt; marketup for html.
140      */

141     protected void filter(String JavaDoc value, AbstractRenderAppender writer, boolean markupHTMLSpaceReturn)
142     {
143         if (value.equals(" ")) {
144             writer.append("&nbsp;");
145             return;
146         }
147
148         HtmlUtils.filter(value, writer, markupHTMLSpaceReturn);
149     }
150
151     /**
152      * Release any acquired resources.
153      */

154     protected void localRelease()
155     {
156         super.localRelease();
157
158         _defaultValue = null;
159         _escapeWhiteSpace = true;
160         _formatters = null;
161         _formatterErrors = false;
162         _formatDefaultValue = false;
163         _value = null;
164     }
165
166     /**
167      *
168      */

169     protected String JavaDoc formatText(Object JavaDoc text)
170             throws JspException JavaDoc
171     {
172         InternalStringBuilder errors = null;
173         if (text == null)
174             return null;
175
176         if (_formatters == null)
177             return text.toString();
178
179         for (int i = 0; i < _formatters.size(); i++) {
180             FormatTag.Formatter currentFormatter = (FormatTag.Formatter) _formatters.get(i);
181
182             // if there are errors in the formatter, we need to report them
183
// and continue to the next one.
184
if (currentFormatter.hasError()) {
185                 if (errors == null) {
186                     errors = new InternalStringBuilder(32);
187                 }
188                 assert(errors != null);
189                 errors.append(currentFormatter.getErrorMessage());
190                 continue;
191             }
192
193             // apply the formatter.
194
try {
195                 text = currentFormatter.format(text);
196             }
197             catch (JspException JavaDoc e) {
198                 registerTagError(e.getMessage(), e);
199             }
200         }
201         // if there were errors we will return the errors followed by the text,
202
// otherwise just return the text.
203
if (errors != null) {
204             return errors.toString() + text.toString();
205         }
206         return text.toString();
207     }
208 }
209
Popular Tags