KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2006 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 org.springframework.util.ObjectUtils;
22 import org.springframework.web.util.HtmlUtils;
23
24 /**
25  * Helper class for formatting values for rendering via a form tag. Supports two styles of
26  * formatting: plain and {@link PropertyEditor}-aware.
27  *
28  * <p>Plain formatting simply prevents the string '<code>null</code>' from appearing, replacing
29  * it with an empty String, and adds HTML escaping as required.
30  *
31  * <p>{@link PropertyEditor}-aware formatting will attempt to use the supplied {@link PropertyEditor}
32  * to render any non-String value before applying the default rules of plain formatting.
33  *
34  * @author Rob Harrop
35  * @since 2.0
36  */

37 final class ValueFormatter {
38
39     /**
40      * Gets the display value of the supplied <code>Object</code>, HTML escaped
41      * as required. This version is <strong>not</strong> {@link PropertyEditor}-aware.
42      * @see #getDisplayString(Object, java.beans.PropertyEditor, boolean)
43      */

44     public String JavaDoc getDisplayString(Object JavaDoc value, boolean htmlEscape) {
45         String JavaDoc displayValue = ObjectUtils.getDisplayString(value);
46         return (htmlEscape ? HtmlUtils.htmlEscape(displayValue) : displayValue);
47     }
48
49     /**
50      * Gets the display value of the supplied <code>Object</code>, HTML escaped
51      * as required. If the supplied value is not a {@link String} and the supplied
52      * {@link PropertyEditor} is not null then the {@link PropertyEditor} is used
53      * to obtain the display value.
54      * @see #getDisplayString(Object, boolean)
55      */

56     public String JavaDoc getDisplayString(Object JavaDoc value, PropertyEditor JavaDoc propertyEditor, boolean htmlEscape) {
57         if (value instanceof String JavaDoc || propertyEditor == null) {
58             return getDisplayString(value, htmlEscape);
59         }
60
61         Object JavaDoc originalValue = propertyEditor.getValue();
62         try {
63             propertyEditor.setValue(value);
64             return getDisplayString(propertyEditor.getAsText(), htmlEscape);
65         }
66         catch (Exception JavaDoc ex) {
67             // the PropertyEditor might not support this value... pass through
68
return getDisplayString(value, htmlEscape);
69         }
70         finally {
71             propertyEditor.setValue(originalValue);
72         }
73     }
74
75 }
76
Popular Tags