diff --git a/org.springframework.context/src/main/java/org/springframework/format/support/FormattingPropertyEditorAdapter.java b/org.springframework.context/src/main/java/org/springframework/format/support/FormattingPropertyEditorAdapter.java deleted file mode 100644 index 30498ccc612c0b438010d2688a38a390ab885565..0000000000000000000000000000000000000000 --- a/org.springframework.context/src/main/java/org/springframework/format/support/FormattingPropertyEditorAdapter.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.format.support; - -import java.beans.PropertyEditorSupport; - -import org.springframework.core.convert.ConversionService; -import org.springframework.util.Assert; - -/** - * Adapter that exposes a {@link java.beans.PropertyEditor} for any given - * {@link org.springframework.format.Formatter}, retrieving the current - * Locale from {@link org.springframework.context.i18n.LocaleContextHolder}. - * - * @author Juergen Hoeller - * @since 3.0 - */ -public class FormattingPropertyEditorAdapter extends PropertyEditorSupport { - - private final ConversionService conversionService; - - private final Class fieldType; - - /** - * Create a new FormattingPropertyEditorAdapter for the given Formatter. - * @param formatter the Formatter to wrap - */ - public FormattingPropertyEditorAdapter(ConversionService formattingService, Class fieldType) { - Assert.notNull(formattingService, "ConversionService must not be null"); - Assert.notNull(formattingService, "FieldType must not be null"); - this.conversionService = formattingService; - this.fieldType = fieldType; - } - - @Override - public void setAsText(String text) throws IllegalArgumentException { - setValue(this.conversionService.convert(text, this.fieldType)); - } - - @Override - public String getAsText() { - return this.conversionService.convert(getValue(), String.class); - } - -} diff --git a/org.springframework.context/src/main/java/org/springframework/validation/AbstractPropertyBindingResult.java b/org.springframework.context/src/main/java/org/springframework/validation/AbstractPropertyBindingResult.java index 330cc40270879ba5392b2604561168690fa7184f..864d9b2c4510246777e6b3c3f289812f4c795dd2 100644 --- a/org.springframework.context/src/main/java/org/springframework/validation/AbstractPropertyBindingResult.java +++ b/org.springframework.context/src/main/java/org/springframework/validation/AbstractPropertyBindingResult.java @@ -24,7 +24,7 @@ import org.springframework.beans.PropertyAccessorUtils; import org.springframework.beans.PropertyEditorRegistry; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; -import org.springframework.format.support.FormattingPropertyEditorAdapter; +import org.springframework.core.convert.support.ConvertingPropertyEditorAdapter; import org.springframework.util.Assert; /** @@ -151,7 +151,7 @@ public abstract class AbstractPropertyBindingResult extends AbstractBindingResul TypeDescriptor td = (field != null ? getPropertyAccessor().getPropertyTypeDescriptor(fixedField(field)) : TypeDescriptor.valueOf(valueType)); - editor = new FormattingPropertyEditorAdapter(this.conversionService, valueType); + editor = new ConvertingPropertyEditorAdapter(this.conversionService, td); } return editor; } diff --git a/org.springframework.context/src/main/java/org/springframework/validation/DataBinder.java b/org.springframework.context/src/main/java/org/springframework/validation/DataBinder.java index 8f287d8ced007b2f84d29b570e0f607893a4c800..6ee866ae2812642773632a838b5eea26042677fa 100644 --- a/org.springframework.context/src/main/java/org/springframework/validation/DataBinder.java +++ b/org.springframework.context/src/main/java/org/springframework/validation/DataBinder.java @@ -22,6 +22,7 @@ import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.beans.ConfigurablePropertyAccessor; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.PropertyAccessException; @@ -459,17 +460,25 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter { return this.validator; } + + //--------------------------------------------------------------------- + // Implementation of PropertyEditorRegistry/TypeConverter interface + //--------------------------------------------------------------------- + /** - * Set the ConversionService to use for field value formatting in preference to JavaBeans PropertyEditors. - * @since 3.0 + * Specify a Spring 3.0 ConversionService to use for converting + * property values, as an alternative to JavaBeans PropertyEditors. */ public void setConversionService(ConversionService conversionService) { this.conversionService = conversionService; } - //--------------------------------------------------------------------- - // Implementation of PropertyEditorRegistry/TypeConverter interface - //--------------------------------------------------------------------- + /** + * Return the associated ConversionService, if any. + */ + public ConversionService getConversionService() { + return this.conversionService; + } @SuppressWarnings("unchecked") public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor) { @@ -492,6 +501,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter { public T convertIfNecessary( Object value, Class requiredType, MethodParameter methodParam) throws TypeMismatchException { + return getTypeConverter().convertIfNecessary(value, requiredType, methodParam); } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConvertingPropertyEditorAdapter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConvertingPropertyEditorAdapter.java new file mode 100644 index 0000000000000000000000000000000000000000..066d2666be87258770f11a489db4175b2324919d --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConvertingPropertyEditorAdapter.java @@ -0,0 +1,66 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.convert.support; + +import java.beans.PropertyEditorSupport; + +import org.springframework.core.convert.ConversionService; +import org.springframework.core.convert.TypeDescriptor; +import org.springframework.util.Assert; + +/** + * Adapter that exposes a {@link java.beans.PropertyEditor} for any given + * {@link org.springframework.core.convert.ConversionService} and specific target type. + * + * @author Juergen Hoeller + * @since 3.0 + */ +public class ConvertingPropertyEditorAdapter extends PropertyEditorSupport { + + private static final TypeDescriptor stringDescriptor = TypeDescriptor.valueOf(String.class); + + private final ConversionService conversionService; + + private final TypeDescriptor targetDescriptor; + + + /** + * Create a new ConvertingPropertyEditorAdapter for a given + * {@link org.springframework.core.convert.ConversionService} + * and the given target type. + * @param conversionService the ConversionService to delegate to + * @param targetDescriptor the target type to convert to + */ + public ConvertingPropertyEditorAdapter(ConversionService conversionService, TypeDescriptor targetDescriptor) { + Assert.notNull(conversionService, "ConversionService must not be null"); + Assert.notNull(targetDescriptor, "TypeDescriptor must not be null"); + this.conversionService = conversionService; + this.targetDescriptor = targetDescriptor; + } + + + @Override + public void setAsText(String text) throws IllegalArgumentException { + setValue(this.conversionService.convert(text, stringDescriptor, this.targetDescriptor)); + } + + @Override + public String getAsText() { + return (String) this.conversionService.convert(getValue(), this.targetDescriptor, stringDescriptor); + } + +}