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.tags.HtmlUtils;
022: import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
023: import org.apache.beehive.netui.tags.rendering.LabelTag;
024: import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
025: import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
026: import org.apache.beehive.netui.util.Bundle;
027: import org.apache.beehive.netui.util.logging.Logger;
028:
029: import javax.servlet.ServletRequest;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.jsp.JspException;
032:
033: /**
034: * @jsptagref.tagdescription Associates text with an input element in a {@link Form}.
035: * @netui:tag name="label" description="a <label> element which may point to a form control."
036: */
037: public class Label extends LabelBase implements IFormattable {
038: private static final Logger logger = Logger
039: .getInstance(Label.class);
040:
041: private LabelTag.State _state = new LabelTag.State();
042:
043: /**
044: * Return the name of the Tag.
045: */
046: public String getTagName() {
047: return "Label";
048: }
049:
050: /**
051: * This method will return the state associated with the tag. This is used by this
052: * base class to access the individual state objects created by the tags.
053: * @return a subclass of the <code>AbstractHtmlState</code> class.
054: */
055: protected AbstractHtmlState getState() {
056: return _state;
057: }
058:
059: /**
060: * Set the <code>for</code> attribute.
061: * @param forAttr the for attribute.
062: * @jsptagref.attributedescription The value of this attribute matches a tagId on an form input and links the value to that control.
063: * @jsptagref.databindable false
064: * @jsptagref.attributesyntaxvalue <i>string_forAttr</i>
065: * @netui:attribute required="false" rtexprvalue="true"
066: * description="The value of this attribute matches a tagId on an form input and links the value to that control."
067: */
068: public void setFor(String forAttr) {
069: _state.forAttr = forAttr;
070: }
071:
072: /**
073: * Prepare the label formatters.
074: * @throws JspException if a JSP exception has occurred
075: */
076: public int doStartTag() throws JspException {
077: return EVAL_BODY_BUFFERED;
078: }
079:
080: /**
081: * Render the label.
082: * @throws JspException if a JSP exception has occurred
083: */
084: public int doEndTag() throws JspException {
085: boolean usingDefault = false;
086: boolean bypassEscape = false;
087:
088: String scriptId = null;
089: ServletRequest req = pageContext.getRequest();
090: Object labelObject = null;
091:
092: String labelValue = null;
093:
094: // if this is not client side binding, evalute the value
095: if (_value != null)
096: labelObject = _value;
097: else {
098: if (_defaultValue != null) {
099: labelObject = _defaultValue;
100: bypassEscape = HtmlUtils.containsEntity(_defaultValue
101: .toString());
102: } else {
103: logger.warn(Bundle.getString(
104: "Tags_LabelExpressionNull", _value));
105: labelObject = DEFAULT_NULL_TEXT;
106: }
107: usingDefault = true;
108: }
109:
110: // we assume that tagId will over have override id if both
111: // are defined.
112: if (_state.id != null) {
113: scriptId = renderNameAndId((HttpServletRequest) req,
114: _state, null);
115: }
116:
117: // push the evaluated expression when we are not client side bound...
118: labelValue = (usingDefault && !_formatDefaultValue) ? labelObject
119: .toString()
120: : formatText(labelObject);
121:
122: if (hasErrors())
123: return reportAndExit(EVAL_PAGE);
124:
125: // fully qualify the for attribute if it exists
126: if (_state.forAttr != null) {
127: _state.forAttr = getIdForTagId(_state.forAttr);
128:
129: }
130:
131: WriteRenderAppender writer = new WriteRenderAppender(
132: pageContext);
133: TagRenderingBase br = TagRenderingBase.Factory.getRendering(
134: TagRenderingBase.LABEL_TAG, req);
135: br.doStartTag(writer, _state);
136:
137: // push the evaluated expression when we are not client side bound...
138: //if (!usingDefault)
139: // labelValue = formatText(labelValue);
140:
141: if (!bypassEscape)
142: filter(labelValue, writer, _escapeWhiteSpace);
143: else
144: write(labelValue);
145: br.doEndTag(writer);
146:
147: if (scriptId != null)
148: write(scriptId);
149:
150: localRelease();
151: return EVAL_PAGE;
152: }
153:
154: /**
155: * Release any acquired resources.
156: */
157: protected void localRelease() {
158: super.localRelease();
159:
160: _state.clear();
161: }
162: }
|