| 1 | /* |
| 2 | * Copyright 2006-2007 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 | package org.springframework.batch.support; |
| 17 | |
| 18 | import java.beans.PropertyEditor; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Iterator; |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import org.springframework.beans.PropertyEditorRegistrar; |
| 24 | import org.springframework.beans.PropertyEditorRegistry; |
| 25 | import org.springframework.beans.factory.config.CustomEditorConfigurer; |
| 26 | import org.springframework.util.ClassUtils; |
| 27 | |
| 28 | /** |
| 29 | * A re-usable {@link PropertyEditorRegistrar} that can be used wherever one |
| 30 | * needs to register custom {@link PropertyEditor} instances with a |
| 31 | * {@link PropertyEditorRegistry} (like a bean wrapper, or a type converter). |
| 32 | * |
| 33 | * @author Dave Syer |
| 34 | * |
| 35 | */ |
| 36 | public class DefaultPropertyEditorRegistrar implements PropertyEditorRegistrar { |
| 37 | |
| 38 | private Map customEditors; |
| 39 | |
| 40 | /** |
| 41 | * Register the custom editors with the given registry. |
| 42 | * |
| 43 | * @see org.springframework.beans.PropertyEditorRegistrar#registerCustomEditors(org.springframework.beans.PropertyEditorRegistry) |
| 44 | */ |
| 45 | public void registerCustomEditors(PropertyEditorRegistry registry) { |
| 46 | if (this.customEditors != null) { |
| 47 | for (Iterator it = customEditors.entrySet().iterator(); it.hasNext();) { |
| 48 | Map.Entry entry = (Map.Entry) it.next(); |
| 49 | Class key = (Class) entry.getKey(); |
| 50 | PropertyEditor value = (PropertyEditor) entry.getValue(); |
| 51 | registry.registerCustomEditor(key, value); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Specify the {@link PropertyEditor custom editors} to register. |
| 58 | * |
| 59 | * |
| 60 | * @param customEditors a map of Class to PropertyEditor (or class name to |
| 61 | * PropertyEditor). |
| 62 | * @see CustomEditorConfigurer#setCustomEditors(Map) |
| 63 | */ |
| 64 | public void setCustomEditors(Map customEditors) { |
| 65 | this.customEditors = new HashMap(); |
| 66 | for (Iterator it = customEditors.entrySet().iterator(); it.hasNext();) { |
| 67 | Map.Entry entry = (Map.Entry) it.next(); |
| 68 | Object key = entry.getKey(); |
| 69 | Class requiredType = null; |
| 70 | if (key instanceof Class) { |
| 71 | requiredType = (Class) key; |
| 72 | } |
| 73 | else if (key instanceof String) { |
| 74 | String className = (String) key; |
| 75 | requiredType = ClassUtils.resolveClassName(className, getClass().getClassLoader()); |
| 76 | } |
| 77 | else { |
| 78 | throw new IllegalArgumentException("Invalid key [" + key |
| 79 | + "] for custom editor: needs to be Class or String."); |
| 80 | } |
| 81 | Object value = entry.getValue(); |
| 82 | if (!(value instanceof PropertyEditor)) { |
| 83 | throw new IllegalArgumentException("Mapped value [" + value + "] for custom editor key [" + key |
| 84 | + "] is not of required type [" + PropertyEditor.class.getName() + "]"); |
| 85 | } |
| 86 | this.customEditors.put(requiredType, value); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | } |