001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.html;
020:
021: import org.apache.beehive.netui.util.internal.InternalStringBuilder;
022:
023: import org.apache.beehive.netui.tags.HtmlUtils;
024: import org.apache.beehive.netui.tags.rendering.AbstractRenderAppender;
025:
026: import javax.servlet.jsp.JspException;
027: import java.util.ArrayList;
028:
029: /**
030: * This is a base class providing implementation for both the Label and FormLabel.
031: */
032: public abstract class LabelBase extends HtmlBaseTag implements
033: IFormattable {
034: protected static final String DEFAULT_NULL_TEXT = "";
035:
036: protected Object _defaultValue; // The attribute value of the defaultValue attribute
037: protected Object _value; // The text of the Label.
038: protected boolean _escapeWhiteSpace = true; // escape white space flag
039: protected boolean _formatterErrors = false; // The formatter has errors.
040: protected boolean _formatDefaultValue = false; // Format the defaultValue
041: private ArrayList _formatters; // The formatters
042:
043: /**
044: * Set the default value of this Label.
045: * This can be an expression. If the default value is an expression
046: * all formatters will be applied, otherwise the default value will be output
047: * without being formatted.
048: * @param defaultValue the default value
049: * @jsptagref.attributedescription The String literal or expression to be used as the
050: * default output. If the default value is an expression all formatters will be applied,
051: * otherwise the default value will be output without being formatted.
052: * @jsptagref.databindable false
053: * @jsptagref.attributesyntaxvalue <i>string_or_expression_defaultValue</i>
054: * @netui:attribute required="false" rtexprvalue="true" type="java.lang.Object"
055: * description="Set the default value of this Label or Span."
056: */
057: public void setDefaultValue(Object defaultValue) {
058: _defaultValue = defaultValue;
059: }
060:
061: /**
062: * Boolean indicating whether the formatter should be applied to the defaultValue.
063: * The default is "false" meaning formatters will not be applied.
064: * @param formatDisplay Apply formatting to the default value.
065: * @jsptagref.attributedescription Boolean indicating whether the formatter should be applied
066: * to the defaultValue. The default is "false" meaning formatters will not be applied.
067: * @jsptagref.databindable false
068: * @jsptagref.attributesyntaxvalue boolean_formatDisplay
069: * @netui:attribute required="false" rtexprvalue="true"
070: * description="Apply formatting to the default value of this Label or Span."
071: */
072: public void setFormatDefaultValue(boolean formatDisplay) {
073: _formatDefaultValue = formatDisplay;
074: }
075:
076: /**
077: * Sets the text of the Label.
078: * @param value the Label value or expression.
079: * @jsptagref.attributedescription The String literal or expression used as the text of the Label.
080: * @jsptagref.databindable false
081: * @jsptagref.attributesyntaxvalue <i>string_or_expression_output</i>
082: * @netui:attribute required="true" rtexprvalue="true" type="java.lang.Object"
083: * description="Sets the text of the Label or Span."
084: */
085: public void setValue(Object value) {
086: _value = value;
087: }
088:
089: /**
090: * Sets a <code>boolean</code> flag indicating if we will escape
091: * white space for HTML. If this is <code>true</code> the white space
092: * charcters ' ' will be converted into " " and '\n' converted into
093: * "<br />". The result is that in HTML white space will be represented
094: * correctly. If this is <code>false</code> then white space will be
095: * output as it is found in the value.
096: * @param escapeWhiteSpace boolean indicating if we are escaping for white space.
097: * @jsptagref.attributedescription Sets a boolean flag indicating if we will escape
098: * white space for HTML.
099: * @jsptagref.attributesyntaxvalue <i>boolean_escapeWhiteSpace</i>
100: * @netui:attribute required="false" rtexprvalue="true" type="boolean"
101: * description="Sets a boolean flag indicating if we will escape
102: * white space for HTML."
103: */
104: public void setEscapeWhiteSpaceForHtml(boolean escapeWhiteSpace) {
105: _escapeWhiteSpace = escapeWhiteSpace;
106:
107: }
108:
109: /**
110: * Adds a FormatTag.Formatter to the Label's set of formatters
111: * @param formatter a FormatTag.Formatter added by a child FormatTag.
112: */
113: public void addFormatter(FormatTag.Formatter formatter) {
114: if (_formatters == null)
115: _formatters = new ArrayList();
116: _formatters.add(formatter);
117: }
118:
119: /**
120: * Indicate that a formatter has reported an error so the formatter should output it's
121: * body text.
122: */
123: public void formatterHasError() {
124: _formatterErrors = true;
125: }
126:
127: /**
128: * Filter the specified string for characters that are senstive to
129: * HTML interpreters, returning the string with these characters replaced
130: * by the corresponding character entities.
131: * @param value The string to be filtered and returned
132: * @param markupHTMLSpaceReturn convert space characters and return characters
133: * to &nbsp; and <br /> marketup for html.
134: */
135: protected void filter(String value, AbstractRenderAppender writer,
136: boolean markupHTMLSpaceReturn) {
137: if (value.equals(" ")) {
138: writer.append(" ");
139: return;
140: }
141:
142: HtmlUtils.filter(value, writer, markupHTMLSpaceReturn);
143: }
144:
145: /**
146: * Release any acquired resources.
147: */
148: protected void localRelease() {
149: super .localRelease();
150:
151: _defaultValue = null;
152: _escapeWhiteSpace = true;
153: _formatters = null;
154: _formatterErrors = false;
155: _formatDefaultValue = false;
156: _value = null;
157: }
158:
159: /**
160: *
161: */
162: protected String formatText(Object text) throws JspException {
163: InternalStringBuilder errors = null;
164: if (text == null)
165: return null;
166:
167: if (_formatters == null)
168: return text.toString();
169:
170: for (int i = 0; i < _formatters.size(); i++) {
171: FormatTag.Formatter currentFormatter = (FormatTag.Formatter) _formatters
172: .get(i);
173:
174: // if there are errors in the formatter, we need to report them
175: // and continue to the next one.
176: if (currentFormatter.hasError()) {
177: if (errors == null) {
178: errors = new InternalStringBuilder(32);
179: }
180: assert (errors != null);
181: errors.append(currentFormatter.getErrorMessage());
182: continue;
183: }
184:
185: // apply the formatter.
186: try {
187: text = currentFormatter.format(text);
188: } catch (JspException e) {
189: registerTagError(e.getMessage(), e);
190: }
191: }
192: // if there were errors we will return the errors followed by the text,
193: // otherwise just return the text.
194: if (errors != null) {
195: return errors.toString() + text.toString();
196: }
197: return text.toString();
198: }
199: }
|