01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.io.Serializable;
20:
21: import org.springframework.beans.BeanWrapper;
22: import org.springframework.beans.BeanWrapperImpl;
23: import org.springframework.beans.ConfigurablePropertyAccessor;
24: import org.springframework.util.Assert;
25:
26: /**
27: * Default implementation of the {@link Errors} and {@link BindingResult}
28: * interfaces, for the registration and evaluation of binding errors on
29: * JavaBean objects.
30: *
31: * <p>Performs standard JavaBean property access, also supporting nested
32: * properties. Normally, application code will work with the
33: * <code>Errors</code> interface or the <code>BindingResult</code> interface.
34: * A {@link DataBinder} returns its <code>BindingResult</code> via
35: * {@link org.springframework.validation.DataBinder#getBindingResult()}.
36: *
37: * @author Juergen Hoeller
38: * @since 2.0
39: * @see DataBinder#getBindingResult()
40: * @see DataBinder#initBeanPropertyAccess()
41: * @see DirectFieldBindingResult
42: */
43: public class BeanPropertyBindingResult extends
44: AbstractPropertyBindingResult implements Serializable {
45:
46: private final Object target;
47:
48: private transient BeanWrapper beanWrapper;
49:
50: /**
51: * Creates a new instance of the {@link BeanPropertyBindingResult} class.
52: * @param target the target bean to bind onto
53: * @param objectName the name of the target object
54: * @throws IllegalArgumentException if the supplied <code>target</code> is <code>null</code>
55: */
56: public BeanPropertyBindingResult(Object target, String objectName) {
57: super (objectName);
58: Assert.notNull(target, "Target bean must not be null");
59: this .target = target;
60: }
61:
62: public final Object getTarget() {
63: return this .target;
64: }
65:
66: /**
67: * Returns the {@link BeanWrapper} that this instance uses.
68: * Creates a new one if none existed before.
69: * @see #createBeanWrapper()
70: */
71: public final ConfigurablePropertyAccessor getPropertyAccessor() {
72: if (this .beanWrapper == null) {
73: this .beanWrapper = createBeanWrapper();
74: this .beanWrapper.setExtractOldValueForEditor(true);
75: }
76: return this .beanWrapper;
77: }
78:
79: /**
80: * Create a new {@link BeanWrapper} for the underlying target object.
81: * @see #getTarget()
82: */
83: protected BeanWrapper createBeanWrapper() {
84: return new BeanWrapperImpl(getTarget());
85: }
86:
87: }
|