KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > tags > form > AbstractFormTag


1 /*
2  * Copyright 2002-2007 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.form;
18
19 import java.beans.PropertyEditor JavaDoc;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22
23 import org.springframework.util.ObjectUtils;
24 import org.springframework.web.servlet.tags.HtmlEscapingAwareTag;
25 import org.springframework.web.util.ExpressionEvaluationUtils;
26
27 /**
28  * Base class for all JSP form tags. Provides utility methods for
29  * null-safe EL evaluation and for accessing and working with a {@link TagWriter}.
30  *
31  * <p>Subclasses should implement the {@link #writeTagContent(TagWriter)} to perform
32  * actual tag rendering.
33  *
34  * <p>Subclasses (or test classes) can override the {@link #createTagWriter()} method to
35  * redirect output to a {@link java.io.Writer} other than the {@link javax.servlet.jsp.JspWriter}
36  * associated with the current {@link javax.servlet.jsp.PageContext}.
37  *
38  * @author Rob Harrop
39  * @since 2.0
40  */

41 public abstract class AbstractFormTag extends HtmlEscapingAwareTag {
42
43     /**
44      * Helper class for rendering values into HTML.
45      */

46     private final ValueFormatter valueFormatter = new ValueFormatter();
47
48     /**
49      * Evaluates the supplied value for the supplied attribute name. If the supplied value
50      * is <code>null</code> then <code>null</code> is returned, otherwise evaluation is
51      * handled using {@link ExpressionEvaluationUtils#evaluate(String, String, javax.servlet.jsp.PageContext)}.
52      */

53     protected Object JavaDoc evaluate(String JavaDoc attributeName, Object JavaDoc value) throws JspException JavaDoc {
54         if (value instanceof String JavaDoc) {
55             return ExpressionEvaluationUtils.evaluate(attributeName, (String JavaDoc)value, this.pageContext);
56         }
57         else {
58             return value;
59         }
60     }
61
62     /**
63      * Optionally writes the supplied value under the supplied attribute name into the supplied
64      * {@link TagWriter}. In this case, the supplied value is {@link #evaluate evaluated} first
65      * and then the {@link ObjectUtils#getDisplayString String representation} is written as the
66      * attribute value. If the resultant <code>String</code> representation is <code>null</code>
67      * or empty, no attribute is written.
68      * @see TagWriter#writeOptionalAttributeValue(String, String)
69      */

70     protected final void writeOptionalAttribute(TagWriter tagWriter, String JavaDoc attributeName, String JavaDoc value) throws JspException JavaDoc {
71         tagWriter.writeOptionalAttributeValue(attributeName, getDisplayString(evaluate(attributeName, value)));
72     }
73
74     /**
75      * Creates the {@link TagWriter} which all output will be written to. By default,
76      * the {@link TagWriter} writes its output to the {@link javax.servlet.jsp.JspWriter}
77      * for the current {@link javax.servlet.jsp.PageContext}. Subclasses may choose to
78      * change the {@link java.io.Writer} to which output is actually written.
79      */

80     protected TagWriter createTagWriter() {
81         return new TagWriter(this.pageContext.getOut());
82     }
83
84     /**
85      * Provides a simple template method that calls {@link #createTagWriter()} and passes
86      * the created {@link TagWriter} to the {@link #writeTagContent(TagWriter)} method.
87      * @return the value returned by {@link #writeTagContent(TagWriter)}
88      */

89     protected final int doStartTagInternal() throws Exception JavaDoc {
90         return writeTagContent(createTagWriter());
91     }
92
93     /**
94      * Gets the display value of the supplied <code>Object</code>, HTML escaped
95      * as required. This version is <strong>not</strong> {@link PropertyEditor}-aware.
96      */

97     protected String JavaDoc getDisplayString(Object JavaDoc value) {
98         return this.valueFormatter.getDisplayString(value, isHtmlEscape());
99     }
100
101     /**
102      * Gets the display value of the supplied <code>Object</code>, HTML escaped
103      * as required. If the supplied value is not a {@link String} and the supplied
104      * {@link PropertyEditor} is not null then the {@link PropertyEditor} is used
105      * to obtain the display value.
106      */

107     protected String JavaDoc getDisplayString(Object JavaDoc value, PropertyEditor JavaDoc propertyEditor) {
108         return this.valueFormatter.getDisplayString(value, propertyEditor, isHtmlEscape());
109     }
110
111     /**
112      * Subclasses should implement this method to perform tag content rendering.
113      * @return valid tag render instruction as per {@link javax.servlet.jsp.tagext.Tag#doStartTag()}.
114      */

115     protected abstract int writeTagContent(TagWriter tagWriter) throws JspException JavaDoc;
116
117 }
118
Popular Tags