001/*
002 * Copyright 2002-2017 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 *      https://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
017package org.springframework.core.io;
018
019import java.beans.PropertyEditorSupport;
020import java.io.IOException;
021
022import org.springframework.core.env.PropertyResolver;
023import org.springframework.core.env.StandardEnvironment;
024import org.springframework.util.Assert;
025import org.springframework.util.StringUtils;
026
027/**
028 * {@link java.beans.PropertyEditor Editor} for {@link Resource}
029 * descriptors, to automatically convert {@code String} locations
030 * e.g. {@code file:C:/myfile.txt} or {@code classpath:myfile.txt} to
031 * {@code Resource} properties instead of using a {@code String} location property.
032 *
033 * <p>The path may contain {@code ${...}} placeholders, to be
034 * resolved as {@link org.springframework.core.env.Environment} properties:
035 * e.g. {@code ${user.dir}}. Unresolvable placeholders are ignored by default.
036 *
037 * <p>Delegates to a {@link ResourceLoader} to do the heavy lifting,
038 * by default using a {@link DefaultResourceLoader}.
039 *
040 * @author Juergen Hoeller
041 * @author Dave Syer
042 * @author Chris Beams
043 * @since 28.12.2003
044 * @see Resource
045 * @see ResourceLoader
046 * @see DefaultResourceLoader
047 * @see PropertyResolver#resolvePlaceholders
048 */
049public class ResourceEditor extends PropertyEditorSupport {
050
051        private final ResourceLoader resourceLoader;
052
053        private PropertyResolver propertyResolver;
054
055        private final boolean ignoreUnresolvablePlaceholders;
056
057
058        /**
059         * Create a new instance of the {@link ResourceEditor} class
060         * using a {@link DefaultResourceLoader} and {@link StandardEnvironment}.
061         */
062        public ResourceEditor() {
063                this(new DefaultResourceLoader(), null);
064        }
065
066        /**
067         * Create a new instance of the {@link ResourceEditor} class
068         * using the given {@link ResourceLoader} and {@link PropertyResolver}.
069         * @param resourceLoader the {@code ResourceLoader} to use
070         * @param propertyResolver the {@code PropertyResolver} to use
071         */
072        public ResourceEditor(ResourceLoader resourceLoader, PropertyResolver propertyResolver) {
073                this(resourceLoader, propertyResolver, true);
074        }
075
076        /**
077         * Create a new instance of the {@link ResourceEditor} class
078         * using the given {@link ResourceLoader}.
079         * @param resourceLoader the {@code ResourceLoader} to use
080         * @param propertyResolver the {@code PropertyResolver} to use
081         * @param ignoreUnresolvablePlaceholders whether to ignore unresolvable placeholders
082         * if no corresponding property could be found in the given {@code propertyResolver}
083         */
084        public ResourceEditor(ResourceLoader resourceLoader, PropertyResolver propertyResolver,
085                        boolean ignoreUnresolvablePlaceholders) {
086
087                Assert.notNull(resourceLoader, "ResourceLoader must not be null");
088                this.resourceLoader = resourceLoader;
089                this.propertyResolver = propertyResolver;
090                this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
091        }
092
093
094        @Override
095        public void setAsText(String text) {
096                if (StringUtils.hasText(text)) {
097                        String locationToUse = resolvePath(text).trim();
098                        setValue(this.resourceLoader.getResource(locationToUse));
099                }
100                else {
101                        setValue(null);
102                }
103        }
104
105        /**
106         * Resolve the given path, replacing placeholders with corresponding
107         * property values from the {@code environment} if necessary.
108         * @param path the original file path
109         * @return the resolved file path
110         * @see PropertyResolver#resolvePlaceholders
111         * @see PropertyResolver#resolveRequiredPlaceholders
112         */
113        protected String resolvePath(String path) {
114                if (this.propertyResolver == null) {
115                        this.propertyResolver = new StandardEnvironment();
116                }
117                return (this.ignoreUnresolvablePlaceholders ? this.propertyResolver.resolvePlaceholders(path) :
118                                this.propertyResolver.resolveRequiredPlaceholders(path));
119        }
120
121
122        @Override
123        public String getAsText() {
124                Resource value = (Resource) getValue();
125                try {
126                        // Try to determine URL for resource.
127                        return (value != null ? value.getURL().toExternalForm() : "");
128                }
129                catch (IOException ex) {
130                        // Couldn't determine resource URL - return null to indicate
131                        // that there is no appropriate text representation.
132                        return null;
133                }
134        }
135
136}