001: /*
002: * Copyright 2002-2007 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.validation;
018:
019: import java.beans.PropertyEditor;
020:
021: import org.springframework.beans.ConfigurablePropertyAccessor;
022: import org.springframework.beans.PropertyAccessorUtils;
023: import org.springframework.beans.PropertyEditorRegistry;
024:
025: /**
026: * Abstract base class for {@link BindingResult} implementations that work with
027: * Spring's {@link org.springframework.beans.PropertyAccessor} mechanism.
028: * Pre-implements field access through delegation to the corresponding
029: * PropertyAccessor methods.
030: *
031: * @author Juergen Hoeller
032: * @since 2.0
033: * @see #getPropertyAccessor()
034: * @see org.springframework.beans.PropertyAccessor
035: * @see org.springframework.beans.ConfigurablePropertyAccessor
036: */
037: public abstract class AbstractPropertyBindingResult extends
038: AbstractBindingResult {
039:
040: /**
041: * Create a new AbstractPropertyBindingResult instance.
042: * @param objectName the name of the target object
043: * @see DefaultMessageCodesResolver
044: */
045: protected AbstractPropertyBindingResult(String objectName) {
046: super (objectName);
047: }
048:
049: /**
050: * Returns the underlying PropertyAccessor.
051: * @see #getPropertyAccessor()
052: */
053: public PropertyEditorRegistry getPropertyEditorRegistry() {
054: return getPropertyAccessor();
055: }
056:
057: /**
058: * Returns the canonical property name.
059: * @see org.springframework.beans.PropertyAccessorUtils#canonicalPropertyName
060: */
061: protected String canonicalFieldName(String field) {
062: return PropertyAccessorUtils.canonicalPropertyName(field);
063: }
064:
065: /**
066: * Determines the field type from the property type.
067: * @see #getPropertyAccessor()
068: */
069: public Class getFieldType(String field) {
070: return getPropertyAccessor().getPropertyType(field);
071: }
072:
073: /**
074: * Fetches the field value from the PropertyAccessor.
075: * @see #getPropertyAccessor()
076: */
077: public Object getActualFieldValue(String field) {
078: return getPropertyAccessor().getPropertyValue(field);
079: }
080:
081: /**
082: * Formats the field value based on registered PropertyEditors.
083: * @see #getCustomEditor
084: */
085: protected Object formatFieldValue(String field, Object value) {
086: PropertyEditor customEditor = getCustomEditor(field);
087: if (customEditor != null) {
088: customEditor.setValue(value);
089: String textValue = customEditor.getAsText();
090: // If the PropertyEditor returned null, there is no appropriate
091: // text representation for this value: only use it if non-null.
092: if (textValue != null) {
093: return textValue;
094: }
095: }
096: return value;
097: }
098:
099: /**
100: * Retrieve the custom PropertyEditor for the given field, if any.
101: * @param field the field name
102: * @return the custom PropertyEditor, or <code>null</code>
103: */
104: public PropertyEditor getCustomEditor(String field) {
105: String fixedField = fixedField(field);
106: Class type = getPropertyAccessor().getPropertyType(fixedField);
107: return getPropertyAccessor().findCustomEditor(type, fixedField);
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: }
|