KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > support > ResourceEditorRegistrar


1 /*
2  * Copyright 2002-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
17 package org.springframework.beans.support;
18
19 import java.io.File JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URL JavaDoc;
23
24 import org.springframework.beans.PropertyEditorRegistrar;
25 import org.springframework.beans.PropertyEditorRegistry;
26 import org.springframework.beans.propertyeditors.ClassEditor;
27 import org.springframework.beans.propertyeditors.FileEditor;
28 import org.springframework.beans.propertyeditors.InputStreamEditor;
29 import org.springframework.beans.propertyeditors.URIEditor;
30 import org.springframework.beans.propertyeditors.URLEditor;
31 import org.springframework.core.JdkVersion;
32 import org.springframework.core.io.Resource;
33 import org.springframework.core.io.ResourceEditor;
34 import org.springframework.core.io.ResourceLoader;
35 import org.springframework.core.io.support.ResourceArrayPropertyEditor;
36 import org.springframework.core.io.support.ResourcePatternResolver;
37
38 /**
39  * PropertyEditorRegistrar implementation that populates a given
40  * {@link org.springframework.beans.PropertyEditorRegistry}
41  * (typically a {@link org.springframework.beans.BeanWrapper} used for bean
42  * creation within an {@link org.springframework.context.ApplicationContext})
43  * with resource editors. Used by
44  * {@link org.springframework.context.support.AbstractApplicationContext}.
45  *
46  * @author Juergen Hoeller
47  * @since 2.0
48  */

49 public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
50
51     private final ResourceLoader resourceLoader;
52
53
54     /**
55      * Create a new ResourceEditorRegistrar for the given ResourceLoader
56      * @param resourceLoader the ResourceLoader (or ResourcePatternResolver)
57      * to create editors for (usually an ApplicationContext)
58      * @see org.springframework.core.io.support.ResourcePatternResolver
59      * @see org.springframework.context.ApplicationContext
60      */

61     public ResourceEditorRegistrar(ResourceLoader resourceLoader) {
62         this.resourceLoader = resourceLoader;
63     }
64
65
66     /**
67      * Populate the given bean factory with the following resource editors:
68      * ResourceEditor, InputStreamEditor, FileEditor, URLEditor, ClassEditor, URIEditor.
69      * <p>In case of a {@link org.springframework.core.io.support.ResourcePatternResolver},
70      * a ResourceArrayPropertyEditor will be registered as well.
71      * @see org.springframework.core.io.ResourceEditor
72      * @see org.springframework.beans.propertyeditors.InputStreamEditor
73      * @see org.springframework.beans.propertyeditors.FileEditor
74      * @see org.springframework.beans.propertyeditors.URLEditor
75      * @see org.springframework.beans.propertyeditors.ClassEditor
76      * @see org.springframework.beans.propertyeditors.URIEditor
77      * @see org.springframework.core.io.support.ResourceArrayPropertyEditor
78      */

79     public void registerCustomEditors(PropertyEditorRegistry registry) {
80         ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader);
81         registry.registerCustomEditor(Resource.class, baseEditor);
82         registry.registerCustomEditor(InputStream JavaDoc.class, new InputStreamEditor(baseEditor));
83         registry.registerCustomEditor(File JavaDoc.class, new FileEditor(baseEditor));
84         registry.registerCustomEditor(URL JavaDoc.class, new URLEditor(baseEditor));
85
86         ClassLoader JavaDoc classLoader = this.resourceLoader.getClassLoader();
87         registry.registerCustomEditor(Class JavaDoc.class, new ClassEditor(classLoader));
88         if (JdkVersion.isAtLeastJava14()) {
89             registry.registerCustomEditor(URI JavaDoc.class, new URIEditor(classLoader));
90         }
91
92         if (this.resourceLoader instanceof ResourcePatternResolver) {
93             registry.registerCustomEditor(Resource[].class,
94                     new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader));
95         }
96     }
97
98 }
99
Popular Tags