KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > tags > TransformTag


1 /*
2  * Copyright 2002-2005 the original author or authors.
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
17 package org.springframework.web.servlet.tags;
18
19 import java.beans.PropertyEditor JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
24
25 import org.springframework.web.util.ExpressionEvaluationUtils;
26 import org.springframework.web.util.HtmlUtils;
27 import org.springframework.web.util.TagUtils;
28
29 /**
30  * Tag for transforming reference data values from form controllers and
31  * other objects inside a <code>spring:bind</code> tag.
32  *
33  * <p>The BindTag has a PropertyEditor that it uses to transform properties of
34  * a bean to a String, useable in HTML forms. This tag uses that PropertyEditor
35  * to transform objects passed into this tag.
36  *
37  * @author Alef Arendsen
38  * @author Juergen Hoeller
39  * @since 20.09.2003
40  * @see BindTag
41  */

42 public class TransformTag extends HtmlEscapingAwareTag {
43
44     /** the value to transform using the appropriate property editor */
45     private Object JavaDoc value;
46
47     /** the variable to put the result in */
48     private String JavaDoc var;
49
50     /** the scope of the variable the result will be put in */
51     private String JavaDoc scope = TagUtils.SCOPE_PAGE;
52
53
54     /**
55      * Set the value to transform, using the appropriate PropertyEditor
56      * from the enclosing BindTag.
57      * <p>The value can either be a plain value to transform (a hard-coded String
58      * value in a JSP or a JSP expression), or a JSP EL expression to be evaluated
59      * (transforming the result of the expression).
60      * <p>Like all of Spring's JSP tags, this tag is capable of parsing EL expressions
61      * itself, on any JSP version. Note, however, that EL expressions in a JSP 2.0 page
62      * will be evaluated by the JSP container, with the result getting passed in here.
63      * For this reason, the type of this property is Object (accepting any result
64      * object from a pre-evaluated expression) rather than String.
65      */

66     public void setValue(Object JavaDoc value) {
67         this.value = value;
68     }
69
70     /**
71      * Set PageContext attribute name under which to expose
72      * a variable that contains the result of the transformation.
73      * @see #setScope
74      * @see javax.servlet.jsp.PageContext#setAttribute
75      */

76     public void setVar(String JavaDoc var) {
77         this.var = var;
78     }
79
80     /**
81      * Set the scope to export the variable to.
82      * Default is SCOPE_PAGE ("page").
83      * @see #setVar
84      * @see org.springframework.web.util.TagUtils#SCOPE_PAGE
85      * @see javax.servlet.jsp.PageContext#setAttribute
86      */

87     public void setScope(String JavaDoc scope) {
88         this.scope = scope;
89     }
90
91
92     protected final int doStartTagInternal() throws JspException JavaDoc {
93         Object JavaDoc resolvedValue = this.value;
94         if (this.value instanceof String JavaDoc) {
95             String JavaDoc strValue = (String JavaDoc) this.value;
96             resolvedValue = ExpressionEvaluationUtils.evaluate("value", strValue, pageContext);
97         }
98         if (resolvedValue != null) {
99             // Find the BindTag, if applicable.
100
BindTag tag = (BindTag) TagSupport.findAncestorWithClass(this, BindTag.class);
101             if (tag == null) {
102                 // The tag can only be used within a BindTag.
103
throw new JspException JavaDoc("TransformTag can only be used within BindTag");
104             }
105             // OK, get the property editor.
106
PropertyEditor JavaDoc editor = tag.getEditor();
107             String JavaDoc result = null;
108             if (editor != null) {
109                 // If an editor was found, edit the value.
110
editor.setValue(resolvedValue);
111                 result = editor.getAsText();
112             }
113             else {
114                 // Else, just do a toString.
115
result = resolvedValue.toString();
116             }
117             result = isHtmlEscape() ? HtmlUtils.htmlEscape(result) : result;
118             String JavaDoc resolvedVar = ExpressionEvaluationUtils.evaluateString("var", this.var, pageContext);
119             if (resolvedVar != null) {
120                 String JavaDoc resolvedScope = ExpressionEvaluationUtils.evaluateString("scope", this.scope, pageContext);
121                 pageContext.setAttribute(resolvedVar, result, TagUtils.getScope(resolvedScope));
122             }
123             else {
124                 try {
125                     // Else, just print it out.
126
pageContext.getOut().print(result);
127                 }
128                 catch (IOException JavaDoc ex) {
129                     throw new JspException JavaDoc(ex);
130                 }
131             }
132         }
133         return SKIP_BODY;
134     }
135
136 }
137
Popular Tags