KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > validation > AbstractPropertyBindingResult


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.validation;
18
19 import java.beans.PropertyEditor JavaDoc;
20
21 import org.springframework.beans.ConfigurablePropertyAccessor;
22 import org.springframework.beans.PropertyAccessorUtils;
23 import org.springframework.beans.PropertyEditorRegistry;
24
25 /**
26  * Abstract base class for BindingResult implementations that work with
27  * Spring's PropertyAccessor mechanism. Pre-implements field access
28  * through delegation to the corresponding PropertyAccessor methods.
29  *
30  * @author Juergen Hoeller
31  * @since 2.0
32  * @see #getPropertyAccessor()
33  * @see org.springframework.beans.PropertyAccessor
34  * @see org.springframework.beans.ConfigurablePropertyAccessor
35  */

36 public abstract class AbstractPropertyBindingResult extends AbstractBindingResult {
37
38     /**
39      * Create a new AbstractPropertyBindingResult instance.
40      * @param objectName the name of the target object
41      * @see DefaultMessageCodesResolver
42      */

43     protected AbstractPropertyBindingResult(String JavaDoc objectName) {
44         super(objectName);
45     }
46
47
48     /**
49      * Returns the underlying PropertyAccessor.
50      * @see #getPropertyAccessor()
51      */

52     public PropertyEditorRegistry getPropertyEditorRegistry() {
53         return getPropertyAccessor();
54     }
55
56     /**
57      * Returns the canonical property name.
58      * @see org.springframework.beans.PropertyAccessorUtils#canonicalPropertyName
59      */

60     protected String JavaDoc canonicalFieldName(String JavaDoc field) {
61         return PropertyAccessorUtils.canonicalPropertyName(field);
62     }
63
64     /**
65      * Determines the field type from the property type.
66      * @see #getPropertyAccessor()
67      */

68     public Class JavaDoc getFieldType(String JavaDoc field) {
69         return getPropertyAccessor().getPropertyType(field);
70     }
71
72     /**
73      * Fetches the field value from the PropertyAccessor.
74      * @see #getPropertyAccessor()
75      */

76     protected Object JavaDoc getActualFieldValue(String JavaDoc field) {
77         return getPropertyAccessor().getPropertyValue(field);
78     }
79
80     /**
81      * Formats the field value based on registered PropertyEditors.
82      * @see #getCustomEditor
83      */

84     protected Object JavaDoc formatFieldValue(String JavaDoc field, Object JavaDoc value) {
85         PropertyEditor JavaDoc customEditor = getCustomEditor(field);
86         if (customEditor != null) {
87             customEditor.setValue(value);
88             String JavaDoc textValue = customEditor.getAsText();
89             // If the PropertyEditor returned null, there is no appropriate
90
// text representation for this value: only use it if non-null.
91
if (textValue != null) {
92                 return textValue;
93             }
94         }
95         return value;
96     }
97
98     /**
99      * Retrieve the custom PropertyEditor for the given field, if any.
100      * @param field the field name
101      * @return the custom PropertyEditor, or <code>null</code>
102      */

103     public PropertyEditor JavaDoc getCustomEditor(String JavaDoc field) {
104         String JavaDoc fixedField = fixedField(field);
105         Class JavaDoc type = getPropertyAccessor().getPropertyType(fixedField);
106         return getPropertyAccessor().findCustomEditor(type, fixedField);
107     }
108
109
110     /**
111      * Provide the PropertyAccessor to work with, according to the
112      * concrete strategy of access.
113      * <p>Note that a PropertyAccessor used by a BindingResult should
114      * always have its "extractOldValueForEditor" flag set to "true"
115      * by default, since this is typically possible without side effects
116      * for model objects that serve as data binding target.
117      * @see ConfigurablePropertyAccessor#setExtractOldValueForEditor
118      */

119     public abstract ConfigurablePropertyAccessor getPropertyAccessor();
120
121 }
122
Popular Tags