From ac6e26388ff4919512a25c34e02e089ed96c6ccf Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 18 Sep 2009 14:15:06 +0000 Subject: [PATCH] added default editors for "java.util.Currency" and "java.util.TimeZone" --- .../beans/PropertyEditorRegistrySupport.java | 26 +++------- .../beans/propertyeditors/CurrencyEditor.java | 47 +++++++++++++++++++ .../beans/propertyeditors/TimeZoneEditor.java | 46 ++++++++++++++++++ 3 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/CurrencyEditor.java create mode 100644 org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/TimeZoneEditor.java diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java b/org.springframework.beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java index 364d89d1e83..52c422459e0 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * 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. @@ -25,6 +25,7 @@ import java.net.URI; import java.net.URL; import java.nio.charset.Charset; import java.util.Collection; +import java.util.Currency; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -37,6 +38,7 @@ import java.util.Properties; import java.util.Set; import java.util.SortedMap; import java.util.SortedSet; +import java.util.TimeZone; import java.util.regex.Pattern; import org.springframework.beans.propertyeditors.ByteArrayPropertyEditor; @@ -45,6 +47,7 @@ import org.springframework.beans.propertyeditors.CharacterEditor; import org.springframework.beans.propertyeditors.CharsetEditor; import org.springframework.beans.propertyeditors.ClassArrayEditor; import org.springframework.beans.propertyeditors.ClassEditor; +import org.springframework.beans.propertyeditors.CurrencyEditor; import org.springframework.beans.propertyeditors.CustomBooleanEditor; import org.springframework.beans.propertyeditors.CustomCollectionEditor; import org.springframework.beans.propertyeditors.CustomMapEditor; @@ -55,6 +58,7 @@ import org.springframework.beans.propertyeditors.LocaleEditor; import org.springframework.beans.propertyeditors.PatternEditor; import org.springframework.beans.propertyeditors.PropertiesEditor; import org.springframework.beans.propertyeditors.StringArrayPropertyEditor; +import org.springframework.beans.propertyeditors.TimeZoneEditor; import org.springframework.beans.propertyeditors.URIEditor; import org.springframework.beans.propertyeditors.URLEditor; import org.springframework.core.convert.ConversionService; @@ -151,24 +155,6 @@ public class PropertyEditorRegistrySupport implements PropertyEditorRegistry { /** * Actually register the default editors for this registry instance. - * @see org.springframework.beans.propertyeditors.ByteArrayPropertyEditor - * @see org.springframework.beans.propertyeditors.ClassEditor - * @see org.springframework.beans.propertyeditors.CharacterEditor - * @see org.springframework.beans.propertyeditors.CustomBooleanEditor - * @see org.springframework.beans.propertyeditors.CustomNumberEditor - * @see org.springframework.beans.propertyeditors.CustomCollectionEditor - * @see org.springframework.beans.propertyeditors.CustomMapEditor - * @see org.springframework.beans.propertyeditors.FileEditor - * @see org.springframework.beans.propertyeditors.InputStreamEditor - * @see org.springframework.jndi.JndiTemplateEditor - * @see org.springframework.beans.propertyeditors.LocaleEditor - * @see org.springframework.beans.propertyeditors.PropertiesEditor - * @see org.springframework.beans.PropertyValuesEditor - * @see org.springframework.core.io.support.ResourceArrayPropertyEditor - * @see org.springframework.core.io.ResourceEditor - * @see org.springframework.transaction.interceptor.TransactionAttributeEditor - * @see org.springframework.transaction.interceptor.TransactionAttributeSourceEditor - * @see org.springframework.beans.propertyeditors.URLEditor */ private void doRegisterDefaultEditors() { this.defaultEditors = new HashMap(64); @@ -178,12 +164,14 @@ public class PropertyEditorRegistrySupport implements PropertyEditorRegistry { this.defaultEditors.put(Charset.class, new CharsetEditor()); this.defaultEditors.put(Class.class, new ClassEditor()); this.defaultEditors.put(Class[].class, new ClassArrayEditor()); + this.defaultEditors.put(Currency.class, new CurrencyEditor()); this.defaultEditors.put(File.class, new FileEditor()); this.defaultEditors.put(InputStream.class, new InputStreamEditor()); this.defaultEditors.put(Locale.class, new LocaleEditor()); this.defaultEditors.put(Pattern.class, new PatternEditor()); this.defaultEditors.put(Properties.class, new PropertiesEditor()); this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor()); + this.defaultEditors.put(TimeZone.class, new TimeZoneEditor()); this.defaultEditors.put(URI.class, new URIEditor()); this.defaultEditors.put(URL.class, new URLEditor()); diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/CurrencyEditor.java b/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/CurrencyEditor.java new file mode 100644 index 00000000000..5c5c0ecad12 --- /dev/null +++ b/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/CurrencyEditor.java @@ -0,0 +1,47 @@ +/* + * 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.beans.propertyeditors; + +import java.beans.PropertyEditorSupport; +import java.util.Currency; + +/** + * Editor for java.util.Currency, translating currency codes into Currency + * objects. Exposes the currency code as text representation of a Currency object. + * + * @author Juergen Hoeller + * @since 3.0 + * @see java.util.Currency + */ +public class CurrencyEditor extends PropertyEditorSupport { + + @Override + public void setAsText(String text) throws IllegalArgumentException { + setValue(Currency.getInstance(text)); + } + + /** + * This implementation returns null to indicate that + * there is no appropriate text representation. + */ + @Override + public String getAsText() { + Currency value = (Currency) getValue(); + return (value != null ? value.getCurrencyCode() : ""); + } + +} diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/TimeZoneEditor.java b/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/TimeZoneEditor.java new file mode 100644 index 00000000000..323f4ca4c9d --- /dev/null +++ b/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/TimeZoneEditor.java @@ -0,0 +1,46 @@ +/* + * 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.beans.propertyeditors; + +import java.beans.PropertyEditorSupport; +import java.util.TimeZone; + +/** + * Editor for java.util.TimeZone, translating timezone IDs into + * TimeZone objects. Does not expose a text representation for TimeZone objects. + * + * @author Juergen Hoeller + * @since 3.0 + * @see java.util.TimeZone + */ +public class TimeZoneEditor extends PropertyEditorSupport { + + @Override + public void setAsText(String text) throws IllegalArgumentException { + setValue(TimeZone.getTimeZone(text)); + } + + /** + * This implementation returns null to indicate that + * there is no appropriate text representation. + */ + @Override + public String getAsText() { + return null; + } + +}