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.io.Resource;
032: import org.springframework.core.io.ResourceEditor;
033: import org.springframework.core.io.ResourceLoader;
034: import org.springframework.core.io.support.ResourceArrayPropertyEditor;
035: import org.springframework.core.io.support.ResourcePatternResolver;
036:
037: /**
038: * PropertyEditorRegistrar implementation that populates a given
039: * {@link org.springframework.beans.PropertyEditorRegistry}
040: * (typically a {@link org.springframework.beans.BeanWrapper} used for bean
041: * creation within an {@link org.springframework.context.ApplicationContext})
042: * with resource editors. Used by
043: * {@link org.springframework.context.support.AbstractApplicationContext}.
044: *
045: * @author Juergen Hoeller
046: * @since 2.0
047: */
048: public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
049:
050: private final ResourceLoader resourceLoader;
051:
052: /**
053: * Create a new ResourceEditorRegistrar for the given ResourceLoader
054: * @param resourceLoader the ResourceLoader (or ResourcePatternResolver)
055: * to create editors for (usually an ApplicationContext)
056: * @see org.springframework.core.io.support.ResourcePatternResolver
057: * @see org.springframework.context.ApplicationContext
058: */
059: public ResourceEditorRegistrar(ResourceLoader resourceLoader) {
060: this .resourceLoader = resourceLoader;
061: }
062:
063: /**
064: * Populate the given bean factory with the following resource editors:
065: * ResourceEditor, InputStreamEditor, FileEditor, URLEditor, ClassEditor, URIEditor.
066: * <p>In case of a {@link org.springframework.core.io.support.ResourcePatternResolver},
067: * a ResourceArrayPropertyEditor will be registered as well.
068: * @see org.springframework.core.io.ResourceEditor
069: * @see org.springframework.beans.propertyeditors.InputStreamEditor
070: * @see org.springframework.beans.propertyeditors.FileEditor
071: * @see org.springframework.beans.propertyeditors.URLEditor
072: * @see org.springframework.beans.propertyeditors.ClassEditor
073: * @see org.springframework.beans.propertyeditors.URIEditor
074: * @see org.springframework.core.io.support.ResourceArrayPropertyEditor
075: */
076: public void registerCustomEditors(PropertyEditorRegistry registry) {
077: ResourceEditor baseEditor = new ResourceEditor(
078: this .resourceLoader);
079: registry.registerCustomEditor(Resource.class, baseEditor);
080: registry.registerCustomEditor(InputStream.class,
081: new InputStreamEditor(baseEditor));
082: registry.registerCustomEditor(File.class, new FileEditor(
083: baseEditor));
084: registry.registerCustomEditor(URL.class, new URLEditor(
085: baseEditor));
086:
087: ClassLoader classLoader = this .resourceLoader.getClassLoader();
088: registry.registerCustomEditor(Class.class, new ClassEditor(
089: classLoader));
090: registry.registerCustomEditor(URI.class, new URIEditor(
091: classLoader));
092:
093: if (this .resourceLoader instanceof ResourcePatternResolver) {
094: registry
095: .registerCustomEditor(
096: Resource[].class,
097: new ResourceArrayPropertyEditor(
098: (ResourcePatternResolver) this.resourceLoader));
099: }
100: }
101:
102: }
|