1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.batch.support;
17
18 import java.beans.PropertyEditor;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Map.Entry;
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
30
31
32
33
34
35
36
37
38
39 public class DefaultPropertyEditorRegistrar implements PropertyEditorRegistrar {
40
41 private Map<Class<?>, PropertyEditor> customEditors;
42
43
44
45
46
47
48 @Override
49 public void registerCustomEditors(PropertyEditorRegistry registry) {
50 if (this.customEditors != null) {
51 for (Entry<Class<?>, PropertyEditor> entry : customEditors.entrySet()) {
52 registry.registerCustomEditor(entry.getKey(), entry.getValue());
53 }
54 }
55 }
56
57
58
59
60
61
62
63
64
65 public void setCustomEditors(Map<? extends Object, ? extends PropertyEditor> customEditors) {
66 this.customEditors = new HashMap<Class<?>, PropertyEditor>();
67 for (Entry<? extends Object, ? extends PropertyEditor> entry : customEditors.entrySet()) {
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 PropertyEditor value = entry.getValue();
82 this.customEditors.put(requiredType, value);
83 }
84 }
85
86 }