001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.servlet.tags;
018:
019: import java.beans.PropertyEditor;
020: import java.io.IOException;
021:
022: import javax.servlet.jsp.JspException;
023: import javax.servlet.jsp.tagext.TagSupport;
024:
025: import org.springframework.web.util.ExpressionEvaluationUtils;
026: import org.springframework.web.util.HtmlUtils;
027: import org.springframework.web.util.TagUtils;
028:
029: /**
030: * Tag for transforming reference data values from form controllers and
031: * other objects inside a <code>spring:bind</code> tag.
032: *
033: * <p>The BindTag has a PropertyEditor that it uses to transform properties of
034: * a bean to a String, useable in HTML forms. This tag uses that PropertyEditor
035: * to transform objects passed into this tag.
036: *
037: * @author Alef Arendsen
038: * @author Juergen Hoeller
039: * @since 20.09.2003
040: * @see BindTag
041: */
042: public class TransformTag extends HtmlEscapingAwareTag {
043:
044: /** the value to transform using the appropriate property editor */
045: private Object value;
046:
047: /** the variable to put the result in */
048: private String var;
049:
050: /** the scope of the variable the result will be put in */
051: private String scope = TagUtils.SCOPE_PAGE;
052:
053: /**
054: * Set the value to transform, using the appropriate PropertyEditor
055: * from the enclosing BindTag.
056: * <p>The value can either be a plain value to transform (a hard-coded String
057: * value in a JSP or a JSP expression), or a JSP EL expression to be evaluated
058: * (transforming the result of the expression).
059: * <p>Like all of Spring's JSP tags, this tag is capable of parsing EL expressions
060: * itself, on any JSP version. Note, however, that EL expressions in a JSP 2.0 page
061: * will be evaluated by the JSP container, with the result getting passed in here.
062: * For this reason, the type of this property is Object (accepting any result
063: * object from a pre-evaluated expression) rather than String.
064: */
065: public void setValue(Object value) {
066: this .value = value;
067: }
068:
069: /**
070: * Set PageContext attribute name under which to expose
071: * a variable that contains the result of the transformation.
072: * @see #setScope
073: * @see javax.servlet.jsp.PageContext#setAttribute
074: */
075: public void setVar(String var) {
076: this .var = var;
077: }
078:
079: /**
080: * Set the scope to export the variable to.
081: * Default is SCOPE_PAGE ("page").
082: * @see #setVar
083: * @see org.springframework.web.util.TagUtils#SCOPE_PAGE
084: * @see javax.servlet.jsp.PageContext#setAttribute
085: */
086: public void setScope(String scope) {
087: this .scope = scope;
088: }
089:
090: protected final int doStartTagInternal() throws JspException {
091: Object resolvedValue = this .value;
092: if (this .value instanceof String) {
093: String strValue = (String) this .value;
094: resolvedValue = ExpressionEvaluationUtils.evaluate("value",
095: strValue, pageContext);
096: }
097: if (resolvedValue != null) {
098: // Find the BindTag, if applicable.
099: BindTag tag = (BindTag) TagSupport.findAncestorWithClass(
100: this , BindTag.class);
101: if (tag == null) {
102: // The tag can only be used within a BindTag.
103: throw new JspException(
104: "TransformTag can only be used within BindTag");
105: }
106: // OK, get the property editor.
107: PropertyEditor editor = tag.getEditor();
108: String result = null;
109: if (editor != null) {
110: // If an editor was found, edit the value.
111: editor.setValue(resolvedValue);
112: result = editor.getAsText();
113: } else {
114: // Else, just do a toString.
115: result = resolvedValue.toString();
116: }
117: result = isHtmlEscape() ? HtmlUtils.htmlEscape(result)
118: : result;
119: String resolvedVar = ExpressionEvaluationUtils
120: .evaluateString("var", this .var, pageContext);
121: if (resolvedVar != null) {
122: String resolvedScope = ExpressionEvaluationUtils
123: .evaluateString("scope", this .scope,
124: pageContext);
125: pageContext.setAttribute(resolvedVar, result, TagUtils
126: .getScope(resolvedScope));
127: } else {
128: try {
129: // Else, just print it out.
130: pageContext.getOut().print(result);
131: } catch (IOException ex) {
132: throw new JspException(ex);
133: }
134: }
135: }
136: return SKIP_BODY;
137: }
138:
139: }
|