001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.support;
018:
019: import java.io.File;
020: import java.io.InputStream;
021: import java.net.URI;
022: import java.net.URL;
023:
024: import org.springframework.beans.PropertyEditorRegistrar;
025: import org.springframework.beans.PropertyEditorRegistry;
026: import org.springframework.beans.propertyeditors.ClassEditor;
027: import org.springframework.beans.propertyeditors.FileEditor;
028: import org.springframework.beans.propertyeditors.InputStreamEditor;
029: import org.springframework.beans.propertyeditors.URIEditor;
030: import org.springframework.beans.propertyeditors.URLEditor;
031: import org.springframework.core.JdkVersion;
032: import org.springframework.core.io.Resource;
033: import org.springframework.core.io.ResourceEditor;
034: import org.springframework.core.io.ResourceLoader;
035: import org.springframework.core.io.support.ResourceArrayPropertyEditor;
036: import org.springframework.core.io.support.ResourcePatternResolver;
037:
038: /**
039: * PropertyEditorRegistrar implementation that populates a given
040: * {@link org.springframework.beans.PropertyEditorRegistry}
041: * (typically a {@link org.springframework.beans.BeanWrapper} used for bean
042: * creation within an {@link org.springframework.context.ApplicationContext})
043: * with resource editors. Used by
044: * {@link org.springframework.context.support.AbstractApplicationContext}.
045: *
046: * @author Juergen Hoeller
047: * @since 2.0
048: */
049: public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
050:
051: private final ResourceLoader resourceLoader;
052:
053: /**
054: * Create a new ResourceEditorRegistrar for the given ResourceLoader
055: * @param resourceLoader the ResourceLoader (or ResourcePatternResolver)
056: * to create editors for (usually an ApplicationContext)
057: * @see org.springframework.core.io.support.ResourcePatternResolver
058: * @see org.springframework.context.ApplicationContext
059: */
060: public ResourceEditorRegistrar(ResourceLoader resourceLoader) {
061: this .resourceLoader = resourceLoader;
062: }
063:
064: /**
065: * Populate the given bean factory with the following resource editors:
066: * ResourceEditor, InputStreamEditor, FileEditor, URLEditor, ClassEditor, URIEditor.
067: * <p>In case of a {@link org.springframework.core.io.support.ResourcePatternResolver},
068: * a ResourceArrayPropertyEditor will be registered as well.
069: * @see org.springframework.core.io.ResourceEditor
070: * @see org.springframework.beans.propertyeditors.InputStreamEditor
071: * @see org.springframework.beans.propertyeditors.FileEditor
072: * @see org.springframework.beans.propertyeditors.URLEditor
073: * @see org.springframework.beans.propertyeditors.ClassEditor
074: * @see org.springframework.beans.propertyeditors.URIEditor
075: * @see org.springframework.core.io.support.ResourceArrayPropertyEditor
076: */
077: public void registerCustomEditors(PropertyEditorRegistry registry) {
078: ResourceEditor baseEditor = new ResourceEditor(
079: this .resourceLoader);
080: registry.registerCustomEditor(Resource.class, baseEditor);
081: registry.registerCustomEditor(InputStream.class,
082: new InputStreamEditor(baseEditor));
083: registry.registerCustomEditor(File.class, new FileEditor(
084: baseEditor));
085: registry.registerCustomEditor(URL.class, new URLEditor(
086: baseEditor));
087:
088: ClassLoader classLoader = this .resourceLoader.getClassLoader();
089: registry.registerCustomEditor(Class.class, new ClassEditor(
090: classLoader));
091: if (JdkVersion.isAtLeastJava14()) {
092: registry.registerCustomEditor(URI.class, new URIEditor(
093: classLoader));
094: }
095:
096: if (this .resourceLoader instanceof ResourcePatternResolver) {
097: registry
098: .registerCustomEditor(
099: Resource[].class,
100: new ResourceArrayPropertyEditor(
101: (ResourcePatternResolver) this.resourceLoader));
102: }
103: }
104:
105: }
|